source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/coverage/SequenceTools.java @ 336

Last change on this file since 336 was 328, checked in by sherbold, 13 years ago
  • improved handling of invalid input values for all methods of de.ugoe.cs.eventbench.coverage.SequenceTools?
  • Property svn:mime-type set to text/plain
File size: 4.0 KB
RevLine 
[123]1package de.ugoe.cs.eventbench.coverage;
2
[328]3import java.security.InvalidParameterException;
[123]4import java.util.Collection;
5import java.util.LinkedHashMap;
6import java.util.LinkedHashSet;
7import java.util.LinkedList;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import de.ugoe.cs.eventbench.data.Event;
13import 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 */
23public 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>();
[328]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                                }
[123]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
[328]78         * @throws InvalidParameterException
79         *             thrown if length less or equal to 0
[123]80         */
81        public static long numSequences(IStochasticProcess process, int length) {
[328]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;
[123]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
[328]104         * @throws InvalidParameterException
105         *             thrown if length less or equal to 0
[123]106         */
107        public static Set<List<? extends Event<?>>> containedSubSequences(
108                        Collection<List<? extends Event<?>>> sequences, int length) {
[328]109                if (length <= 0) {
110                        throw new InvalidParameterException(
111                                        "length must be a positive integer");
112                }
[123]113                Set<List<? extends Event<?>>> containedSubSeqs = new LinkedHashSet<List<? extends Event<?>>>();
[328]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);
[123]126                                        }
[328]127                                        if (minLengthReached) {
128                                                if (!containedSubSeqs.contains(subSeq)) {
129                                                        containedSubSeqs.add(new LinkedList<Event<?>>(
130                                                                        subSeq));
131                                                }
[123]132                                        }
133                                }
134                        }
135                }
136                return containedSubSeqs;
137        }
138}
Note: See TracBrowser for help on using the repository browser.