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