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

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