1 | package de.ugoe.cs.eventbench.coverage;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.LinkedHashMap;
|
---|
5 | import java.util.LinkedHashSet;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import de.ugoe.cs.eventbench.data.Event;
|
---|
12 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * <p>
|
---|
16 | * This class gathers some helper functions to work with sequences.
|
---|
17 | * </p>
|
---|
18 | *
|
---|
19 | * @author Steffen Herbold
|
---|
20 | * @version 1.0
|
---|
21 | */
|
---|
22 | public class SequenceTools {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * <p>
|
---|
26 | * Calculates the weights for all sequences passed to this function as
|
---|
27 | * defined by the stochastic process and stores them in a {@link Map}. The
|
---|
28 | * weights are normalized, i.e., the sum of all weights contained in this
|
---|
29 | * map is 1.
|
---|
30 | * </p>
|
---|
31 | *
|
---|
32 | * @param process
|
---|
33 | * process used for weight calculation
|
---|
34 | * @param sequences
|
---|
35 | * sequences for which the weights are calculated
|
---|
36 | * @return {@link Map} of weights
|
---|
37 | */
|
---|
38 | public static Map<List<? extends Event<?>>, Double> generateWeights(
|
---|
39 | IStochasticProcess process,
|
---|
40 | Collection<List<? extends Event<?>>> sequences) {
|
---|
41 | Map<List<? extends Event<?>>, Double> subSeqWeights = new LinkedHashMap<List<? extends Event<?>>, Double>();
|
---|
42 | double sum = 0.0;
|
---|
43 | for (List<? extends Event<?>> sequence : sequences) {
|
---|
44 | double prob = process.getProbability(sequence);
|
---|
45 | subSeqWeights.put(sequence, prob);
|
---|
46 | sum += prob;
|
---|
47 | }
|
---|
48 | if (sum < 1.0) {
|
---|
49 | for (Map.Entry<List<? extends Event<?>>, Double> entry : subSeqWeights
|
---|
50 | .entrySet()) {
|
---|
51 | entry.setValue(entry.getValue() / sum);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | return subSeqWeights;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * Calculates the number of all existing sequences of a given length,
|
---|
60 | * regardless whether they are possible or not.
|
---|
61 | * </p>
|
---|
62 | *
|
---|
63 | * @param process
|
---|
64 | * stochastic process whose symbols are the basis for this
|
---|
65 | * calculation
|
---|
66 | * @param length
|
---|
67 | * lenght of the sequences
|
---|
68 | * @return numStates^length
|
---|
69 | */
|
---|
70 | public static long numSequences(IStochasticProcess process, int length) {
|
---|
71 | return (long) Math.pow(process.getNumStates(), length);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * <p>
|
---|
76 | * Creates a {@link Set} of all subsequences of a given length that are
|
---|
77 | * contained in a sequence collection.
|
---|
78 | * </p>
|
---|
79 | *
|
---|
80 | * @param sequences
|
---|
81 | * sequences from which the subsequences are extracted
|
---|
82 | * @param length
|
---|
83 | * length of the subsequences
|
---|
84 | * @return {@link Set} of all subsequences
|
---|
85 | */
|
---|
86 | public static Set<List<? extends Event<?>>> containedSubSequences(
|
---|
87 | Collection<List<? extends Event<?>>> sequences, int length) {
|
---|
88 | Set<List<? extends Event<?>>> containedSubSeqs = new LinkedHashSet<List<? extends Event<?>>>();
|
---|
89 | for (List<? extends Event<?>> sequence : sequences) {
|
---|
90 | List<Event<?>> subSeq = new LinkedList<Event<?>>();
|
---|
91 | boolean minLengthReached = false;
|
---|
92 | for (Event<?> event : sequence) {
|
---|
93 | subSeq.add(event);
|
---|
94 | if (!minLengthReached) {
|
---|
95 | if (subSeq.size() == length) {
|
---|
96 | minLengthReached = true;
|
---|
97 | }
|
---|
98 | } else {
|
---|
99 | subSeq.remove(0);
|
---|
100 | }
|
---|
101 | if (minLengthReached) {
|
---|
102 | if (!containedSubSeqs.contains(subSeq)) {
|
---|
103 | containedSubSeqs.add(new LinkedList<Event<?>>(subSeq));
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return containedSubSeqs;
|
---|
109 | }
|
---|
110 | }
|
---|