source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage/CoverageCalculatorObserved.java @ 126

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

+ added commands to obtain absolut number of observed/covered/new/possible subsequences of a given length

  • Property svn:mime-type set to text/plain
File size: 7.5 KB
Line 
1package de.ugoe.cs.eventbench.coverage;
2
3import java.security.InvalidParameterException;
4import java.util.Collection;
5import java.util.LinkedHashSet;
6import java.util.List;
7import java.util.Map;
8
9import de.ugoe.cs.eventbench.data.Event;
10import de.ugoe.cs.eventbench.models.IStochasticProcess;
11
12/**
13 * <p>
14 * This class calculates various types of sequence coverage in relation to a
15 * collection of observed sequences.
16 * </p>
17 *
18 * @author Steffen Herbold
19 * @version 1.0
20 */
21public class CoverageCalculatorObserved {
22
23        /**
24         * <p>
25         * Sequences for which the coverage is calculated.
26         * </p>
27         */
28        private final Collection<List<? extends Event<?>>> sequences;
29
30        /**
31         * <p>
32         * Observed sequences that are baseline for the coverage calculation.
33         * </p>
34         */
35        private final Collection<List<? extends Event<?>>> observedSequences;
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<?>>> subSeqsGenerated = null;
51
52        /**
53         * <p>
54         * All subsequences of {@link #length} of {@link #observedSequences}.
55         * </p>
56         */
57        private Collection<List<? extends Event<?>>> subSeqsObserved = null;
58
59        /**
60         * <p>
61         * Constructor. Creates a new CoverageCalculatorObserved for given
62         * collections of observed sequences and generated sequences.
63         * </p>
64         *
65         * @param observedSequences
66         *            observed sequences in relation to which the coverage is
67         *            calculated; must not be null
68         * @param sequences
69         *            sequences for which the coverage is calculated; must not be
70         *            null
71         * @param length
72         *            length of the subsequences for which the coverage is analyzed;
73         *            must be >0
74         */
75        public CoverageCalculatorObserved(
76                        Collection<List<? extends Event<?>>> observedSequences,
77                        Collection<List<? extends Event<?>>> sequences, int length) {
78                if (observedSequences == null) {
79                        throw new InvalidParameterException("process must not be null");
80                }
81                if (sequences == null) {
82                        throw new InvalidParameterException("sequences must not be null");
83                }
84                if (length <= 0) {
85                        throw new InvalidParameterException(
86                                        "length must be >0; actual value: " + length);
87                }
88                this.observedSequences = observedSequences;
89                this.sequences = sequences;
90                this.length = length;
91        }
92
93        /**
94         * <p>
95         * Calculates the percentage of subsequences of length k that occur, with
96         * reference to those that were observed.
97         * </p>
98         *
99         * @return coverage percentage
100         */
101        public double getCoverageObserved() {
102                createSubSeqs();
103                Collection<List<? extends Event<?>>> subSeqsObservedCopy = new LinkedHashSet<List<? extends Event<?>>>(
104                                subSeqsObserved);
105                subSeqsObservedCopy.retainAll(subSeqsGenerated);
106                return ((double) subSeqsObservedCopy.size()) / subSeqsObserved.size();
107        }
108
109        /**
110         * <p>
111         * Calculates the weight of subsequences of length k that occur, with
112         * reference to those that were observed.
113         * </p>
114         *
115         * @param process
116         *            stochastic process in reference to which the weight is
117         *            calculated
118         * @return coverage percentage
119         */
120
121        public double getCoverageObservedWeigth(IStochasticProcess process) {
122                createSubSeqs();
123                Map<List<? extends Event<?>>, Double> weightMap = SequenceTools
124                                .generateWeights(process, subSeqsObserved);
125
126                Collection<List<? extends Event<?>>> subSeqsObservedCopy = new LinkedHashSet<List<? extends Event<?>>>(
127                                subSeqsObserved);
128                subSeqsObservedCopy.retainAll(subSeqsGenerated);
129                double weight = 0.0d;
130                for (List<? extends Event<?>> subSeq : subSeqsObservedCopy) {
131                        weight += weightMap.get(subSeq);
132                }
133                return weight;
134        }
135
136        /**
137         * <p>
138         * Calculates the percentage of generated subsequences of length k that
139         * occur and have not been observed, with reference to all generated
140         * subsequences.
141         * </p>
142         *
143         * @return coverage percentage
144         */
145        public double getNewPercentage() {
146                createSubSeqs();
147                Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
148                                subSeqsGenerated);
149                subSeqsGeneratedCopy.removeAll(subSeqsObserved);
150                return ((double) subSeqsGeneratedCopy.size()) / subSeqsGenerated.size();
151        }
152
153        /**
154         * <p>
155         * Calculates the percentage of generated subsequences of length k that
156         * occur and have not been observed, with references to all possible new
157         * subsequences.
158         * </p>
159         *
160         * @param process
161         *            stochastic process which is used to determine which
162         *            subsequences are possible
163         * @return coverage percentage
164         */
165        public double getCoveragePossibleNew(IStochasticProcess process) {
166                createSubSeqs();
167                Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
168                                subSeqsGenerated);
169                Collection<List<? extends Event<?>>> subSeqsPossible = process
170                                .generateSequences(length);
171                subSeqsGeneratedCopy.removeAll(subSeqsObserved);
172                subSeqsPossible.removeAll(subSeqsObserved);
173                int possibleSize = subSeqsPossible.size();
174                subSeqsPossible.retainAll(subSeqsGeneratedCopy);
175                return ((double) subSeqsPossible.size()) / possibleSize;
176        }
177
178        /**
179         * <p>
180         * Calculates the weight of generated subsequences of length k that occur
181         * and have not been observed, with references to all possible new
182         * subsequences.
183         * </p>
184         *
185         * @param process
186         *            stochastic process which is used to determine the weights and
187         *            which subsequences are possible
188         * @return coverage percentage
189         */
190        public double getCoveragePossibleNewWeight(IStochasticProcess process) {
191                createSubSeqs();
192                Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
193                                subSeqsGenerated);
194                Collection<List<? extends Event<?>>> subSeqsPossible = process
195                                .generateSequences(length);
196                subSeqsGeneratedCopy.removeAll(subSeqsObserved);
197                subSeqsPossible.removeAll(subSeqsObserved);
198                Map<List<? extends Event<?>>, Double> weightMap = SequenceTools
199                                .generateWeights(process, subSeqsPossible);
200                double weight = 0.0d;
201                for (List<? extends Event<?>> subSeq : subSeqsGeneratedCopy) {
202                        weight += weightMap.get(subSeq);
203                }
204                return weight;
205        }
206       
207        /**
208         * <p>
209         * Returns the number of covered subsequences of length k.
210         * </p>
211         * 
212         * @return number of covered subsequences
213         */
214        public int getNumObserved() {
215                createSubSeqs();
216                return subSeqsObserved.size();
217        }
218       
219        /**
220         * <p>
221         * Returns the number of covered subsequences of length k.
222         * </p>
223         * @return
224         */
225        public int getNumCovered() {
226                createSubSeqs();
227                return subSeqsGenerated.size();
228        }
229       
230        public int getNumNew() {
231                createSubSeqs();
232                Collection<List<? extends Event<?>>> subSeqsGeneratedCopy = new LinkedHashSet<List<? extends Event<?>>>(
233                                subSeqsGenerated);
234                subSeqsGeneratedCopy.removeAll(subSeqsObserved);
235                return subSeqsGeneratedCopy.size();
236        }
237       
238
239        /**
240         * <p>
241         * Helper function that calcuates the subsequences of length k that have
242         * been observed and generated.
243         * </p>
244         */
245        private void createSubSeqs() {
246                if (subSeqsObserved == null) {
247                        subSeqsObserved = SequenceTools.containedSubSequences(
248                                        observedSequences, length);
249                }
250                if (subSeqsGenerated == null) {
251                        subSeqsGenerated = SequenceTools.containedSubSequences(sequences,
252                                        length);
253                }
254        }
255}
Note: See TracBrowser for help on using the repository browser.