source: trunk/quest-core-testgeneration/src/main/java/de/ugoe/cs/quest/testgeneration/DrawFromAllSequencesGenerator.java @ 639

Last change on this file since 639 was 639, checked in by sherbold, 12 years ago
  • all usages of the Console tracing API now define log levels
  • Property svn:mime-type set to text/plain
File size: 6.2 KB
Line 
1
2package de.ugoe.cs.quest.testgeneration;
3
4import java.security.InvalidParameterException;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.HashSet;
8import java.util.LinkedHashSet;
9import java.util.LinkedList;
10import java.util.List;
11import java.util.Random;
12import java.util.Set;
13import java.util.logging.Level;
14
15import de.ugoe.cs.quest.eventcore.Event;
16import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
17import de.ugoe.cs.util.console.Console;
18
19/**
20 * <p>
21 * Generates a test suite by drawing from all possible sequences of a fixed length according to the
22 * probabilities of the sequences in a {@link IStochasticProcess}.
23 * </p>
24 *
25 * @author Steffen Herbold
26 * @version 1.0
27 */
28public class DrawFromAllSequencesGenerator {
29
30    /**
31     * <p>
32     * Number of sequences in the test suite.
33     * </p>
34     */
35    private final int numSequences;
36
37    /**
38     * <p>
39     * Minimal length of a test sequence.
40     * </p>
41     */
42    private final int minLength;
43
44    /**
45     * <p>
46     * Maximal length of a test sequence.
47     * </p>
48     */
49    private final int maxLength;
50
51    /**
52     * <p>
53     * In case this member is true, only test cases that end in the global end event
54     * {@link Event#ENDEVENT} are generated. If it is false, the end event can be any event.
55     * </p>
56     */
57    private final boolean validEnd;
58
59    /**
60     * <p>
61     * If this member is true, the generated test suite contains all possible sequences and
62     * {@link #numSequences} is ignored.
63     * </p>
64     */
65    private final boolean generateAll;
66
67    /**
68     * <p>
69     * Constructor. Creates a new DrawFromAllSequencesGenerator and ensures the validity of the
70     * parameters:
71     * <ul>
72     * <li>numSequences must at least be 1
73     * <li>maxLength must at least be 1
74     * <li>minLength must be less than or equal to maxLength
75     * </ul>
76     * If one of these conditions is violated an {@link InvalidParameterException} is thrown.
77     * </p>
78     *
79     * @param numSequences
80     *            number of sequences desired for the test suite
81     * @param minLength
82     *            minimal length of a test sequence
83     * @param maxLength
84     *            maximal length of a test sequence
85     * @param validEnd
86     *            defines if test cases have to end with the global end event {@link Event#ENDEVENT}
87     *            (see {@link #validEnd})
88     * @param generateAll
89     *            if this parameter is true, the test suite contains all possible sequences and
90     *            numSequences is ignored
91     */
92    public DrawFromAllSequencesGenerator(int numSequences,
93                                         int minLength,
94                                         int maxLength,
95                                         boolean validEnd,
96                                         boolean generateAll)
97    {
98        // check validity of the parameters
99        if (numSequences < 1) {
100            throw new InvalidParameterException("number of sequences must be at least 1 but is " +
101                numSequences);
102        }
103        if (maxLength < 1) {
104            throw new InvalidParameterException(
105                                                "maximal allowed length of test cases must be at least 1 but is " +
106                                                    maxLength);
107        }
108        if (minLength > maxLength) {
109            throw new InvalidParameterException(
110                                                "minimal allowed length of test cases must be less than or equal to the maximal allowed length (min length: " +
111                                                    minLength + " ; max length: " + maxLength + ")");
112        }
113        this.numSequences = numSequences;
114        this.minLength = minLength;
115        this.maxLength = maxLength;
116        this.validEnd = validEnd;
117        this.generateAll = generateAll;
118    }
119
120    /**
121     * <p>
122     * Generates a test suite by drawing from all possible sequences with valid lengths.
123     * </p>
124     *
125     * @param model
126     *            model used to determine the probability of each possible sequence
127     * @return the test suite
128     */
129    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) {
130        if (model == null) {
131            throw new InvalidParameterException("model must not be null!");
132        }
133
134        Collection<List<Event>> sequences = new LinkedHashSet<List<Event>>();
135        for (int length = minLength; length <= maxLength; length++) {
136            if (validEnd) {
137                sequences.addAll(model.generateValidSequences(length + 2));
138            }
139            else {
140                sequences.addAll(model.generateSequences(length + 1, true));
141            }
142        }
143        Console.traceln(Level.INFO, "" + sequences.size() + " possible");
144        if (!generateAll && numSequences < sequences.size()) {
145            List<Double> probabilities = new ArrayList<Double>(sequences.size());
146            double probSum = 0.0;
147            for (List<Event> sequence : sequences) {
148                double prob = model.getProbability(sequence);
149                probabilities.add(prob);
150                probSum += prob;
151            }
152            Set<Integer> drawnSequences = new HashSet<Integer>(numSequences);
153            Random r = new Random();
154            while (drawnSequences.size() < numSequences) {
155                double randVal = r.nextDouble() * probSum;
156                double sum = 0.0d;
157                int index = -1;
158                while (sum < randVal) {
159                    index++;
160                    double currentProb = probabilities.get(index);
161                    sum += currentProb;
162                }
163                if (!drawnSequences.contains(index)) {
164                    drawnSequences.add(index);
165                    probSum -= probabilities.get(index);
166                    probabilities.set(index, 0.0d);
167                }
168            }
169            Collection<List<Event>> retainedSequences = new LinkedList<List<Event>>();
170            int index = 0;
171            for (List<Event> sequence : sequences) {
172                if (drawnSequences.contains(index)) {
173                    retainedSequences.add(sequence);
174                }
175                index++;
176            }
177            sequences = retainedSequences;
178        }
179        return sequences;
180    }
181
182}
Note: See TracBrowser for help on using the repository browser.