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