[518] | 1 | package de.ugoe.cs.quest.coverage;
|
---|
| 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.List;
|
---|
| 6 | import java.util.Map;
|
---|
| 7 |
|
---|
| 8 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
| 9 | import de.ugoe.cs.quest.usageprofiles.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 | */
|
---|
| 20 | public 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 | */
|
---|
[547] | 35 | private Collection<List<Event>> sequences;
|
---|
[518] | 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 | */
|
---|
[547] | 50 | private Collection<List<Event>> containedSubSeqs = null;
|
---|
[518] | 51 |
|
---|
| 52 | /**
|
---|
| 53 | * <p>
|
---|
| 54 | * All subsequences of {@link #length} that can be generated by
|
---|
| 55 | * {@link #process}.
|
---|
| 56 | * </p>
|
---|
| 57 | */
|
---|
[547] | 58 | private Collection<List<Event>> allPossibleSubSeqs = null;
|
---|
[518] | 59 |
|
---|
| 60 | /**
|
---|
| 61 | * <p>
|
---|
| 62 | * The probabilities of all subsequences of {@link #length} according to
|
---|
| 63 | * {@link #process}.
|
---|
| 64 | * </p>
|
---|
| 65 | */
|
---|
[547] | 66 | private Map<List<Event>, Double> subSeqWeights = null;
|
---|
[518] | 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,
|
---|
[547] | 87 | Collection<List<Event>> sequences, int length) {
|
---|
[518] | 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;
|
---|
[547] | 161 | for (List<Event> subSeq : containedSubSeqs) {
|
---|
[518] | 162 | Double curWeight = subSeqWeights.get(subSeq);
|
---|
| 163 | if( curWeight!=null ) {
|
---|
| 164 | weight += curWeight;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | return weight;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | /**
|
---|
| 171 | * <p>
|
---|
| 172 | * Returns the number of covered subsequences of length k.
|
---|
| 173 | * </p>
|
---|
| 174 | *
|
---|
| 175 | * @return number of covered subsequences
|
---|
| 176 | */
|
---|
| 177 | public int getNumCovered() {
|
---|
| 178 | if (containedSubSeqs == null) {
|
---|
| 179 | containedSubSeqs = SequenceTools.containedSubSequences(sequences,
|
---|
| 180 | length);
|
---|
| 181 | }
|
---|
| 182 | return containedSubSeqs.size();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * <p>
|
---|
| 187 | * Returns the number of possible subsequences of length k according to the
|
---|
| 188 | * stochastic process.
|
---|
| 189 | * </p>
|
---|
| 190 | *
|
---|
| 191 | * @return number of possible subsequences
|
---|
| 192 | */
|
---|
| 193 | public int getNumPossible() {
|
---|
| 194 | if (allPossibleSubSeqs == null) {
|
---|
| 195 | allPossibleSubSeqs = process.generateSequences(length);
|
---|
| 196 | }
|
---|
| 197 | return allPossibleSubSeqs.size();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * <p>
|
---|
| 202 | * Sets a new collection of sequences for which the coverage is analyzed.
|
---|
| 203 | * </p>
|
---|
| 204 | *
|
---|
| 205 | * @param newSequences
|
---|
| 206 | * new collection of sequences
|
---|
| 207 | * @throws InvalidParameterException
|
---|
| 208 | * thrown is newSequences is null
|
---|
| 209 | */
|
---|
[547] | 210 | public void setSequences(Collection<List<Event>> newSequences) {
|
---|
[518] | 211 | if (newSequences == null) {
|
---|
| 212 | throw new InvalidParameterException("sequences must not be null");
|
---|
| 213 | }
|
---|
| 214 | this.sequences = newSequences;
|
---|
| 215 | containedSubSeqs = null;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | }
|
---|