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