1 | package de.ugoe.cs.eventbench.models;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.LinkedHashSet;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.util.StringTools;
|
---|
10 |
|
---|
11 | import edu.uci.ics.jung.graph.DelegateTree;
|
---|
12 | import edu.uci.ics.jung.graph.Tree;
|
---|
13 |
|
---|
14 | public class Trie<T> implements IDotCompatible, Serializable {
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Id for object serialization.
|
---|
18 | */
|
---|
19 | private static final long serialVersionUID = 1L;
|
---|
20 |
|
---|
21 | private Set<T> knownSymbols;
|
---|
22 |
|
---|
23 | private final TrieNode<T> rootNode;
|
---|
24 |
|
---|
25 | public Trie() {
|
---|
26 | rootNode = new TrieNode<T>();
|
---|
27 | knownSymbols = new LinkedHashSet<T>();
|
---|
28 | }
|
---|
29 |
|
---|
30 | public Set<T> getKnownSymbols() {
|
---|
31 | return new LinkedHashSet<T>(knownSymbols);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // trains the current Trie using the given sequence and adds all subsequence of length trieOrder
|
---|
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 |
|
---|
52 |
|
---|
53 | // increases the counters for each symbol in the subsequence
|
---|
54 | public void add(List<T> subsequence) {
|
---|
55 | if( subsequence!=null && !subsequence.isEmpty() ) {
|
---|
56 | knownSymbols.addAll(subsequence);
|
---|
57 | subsequence = new LinkedList<T>(subsequence); // defensive copy!
|
---|
58 | T firstSymbol = subsequence.get(0);
|
---|
59 | TrieNode<T> node = getChildCreate(firstSymbol);
|
---|
60 | node.add(subsequence);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
65 | return rootNode.getChildCreate(symbol);
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected TrieNode<T> getChild(T symbol) {
|
---|
69 | return rootNode.getChild(symbol);
|
---|
70 | }
|
---|
71 |
|
---|
72 | // get the count of "sequence"
|
---|
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 |
|
---|
82 | // get the count of "sequence,follower"
|
---|
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) {
|
---|
91 | if( sequence==null || sequence.isEmpty() ) {
|
---|
92 | return rootNode;
|
---|
93 | }
|
---|
94 | List<T> sequenceCopy = new LinkedList<T>(sequence);
|
---|
95 | TrieNode<T> result = null;
|
---|
96 | TrieNode<T> node = getChild(sequenceCopy.get(0));
|
---|
97 | if( node!=null ) {
|
---|
98 | sequenceCopy.remove(0);
|
---|
99 | result = node.find(sequenceCopy);
|
---|
100 | }
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // returns all symbols that follow the defined sequence
|
---|
105 | public List<T> getFollowingSymbols(List<T> sequence) {
|
---|
106 | List<T> result = new LinkedList<T>();
|
---|
107 | TrieNode<T> node = find(sequence);
|
---|
108 | if( node!=null ) {
|
---|
109 | result = node.getFollowingSymbols();
|
---|
110 | }
|
---|
111 | return result;
|
---|
112 | }
|
---|
113 |
|
---|
114 | // longest suffix of context, that is contained in the tree and whose children are leaves
|
---|
115 | // possibly already deprecated
|
---|
116 | public List<T> getContextSuffix(List<T> context) {
|
---|
117 | List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
|
---|
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 | }
|
---|
130 | if( !suffixFound ) {
|
---|
131 | contextSuffix.remove(0);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | return contextSuffix;
|
---|
137 | }
|
---|
138 |
|
---|
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 |
|
---|
152 | protected Tree<TrieVertex, Edge> getGraph() {
|
---|
153 | DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
|
---|
154 | rootNode.getGraph(null, graph);
|
---|
155 | return graph;
|
---|
156 | }
|
---|
157 |
|
---|
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 |
|
---|
166 | @Override
|
---|
167 | public String toString() {
|
---|
168 | return rootNode.toString();
|
---|
169 | }
|
---|
170 |
|
---|
171 | public int getNumSymbols() {
|
---|
172 | return knownSymbols.size();
|
---|
173 | }
|
---|
174 | }
|
---|