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