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

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