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