1 | package de.ugoe.cs.eventbench.coverage;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.LinkedHashSet;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.eventbench.data.Event;
|
---|
10 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * <p>
|
---|
14 | * This class calculates various types of sequence coverage in relation to a
|
---|
15 | * collection of observed sequences.
|
---|
16 | * </p>
|
---|
17 | *
|
---|
18 | * @author Steffen Herbold
|
---|
19 | * @version 1.0
|
---|
20 | */
|
---|
21 | public class CoverageCalculatorObserved {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * <p>
|
---|
25 | * Sequences for which the coverage is calculated.
|
---|
26 | * </p>
|
---|
27 | */
|
---|
28 | private final Collection<List<? extends Event<?>>> sequences;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * <p>
|
---|
32 | * Observed sequences that are baseline for the coverage calculation.
|
---|
33 | * </p>
|
---|
34 | */
|
---|
35 | private final Collection<List<? extends Event<?>>> observedSequences;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * <p>
|
---|
39 | * Length of the subsequences in relation to which the covarage is
|
---|
40 | * calculated.
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | private final int length;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * All subsequences of {@link #length} of {@link #sequences}.
|
---|
48 | * </p>
|
---|
49 | */
|
---|
50 | private Collection<List<? extends Event<?>>> subSeqsGenerated = null;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * <p>
|
---|
54 | * All subsequences of {@link #length} of {@link #observedSequences}.
|
---|
55 | * </p>
|
---|
56 | */
|
---|
57 | private Collection<List<? extends Event<?>>> subSeqsObserved = null;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * <p>
|
---|
61 | * Constructor. Creates a new CoverageCalculatorObserved for given
|
---|
62 | * collections of observed sequences and generated sequences.
|
---|
63 | * </p>
|
---|
64 | *
|
---|
65 | * @param observedSequences
|
---|
66 | * observed sequences in relation to which the coverage is
|
---|
67 | * calculated; must not be null
|
---|
68 | * @param sequences
|
---|
69 | * sequences for which the coverage is calculated; must not be
|
---|
70 | * null
|
---|
71 | * @param length
|
---|
72 | * length of the subsequences for which the coverage is analyzed;
|
---|
73 | * must be >0
|
---|
74 | * @throws InvalidParameterException
|
---|
75 | * thrown if observedSequences or sequences is null or length
|
---|
76 | * less than or equal to 0
|
---|
77 | */
|
---|
78 | public CoverageCalculatorObserved(
|
---|
79 | Collection<List<? extends Event<?>>> observedSequences,
|
---|
80 | Collection<List<? extends Event<?>>> sequences, int length) {
|
---|
81 | if (observedSequences == null) {
|
---|
82 | throw new InvalidParameterException(
|
---|
83 | "observed sequences must not be null");
|
---|
84 | }
|
---|
85 | if (sequences == null) {
|
---|
86 | throw new InvalidParameterException("sequences must not be null");
|
---|
87 | }
|
---|
88 | if (length <= 0) {
|
---|
89 | throw new InvalidParameterException(
|
---|
90 | "length must be >0; actual value: " + length);
|
---|
91 | }
|
---|
92 | this.observedSequences = observedSequences;
|
---|
93 | this.sequences = sequences;
|
---|
94 | this.length = length;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * <p>
|
---|
99 | * Calculates the percentage of subsequences of length k that occur, with
|
---|
100 | * reference to those that were observed.
|
---|
101 | * </p>
|
---|
102 | *
|
---|
103 | * @return coverage percentage
|
---|
104 | */
|
---|
105 | public double getCoverageObserved() {
|
---|
106 | createSubSeqs();
|
---|
107 | Collection<List<? extends Event<?>>> subSeqsObservedCopy = new LinkedHashSet<List<? extends Event<?>>>(
|
---|
108 | subSeqsObserved);
|
---|
109 | subSeqsObservedCopy.retainAll(subSeqsGenerated);
|
---|
110 | return ((double) subSeqsObservedCopy.size()) / subSeqsObserved.size();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * <p>
|
---|
115 | * Calculates the weight of subsequences of length k that occur, with
|
---|
116 | * reference to those that were observed.
|
---|
117 | * </p>
|
---|
118 | *
|
---|
119 | * @param process
|
---|
120 | * stochastic process in reference to which the weight is
|
---|
121 | * calculated
|
---|
122 | * @return coverage percentage
|
---|
123 | */
|
---|
124 |
|
---|
125 | public double getCoverageObservedWeigth(IStochasticProcess process) {
|
---|
126 | createSubSeqs();
|
---|
127 | Map<List<? extends Event<?>>, Double> weightMap = SequenceTools
|
---|
128 | .generateWeights(process, subSeqsObserved);
|
---|
129 |
|
---|
130 | Collection<List<? extends Event<?>>> subSeqsObservedCopy = new LinkedHashSet<List<? extends Event<?>>>(
|
---|
131 | subSeqsObserved);
|
---|
132 | subSeqsObservedCopy.retainAll(subSeqsGenerated);
|
---|
133 | double weight = 0.0d;
|
---|
134 | for (List<? extends Event<?>> subSeq : subSeqsObservedCopy) {
|
---|
135 | weight += weightMap.get(subSeq);
|
---|
136 | }
|
---|
137 | return weight;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * <p>
|
---|
142 | * Calculates the percentage of generated subsequences of length k that
|
---|
143 | * occur and have not been observed, with reference to all generated
|
---|
144 | * subsequences.
|
---|
145 | * </p>
|
---|
146 | *
|
---|
147 | * @return coverage percentage
|
---|
148 | */
|
---|
149 | public double getNewPercentage() {
|
---|
150 | createSubSeqs();
|
---|
151 | Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
|
---|
152 | subSeqsGenerated);
|
---|
153 | subSeqsGeneratedCopy.removeAll(subSeqsObserved);
|
---|
154 | return ((double) subSeqsGeneratedCopy.size()) / subSeqsGenerated.size();
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * <p>
|
---|
159 | * Calculates the percentage of generated subsequences of length k that
|
---|
160 | * occur and have not been observed, with references to all possible new
|
---|
161 | * subsequences.
|
---|
162 | * </p>
|
---|
163 | *
|
---|
164 | * @param process
|
---|
165 | * stochastic process which is used to determine which
|
---|
166 | * subsequences are possible
|
---|
167 | * @return coverage percentage
|
---|
168 | * @throws InvalidParameterException
|
---|
169 | * thrown if process is null
|
---|
170 | */
|
---|
171 | public double getCoveragePossibleNew(IStochasticProcess process) {
|
---|
172 | if (process == null) {
|
---|
173 | throw new InvalidParameterException("process must not be null");
|
---|
174 | }
|
---|
175 | createSubSeqs();
|
---|
176 | Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
|
---|
177 | subSeqsGenerated);
|
---|
178 | Collection<List<? extends Event<?>>> subSeqsPossible = process
|
---|
179 | .generateSequences(length);
|
---|
180 | subSeqsGeneratedCopy.removeAll(subSeqsObserved);
|
---|
181 | subSeqsPossible.removeAll(subSeqsObserved);
|
---|
182 | int possibleSize = subSeqsPossible.size();
|
---|
183 | subSeqsPossible.retainAll(subSeqsGeneratedCopy);
|
---|
184 | return ((double) subSeqsPossible.size()) / possibleSize;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * <p>
|
---|
189 | * Calculates the weight of generated subsequences of length k that occur
|
---|
190 | * and have not been observed, with references to all possible new
|
---|
191 | * subsequences.
|
---|
192 | * </p>
|
---|
193 | *
|
---|
194 | * @param process
|
---|
195 | * stochastic process which is used to determine the weights and
|
---|
196 | * which subsequences are possible
|
---|
197 | * @return coverage percentage
|
---|
198 | * @throws InvalidParameterException
|
---|
199 | * thrown if process is null
|
---|
200 | */
|
---|
201 | public double getCoveragePossibleNewWeight(IStochasticProcess process) {
|
---|
202 | if (process == null) {
|
---|
203 | throw new InvalidParameterException("process must not be null");
|
---|
204 | }
|
---|
205 | createSubSeqs();
|
---|
206 | Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
|
---|
207 | subSeqsGenerated);
|
---|
208 | Collection<List<? extends Event<?>>> subSeqsPossible = process
|
---|
209 | .generateSequences(length);
|
---|
210 | subSeqsGeneratedCopy.removeAll(subSeqsObserved);
|
---|
211 | subSeqsPossible.removeAll(subSeqsObserved);
|
---|
212 | Map<List<? extends Event<?>>, Double> weightMap = SequenceTools
|
---|
213 | .generateWeights(process, subSeqsPossible);
|
---|
214 | double weight = 0.0d;
|
---|
215 | for (List<? extends Event<?>> subSeq : subSeqsGeneratedCopy) {
|
---|
216 | Double currentWeight = weightMap.get(subSeq);
|
---|
217 | if( currentWeight!=null ) {
|
---|
218 | weight += currentWeight;
|
---|
219 | }
|
---|
220 | }
|
---|
221 | return weight;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * <p>
|
---|
226 | * Returns the number of covered subsequences of length k.
|
---|
227 | * </p>
|
---|
228 | *
|
---|
229 | * @return number of covered subsequences
|
---|
230 | */
|
---|
231 | public int getNumObserved() {
|
---|
232 | createSubSeqs();
|
---|
233 | return subSeqsObserved.size();
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * <p>
|
---|
238 | * Returns the number of covered subsequences of length k.
|
---|
239 | * </p>
|
---|
240 | *
|
---|
241 | * @return number of covered subsequences
|
---|
242 | */
|
---|
243 | public int getNumCovered() {
|
---|
244 | createSubSeqs();
|
---|
245 | return subSeqsGenerated.size();
|
---|
246 | }
|
---|
247 |
|
---|
248 | public int getNumNew() {
|
---|
249 | createSubSeqs();
|
---|
250 | Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
|
---|
251 | subSeqsGenerated);
|
---|
252 | subSeqsGeneratedCopy.removeAll(subSeqsObserved);
|
---|
253 | return subSeqsGeneratedCopy.size();
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * <p>
|
---|
258 | * Helper function that calcuates the subsequences of length k that have
|
---|
259 | * been observed and generated.
|
---|
260 | * </p>
|
---|
261 | */
|
---|
262 | private void createSubSeqs() {
|
---|
263 | if (subSeqsObserved == null) {
|
---|
264 | subSeqsObserved = SequenceTools.containedSubSequences(
|
---|
265 | observedSequences, length);
|
---|
266 | }
|
---|
267 | if (subSeqsGenerated == null) {
|
---|
268 | subSeqsGenerated = SequenceTools.containedSubSequences(sequences,
|
---|
269 | length);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|