[518] | 1 | package de.ugoe.cs.quest.coverage;
|
---|
| 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
| 4 | import java.util.Collection;
|
---|
| 5 | import java.util.LinkedHashMap;
|
---|
| 6 | import java.util.LinkedHashSet;
|
---|
| 7 | import java.util.LinkedList;
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Map;
|
---|
| 10 | import java.util.Set;
|
---|
| 11 |
|
---|
| 12 | import de.ugoe.cs.quest.eventcore.Event;
|
---|
| 13 | import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * <p>
|
---|
| 17 | * This class gathers some helper functions to work with sequences.
|
---|
| 18 | * </p>
|
---|
| 19 | *
|
---|
| 20 | * @author Steffen Herbold
|
---|
| 21 | * @version 1.0
|
---|
| 22 | */
|
---|
| 23 | public class SequenceTools {
|
---|
| 24 |
|
---|
[559] | 25 | /**
|
---|
| 26 | * <p>
|
---|
| 27 | * Private constructor to prevent initializing of the class.
|
---|
| 28 | * </p>
|
---|
| 29 | */
|
---|
| 30 | private SequenceTools() {
|
---|
[518] | 31 |
|
---|
[559] | 32 | }
|
---|
[518] | 33 |
|
---|
[559] | 34 | /**
|
---|
| 35 | * <p>
|
---|
| 36 | * Calculates the weights for all sequences passed to this function as defined by the stochastic
|
---|
| 37 | * process and stores them in a {@link Map}. The weights are normalized, i.e., the sum of all
|
---|
| 38 | * weights contained in this map is 1.
|
---|
| 39 | * </p>
|
---|
| 40 | *
|
---|
| 41 | * @param process
|
---|
| 42 | * process used for weight calculation
|
---|
| 43 | * @param sequences
|
---|
| 44 | * sequences for which the weights are calculated
|
---|
| 45 | * @return {@link Map} of weights
|
---|
| 46 | */
|
---|
| 47 | public static Map<List<Event>, Double> generateWeights(IStochasticProcess process,
|
---|
| 48 | Collection<List<Event>> sequences)
|
---|
| 49 | {
|
---|
| 50 | Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>();
|
---|
| 51 | if (sequences != null && !sequences.isEmpty()) {
|
---|
| 52 | if (process != null) {
|
---|
| 53 | double sum = 0.0;
|
---|
| 54 | for (List<Event> sequence : sequences) {
|
---|
| 55 | double prob = process.getProbability(sequence);
|
---|
| 56 | subSeqWeights.put(sequence, prob);
|
---|
| 57 | sum += prob;
|
---|
| 58 | }
|
---|
| 59 | if (sum < 1.0) {
|
---|
| 60 | for (Map.Entry<List<Event>, Double> entry : subSeqWeights.entrySet()) {
|
---|
| 61 | entry.setValue(entry.getValue() / sum);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | else {
|
---|
| 66 | for (List<Event> sequence : sequences) {
|
---|
| 67 | subSeqWeights.put(sequence, 0.0d);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | return subSeqWeights;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * <p>
|
---|
| 76 | * Calculates the number of all existing sequences of a given length, regardless whether they
|
---|
| 77 | * are possible or not.
|
---|
| 78 | * </p>
|
---|
| 79 | *
|
---|
| 80 | * @param process
|
---|
| 81 | * stochastic process whose symbols are the basis for this calculation
|
---|
| 82 | * @param length
|
---|
| 83 | * lenght of the sequences
|
---|
| 84 | * @return numStates^length
|
---|
| 85 | * @throws InvalidParameterException
|
---|
| 86 | * thrown if length less or equal to 0
|
---|
| 87 | */
|
---|
| 88 | public static long numSequences(IStochasticProcess process, int length) {
|
---|
| 89 | if (length <= 0) {
|
---|
| 90 | throw new InvalidParameterException("length must be a positive integer");
|
---|
| 91 | }
|
---|
| 92 | long result = 0;
|
---|
| 93 | if (process != null) {
|
---|
| 94 | result = (long) Math.pow(process.getNumSymbols(), length);
|
---|
| 95 | }
|
---|
| 96 | return result;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * <p>
|
---|
| 101 | * Creates a {@link Set} of all subsequences of a given length that are contained in a sequence
|
---|
| 102 | * collection.
|
---|
| 103 | * </p>
|
---|
| 104 | *
|
---|
| 105 | * @param sequences
|
---|
| 106 | * sequences from which the subsequences are extracted
|
---|
| 107 | * @param length
|
---|
| 108 | * length of the subsequences
|
---|
| 109 | * @return {@link Set} of all subsequences
|
---|
| 110 | * @throws InvalidParameterException
|
---|
| 111 | * thrown if length less or equal to 0
|
---|
| 112 | */
|
---|
| 113 | public static Set<List<Event>> containedSubSequences(Collection<List<Event>> sequences,
|
---|
| 114 | int length)
|
---|
| 115 | {
|
---|
| 116 | if (length <= 0) {
|
---|
| 117 | throw new InvalidParameterException("length must be a positive integer");
|
---|
| 118 | }
|
---|
| 119 | Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>();
|
---|
| 120 | if (sequences != null) {
|
---|
| 121 | for (List<Event> sequence : sequences) {
|
---|
| 122 | List<Event> subSeq = new LinkedList<Event>();
|
---|
| 123 | boolean minLengthReached = false;
|
---|
| 124 | for (Event event : sequence) {
|
---|
| 125 | subSeq.add(event);
|
---|
| 126 | if (!minLengthReached) {
|
---|
| 127 | if (subSeq.size() == length) {
|
---|
| 128 | minLengthReached = true;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | else {
|
---|
| 132 | subSeq.remove(0);
|
---|
| 133 | }
|
---|
| 134 | if (minLengthReached) {
|
---|
| 135 | if (!containedSubSeqs.contains(subSeq)) {
|
---|
| 136 | containedSubSeqs.add(new LinkedList<Event>(subSeq));
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | return containedSubSeqs;
|
---|
| 143 | }
|
---|
[518] | 144 | }
|
---|