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