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