source: trunk/autoquest-core-testgeneration/src/main/java/de/ugoe/cs/autoquest/testgeneration/DrawFromAllSequencesGenerator.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.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     * <ul>
84     * <li>numSequences must at least be 1
85     * <li>maxLength must at least be 1
86     * <li>minLength must be less than or equal to maxLength
87     * </ul>
88     * If one of these conditions is violated an {@link IllegalArgumentException} is thrown.
89     * </p>
90     *
91     * @param numSequences
92     *            number of sequences desired for the test suite
93     * @param minLength
94     *            minimal length of a test sequence
95     * @param maxLength
96     *            maximal length of a test sequence
97     * @param validEnd
98     *            defines if test cases have to end with the global end event {@link Event#ENDEVENT}
99     *            (see {@link #validEnd})
100     * @param generateAll
101     *            if this parameter is true, the test suite contains all possible sequences and
102     *            numSequences is ignored
103     */
104    public DrawFromAllSequencesGenerator(int numSequences,
105                                         int minLength,
106                                         int maxLength,
107                                         boolean validEnd,
108                                         boolean generateAll)
109    {
110        // check validity of the parameters
111        if (numSequences < 1) {
112            throw new IllegalArgumentException("number of sequences must be at least 1 but is " +
113                numSequences);
114        }
115        if (maxLength < 1) {
116            throw new IllegalArgumentException(
117                                                "maximal allowed length of test cases must be at least 1 but is " +
118                                                    maxLength);
119        }
120        if (minLength > maxLength) {
121            throw new IllegalArgumentException(
122                                                "minimal allowed length of test cases must be less than or equal to the maximal allowed length (min length: " +
123                                                    minLength + " ; max length: " + maxLength + ")");
124        }
125        this.numSequences = numSequences;
126        this.minLength = minLength;
127        this.maxLength = maxLength;
128        this.validEnd = validEnd;
129        this.generateAll = generateAll;
130    }
131
132    /**
133     * <p>
134     * Generates a test suite by drawing from all possible sequences with valid lengths.
135     * </p>
136     *
137     * @param model
138     *            model used to determine the probability of each possible sequence
139     * @return the test suite
140     */
141    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) {
142        if (model == null) {
143            throw new IllegalArgumentException("model must not be null!");
144        }
145
146        Collection<List<Event>> sequences = new LinkedHashSet<List<Event>>();
147        for (int length = minLength; length <= maxLength; length++) {
148            if (validEnd) {
149                sequences.addAll(model.generateValidSequences(length + 2));
150            }
151            else {
152                sequences.addAll(model.generateSequences(length + 1, true));
153            }
154        }
155        Console.traceln(Level.INFO, "" + sequences.size() + " possible");
156        if (!generateAll && numSequences < sequences.size()) {
157            List<Double> probabilities = new ArrayList<Double>(sequences.size());
158            double probSum = 0.0;
159            for (List<Event> sequence : sequences) {
160                double prob = model.getProbability(sequence);
161                probabilities.add(prob);
162                probSum += prob;
163            }
164            Set<Integer> drawnSequences = new HashSet<Integer>(numSequences);
165            Random r = new Random();
166            while (drawnSequences.size() < numSequences) {
167                double randVal = r.nextDouble() * probSum;
168                double sum = 0.0d;
169                int index = -1;
170                while (sum < randVal) {
171                    index++;
172                    double currentProb = probabilities.get(index);
173                    sum += currentProb;
174                }
175                if (!drawnSequences.contains(index)) {
176                    drawnSequences.add(index);
177                    probSum -= probabilities.get(index);
178                    probabilities.set(index, 0.0d);
179                }
180            }
181            Collection<List<Event>> retainedSequences = new LinkedList<List<Event>>();
182            int index = 0;
183            for (List<Event> sequence : sequences) {
184                if (drawnSequences.contains(index)) {
185                    retainedSequences.add(sequence);
186                }
187                index++;
188            }
189            sequences = retainedSequences;
190        }
191        return sequences;
192    }
193
194}
Note: See TracBrowser for help on using the repository browser.