source: trunk/quest-core-coverage/src/main/java/de/ugoe/cs/quest/coverage/CoverageCalculatorObserved.java @ 559

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