source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage/CoverageCalculator.java @ 123

Last change on this file since 123 was 123, checked in by sherbold, 13 years ago

+ added class de.ugoe.cs.eventbench.coverage.SequenceTools? and refactored static helper methods from de.ugoe.cs.eventbench.coverage.CoverageCalculater? into the new class
+ added class de.ugoe.cs.eventbench.coverage.CoverageCalculatorObserved? for calculation of coverage criteria with reference to the observed sequences

  • Property svn:mime-type set to text/plain
File size: 4.3 KB
Line 
1package de.ugoe.cs.eventbench.coverage;
2
3import java.security.InvalidParameterException;
4import java.util.Collection;
5import java.util.List;
6import java.util.Map;
7
8import de.ugoe.cs.eventbench.data.Event;
9import de.ugoe.cs.eventbench.models.IStochasticProcess;
10
11/**
12 * <p>
13 * This class calculates various types of sequence coverage in relation to a
14 * stochastic process.
15 * </p>
16 *
17 * @author Steffen Herbold
18 * @version 1.0
19 */
20public class CoverageCalculator {
21
22        /**
23         * <p>
24         * Stochastic process that is the foundation for probabilistic coverages and
25         * coverages with 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 final Collection<List<? extends Event<?>>> sequences;
36
37        /**
38         * <p>
39         * Length of the subsequences in relation to which the covarage is
40         * calculated.
41         * </p>
42         */
43        private final int length;
44
45        /**
46         * <p>
47         * All subsequences of {@link #length} of {@link #sequences}.
48         * </p>
49         */
50        private Collection<List<? extends Event<?>>> containedSubSeqs = null;
51
52        /**
53         * <p>
54         * All subsequences of {@link #length} that can be generated by
55         * {@link #process}.
56         * </p>
57         */
58        private Collection<List<? extends Event<?>>> allPossibleSubSeqs = null;
59
60        /**
61         * <p>
62         * The probabilities of al subsequences of {@link #length} according to
63         * {@link #process}.
64         * </p>
65         */
66        private Map<List<? extends Event<?>>, Double> subSeqWeights = null;
67
68        /**
69         * <p>
70         * Constructor. Creates a new CoverageCalculator for a given stochastic
71         * process and generated sequences.
72         * </p>
73         *
74         * @param process
75         *            stochastic process used for coverage calculations; must not be
76         *            null
77         * @param sequences
78         *            sequences for which the coverage is calculated; must not be
79         *            null
80         * @param length
81         *            length of the subsequences for which the coverage is analyzed;
82         *            must be >0
83         */
84        public CoverageCalculator(IStochasticProcess process,
85                        Collection<List<? extends Event<?>>> sequences, int length) {
86                if (process == null) {
87                        throw new InvalidParameterException("process must not be null");
88                }
89                if (sequences == null) {
90                        throw new InvalidParameterException("sequences must not be null");
91                }
92                if (length <= 0) {
93                        throw new InvalidParameterException(
94                                        "length must be >0; actual value: " + length);
95                }
96                this.process = process;
97                this.sequences = sequences;
98                this.length = length;
99        }
100
101        /**
102         * <p>
103         * Calculates the percentage of subsequences of length k that occur,
104         * including those that cannot be generated by {@link #process}.
105         * </p>
106         *
107         * @return coverage percentage
108         */
109        public double getCoverageAllNoWeight() {
110                if (containedSubSeqs == null) {
111                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
112                                        length);
113                }
114                return ((double) containedSubSeqs.size())
115                                / SequenceTools.numSequences(process, length);
116        }
117
118        /**
119         * <p>
120         * Calculates the percentage of subsequences of length k that occur and can
121         * generated by {@link #process}.
122         * </p>
123         *
124         * @return coverage percentage
125         */
126        public double getCoveragePossibleNoWeight() {
127                if (containedSubSeqs == null) {
128                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
129                                        length);
130                }
131                if (allPossibleSubSeqs == null) {
132                        allPossibleSubSeqs = process.generateSequences(length);
133                }
134                return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size();
135        }
136
137        /**
138         * <p>
139         * Calculates the weight of the subsequences that occur with relation to
140         * {@link #process}, i.e., the mass of the subsequence probability covered
141         * by the subsequences.
142         * </p>
143         *
144         * @return coverage weight
145         */
146        public double getCoveragePossibleWeight() {
147                if (containedSubSeqs == null) {
148                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
149                                        length);
150                }
151                if (allPossibleSubSeqs == null) {
152                        allPossibleSubSeqs = process.generateSequences(length);
153                }
154                if (subSeqWeights == null) {
155                        subSeqWeights = SequenceTools.generateWeights(process,
156                                        allPossibleSubSeqs);
157                }
158                double weight = 0.0;
159                for (List<? extends Event<?>> subSeq : containedSubSeqs) {
160                        weight += subSeqWeights.get(subSeq);
161                }
162                return weight;
163        }
164
165}
Note: See TracBrowser for help on using the repository browser.