source: trunk/autoquest-core-testgeneration/src/main/java/de/ugoe/cs/autoquest/testgeneration/DrawFromAllSequencesGenerator.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: 6.8 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.ArrayList;
18import java.util.Collection;
19import java.util.HashSet;
20import java.util.LinkedHashSet;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.Random;
24import java.util.Set;
25import java.util.logging.Level;
26
27import de.ugoe.cs.autoquest.eventcore.Event;
28import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
29import de.ugoe.cs.util.console.Console;
30
31/**
32 * <p>
33 * Generates a test suite by drawing from all possible sequences of a fixed length according to the
34 * probabilities of the sequences in a {@link IStochasticProcess}.
35 * </p>
36 *
37 * @author Steffen Herbold
38 * @version 1.0
39 */
40public class DrawFromAllSequencesGenerator {
41
42    /**
43     * <p>
44     * Number of sequences in the test suite.
45     * </p>
46     */
47    private final int numSequences;
48
49    /**
50     * <p>
51     * Minimal length of a test sequence.
52     * </p>
53     */
54    private final int minLength;
55
56    /**
57     * <p>
58     * Maximal length of a test sequence.
59     * </p>
60     */
61    private final int maxLength;
62
63    /**
64     * <p>
65     * In case this member is true, only test cases that end in the global end event
66     * {@link Event#ENDEVENT} are generated. If it is false, the end event can be any event.
67     * </p>
68     */
69    private final boolean validEnd;
70
71    /**
72     * <p>
73     * If this member is true, the generated test suite contains all possible sequences and
74     * {@link #numSequences} is ignored.
75     * </p>
76     */
77    private final boolean generateAll;
78
79    /**
80     * <p>
81     * Constructor. Creates a new DrawFromAllSequencesGenerator and ensures the validity of the
82     * parameters:
83     * </p>
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     * </ul>
89     * <p>
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 generateAll
103     *            if this parameter is true, the test suite contains all possible sequences and
104     *            numSequences is ignored
105     */
106    public DrawFromAllSequencesGenerator(int numSequences,
107                                         int minLength,
108                                         int maxLength,
109                                         boolean validEnd,
110                                         boolean generateAll)
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        this.numSequences = numSequences;
128        this.minLength = minLength;
129        this.maxLength = maxLength;
130        this.validEnd = validEnd;
131        this.generateAll = generateAll;
132    }
133
134    /**
135     * <p>
136     * Generates a test suite by drawing from all possible sequences with valid lengths.
137     * </p>
138     *
139     * @param model
140     *            model used to determine the probability of each possible sequence
141     * @return the test suite
142     */
143    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) {
144        if (model == null) {
145            throw new IllegalArgumentException("model must not be null!");
146        }
147
148        Collection<List<Event>> sequences = new LinkedHashSet<List<Event>>();
149        for (int length = minLength; length <= maxLength; length++) {
150            if (validEnd) {
151                sequences.addAll(model.generateValidSequences(length + 2));
152            }
153            else {
154                sequences.addAll(model.generateSequences(length + 1, true));
155            }
156        }
157        Console.traceln(Level.INFO, "" + sequences.size() + " possible");
158        if (!generateAll && numSequences < sequences.size()) {
159            List<Double> probabilities = new ArrayList<Double>(sequences.size());
160            double probSum = 0.0;
161            for (List<Event> sequence : sequences) {
162                double prob = model.getProbability(sequence);
163                probabilities.add(prob);
164                probSum += prob;
165            }
166            Set<Integer> drawnSequences = new HashSet<Integer>(numSequences);
167            Random r = new Random();
168            while (drawnSequences.size() < numSequences) {
169                double randVal = r.nextDouble() * probSum;
170                double sum = 0.0d;
171                int index = -1;
172                while (sum < randVal) {
173                    index++;
174                    double currentProb = probabilities.get(index);
175                    sum += currentProb;
176                }
177                if (!drawnSequences.contains(index)) {
178                    drawnSequences.add(index);
179                    probSum -= probabilities.get(index);
180                    probabilities.set(index, 0.0d);
181                }
182            }
183            Collection<List<Event>> retainedSequences = new LinkedList<List<Event>>();
184            int index = 0;
185            for (List<Event> sequence : sequences) {
186                if (drawnSequences.contains(index)) {
187                    retainedSequences.add(sequence);
188                }
189                index++;
190            }
191            sequences = retainedSequences;
192        }
193        return sequences;
194    }
195
196}
Note: See TracBrowser for help on using the repository browser.