source: trunk/quest-core-testgeneration/src/main/java/de/ugoe/cs/quest/testgeneration/RandomWalkGenerator.java @ 655

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
  • Property svn:mime-type set to text/plain
File size: 5.9 KB
Line 
1package de.ugoe.cs.quest.testgeneration;
2
3import java.security.InvalidParameterException;
4import java.util.Collection;
5import java.util.HashSet;
6import java.util.List;
7import java.util.Set;
8
9import de.ugoe.cs.quest.eventcore.Event;
10import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
11
12/**
13 * <p>
14 * Generates a test suite by randomly walking an {@link IStochasticProcess}.
15 * </p>
16 *
17 * @author Steffen Herbold
18 * @version 1.0
19 */
20public class RandomWalkGenerator {
21
22    /**
23     * <p>
24     * Number of sequences in the test suite.
25     * </p>
26     */
27    private final int numSequences;
28
29    /**
30     * <p>
31     * Minimal length of a test sequence.
32     * </p>
33     */
34    private final int minLength;
35
36    /**
37     * <p>
38     * Maximal length of a test sequence.
39     * </p>
40     */
41    private final int maxLength;
42
43    /**
44     * <p>
45     * In case this member is true, only test cases that end in the global end event
46     * {@link Event#ENDEVENT} are generated. If it is false, the end event can be any event.
47     * </p>
48     */
49    private final boolean validEnd;
50
51    /**
52     * <p>
53     * Maximal number of random walks performed before aborting the test case generation and
54     * returning a test suite with less than {@link #numSequences} test cases. This can happen if
55     * too many generated random walks have to be discarded because their length is not between
56     * {@link #minLength} and {@link #maxLength}.
57     * </p>
58     */
59    private final long maxIter;
60
61    /**
62     * <p>
63     * Actual number of random walks performed to generate the test suite.
64     * </p>
65     */
66    private long actualIter = -1;
67
68    /**
69     * <p>
70     * Constructor. Creates a new RandomWalkGenerator and ensures the validity of the parameters:
71     * <ul>
72     * <li>numSequences must at least be 1
73     * <li>maxLength must at least be 1
74     * <li>minLength must be less than or equal to maxLength
75     * <li>maxIter must be greater than or equal to numSequences
76     * </ul>
77     * If one of these conditions is violated an {@link InvalidParameterException} is thrown.
78     * </p>
79     *
80     * @param numSequences
81     *            number of sequences desired for the test suite
82     * @param minLength
83     *            minimal length of a test sequence
84     * @param maxLength
85     *            maximal length of a test sequence
86     * @param validEnd
87     *            defines if test cases have to end with the global end event {@link Event#ENDEVENT}
88     *            (see {@link #validEnd})
89     * @param maxIter
90     *            maximal number of random walks before aborting the test case generation (see
91     *            {@link #maxIter})
92     */
93    public RandomWalkGenerator(int numSequences,
94                               int minLength,
95                               int maxLength,
96                               boolean validEnd,
97                               long maxIter)
98    {
99        // check validity of the parameters
100        if (numSequences < 1) {
101            throw new InvalidParameterException("number of sequences must be at least 1 but is " +
102                numSequences);
103        }
104        if (maxLength < 1) {
105            throw new InvalidParameterException(
106                                                "maximal allowed length of test cases must be at least 1 but is " +
107                                                    maxLength);
108        }
109        if (minLength > maxLength) {
110            throw new InvalidParameterException(
111                                                "minimal allowed length of test cases must be less than or equal to the maximal allowed length (min length: " +
112                                                    minLength + " ; max length: " + maxLength + ")");
113        }
114        if (maxIter < numSequences) {
115            throw new InvalidParameterException(
116                                                "maximal number of iterations must greater than or equal to the number of sequences (number of sequences: " +
117                                                    numSequences +
118                                                    " ; max iterations: " +
119                                                    maxIter +
120                                                    ")");
121        }
122        this.numSequences = numSequences;
123        this.minLength = minLength;
124        this.maxLength = maxLength;
125        this.validEnd = validEnd;
126        this.maxIter = maxIter;
127    }
128
129    /**
130     * <p>
131     * Generates a test suite by repeatedly randomly walking a stochastic process.
132     * </p>
133     *
134     * @param model
135     *            stochastic process which performs the random walks
136     * @return the test suite
137     */
138    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) {
139        if (model == null) {
140            throw new InvalidParameterException("model must not be null!");
141        }
142
143        Set<List<Event>> sequences = new HashSet<List<Event>>(numSequences);
144        actualIter = 0;
145        while (sequences.size() < numSequences && actualIter < maxIter) {
146            List<Event> generatedSequence = model.randomSequence(maxLength, validEnd);
147            if (generatedSequence.size() >= minLength && generatedSequence.size() <= maxLength) {
148                ((List<Event>) generatedSequence).add(0, Event.STARTEVENT);
149                if (validEnd) {
150                    ((List<Event>) generatedSequence).add(Event.ENDEVENT);
151                }
152                sequences.add(generatedSequence);
153            }
154            actualIter++;
155        }
156
157        return sequences;
158    }
159
160    /**
161     * <p>
162     * Returns the actual number of random walks performed during the last call of
163     * {@link #generateTestSuite(IStochasticProcess)} or -1 if
164     * {@link #generateTestSuite(IStochasticProcess)} has not been called yet.
165     * </p>
166     *
167     * @return actual number of random walks or -1 if {@link #generateTestSuite(IStochasticProcess)}
168     *         has not been called
169     */
170    public long getActualIter() {
171        return actualIter;
172    }
173}
Note: See TracBrowser for help on using the repository browser.