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

Last change on this file since 2218 was 2218, checked in by pharms, 7 years ago
  • java doc issues removal
  • Property svn:mime-type set to text/plain
File size: 7.2 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     * Maximal number of attempts for creating a valid sequence. If this value is reached, the
77     * sequence generation will be aborted.
78     * </p>
79     */
80    private final long maxIterValidSequence;
81
82    /**
83     * <p>
84     * Actual number of random walks performed to generate the test suite.
85     * </p>
86     */
87    private long actualIter = -1;
88
89    /**
90     * <p>
91     * Constructor. Creates a new RandomWalkGenerator and ensures the validity of the parameters:
92     * </p>
93     * <ul>
94     * <li>numSequences must at least be 1
95     * <li>maxLength must at least be 1
96     * <li>minLength must be less than or equal to maxLength
97     * <li>maxIter must be greater than or equal to numSequences
98     * </ul>
99     * <p>
100     * If one of these conditions is violated an {@link IllegalArgumentException} is thrown.
101     * </p>
102     *
103     * @param numSequences
104     *            number of sequences desired for the test suite
105     * @param minLength
106     *            minimal length of a test sequence
107     * @param maxLength
108     *            maximal length of a test sequence
109     * @param validEnd
110     *            defines if test cases have to end with the global end event {@link Event#ENDEVENT}
111     *            (see {@link #validEnd})
112     * @param maxIter
113     *            maximal number of random walks before aborting the test case generation (see
114     *            {@link #maxIter})
115     * @param maxIterValidSequence
116     *            maximal number of attempts to create a valid sequence within the given restrictions
117     */
118    public RandomWalkGenerator(int numSequences,
119                               int minLength,
120                               int maxLength,
121                               boolean validEnd,
122                               long maxIter,
123                               long maxIterValidSequence)
124    {
125        // check validity of the parameters
126        if (numSequences < 1) {
127            throw new IllegalArgumentException("number of sequences must be at least 1 but is " +
128                numSequences);
129        }
130        if (maxLength < 1) {
131            throw new IllegalArgumentException(
132                                               "maximal allowed length of test cases must be at least 1 but is " +
133                                                   maxLength);
134        }
135        if (minLength > maxLength) {
136            throw new IllegalArgumentException(
137                                               "minimal allowed length of test cases must be less than or equal to the maximal allowed length (min length: " +
138                                                   minLength + " ; max length: " + maxLength + ")");
139        }
140        if (maxIter < numSequences) {
141            throw new IllegalArgumentException(
142                                               "maximal number of iterations must greater than or equal to the number of sequences (number of sequences: " +
143                                                   numSequences +
144                                                   " ; max iterations: " +
145                                                   maxIter +
146                                                   ")");
147        }
148        if( maxIterValidSequence < 1) {
149            throw new IllegalArgumentException("maximal number of attempts to get a valid sequence must be positive, but is: " + maxIterValidSequence);
150        }
151        this.numSequences = numSequences;
152        this.minLength = minLength;
153        this.maxLength = maxLength;
154        this.validEnd = validEnd;
155        this.maxIter = maxIter;
156        this.maxIterValidSequence = maxIterValidSequence;
157    }
158
159    /**
160     * <p>
161     * Generates a test suite by repeatedly randomly walking a stochastic process.
162     * </p>
163     *
164     * @param model
165     *            stochastic process which performs the random walks
166     * @return the test suite
167     */
168    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) {
169        if (model == null) {
170            throw new IllegalArgumentException("model must not be null!");
171        }
172
173        Set<List<Event>> sequences = new HashSet<List<Event>>(numSequences);
174        actualIter = 0;
175        while (sequences.size() < numSequences && actualIter < maxIter) {
176            List<Event> generatedSequence = model.randomSequence(maxLength, validEnd, maxIterValidSequence);
177            if (generatedSequence.size() >= minLength && generatedSequence.size() <= maxLength) {
178                ((List<Event>) generatedSequence).add(0, Event.STARTEVENT);
179                if (validEnd) {
180                    ((List<Event>) generatedSequence).add(Event.ENDEVENT);
181                }
182                sequences.add(generatedSequence);
183            }
184            actualIter++;
185        }
186
187        return sequences;
188    }
189
190    /**
191     * <p>
192     * Returns the actual number of random walks performed during the last call of
193     * {@link #generateTestSuite(IStochasticProcess)} or -1 if
194     * {@link #generateTestSuite(IStochasticProcess)} has not been called yet.
195     * </p>
196     *
197     * @return actual number of random walks or -1 if {@link #generateTestSuite(IStochasticProcess)}
198     *         has not been called
199     */
200    public long getActualIter() {
201        return actualIter;
202    }
203}
Note: See TracBrowser for help on using the repository browser.