source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage/CoverageCalculatorProcess.java @ 420

Last change on this file since 420 was 420, checked in by sherbold, 12 years ago
  • fixed bug with possible NaN values in coverage calculation
  • Property svn:mime-type set to text/plain
File size: 5.7 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 CoverageCalculatorProcess {
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 Collection<List<? extends Event<?>>> sequences;
36
37        /**
38         * <p>
39         * Length of the subsequences in relation to which the coverage 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 all 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 CoverageCalculatorProcess for a given
71         * stochastic 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         * @throws InvalidParameterException
84         *             thrown if process or sequences is null or length less than or equal to 0
85         */
86        public CoverageCalculatorProcess(IStochasticProcess process,
87                        Collection<List<? extends Event<?>>> sequences, int length) {
88                if (process == null) {
89                        throw new InvalidParameterException("process must not be null");
90                }
91                if (sequences == null) {
92                        throw new InvalidParameterException("sequences must not be null");
93                }
94                if (length <= 0) {
95                        throw new InvalidParameterException(
96                                        "length must be >0; actual value: " + length);
97                }
98                this.process = process;
99                this.sequences = sequences;
100                this.length = length;
101        }
102
103        /**
104         * <p>
105         * Calculates the percentage of subsequences of length k that occur,
106         * including those that cannot be generated by {@link #process}.
107         * </p>
108         *
109         * @return coverage percentage
110         */
111        public double getCoverageAllNoWeight() {
112                if (containedSubSeqs == null) {
113                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
114                                        length);
115                }
116                return ((double) containedSubSeqs.size())
117                                / SequenceTools.numSequences(process, length);
118        }
119
120        /**
121         * <p>
122         * Calculates the percentage of subsequences of length k that occur and can
123         * generated by {@link #process}.
124         * </p>
125         *
126         * @return coverage percentage
127         */
128        public double getCoveragePossibleNoWeight() {
129                if (containedSubSeqs == null) {
130                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
131                                        length);
132                }
133                if (allPossibleSubSeqs == null) {
134                        allPossibleSubSeqs = process.generateSequences(length);
135                }
136                return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size();
137        }
138
139        /**
140         * <p>
141         * Calculates the weight of the subsequences that occur with relation to
142         * {@link #process}, i.e., the mass of the subsequence probability covered
143         * by the subsequences.
144         * </p>
145         *
146         * @return coverage weight
147         */
148        public double getCoveragePossibleWeight() {
149                if (containedSubSeqs == null) {
150                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
151                                        length);
152                }
153                if (allPossibleSubSeqs == null) {
154                        allPossibleSubSeqs = process.generateSequences(length);
155                }
156                if (subSeqWeights == null) {
157                        subSeqWeights = SequenceTools.generateWeights(process,
158                                        allPossibleSubSeqs);
159                }
160                double weight = 0.0;
161                for (List<? extends Event<?>> subSeq : containedSubSeqs) {
162                        Double curWeight = subSeqWeights.get(subSeq);
163                        if( curWeight!=null ) {
164                                weight += curWeight;
165                        }
166                }
167                return weight;
168        }
169
170        /**
171         * <p>
172         * Returns the number of covered subsequences of length k.
173         * </p>
174         *
175         * @return number of covered subsequences
176         */
177        public int getNumCovered() {
178                if (containedSubSeqs == null) {
179                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
180                                        length);
181                }
182                return containedSubSeqs.size();
183        }
184
185        /**
186         * <p>
187         * Returns the number of possible subsequences of length k according to the
188         * stochastic process.
189         * </p>
190         *
191         * @return number of possible subsequences
192         */
193        public int getNumPossible() {
194                if (allPossibleSubSeqs == null) {
195                        allPossibleSubSeqs = process.generateSequences(length);
196                }
197                return allPossibleSubSeqs.size();
198        }
199
200        /**
201         * <p>
202         * Sets a new collection of sequences for which the coverage is analyzed.
203         * </p>
204         *
205         * @param newSequences
206         *            new collection of sequences
207         * @throws InvalidParameterException
208         *             thrown is newSequences is null
209         */
210        public void setSequences(Collection<List<? extends Event<?>>> newSequences) {
211                if (newSequences == null) {
212                        throw new InvalidParameterException("sequences must not be null");
213                }
214                this.sequences = newSequences;
215                containedSubSeqs = null;
216        }
217
218}
Note: See TracBrowser for help on using the repository browser.