source: trunk/autoquest-core-coverage/src/main/java/de/ugoe/cs/autoquest/coverage/CoverageCalculatorProcess.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: 6.9 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.List;
19import java.util.Map;
20
21import de.ugoe.cs.autoquest.eventcore.Event;
22import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
23
24/**
25 * <p>
26 * This class calculates various types of sequence coverage in relation to a stochastic process.
27 * </p>
28 *
29 * @author Steffen Herbold
30 * @version 1.0
31 */
32public class CoverageCalculatorProcess {
33
34    /**
35     * <p>
36     * Stochastic process that is the foundation for probabilistic coverages and coverages with
37     * reference to all possible sequences.
38     * </p>
39     */
40    private final IStochasticProcess process;
41
42    /**
43     * <p>
44     * Sequences for which the coverage is calculated.
45     * </p>
46     */
47    private Collection<List<Event>> sequences;
48
49    /**
50     * <p>
51     * Length of the subsequences in relation to which the coverage is calculated.
52     * </p>
53     */
54    private final int length;
55
56    /**
57     * <p>
58     * All subsequences of {@link #length} of {@link #sequences}.
59     * </p>
60     */
61    private Collection<List<Event>> containedSubSeqs = null;
62
63    /**
64     * <p>
65     * All subsequences of {@link #length} that can be generated by {@link #process}.
66     * </p>
67     */
68    private Collection<List<Event>> allPossibleSubSeqs = null;
69
70    /**
71     * <p>
72     * The probabilities of all subsequences of {@link #length} according to {@link #process}.
73     * </p>
74     */
75    private Map<List<Event>, Double> subSeqWeights = null;
76
77    /**
78     * <p>
79     * Constructor. Creates a new CoverageCalculatorProcess for a given stochastic process and
80     * generated sequences.
81     * </p>
82     *
83     * @param process
84     *            stochastic process used for coverage calculations; must not be null
85     * @param sequences
86     *            sequences for which the coverage is calculated; must not be null
87     * @param length
88     *            length of the subsequences for which the coverage is analyzed; must be >0
89     * @throws IllegalArgumentException
90     *             thrown if process or sequences is null or length less than or equal to 0
91     */
92    public CoverageCalculatorProcess(IStochasticProcess process,
93                                     Collection<List<Event>> sequences,
94                                     int length)
95    {
96        if (process == null) {
97            throw new IllegalArgumentException("process must not be null");
98        }
99        if (sequences == null) {
100            throw new IllegalArgumentException("sequences must not be null");
101        }
102        if (length <= 0) {
103            throw new IllegalArgumentException("length must be >0; actual value: " + length);
104        }
105        this.process = process;
106        this.sequences = sequences;
107        this.length = length;
108    }
109
110    /**
111     * <p>
112     * Calculates the percentage of subsequences of length k that occur, including those that cannot
113     * be generated by {@link #process}.
114     * </p>
115     *
116     * @return coverage percentage
117     */
118    public double getCoverageAllNoWeight() {
119        if (containedSubSeqs == null) {
120            containedSubSeqs = SequenceTools.containedSubSequences(sequences, length);
121        }
122        return ((double) containedSubSeqs.size()) / SequenceTools.numSequences(process, length);
123    }
124
125    /**
126     * <p>
127     * Calculates the percentage of subsequences of length k that occur and can generated by
128     * {@link #process}.
129     * </p>
130     *
131     * @return coverage percentage
132     */
133    public double getCoveragePossibleNoWeight() {
134        if (containedSubSeqs == null) {
135            containedSubSeqs = SequenceTools.containedSubSequences(sequences, length);
136        }
137        if (allPossibleSubSeqs == null) {
138            allPossibleSubSeqs = process.generateSequences(length);
139        }
140        return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size();
141    }
142
143    /**
144     * <p>
145     * Calculates the weight of the subsequences that occur with relation to {@link #process}, i.e.,
146     * the mass of the subsequence probability covered by the subsequences.
147     * </p>
148     *
149     * @return coverage weight
150     */
151    public double getCoveragePossibleWeight() {
152        if (containedSubSeqs == null) {
153            containedSubSeqs = SequenceTools.containedSubSequences(sequences, length);
154        }
155        if (allPossibleSubSeqs == null) {
156            allPossibleSubSeqs = process.generateSequences(length);
157        }
158        if (subSeqWeights == null) {
159            subSeqWeights = SequenceTools.generateWeights(process, allPossibleSubSeqs);
160        }
161        double weight = 0.0;
162        for (List<Event> subSeq : containedSubSeqs) {
163            Double curWeight = subSeqWeights.get(subSeq);
164            if (curWeight != null) {
165                weight += curWeight;
166            }
167        }
168        return weight;
169    }
170
171    /**
172     * <p>
173     * Returns the number of covered subsequences of length k.
174     * </p>
175     *
176     * @return number of covered subsequences
177     */
178    public int getNumCovered() {
179        if (containedSubSeqs == null) {
180            containedSubSeqs = SequenceTools.containedSubSequences(sequences, length);
181        }
182        return containedSubSeqs.size();
183    }
184
185    /**
186     * <p>
187     * Returns the number of possible subsequences of length k according to the stochastic process.
188     * </p>
189     *
190     * @return number of possible subsequences
191     */
192    public int getNumPossible() {
193        if (allPossibleSubSeqs == null) {
194            allPossibleSubSeqs = process.generateSequences(length);
195        }
196        return allPossibleSubSeqs.size();
197    }
198
199    /**
200     * <p>
201     * Sets a new collection of sequences for which the coverage is analyzed.
202     * </p>
203     *
204     * @param newSequences
205     *            new collection of sequences
206     * @throws IllegalArgumentException
207     *             thrown is newSequences is null
208     */
209    public void setSequences(Collection<List<Event>> newSequences) {
210        if (newSequences == null) {
211            throw new IllegalArgumentException("sequences must not be null");
212        }
213        this.sequences = newSequences;
214        containedSubSeqs = null;
215    }
216
217}
Note: See TracBrowser for help on using the repository browser.