1 | package de.ugoe.cs.eventbench.ppm;
|
---|
2 |
|
---|
3 | import java.awt.Dimension;
|
---|
4 | import java.awt.Rectangle;
|
---|
5 | import java.awt.Shape;
|
---|
6 | import java.util.LinkedHashSet;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import javax.swing.JFrame;
|
---|
12 |
|
---|
13 | import org.apache.commons.collections15.Transformer;
|
---|
14 |
|
---|
15 | import de.ugoe.cs.eventbench.markov.IncompleteMemory;
|
---|
16 |
|
---|
17 | import edu.uci.ics.jung.algorithms.layout.Layout;
|
---|
18 | import edu.uci.ics.jung.algorithms.layout.TreeLayout;
|
---|
19 | import edu.uci.ics.jung.graph.DelegateTree;
|
---|
20 | import edu.uci.ics.jung.graph.Tree;
|
---|
21 | import edu.uci.ics.jung.visualization.BasicVisualizationServer;
|
---|
22 | import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
|
---|
23 | import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
|
---|
24 |
|
---|
25 | public class Trie<T> {
|
---|
26 |
|
---|
27 | private Set<T> knownSymbols;
|
---|
28 |
|
---|
29 | private final TrieNode<T> rootNode;
|
---|
30 |
|
---|
31 | public Trie() {
|
---|
32 | rootNode = new TrieNode<T>();
|
---|
33 | knownSymbols = new LinkedHashSet<T>();
|
---|
34 | }
|
---|
35 |
|
---|
36 | public Set<T> getKnownSymbols() {
|
---|
37 | return new LinkedHashSet<T>(knownSymbols);
|
---|
38 | }
|
---|
39 |
|
---|
40 | // trains the current Trie using the given sequence and adds all subsequence of length maxOrder
|
---|
41 | public void train(List<T> sequence, int maxOrder) {
|
---|
42 | IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder);
|
---|
43 | int i=0;
|
---|
44 | for(T currentEvent : sequence) {
|
---|
45 | latestActions.add(currentEvent);
|
---|
46 | knownSymbols.add(currentEvent);
|
---|
47 | i++;
|
---|
48 | if( i>=maxOrder ) {
|
---|
49 | add(latestActions.getLast(maxOrder));
|
---|
50 | }
|
---|
51 | }
|
---|
52 | int sequenceLength = sequence.size();
|
---|
53 | for( int j=maxOrder-1 ; j>0 ; j-- ) {
|
---|
54 | add(sequence.subList(sequenceLength-j, sequenceLength));
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | // increases the counters for each symbol in the subsequence
|
---|
60 | public void add(List<T> subsequence) {
|
---|
61 | if( subsequence!=null && !subsequence.isEmpty() ) {
|
---|
62 | knownSymbols.addAll(subsequence);
|
---|
63 | subsequence = new LinkedList<T>(subsequence); // defensive copy!
|
---|
64 | T firstSymbol = subsequence.get(0);
|
---|
65 | TrieNode<T> node = getChildCreate(firstSymbol);
|
---|
66 | node.add(subsequence);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
71 | return rootNode.getChildCreate(symbol);
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected TrieNode<T> getChild(T symbol) {
|
---|
75 | return rootNode.getChild(symbol);
|
---|
76 | }
|
---|
77 |
|
---|
78 | // get the count of "sequence"
|
---|
79 | public int getCount(List<T> sequence) {
|
---|
80 | int count = 0;
|
---|
81 | TrieNode<T> node = find(sequence);
|
---|
82 | if( node!=null ) {
|
---|
83 | count = node.getCount();
|
---|
84 | }
|
---|
85 | return count;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // get the count of "sequence,follower"
|
---|
89 | public int getCount(List<T> sequence, T follower) {
|
---|
90 | List<T> tmpSequence = new LinkedList<T>(sequence);
|
---|
91 | tmpSequence.add(follower);
|
---|
92 | return getCount(tmpSequence);
|
---|
93 |
|
---|
94 | }
|
---|
95 |
|
---|
96 | public TrieNode<T> find(List<T> sequence) {
|
---|
97 | if( sequence==null || sequence.isEmpty() ) {
|
---|
98 | return rootNode;
|
---|
99 | }
|
---|
100 | List<T> sequenceCopy = new LinkedList<T>(sequence);
|
---|
101 | TrieNode<T> result = null;
|
---|
102 | TrieNode<T> node = getChild(sequenceCopy.get(0));
|
---|
103 | if( node!=null ) {
|
---|
104 | sequenceCopy.remove(0);
|
---|
105 | result = node.find(sequenceCopy);
|
---|
106 | }
|
---|
107 | return result;
|
---|
108 | }
|
---|
109 |
|
---|
110 | // returns all symbols that follow the defined sequence
|
---|
111 | public List<T> getFollowingSymbols(List<T> sequence) {
|
---|
112 | List<T> result = new LinkedList<T>();
|
---|
113 | TrieNode<T> node = find(sequence);
|
---|
114 | if( node!=null ) {
|
---|
115 | result = node.getFollowingSymbols();
|
---|
116 | }
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | // longest suffix of context, that is contained in the tree and whose children are leaves
|
---|
121 | // possibly already deprecated
|
---|
122 | public List<T> getContextSuffix(List<T> context) {
|
---|
123 | List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
|
---|
124 | boolean suffixFound = false;
|
---|
125 |
|
---|
126 | while(!suffixFound) {
|
---|
127 | if( contextSuffix.isEmpty() ) {
|
---|
128 | suffixFound = true; // suffix is the empty word
|
---|
129 | } else {
|
---|
130 | TrieNode<T> node = find(contextSuffix);
|
---|
131 | if( node!=null ) {
|
---|
132 | if( !node.getFollowingSymbols().isEmpty() ) {
|
---|
133 | suffixFound = true;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if( !suffixFound ) {
|
---|
137 | contextSuffix.remove(0);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | return contextSuffix;
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | static public class Edge{}
|
---|
147 |
|
---|
148 | static public class TrieVertex {
|
---|
149 | private String id;
|
---|
150 | protected TrieVertex(String id) {
|
---|
151 | this.id = id;
|
---|
152 | }
|
---|
153 | public String toString() {
|
---|
154 | return id;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private Tree<TrieVertex, Edge> getGraph() {
|
---|
159 | DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
|
---|
160 | rootNode.getGraph(null, graph);
|
---|
161 | return graph;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void display() {
|
---|
165 | Tree<TrieVertex, Edge> graph = this.getGraph();
|
---|
166 | Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(graph, 60);
|
---|
167 | // The BasicVisualizationServer<V,E> is parameterized by the edge types
|
---|
168 | BasicVisualizationServer<TrieVertex,Edge> vv =
|
---|
169 | new BasicVisualizationServer<TrieVertex,Edge>(layout);
|
---|
170 | vv.setPreferredSize(new Dimension(1100,850)); //Sets the viewing area size
|
---|
171 |
|
---|
172 |
|
---|
173 | final Rectangle rect = new Rectangle(40, 20);
|
---|
174 |
|
---|
175 | Transformer<TrieVertex, Shape> vertexShapeTransformer =
|
---|
176 | new Transformer<TrieVertex, Shape>() {
|
---|
177 | public Shape transform(TrieVertex s) {
|
---|
178 | return rect;
|
---|
179 | }
|
---|
180 | };
|
---|
181 | vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
|
---|
182 | vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
|
---|
183 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<TrieVertex>());
|
---|
184 |
|
---|
185 | JFrame frame = new JFrame("Trie");
|
---|
186 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
187 | frame.getContentPane().add(vv);
|
---|
188 | frame.pack();
|
---|
189 | frame.setVisible(true);
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public String toString() {
|
---|
194 | return rootNode.toString();
|
---|
195 | }
|
---|
196 | }
|
---|