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