Changeset 123 for trunk/EventBenchCore/src/de/ugoe/cs/eventbench
- Timestamp:
- 07/14/11 12:04:30 (13 years ago)
- Location:
- trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage/CoverageCalculator.java
r118 r123 3 3 import java.security.InvalidParameterException; 4 4 import java.util.Collection; 5 import java.util.LinkedHashMap;6 import java.util.LinkedHashSet;7 import java.util.LinkedList;8 5 import java.util.List; 9 6 import java.util.Map; 10 import java.util.Set;11 7 12 8 import de.ugoe.cs.eventbench.data.Event; … … 15 11 /** 16 12 * <p> 17 * This class calculates various types of sequence coverage. 13 * This class calculates various types of sequence coverage in relation to a 14 * stochastic process. 18 15 * </p> 19 16 * … … 76 73 * 77 74 * @param process 78 * stochastic process used for coverage calculations; must not be null 75 * stochastic process used for coverage calculations; must not be 76 * null 79 77 * @param sequences 80 78 * sequences for which the coverage is calculated; must not be … … 86 84 public CoverageCalculator(IStochasticProcess process, 87 85 Collection<List<? extends Event<?>>> sequences, int length) { 88 if ( process==null) {86 if (process == null) { 89 87 throw new InvalidParameterException("process must not be null"); 90 88 } 91 if ( sequences==null) {89 if (sequences == null) { 92 90 throw new InvalidParameterException("sequences must not be null"); 93 91 } 94 if( length<=0 ) { 95 throw new InvalidParameterException("length must be >0; actual value: " + length); 92 if (length <= 0) { 93 throw new InvalidParameterException( 94 "length must be >0; actual value: " + length); 96 95 } 97 96 this.process = process; … … 102 101 /** 103 102 * <p> 104 * Calculates the percentage of subsequences of length k that existoccur,103 * Calculates the percentage of subsequences of length k that occur, 105 104 * including those that cannot be generated by {@link #process}. 106 105 * </p> … … 110 109 public double getCoverageAllNoWeight() { 111 110 if (containedSubSeqs == null) { 112 containedSubSeqs = containedSubSequences(sequences, length); 111 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 112 length); 113 113 } 114 114 return ((double) containedSubSeqs.size()) 115 / numSequences(process, length);115 / SequenceTools.numSequences(process, length); 116 116 } 117 117 … … 126 126 public double getCoveragePossibleNoWeight() { 127 127 if (containedSubSeqs == null) { 128 containedSubSeqs = containedSubSequences(sequences, length); 128 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 129 length); 129 130 } 130 131 if (allPossibleSubSeqs == null) { … … 145 146 public double getCoveragePossibleWeight() { 146 147 if (containedSubSeqs == null) { 147 containedSubSeqs = containedSubSequences(sequences, length); 148 containedSubSeqs = SequenceTools.containedSubSequences(sequences, 149 length); 148 150 } 149 151 if (allPossibleSubSeqs == null) { … … 151 153 } 152 154 if (subSeqWeights == null) { 153 subSeqWeights = generateWeights(process, allPossibleSubSeqs); 155 subSeqWeights = SequenceTools.generateWeights(process, 156 allPossibleSubSeqs); 154 157 } 155 158 double weight = 0.0; … … 160 163 } 161 164 162 /**163 * <p>164 * Calculates the weights for all sequences passed to this function as165 * defined by the stochastic process and stores them in a {@link Map}.166 * </p>167 *168 * @param process169 * process used for weight calculation170 * @param sequences171 * sequences for which the weights are calculated172 * @return {@link Map} of weights173 */174 private static Map<List<? extends Event<?>>, Double> generateWeights(175 IStochasticProcess process,176 Collection<List<? extends Event<?>>> sequences) {177 Map<List<? extends Event<?>>, Double> subSeqWeights = new LinkedHashMap<List<? extends Event<?>>, Double>();178 double sum = 0.0;179 for (List<? extends Event<?>> sequence : sequences) {180 double prob = process.getProbability(sequence);181 subSeqWeights.put(sequence, prob);182 sum += prob;183 }184 if (sum < 1.0) {185 for (Map.Entry<List<? extends Event<?>>, Double> entry : subSeqWeights186 .entrySet()) {187 entry.setValue(entry.getValue() / sum);188 }189 }190 return subSeqWeights;191 }192 193 /**194 * <p>195 * Calculates the number of all existing sequences of a given length,196 * regardless whether they are possible or not.197 * </p>198 *199 * @param process200 * stochastic process whose symbols are the basis for this201 * calculation202 * @param length203 * lenght of the sequences204 * @return numStates^length205 */206 private static long numSequences(IStochasticProcess process, int length) {207 return (long) Math.pow(process.getNumStates(), length);208 }209 210 /**211 * <p>212 * Creates a {@link Set} of all subsequences of a given length that are213 * contained in a sequence collection.214 * </p>215 *216 * @param sequences217 * sequences from which the subsequences are extracted218 * @param length219 * length of the subsequences220 * @return {@link Set} of all subsequences221 */222 private static Set<List<? extends Event<?>>> containedSubSequences(223 Collection<List<? extends Event<?>>> sequences, int length) {224 Set<List<? extends Event<?>>> containedSubSeqs = new LinkedHashSet<List<? extends Event<?>>>();225 for (List<? extends Event<?>> sequence : sequences) {226 List<Event<?>> subSeq = new LinkedList<Event<?>>();227 boolean minLengthReached = false;228 for (Event<?> event : sequence) {229 subSeq.add(event);230 if (!minLengthReached) {231 if (subSeq.size() == length) {232 minLengthReached = true;233 }234 } else {235 subSeq.remove(0);236 }237 if (minLengthReached) {238 if (!containedSubSeqs.contains(subSeq)) {239 containedSubSeqs.add(new LinkedList<Event<?>>(subSeq));240 }241 }242 }243 }244 return containedSubSeqs;245 }246 247 165 }
Note: See TracChangeset
for help on using the changeset viewer.