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

Last change on this file 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
RevLine 
[927]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
[922]15package de.ugoe.cs.autoquest.testgeneration;
[523]16
17import java.util.Collection;
18import java.util.HashSet;
19import java.util.List;
20import java.util.Set;
21
[922]22import de.ugoe.cs.autoquest.eventcore.Event;
23import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
[523]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
[559]35    /**
36     * <p>
37     * Number of sequences in the test suite.
38     * </p>
39     */
40    private final int numSequences;
[523]41
[559]42    /**
43     * <p>
44     * Minimal length of a test sequence.
45     * </p>
46     */
47    private final int minLength;
[523]48
[559]49    /**
50     * <p>
51     * Maximal length of a test sequence.
52     * </p>
53     */
54    private final int maxLength;
[523]55
[559]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;
[523]63
[559]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;
[523]73
[559]74    /**
75     * <p>
[2026]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>
[559]84     * Actual number of random walks performed to generate the test suite.
85     * </p>
86     */
87    private long actualIter = -1;
[523]88
[559]89    /**
90     * <p>
91     * Constructor. Creates a new RandomWalkGenerator and ensures the validity of the parameters:
[2218]92     * </p>
[559]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>
[2218]99     * <p>
[766]100     * If one of these conditions is violated an {@link IllegalArgumentException} is thrown.
[559]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})
[2026]115     * @param maxIterValidSequence
116     *            maximal number of attempts to create a valid sequence within the given restrictions
[559]117     */
118    public RandomWalkGenerator(int numSequences,
119                               int minLength,
120                               int maxLength,
121                               boolean validEnd,
[2026]122                               long maxIter,
123                               long maxIterValidSequence)
[559]124    {
125        // check validity of the parameters
126        if (numSequences < 1) {
[766]127            throw new IllegalArgumentException("number of sequences must be at least 1 but is " +
[559]128                numSequences);
129        }
130        if (maxLength < 1) {
[766]131            throw new IllegalArgumentException(
[2026]132                                               "maximal allowed length of test cases must be at least 1 but is " +
133                                                   maxLength);
[559]134        }
135        if (minLength > maxLength) {
[766]136            throw new IllegalArgumentException(
[2026]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 + ")");
[559]139        }
140        if (maxIter < numSequences) {
[766]141            throw new IllegalArgumentException(
[2026]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                                                   ")");
[559]147        }
[2026]148        if( maxIterValidSequence < 1) {
149            throw new IllegalArgumentException("maximal number of attempts to get a valid sequence must be positive, but is: " + maxIterValidSequence);
150        }
[559]151        this.numSequences = numSequences;
152        this.minLength = minLength;
153        this.maxLength = maxLength;
154        this.validEnd = validEnd;
155        this.maxIter = maxIter;
[2026]156        this.maxIterValidSequence = maxIterValidSequence;
[559]157    }
[523]158
[559]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) {
[766]170            throw new IllegalArgumentException("model must not be null!");
[559]171        }
[523]172
[559]173        Set<List<Event>> sequences = new HashSet<List<Event>>(numSequences);
174        actualIter = 0;
175        while (sequences.size() < numSequences && actualIter < maxIter) {
[2026]176            List<Event> generatedSequence = model.randomSequence(maxLength, validEnd, maxIterValidSequence);
[559]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        }
[523]186
[559]187        return sequences;
188    }
[523]189
[559]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    }
[523]203}
Note: See TracBrowser for help on using the repository browser.