source: trunk/autoquest-core-coverage/src/main/java/de/ugoe/cs/autoquest/coverage/SequenceTools.java @ 922

Last change on this file since 922 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
  • Property svn:mime-type set to text/plain
File size: 4.8 KB
Line 
1package de.ugoe.cs.autoquest.coverage;
2
3import java.util.Collection;
4import java.util.LinkedHashMap;
5import java.util.LinkedHashSet;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Map;
9import java.util.Set;
10
11import de.ugoe.cs.autoquest.eventcore.Event;
12import de.ugoe.cs.autoquest.usageprofiles.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 */
22public class SequenceTools {
23
24    /**
25     * <p>
26     * Private constructor to prevent initializing of the class.
27     * </p>
28     */
29    private SequenceTools() {
30
31    }
32
33    /**
34     * <p>
35     * Calculates the weights for all sequences passed to this function as defined by the stochastic
36     * process and stores them in a {@link Map}. The weights are normalized, i.e., the sum of all
37     * weights contained in this map is 1.
38     * </p>
39     *
40     * @param process
41     *            process used for weight calculation
42     * @param sequences
43     *            sequences for which the weights are calculated
44     * @return {@link Map} of weights
45     */
46    public static Map<List<Event>, Double> generateWeights(IStochasticProcess process,
47                                                           Collection<List<Event>> sequences)
48    {
49        Map<List<Event>, Double> subSeqWeights = new LinkedHashMap<List<Event>, Double>();
50        if (sequences != null && !sequences.isEmpty()) {
51            if (process != null) {
52                double sum = 0.0;
53                for (List<Event> sequence : sequences) {
54                    double prob = process.getProbability(sequence);
55                    subSeqWeights.put(sequence, prob);
56                    sum += prob;
57                }
58                if (sum < 1.0) {
59                    for (Map.Entry<List<Event>, Double> entry : subSeqWeights.entrySet()) {
60                        entry.setValue(entry.getValue() / sum);
61                    }
62                }
63            }
64            else {
65                for (List<Event> sequence : sequences) {
66                    subSeqWeights.put(sequence, 0.0d);
67                }
68            }
69        }
70        return subSeqWeights;
71    }
72
73    /**
74     * <p>
75     * Calculates the number of all existing sequences of a given length, regardless whether they
76     * are possible or not.
77     * </p>
78     *
79     * @param process
80     *            stochastic process whose symbols are the basis for this calculation
81     * @param length
82     *            lenght of the sequences
83     * @return numStates^length
84     * @throws IllegalArgumentException
85     *             thrown if length less or equal to 0
86     */
87    public static long numSequences(IStochasticProcess process, int length) {
88        if (length <= 0) {
89            throw new IllegalArgumentException("length must be a positive integer");
90        }
91        long result = 0;
92        if (process != null) {
93            result = (long) Math.pow(process.getNumSymbols(), length);
94        }
95        return result;
96    }
97
98    /**
99     * <p>
100     * Creates a {@link Set} of all subsequences of a given length that are contained in a sequence
101     * collection.
102     * </p>
103     *
104     * @param sequences
105     *            sequences from which the subsequences are extracted
106     * @param length
107     *            length of the subsequences
108     * @return {@link Set} of all subsequences
109     * @throws IllegalArgumentException
110     *             thrown if length less or equal to 0
111     */
112    public static Set<List<Event>> containedSubSequences(Collection<List<Event>> sequences,
113                                                         int length)
114    {
115        if (length <= 0) {
116            throw new IllegalArgumentException("length must be a positive integer");
117        }
118        Set<List<Event>> containedSubSeqs = new LinkedHashSet<List<Event>>();
119        if (sequences != null) {
120            for (List<Event> sequence : sequences) {
121                List<Event> subSeq = new LinkedList<Event>();
122                boolean minLengthReached = false;
123                for (Event event : sequence) {
124                    subSeq.add(event);
125                    if (!minLengthReached) {
126                        if (subSeq.size() == length) {
127                            minLengthReached = true;
128                        }
129                    }
130                    else {
131                        subSeq.remove(0);
132                    }
133                    if (minLengthReached) {
134                        if (!containedSubSeqs.contains(subSeq)) {
135                            containedSubSeqs.add(new LinkedList<Event>(subSeq));
136                        }
137                    }
138                }
139            }
140        }
141        return containedSubSeqs;
142    }
143}
Note: See TracBrowser for help on using the repository browser.