[288] | 1 | package de.ugoe.cs.eventbench.commands;
|
---|
| 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
[395] | 4 | import java.util.Collection;
|
---|
| 5 | import java.util.Iterator;
|
---|
[288] | 6 | import java.util.LinkedHashSet;
|
---|
[293] | 7 | import java.util.LinkedList;
|
---|
[288] | 8 | import java.util.List;
|
---|
[293] | 9 | import java.util.Map;
|
---|
| 10 | import java.util.Set;
|
---|
[288] | 11 |
|
---|
| 12 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
[293] | 13 | import de.ugoe.cs.eventbench.coverage.SequenceTools;
|
---|
[288] | 14 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 15 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
| 16 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
[293] | 17 | import de.ugoe.cs.util.ArrayTools;
|
---|
[288] | 18 | import de.ugoe.cs.util.console.Command;
|
---|
| 19 | import de.ugoe.cs.util.console.Console;
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * <p>
|
---|
[294] | 23 | * Command to generate test suite with a greedy strategy to achieve a desired
|
---|
| 24 | * coverage.
|
---|
[288] | 25 | * </p>
|
---|
[294] | 26 | *
|
---|
[288] | 27 | * @author Steffen Herbold
|
---|
| 28 | * @version 1.0
|
---|
| 29 | */
|
---|
| 30 | public class CMDgenerateGreedy implements Command {
|
---|
[294] | 31 |
|
---|
| 32 | /**
|
---|
| 33 | * <p>
|
---|
| 34 | * Tolerance for double comparisons
|
---|
| 35 | * </p>
|
---|
| 36 | */
|
---|
[311] | 37 | final static double eps = 0.000000000001;
|
---|
[288] | 38 |
|
---|
[294] | 39 | /*
|
---|
| 40 | * (non-Javadoc)
|
---|
| 41 | *
|
---|
| 42 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 43 | */
|
---|
[288] | 44 | @Override
|
---|
| 45 | public void run(List<Object> parameters) {
|
---|
| 46 | String modelname;
|
---|
| 47 | String sequencesName;
|
---|
| 48 | int minLength;
|
---|
| 49 | int maxLength;
|
---|
[293] | 50 | int coverageDepth;
|
---|
[288] | 51 | float desiredCoverage;
|
---|
[395] | 52 | boolean validEnd = true;
|
---|
[288] | 53 | try {
|
---|
| 54 | modelname = (String) parameters.get(0);
|
---|
| 55 | sequencesName = (String) parameters.get(1);
|
---|
| 56 | minLength = Integer.parseInt((String) parameters.get(2));
|
---|
| 57 | maxLength = Integer.parseInt((String) parameters.get(3));
|
---|
[293] | 58 | coverageDepth = Integer.parseInt((String) parameters.get(4));
|
---|
| 59 | desiredCoverage = Float.parseFloat((String) parameters.get(5));
|
---|
[395] | 60 | if (parameters.size() >= 7) {
|
---|
| 61 | validEnd = Boolean.parseBoolean((String) parameters.get(6));
|
---|
| 62 | }
|
---|
[288] | 63 | } catch (Exception e) {
|
---|
| 64 | throw new InvalidParameterException();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | IStochasticProcess model = null;
|
---|
| 68 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
| 69 | .getData(modelname);
|
---|
| 70 | if (dataObject == null) {
|
---|
| 71 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
| 72 | return;
|
---|
| 73 | } else if (!(dataObject instanceof IStochasticProcess)) {
|
---|
| 74 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
| 75 | return;
|
---|
| 76 | }
|
---|
| 77 | model = (IStochasticProcess) dataObject;
|
---|
[294] | 78 |
|
---|
[293] | 79 | // set up everything
|
---|
[395] | 80 | List<List<? extends Event<?>>> allSequences = new LinkedList<List<? extends Event<?>>>();
|
---|
[288] | 81 | for (int length = minLength; length <= maxLength; length++) {
|
---|
[395] | 82 | if (validEnd) {
|
---|
| 83 | allSequences.addAll(model.generateValidSequences(length + 2));
|
---|
| 84 | } else {
|
---|
| 85 | allSequences.addAll(model.generateSequences(length + 1, true));
|
---|
| 86 | }
|
---|
[288] | 87 | }
|
---|
[293] | 88 | Console.traceln("" + allSequences.size() + " possible");
|
---|
[294] | 89 |
|
---|
[395] | 90 | Collection<List<? extends Event<?>>> allSubSeqs = model
|
---|
| 91 | .generateSequences(coverageDepth);
|
---|
[294] | 92 | Map<List<? extends Event<?>>, Double> weightMap = SequenceTools
|
---|
| 93 | .generateWeights(model, allSubSeqs);
|
---|
[293] | 94 | Set<List<? extends Event<?>>> coveredSubSeqs = new LinkedHashSet<List<? extends Event<?>>>();
|
---|
[294] | 95 |
|
---|
[395] | 96 | List<Set<List<? extends Event<?>>>> containedSubSeqs = new LinkedList<Set<List<? extends Event<?>>>>();
|
---|
[294] | 97 | for (List<? extends Event<?>> sequence : allSequences) {
|
---|
| 98 | List<List<? extends Event<?>>> wrapper = new LinkedList<List<? extends Event<?>>>();
|
---|
[293] | 99 | wrapper.add(sequence);
|
---|
[294] | 100 | Set<List<? extends Event<?>>> currentSubSeqs = SequenceTools
|
---|
| 101 | .containedSubSequences(wrapper, coverageDepth);
|
---|
[293] | 102 | containedSubSeqs.add(currentSubSeqs);
|
---|
| 103 | }
|
---|
[294] | 104 |
|
---|
[293] | 105 | List<List<? extends Event<?>>> testSuite = new LinkedList<List<? extends Event<?>>>();
|
---|
| 106 | double currentCoverage = 0.0d;
|
---|
[294] | 107 |
|
---|
[293] | 108 | // Build test suite
|
---|
[395] | 109 | double prevGain = 1.0d;
|
---|
| 110 | boolean gainEqual = false;
|
---|
[294] | 111 | while (currentCoverage < desiredCoverage) {
|
---|
[395] | 112 | Double[] sequenceGain = new Double[allSequences.size()];
|
---|
| 113 | int i = 0;
|
---|
| 114 | for (Set<List<? extends Event<?>>> containedSubSeq : containedSubSeqs) {
|
---|
[293] | 115 | double gain = 0.0d;
|
---|
[395] | 116 | Iterator<List<? extends Event<?>>> subSeqIter = containedSubSeq
|
---|
| 117 | .iterator();
|
---|
| 118 | while (subSeqIter.hasNext()) {
|
---|
| 119 | List<? extends Event<?>> subSeq = subSeqIter.next();
|
---|
[294] | 120 | if (!coveredSubSeqs.contains(subSeq)) {
|
---|
[293] | 121 | gain += weightMap.get(subSeq);
|
---|
[395] | 122 | } else {
|
---|
| 123 | subSeqIter.remove();
|
---|
[293] | 124 | }
|
---|
| 125 | }
|
---|
| 126 | sequenceGain[i] = gain;
|
---|
[395] | 127 | // optimization using that the gain is monotonically decreasing
|
---|
| 128 | if (Math.abs(gain - prevGain) <= eps) {
|
---|
| 129 | gainEqual = true;
|
---|
| 130 | break;
|
---|
| 131 | }
|
---|
| 132 | i++;
|
---|
[293] | 133 | }
|
---|
[395] | 134 | int maxIndex;
|
---|
| 135 | if (gainEqual) {
|
---|
| 136 | maxIndex = i;
|
---|
| 137 | } else {
|
---|
| 138 | maxIndex = ArrayTools.findMax(sequenceGain);
|
---|
| 139 | }
|
---|
| 140 | if (maxIndex < 0 || sequenceGain[maxIndex] <= 0.0 + eps) {
|
---|
[293] | 141 | Console.traceln("No gain anymore! Desired coverage cannot be satisfied!");
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
[395] | 144 | prevGain = sequenceGain[maxIndex];
|
---|
[293] | 145 | testSuite.add(allSequences.get(maxIndex));
|
---|
| 146 | coveredSubSeqs.addAll(containedSubSeqs.get(maxIndex));
|
---|
[395] | 147 | currentCoverage += sequenceGain[maxIndex];
|
---|
| 148 | if (gainEqual) {
|
---|
| 149 | allSequences.remove(maxIndex);
|
---|
| 150 | containedSubSeqs.remove(maxIndex);
|
---|
| 151 | gainEqual = false;
|
---|
| 152 | } else {
|
---|
| 153 | for (int j = sequenceGain.length - 1; j >= 0; j--) {
|
---|
| 154 | if (j == maxIndex || sequenceGain[j] <= 0.0 + eps) {
|
---|
| 155 | allSequences.remove(j);
|
---|
| 156 | containedSubSeqs.remove(j);
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
[293] | 160 | }
|
---|
[294] | 161 |
|
---|
[293] | 162 | if (GlobalDataContainer.getInstance().addData(sequencesName, testSuite)) {
|
---|
[288] | 163 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
| 164 | }
|
---|
[293] | 165 | Console.println("" + testSuite.size() + " sequences generated");
|
---|
| 166 | Console.println("" + currentCoverage + " coverage achieved");
|
---|
[288] | 167 | }
|
---|
| 168 |
|
---|
[294] | 169 | /*
|
---|
| 170 | * (non-Javadoc)
|
---|
| 171 | *
|
---|
| 172 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 173 | */
|
---|
[288] | 174 | @Override
|
---|
| 175 | public void help() {
|
---|
[395] | 176 | Console.println("generateGreedy <modelname> <sequencesName> <minLength> <maxLength> <coverageDepth> <desiredCoverage> {<validEnd>}");
|
---|
[288] | 177 | }
|
---|
| 178 |
|
---|
| 179 | }
|
---|