source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/ModelFlattener.java @ 399

Last change on this file since 399 was 361, checked in by sherbold, 12 years ago
  • implemented copy-constructors for de.ugoe.cs.eventbench.models.Trie/TrieNode?
  • de.ugoe.cs.eventbench.ModelFlatterner? now handles high order markov models that are really first order markov models better (copies the internal trie directly)
  • Property svn:mime-type set to text/plain
File size: 4.4 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.util.LinkedList;
4import java.util.List;
5import java.util.Random;
6
7import de.ugoe.cs.eventbench.data.Event;
8import de.ugoe.cs.eventbench.data.ReplayableEvent;
9
10/**
11 * <p>
12 * This class provides functions to create flattened first-order Markov models
13 * from {@link HighOrderMarkovModel}s and {@link PredictionByPartialMatch}
14 * models through state splitting.
15 * </p>
16 * <p>
17 * If possible, the normal high-order markov model should be used, as the Events
18 * may be broken by the flattener, as, e.g., the information
19 * {@link ReplayableEvent}s contain is not preserved.
20 * </p>
21 *
22 * @author Steffen Herbold
23 * @version 1.0
24 */
25public class ModelFlattener {
26
27        private static final String EVENT_SEPARATOR = "-=-";
28
29        Trie<Event<?>> firstOrderTrie;
30
31        /**
32         * <p>
33         * Takes a {@link HighOrderMarkovModel} and returns a
34         * {@link FirstOrderMarkovModel} that conserves the high-order memory
35         * through state splitting.
36         * </p>
37         *
38         * @param model
39         *            model that is flattened
40         * @return flattened first-order Markov model
41         */
42        public FirstOrderMarkovModel flattenHighOrderMarkovModel(
43                        HighOrderMarkovModel model) {
44                int markovOrder = model.trieOrder - 1;
45                FirstOrderMarkovModel firstOrderModel = new FirstOrderMarkovModel(
46                                new Random());
47                firstOrderModel.trieOrder = 2;
48                if (markovOrder == 1) {
49                        firstOrderModel.trie = new Trie<Event<?>>(model.trie);
50                        firstOrderModel.trieOrder = 2;
51                } else {
52                        firstOrderTrie = new Trie<Event<?>>();
53                        TrieNode<Event<?>> rootNode = model.trie.find(null);
54                        generateFirstOrderTrie(rootNode, new LinkedList<String>(), markovOrder);
55                        firstOrderTrie.updateKnownSymbols();
56                        firstOrderModel.trie = firstOrderTrie;
57                }
58
59                return firstOrderModel;
60        }
61
62        /**
63         * <p>
64         * <b>This method is not available yet and always return null!</b>
65         * </p>
66         * <p>
67         * Takes a {@link PredictionByPartialMatch} model and returns a
68         * {@link FirstOrderMarkovModel} that conserves the high-order memory
69         * through state splitting.
70         * </p>
71         *
72         * @param model
73         *            model that is flattened
74         * @return flattened first-order Markov model
75         */
76        public FirstOrderMarkovModel flattenPredictionByPartialMatch(
77                        PredictionByPartialMatch model) {
78                // TODO implement method
79                return null;
80        }
81
82        /**
83         * <p>
84         * Converts all nodes of the given depth into first-order node through
85         * state-splitting. For each node at the given depth a new node is created
86         * and appropriate transitions will be added.
87         * </p>
88         * <p>
89         * This method traverses through the tree recursively. If the recursion
90         * reaches the desired depth in the tree, node are added.
91         * </p>
92         *
93         * @param currentNode
94         *            node whose sub-trie is currently traversed
95         * @param parentIDs
96         *            ID strings of the ancestors of the currentNode
97         * @param depth
98         *            depth to go - NOT the current depth.
99         */
100        private void generateFirstOrderTrie(TrieNode<Event<?>> currentNode,
101                        List<String> parentIDs, int depth) {
102                for (TrieNode<Event<?>> child : currentNode.getChildren()) {
103                        String currentId = child.getSymbol().getStandardId();
104                        if (depth > 1) {
105                                List<String> childParentIDs = new LinkedList<String>(parentIDs);
106                                childParentIDs.add(currentId);
107                                generateFirstOrderTrie(child, childParentIDs, depth - 1);
108
109                        } else {
110                                StringBuilder firstOrderID = new StringBuilder();
111                                for (String parentID : parentIDs) {
112                                        firstOrderID.append(parentID + EVENT_SEPARATOR);
113                                }
114                                firstOrderID.append(currentId);
115                                TrieNode<Event<?>> firstOrderNode = firstOrderTrie
116                                                .getChildCreate(new Event<Object>(firstOrderID
117                                                                .toString()));
118                                firstOrderNode.setCount(child.getCount());
119                                for (TrieNode<Event<?>> transitionChild : child.getChildren()) {
120                                        StringBuilder transitionID = new StringBuilder();
121                                        for (String parentID : parentIDs.subList(1,
122                                                        parentIDs.size())) {
123                                                transitionID.append(parentID + EVENT_SEPARATOR);
124                                        }
125                                        transitionID.append(currentId + EVENT_SEPARATOR);
126                                        transitionID.append(transitionChild.getSymbol()
127                                                        .getStandardId());
128                                        TrieNode<Event<?>> firstOrderTransitionChild = firstOrderNode
129                                                        .getChildCreate(new Event<Object>(transitionID
130                                                                        .toString()));
131                                        firstOrderTransitionChild.setCount(transitionChild
132                                                        .getCount());
133                                }
134                        }
135                }
136        }
137}
Note: See TracBrowser for help on using the repository browser.