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

Last change on this file since 331 was 331, checked in by sherbold, 12 years ago
  • 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                        weight += subSeqWeights.get(subSeq);
163                }
164                return weight;
165        }
166
167        /**
168         * <p>
169         * Returns the number of covered subsequences of length k.
170         * </p>
171         *
172         * @return number of covered subsequences
173         */
174        public int getNumCovered() {
175                if (containedSubSeqs == null) {
176                        containedSubSeqs = SequenceTools.containedSubSequences(sequences,
177                                        length);
178                }
179                return containedSubSeqs.size();
180        }
181
182        /**
183         * <p>
184         * Returns the number of possible subsequences of length k according to the
185         * stochastic process.
186         * </p>
187         *
188         * @return number of possible subsequences
189         */
190        public int getNumPossible() {
191                if (allPossibleSubSeqs == null) {
192                        allPossibleSubSeqs = process.generateSequences(length);
193                }
194                return allPossibleSubSeqs.size();
195        }
196
197        /**
198         * <p>
199         * Sets a new collection of sequences for which the coverage is analyzed.
200         * </p>
201         *
202         * @param newSequences
203         *            new collection of sequences
204         * @throws InvalidParameterException
205         *             thrown is newSequences is null
206         */
207        public void setSequences(Collection<List<? extends Event<?>>> newSequences) {
208                if (newSequences == null) {
209                        throw new InvalidParameterException("sequences must not be null");
210                }
211                this.sequences = newSequences;
212                containedSubSeqs = null;
213        }
214
215}
Note: See TracBrowser for help on using the repository browser.