1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.coverage;
|
---|
16 |
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.LinkedHashMap;
|
---|
19 | import java.util.LinkedHashSet;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Map;
|
---|
23 | import java.util.Set;
|
---|
24 |
|
---|
25 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
26 | import de.ugoe.cs.autoquest.usageprofiles.IStochasticProcess;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * <p>
|
---|
30 | * This class gathers some helper functions to work with sequences.
|
---|
31 | * </p>
|
---|
32 | *
|
---|
33 | * @author Steffen Herbold
|
---|
34 | * @version 1.0
|
---|
35 | */
|
---|
36 | public class SequenceTools {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Private constructor to prevent initializing of the class.
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | private SequenceTools() {
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * <p>
|
---|
49 | * Calculates the weights for all sequences passed to this function as defined by the stochastic
|
---|
50 | * process and stores them in a {@link Map}. The weights are normalized, i.e., the sum of all
|
---|
51 | * weights contained in this map is 1.
|
---|
52 | * </p>
|
---|
53 | *
|
---|
54 | * @param process
|
---|
55 | * process used for weight calculation
|
---|
56 | * @param sequences
|
---|
57 | * sequences for which the weights are calculated
|
---|
58 | * @return {@link Map} of weights
|
---|
59 | */
|
---|
60 | public static Map<List<Event>, Double> generateWeights(IStochasticProcess process,
|
---|
61 | Collection<List<Event>> sequences)
|
---|
62 | {
|
---|
63 | Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>();
|
---|
64 | if (sequences != null && !sequences.isEmpty()) {
|
---|
65 | if (process != null) {
|
---|
66 | double sum = 0.0;
|
---|
67 | for (List<Event> sequence : sequences) {
|
---|
68 | double prob = process.getProbability(sequence);
|
---|
69 | subSeqWeights.put(sequence, prob);
|
---|
70 | sum += prob;
|
---|
71 | }
|
---|
72 | if (sum < 1.0) {
|
---|
73 | for (Map.Entry<List<Event>, Double> entry : subSeqWeights.entrySet()) {
|
---|
74 | entry.setValue(entry.getValue() / sum);
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | for (List<Event> sequence : sequences) {
|
---|
80 | subSeqWeights.put(sequence, 0.0d);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return subSeqWeights;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * <p>
|
---|
89 | * Calculates the number of all existing sequences of a given length, regardless whether they
|
---|
90 | * are possible or not.
|
---|
91 | * </p>
|
---|
92 | *
|
---|
93 | * @param process
|
---|
94 | * stochastic process whose symbols are the basis for this calculation
|
---|
95 | * @param length
|
---|
96 | * lenght of the sequences
|
---|
97 | * @return numStates^length
|
---|
98 | * @throws IllegalArgumentException
|
---|
99 | * thrown if length less or equal to 0
|
---|
100 | */
|
---|
101 | public static long numSequences(IStochasticProcess process, int length) {
|
---|
102 | if (length <= 0) {
|
---|
103 | throw new IllegalArgumentException("length must be a positive integer");
|
---|
104 | }
|
---|
105 | long result = 0;
|
---|
106 | if (process != null) {
|
---|
107 | result = (long) Math.pow(process.getNumSymbols(), length);
|
---|
108 | }
|
---|
109 | return result;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * <p>
|
---|
114 | * Creates a {@link Set} of all subsequences of a given length that are contained in a sequence
|
---|
115 | * collection.
|
---|
116 | * </p>
|
---|
117 | *
|
---|
118 | * @param sequences
|
---|
119 | * sequences from which the subsequences are extracted
|
---|
120 | * @param length
|
---|
121 | * length of the subsequences
|
---|
122 | * @return {@link Set} of all subsequences
|
---|
123 | * @throws IllegalArgumentException
|
---|
124 | * thrown if length less or equal to 0
|
---|
125 | */
|
---|
126 | public static Set<List<Event>> containedSubSequences(Collection<List<Event>> sequences,
|
---|
127 | int length)
|
---|
128 | {
|
---|
129 | if (length <= 0) {
|
---|
130 | throw new IllegalArgumentException("length must be a positive integer");
|
---|
131 | }
|
---|
132 | Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>();
|
---|
133 | if (sequences != null) {
|
---|
134 | for (List<Event> sequence : sequences) {
|
---|
135 | List<Event> subSeq = new LinkedList<Event>();
|
---|
136 | boolean minLengthReached = false;
|
---|
137 | for (Event event : sequence) {
|
---|
138 | subSeq.add(event);
|
---|
139 | if (!minLengthReached) {
|
---|
140 | if (subSeq.size() == length) {
|
---|
141 | minLengthReached = true;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | subSeq.remove(0);
|
---|
146 | }
|
---|
147 | if (minLengthReached) {
|
---|
148 | if (!containedSubSeqs.contains(subSeq)) {
|
---|
149 | containedSubSeqs.add(new LinkedList<Event>(subSeq));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return containedSubSeqs;
|
---|
156 | }
|
---|
157 | }
|
---|