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