[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] | 15 | package de.ugoe.cs.autoquest.testgeneration; |
---|
[523] | 16 | |
---|
| 17 | import java.util.ArrayList; |
---|
| 18 | import java.util.Collection; |
---|
| 19 | import java.util.HashSet; |
---|
| 20 | import java.util.LinkedHashSet; |
---|
| 21 | import java.util.LinkedList; |
---|
| 22 | import java.util.List; |
---|
| 23 | import java.util.Random; |
---|
| 24 | import java.util.Set; |
---|
[639] | 25 | import java.util.logging.Level; |
---|
[523] | 26 | |
---|
[922] | 27 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 28 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess; |
---|
[523] | 29 | import de.ugoe.cs.util.console.Console; |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * <p> |
---|
[559] | 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}. |
---|
[523] | 35 | * </p> |
---|
| 36 | * |
---|
| 37 | * @author Steffen Herbold |
---|
| 38 | * @version 1.0 |
---|
| 39 | */ |
---|
| 40 | public class DrawFromAllSequencesGenerator { |
---|
| 41 | |
---|
[559] | 42 | /** |
---|
| 43 | * <p> |
---|
| 44 | * Number of sequences in the test suite. |
---|
| 45 | * </p> |
---|
| 46 | */ |
---|
| 47 | private final int numSequences; |
---|
[523] | 48 | |
---|
[559] | 49 | /** |
---|
| 50 | * <p> |
---|
| 51 | * Minimal length of a test sequence. |
---|
| 52 | * </p> |
---|
| 53 | */ |
---|
| 54 | private final int minLength; |
---|
[523] | 55 | |
---|
[559] | 56 | /** |
---|
| 57 | * <p> |
---|
| 58 | * Maximal length of a test sequence. |
---|
| 59 | * </p> |
---|
| 60 | */ |
---|
| 61 | private final int maxLength; |
---|
[523] | 62 | |
---|
[559] | 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; |
---|
[523] | 70 | |
---|
[559] | 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; |
---|
[523] | 78 | |
---|
[559] | 79 | /** |
---|
| 80 | * <p> |
---|
| 81 | * Constructor. Creates a new DrawFromAllSequencesGenerator and ensures the validity of the |
---|
| 82 | * parameters: |
---|
[2218] | 83 | * </p> |
---|
[559] | 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> |
---|
[2218] | 89 | * <p> |
---|
[766] | 90 | * If one of these conditions is violated an {@link IllegalArgumentException} is thrown. |
---|
[559] | 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) { |
---|
[766] | 114 | throw new IllegalArgumentException("number of sequences must be at least 1 but is " + |
---|
[559] | 115 | numSequences); |
---|
| 116 | } |
---|
| 117 | if (maxLength < 1) { |
---|
[766] | 118 | throw new IllegalArgumentException( |
---|
[559] | 119 | "maximal allowed length of test cases must be at least 1 but is " + |
---|
| 120 | maxLength); |
---|
| 121 | } |
---|
| 122 | if (minLength > maxLength) { |
---|
[766] | 123 | throw new IllegalArgumentException( |
---|
[559] | 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 | } |
---|
[523] | 133 | |
---|
[559] | 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) { |
---|
[766] | 145 | throw new IllegalArgumentException("model must not be null!"); |
---|
[559] | 146 | } |
---|
[523] | 147 | |
---|
[559] | 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 | } |
---|
[639] | 157 | Console.traceln(Level.INFO, "" + sequences.size() + " possible"); |
---|
[559] | 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 | } |
---|
[523] | 195 | |
---|
| 196 | } |
---|