package de.ugoe.cs.quest.coverage; import java.security.InvalidParameterException; import java.util.Collection; import java.util.List; import java.util.Map; import de.ugoe.cs.quest.eventcore.Event; import de.ugoe.cs.quest.usageprofiles.IStochasticProcess; /** *

* This class calculates various types of sequence coverage in relation to a * stochastic process. *

* * @author Steffen Herbold * @version 1.0 */ public class CoverageCalculatorProcess { /** *

* 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 Collection> sequences; /** *

* Length of the subsequences in relation to which the coverage is * calculated. *

*/ private final int length; /** *

* All subsequences of {@link #length} of {@link #sequences}. *

*/ private Collection> containedSubSeqs = null; /** *

* All subsequences of {@link #length} that can be generated by * {@link #process}. *

*/ private Collection> allPossibleSubSeqs = null; /** *

* The probabilities of all subsequences of {@link #length} according to * {@link #process}. *

*/ private Map, Double> subSeqWeights = null; /** *

* Constructor. Creates a new CoverageCalculatorProcess for a given * stochastic process and generated sequences. *

* * @param process * stochastic process used for coverage calculations; must not be * null * @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 * @throws InvalidParameterException * thrown if process or sequences is null or length less than or equal to 0 */ public CoverageCalculatorProcess(IStochasticProcess process, Collection> sequences, int length) { if (process == null) { throw new InvalidParameterException("process must not be null"); } if (sequences == null) { throw new InvalidParameterException("sequences must not be null"); } if (length <= 0) { throw new InvalidParameterException( "length must be >0; actual value: " + length); } this.process = process; this.sequences = sequences; this.length = length; } /** *

* Calculates the percentage of subsequences of length k that occur, * including those that cannot be generated by {@link #process}. *

* * @return coverage percentage */ public double getCoverageAllNoWeight() { if (containedSubSeqs == null) { containedSubSeqs = SequenceTools.containedSubSequences(sequences, length); } return ((double) containedSubSeqs.size()) / SequenceTools.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 = SequenceTools.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 = SequenceTools.containedSubSequences(sequences, length); } if (allPossibleSubSeqs == null) { allPossibleSubSeqs = process.generateSequences(length); } if (subSeqWeights == null) { subSeqWeights = SequenceTools.generateWeights(process, allPossibleSubSeqs); } double weight = 0.0; for (List subSeq : containedSubSeqs) { Double curWeight = subSeqWeights.get(subSeq); if( curWeight!=null ) { weight += curWeight; } } return weight; } /** *

* Returns the number of covered subsequences of length k. *

* * @return number of covered subsequences */ public int getNumCovered() { if (containedSubSeqs == null) { containedSubSeqs = SequenceTools.containedSubSequences(sequences, length); } return containedSubSeqs.size(); } /** *

* Returns the number of possible subsequences of length k according to the * stochastic process. *

* * @return number of possible subsequences */ public int getNumPossible() { if (allPossibleSubSeqs == null) { allPossibleSubSeqs = process.generateSequences(length); } return allPossibleSubSeqs.size(); } /** *

* Sets a new collection of sequences for which the coverage is analyzed. *

* * @param newSequences * new collection of sequences * @throws InvalidParameterException * thrown is newSequences is null */ public void setSequences(Collection> newSequences) { if (newSequences == null) { throw new InvalidParameterException("sequences must not be null"); } this.sequences = newSequences; containedSubSeqs = null; } }