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

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