source: trunk/autoquest-core-testgeneration/src/main/java/de/ugoe/cs/autoquest/testgeneration/HybridGenerator.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: 7.7 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.LinkedHashSet;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Random;
23import java.util.logging.Level;
24
25import de.ugoe.cs.autoquest.eventcore.Event;
26import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
27import de.ugoe.cs.util.console.Console;
28
29/**
30 * <p>
31 * Generates a test suite with a hybrid approach that is a mixture of random walks and drawing from
32 * all possible sequences.
33 * </p>
34 *
35 * @author Steffen Herbold
36 * @version 1.0
37 */
38public class HybridGenerator {
39
40    /**
41     * <p>
42     * Number of sequences in the test suite.
43     * </p>
44     */
45    private final int numSequences;
46
47    /**
48     * <p>
49     * Length of a test sequence.
50     * </p>
51     */
52    private final int length;
53
54    /**
55     * <p>
56     * Maximal length where it is possible to generate all sequences and draw from them.
57     * </p>
58     */
59    private final int maxLengthAll;
60
61    /**
62     * <p>
63     * In case this member is true, only test cases that end in the global end event
64     * {@link Event#ENDEVENT} are generated. If it is false, the end event can be any event.
65     * </p>
66     */
67    private final boolean validEnd;
68
69    /**
70     * <p>
71     * Constructor. Creates a new HybridGenerator and ensures the validity of the parameters:
72     * <ul>
73     * <li>numSequences must at least be 1
74     * <li>length must be at least 1
75     * </ul>
76     * If one of these conditions is violated an {@link IllegalArgumentException} is thrown.
77     * </p>
78     *
79     * @param numSequences
80     *            number of sequences desired for the test suite
81     * @param length
82     *            length of a test sequence
83     * @param maxLengthAll
84     *            maximal length where it is possible to generate all sequences and draw from them
85     * @param validEnd
86     *            defines if test cases have to end with the global end event {@link Event#ENDEVENT}
87     *            (see {@link #validEnd})
88     */
89    public HybridGenerator(int numSequences, int length, int maxLengthAll, boolean validEnd) {
90        // check validity of the parameters
91        if (numSequences < 1) {
92            throw new IllegalArgumentException("number of sequences must be at least 1 but is " +
93                numSequences);
94        }
95        if (length < 1) {
96            throw new IllegalArgumentException("length of test cases must be at least 1 but is " +
97                length);
98        }
99        this.numSequences = numSequences;
100        this.length = length;
101        this.maxLengthAll = maxLengthAll;
102        this.validEnd = validEnd;
103    }
104
105    /**
106     * <p>
107     * Generates a test suite with a hybrid approach that is a mixture of random walks and drawing
108     * from all possible sequences
109     * </p>
110     *
111     * @param model
112     *            model used to determine the probability of each possible sequence
113     * @return the test suite
114     */
115    public Collection<List<Event>> generateTestSuite(IStochasticProcess model) {
116        if (model == null) {
117            throw new IllegalArgumentException("model must not be null!");
118        }
119
120        Collection<List<Event>> sequences = new LinkedHashSet<List<Event>>();
121
122        List<List<Event>> seqsTmp =
123            new ArrayList<List<Event>>(model.generateSequences(maxLengthAll + 1, true));
124
125        Console.traceln(Level.INFO, "" + seqsTmp.size() + " of length " + maxLengthAll + " possible");
126        List<Double> probabilities = new ArrayList<Double>(seqsTmp.size());
127        double probSum = 0.0;
128        for (List<Event> sequence : seqsTmp) {
129            double prob = model.getProbability(sequence);
130            probabilities.add(prob);
131            probSum += prob;
132        }
133
134        Random r = new Random();
135        int j = 0;
136        while (sequences.size() < numSequences && j <= numSequences * 100) {
137            j++;
138            double randVal = r.nextDouble() * probSum;
139            double sum = 0.0d;
140            int index = -1;
141            while (sum < randVal) {
142                index++;
143                double currentProb = probabilities.get(index);
144                sum += currentProb;
145            }
146            List<Event> seqTmp = seqsTmp.get(index);
147            if (!Event.ENDEVENT.equals(seqTmp.get(seqTmp.size() - 1))) {
148                List<Event> sequence;
149                if (validEnd) {
150                    sequence = finishSequence(seqTmp, model, length + 2, validEnd);
151                    if (sequence != null && sequence.size() != length + 2) {
152                        sequence = null;
153                    }
154                }
155                else {
156                    sequence = finishSequence(seqTmp, model, length + 1, validEnd);
157                    if (sequence != null && sequence.size() != length + 1) {
158                        sequence = null;
159                    }
160                }
161                if (sequence != null) {
162                    sequences.add(sequence);
163                }
164            }
165        }
166
167        return sequences;
168    }
169
170    /**
171     * <p>
172     * Finishes a sequence with a random walk.
173     * </p>
174     *
175     * @param sequence
176     *            sequence to be finished
177     * @param model
178     *            model used for the random walk
179     * @param length
180     *            desired length of the sequence
181     * @param validEnd
182     *            if the sequence should end in {@link Event#ENDEVENT}.
183     * @return finished sequence of the desired length
184     */
185    private List<Event> finishSequence(List<Event> sequence,
186                                       IStochasticProcess model,
187                                       int length,
188                                       boolean validEnd)
189    {
190        Random r = new Random();
191        boolean endFound = false;
192        List<Event> sequenceCopy = new LinkedList<Event>(sequence);
193        final int maxIter = 30000;
194        int iter = 0;
195        while (!endFound && iter < maxIter) {
196            iter++;
197            sequenceCopy = new LinkedList<Event>(sequence);
198            while (!endFound && sequenceCopy.size() <= length) {
199                double randVal = r.nextDouble();
200                double probSum = 0.0;
201                for (Event symbol : model.getEvents()) {
202                    probSum += model.getProbability(sequenceCopy, symbol);
203                    if (probSum >= randVal) {
204                        if (!(Event.STARTEVENT.equals(symbol) || (!validEnd && Event.ENDEVENT
205                            .equals(symbol))))
206                        {
207                            // only add the symbol the sequence if it is not
208                            // START
209                            // or END
210                            sequenceCopy.add(symbol);
211                        }
212                        endFound =
213                            Event.ENDEVENT.equals(symbol) ||
214                                (!validEnd && sequenceCopy.size() == length);
215                        break;
216                    }
217                }
218            }
219        }
220        if (iter == maxIter) {
221            return null;
222        }
223        return sequenceCopy;
224    }
225
226}
Note: See TracBrowser for help on using the repository browser.