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

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