[13] | 1 | package de.ugoe.cs.eventbench.models;
|
---|
[1] | 2 |
|
---|
[86] | 3 | import java.io.Serializable;
|
---|
[9] | 4 | import java.util.LinkedHashSet;
|
---|
[1] | 5 | import java.util.LinkedList;
|
---|
| 6 | import java.util.List;
|
---|
[9] | 7 | import java.util.Set;
|
---|
[1] | 8 |
|
---|
[30] | 9 | import de.ugoe.cs.util.StringTools;
|
---|
| 10 |
|
---|
[5] | 11 | import edu.uci.ics.jung.graph.DelegateTree;
|
---|
| 12 | import edu.uci.ics.jung.graph.Tree;
|
---|
| 13 |
|
---|
[86] | 14 | public class Trie<T> implements IDotCompatible, Serializable {
|
---|
[1] | 15 |
|
---|
[86] | 16 | /**
|
---|
| 17 | * Id for object serialization.
|
---|
| 18 | */
|
---|
| 19 | private static final long serialVersionUID = 1L;
|
---|
| 20 |
|
---|
[9] | 21 | private Set<T> knownSymbols;
|
---|
| 22 |
|
---|
[6] | 23 | private final TrieNode<T> rootNode;
|
---|
[1] | 24 |
|
---|
[6] | 25 | public Trie() {
|
---|
| 26 | rootNode = new TrieNode<T>();
|
---|
[9] | 27 | knownSymbols = new LinkedHashSet<T>();
|
---|
[6] | 28 | }
|
---|
| 29 |
|
---|
[9] | 30 | public Set<T> getKnownSymbols() {
|
---|
| 31 | return new LinkedHashSet<T>(knownSymbols);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[16] | 34 | // trains the current Trie using the given sequence and adds all subsequence of length trieOrder
|
---|
[9] | 35 | public void train(List<T> sequence, int maxOrder) {
|
---|
| 36 | IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder);
|
---|
| 37 | int i=0;
|
---|
| 38 | for(T currentEvent : sequence) {
|
---|
| 39 | latestActions.add(currentEvent);
|
---|
| 40 | knownSymbols.add(currentEvent);
|
---|
| 41 | i++;
|
---|
| 42 | if( i>=maxOrder ) {
|
---|
| 43 | add(latestActions.getLast(maxOrder));
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 | int sequenceLength = sequence.size();
|
---|
| 47 | for( int j=maxOrder-1 ; j>0 ; j-- ) {
|
---|
| 48 | add(sequence.subList(sequenceLength-j, sequenceLength));
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[1] | 52 |
|
---|
[9] | 53 | // increases the counters for each symbol in the subsequence
|
---|
[1] | 54 | public void add(List<T> subsequence) {
|
---|
[9] | 55 | if( subsequence!=null && !subsequence.isEmpty() ) {
|
---|
| 56 | knownSymbols.addAll(subsequence);
|
---|
[5] | 57 | subsequence = new LinkedList<T>(subsequence); // defensive copy!
|
---|
[1] | 58 | T firstSymbol = subsequence.get(0);
|
---|
[6] | 59 | TrieNode<T> node = getChildCreate(firstSymbol);
|
---|
| 60 | node.add(subsequence);
|
---|
[1] | 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
[6] | 65 | return rootNode.getChildCreate(symbol);
|
---|
[1] | 66 | }
|
---|
| 67 |
|
---|
| 68 | protected TrieNode<T> getChild(T symbol) {
|
---|
[6] | 69 | return rootNode.getChild(symbol);
|
---|
[1] | 70 | }
|
---|
| 71 |
|
---|
[5] | 72 | // get the count of "sequence"
|
---|
[1] | 73 | public int getCount(List<T> sequence) {
|
---|
| 74 | int count = 0;
|
---|
| 75 | TrieNode<T> node = find(sequence);
|
---|
| 76 | if( node!=null ) {
|
---|
| 77 | count = node.getCount();
|
---|
| 78 | }
|
---|
| 79 | return count;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[5] | 82 | // get the count of "sequence,follower"
|
---|
[1] | 83 | public int getCount(List<T> sequence, T follower) {
|
---|
| 84 | List<T> tmpSequence = new LinkedList<T>(sequence);
|
---|
| 85 | tmpSequence.add(follower);
|
---|
| 86 | return getCount(tmpSequence);
|
---|
| 87 |
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public TrieNode<T> find(List<T> sequence) {
|
---|
[6] | 91 | if( sequence==null || sequence.isEmpty() ) {
|
---|
| 92 | return rootNode;
|
---|
| 93 | }
|
---|
[5] | 94 | List<T> sequenceCopy = new LinkedList<T>(sequence);
|
---|
[1] | 95 | TrieNode<T> result = null;
|
---|
[6] | 96 | TrieNode<T> node = getChild(sequenceCopy.get(0));
|
---|
| 97 | if( node!=null ) {
|
---|
| 98 | sequenceCopy.remove(0);
|
---|
| 99 | result = node.find(sequenceCopy);
|
---|
[1] | 100 | }
|
---|
| 101 | return result;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[5] | 104 | // returns all symbols that follow the defined sequence
|
---|
[1] | 105 | public List<T> getFollowingSymbols(List<T> sequence) {
|
---|
[5] | 106 | List<T> result = new LinkedList<T>();
|
---|
[6] | 107 | TrieNode<T> node = find(sequence);
|
---|
| 108 | if( node!=null ) {
|
---|
| 109 | result = node.getFollowingSymbols();
|
---|
[1] | 110 | }
|
---|
| 111 | return result;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[5] | 114 | // longest suffix of context, that is contained in the tree and whose children are leaves
|
---|
[6] | 115 | // possibly already deprecated
|
---|
[1] | 116 | public List<T> getContextSuffix(List<T> context) {
|
---|
[5] | 117 | List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
|
---|
[1] | 118 | boolean suffixFound = false;
|
---|
| 119 |
|
---|
| 120 | while(!suffixFound) {
|
---|
| 121 | if( contextSuffix.isEmpty() ) {
|
---|
| 122 | suffixFound = true; // suffix is the empty word
|
---|
| 123 | } else {
|
---|
| 124 | TrieNode<T> node = find(contextSuffix);
|
---|
| 125 | if( node!=null ) {
|
---|
| 126 | if( !node.getFollowingSymbols().isEmpty() ) {
|
---|
| 127 | suffixFound = true;
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
[5] | 130 | if( !suffixFound ) {
|
---|
| 131 | contextSuffix.remove(0);
|
---|
| 132 | }
|
---|
[1] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | return contextSuffix;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[5] | 139 |
|
---|
| 140 | static public class Edge{}
|
---|
| 141 |
|
---|
| 142 | static public class TrieVertex {
|
---|
| 143 | private String id;
|
---|
| 144 | protected TrieVertex(String id) {
|
---|
| 145 | this.id = id;
|
---|
| 146 | }
|
---|
| 147 | public String toString() {
|
---|
| 148 | return id;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[23] | 152 | protected Tree<TrieVertex, Edge> getGraph() {
|
---|
[5] | 153 | DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
|
---|
[6] | 154 | rootNode.getGraph(null, graph);
|
---|
[5] | 155 | return graph;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[30] | 158 | public String getDotRepresentation() {
|
---|
| 159 | StringBuilder stringBuilder = new StringBuilder();
|
---|
| 160 | stringBuilder.append("digraph model {" + StringTools.ENDLINE);
|
---|
| 161 | rootNode.appendDotRepresentation(stringBuilder);
|
---|
| 162 | stringBuilder.append('}' + StringTools.ENDLINE);
|
---|
| 163 | return stringBuilder.toString();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[1] | 166 | @Override
|
---|
| 167 | public String toString() {
|
---|
[6] | 168 | return rootNode.toString();
|
---|
[1] | 169 | }
|
---|
[66] | 170 |
|
---|
| 171 | public int getNumSymbols() {
|
---|
| 172 | return knownSymbols.size();
|
---|
| 173 | }
|
---|
[1] | 174 | }
|
---|