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

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