package de.ugoe.cs.eventbench.coverage; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import de.ugoe.cs.eventbench.data.Event; import de.ugoe.cs.eventbench.models.IStochasticProcess; /** *
* This class calculates various types of sequence coverage. *
* * @author Steffen Herbold * @version 1.0 */ public class CoverageCalculator { /** ** Stochastic process that is the foundation for probabilistic coverages and * coverages with reference to all possible sequences. *
*/ private final IStochasticProcess process; /** ** Sequences for which the coverage is calculated. *
*/ private final Collection* Length of the subsequences in relation to which the covarage is * calculated. *
*/ private final int length; /** ** All subsequences of {@link #length} of {@link #sequences}. *
*/ private Collection* All subsequences of {@link #length} that can be generated by * {@link #process}. *
*/ private Collection* The probabilities of al subsequences of {@link #length} according to * {@link #process}. *
*/ private Map* Constructor. Creates a new CoverageCalculator for a given stochastic * process and generated sequences. *
* * @param process * stochastic process used for coverage calculations; if it is * zero, not all calculations are possible * @param sequences * sequences for which the coverage is calculated; must not be * null. * @param length * length of the subsequences for which the coverage is analyzed; * must be >0 */ public CoverageCalculator(IStochasticProcess process, Collection* Calculates the percentage of subsequences of length k that exist occur, * including those that cannot be generated by {@link #process}. *
* * @return coverage percentage */ public double getCoverageAllNoWeight() { if (containedSubSeqs == null) { containedSubSeqs = containedSubSequences(sequences, length); } return ((double) containedSubSeqs.size()) / numSequences(process, length); } /** ** Calculates the percentage of subsequences of length k that occur and can * generated by {@link #process}. *
* * @return coverage percentage */ public double getCoveragePossibleNoWeight() { if (containedSubSeqs == null) { containedSubSeqs = containedSubSequences(sequences, length); } if (allPossibleSubSeqs == null) { allPossibleSubSeqs = process.generateSequences(length); } return ((double) containedSubSeqs.size()) / allPossibleSubSeqs.size(); } /** ** Calculates the weight of the subsequences that occur with relation to * {@link #process}, i.e., the mass of the subsequence probability covered * by the subsequences. *
* * @return coverage weight */ public double getCoveragePossibleWeight() { if (containedSubSeqs == null) { containedSubSeqs = containedSubSequences(sequences, length); } if (allPossibleSubSeqs == null) { allPossibleSubSeqs = process.generateSequences(length); } if (subSeqWeights == null) { subSeqWeights = generateWeights(process, allPossibleSubSeqs); } double weight = 0.0; for (List extends Event>> subSeq : containedSubSeqs) { weight += subSeqWeights.get(subSeq); } return weight; } /** ** Calculates the weights for all sequences passed to this function as * defined by the stochastic process and stores them in a {@link Map}. *
* * @param process * process used for weight calculation * @param sequences * sequences for which the weights are calculated * @return {@link Map} of weights */ private static Map* Calculates the number of all existing sequences of a given length, * regardless whether they are possible or not. *
* * @param process * stochastic process whose symbols are the basis for this * calculation * @param length * lenght of the sequences * @return numStates^length */ private static long numSequences(IStochasticProcess process, int length) { return (long) Math.pow(process.getNumStates(), length); } /** ** Creates a {@link Set} of all subsequences of a given length that are * contained in a sequence collection. *
* * @param sequences * sequences from which the subsequences are extracted * @param length * length of the subsequences * @return {@link Set} of all subsequences */ private static Set