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