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 | private final TrieNode<T> rootNode;
|
---|
24 |
|
---|
25 | public Trie() {
|
---|
26 | rootNode = new TrieNode<T>();
|
---|
27 | }
|
---|
28 |
|
---|
29 |
|
---|
30 | public void add(List<T> subsequence) {
|
---|
31 | if( !subsequence.isEmpty() ) {
|
---|
32 | subsequence = new LinkedList<T>(subsequence); // defensive copy!
|
---|
33 | T firstSymbol = subsequence.get(0);
|
---|
34 | TrieNode<T> node = getChildCreate(firstSymbol);
|
---|
35 | node.add(subsequence);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
40 | return rootNode.getChildCreate(symbol);
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected TrieNode<T> getChild(T symbol) {
|
---|
44 | return rootNode.getChild(symbol);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // get the count of "sequence"
|
---|
48 | public int getCount(List<T> sequence) {
|
---|
49 | int count = 0;
|
---|
50 | TrieNode<T> node = find(sequence);
|
---|
51 | if( node!=null ) {
|
---|
52 | count = node.getCount();
|
---|
53 | }
|
---|
54 | return count;
|
---|
55 | }
|
---|
56 |
|
---|
57 | // get the count of "sequence,follower"
|
---|
58 | public int getCount(List<T> sequence, T follower) {
|
---|
59 | List<T> tmpSequence = new LinkedList<T>(sequence);
|
---|
60 | tmpSequence.add(follower);
|
---|
61 | return getCount(tmpSequence);
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 | public TrieNode<T> find(List<T> sequence) {
|
---|
66 | if( sequence==null || sequence.isEmpty() ) {
|
---|
67 | return rootNode;
|
---|
68 | }
|
---|
69 | List<T> sequenceCopy = new LinkedList<T>(sequence);
|
---|
70 | TrieNode<T> result = null;
|
---|
71 | TrieNode<T> node = getChild(sequenceCopy.get(0));
|
---|
72 | if( node!=null ) {
|
---|
73 | sequenceCopy.remove(0);
|
---|
74 | result = node.find(sequenceCopy);
|
---|
75 | }
|
---|
76 | return result;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // returns all symbols that follow the defined sequence
|
---|
80 | public List<T> getFollowingSymbols(List<T> sequence) {
|
---|
81 | List<T> result = new LinkedList<T>();
|
---|
82 | TrieNode<T> node = find(sequence);
|
---|
83 | if( node!=null ) {
|
---|
84 | result = node.getFollowingSymbols();
|
---|
85 | }
|
---|
86 | return result;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // longest suffix of context, that is contained in the tree and whose children are leaves
|
---|
90 | // possibly already deprecated
|
---|
91 | public List<T> getContextSuffix(List<T> context) {
|
---|
92 | List<T> contextSuffix = new LinkedList<T>(context); // defensive copy
|
---|
93 | boolean suffixFound = false;
|
---|
94 |
|
---|
95 | while(!suffixFound) {
|
---|
96 | if( contextSuffix.isEmpty() ) {
|
---|
97 | suffixFound = true; // suffix is the empty word
|
---|
98 | } else {
|
---|
99 | TrieNode<T> node = find(contextSuffix);
|
---|
100 | if( node!=null ) {
|
---|
101 | if( !node.getFollowingSymbols().isEmpty() ) {
|
---|
102 | suffixFound = true;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | if( !suffixFound ) {
|
---|
106 | contextSuffix.remove(0);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return contextSuffix;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | static public class Edge{}
|
---|
116 |
|
---|
117 | static public class TrieVertex {
|
---|
118 | private String id;
|
---|
119 | protected TrieVertex(String id) {
|
---|
120 | this.id = id;
|
---|
121 | }
|
---|
122 | public String toString() {
|
---|
123 | return id;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private Tree<TrieVertex, Edge> getGraph() {
|
---|
128 | DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
|
---|
129 | rootNode.getGraph(null, graph);
|
---|
130 | return graph;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public void display() {
|
---|
134 | Tree<TrieVertex, Edge> graph = this.getGraph();
|
---|
135 | Layout<TrieVertex, Edge> layout = new TreeLayout<TrieVertex, Edge>(graph, 60);
|
---|
136 | // The BasicVisualizationServer<V,E> is parameterized by the edge types
|
---|
137 | BasicVisualizationServer<TrieVertex,Edge> vv =
|
---|
138 | new BasicVisualizationServer<TrieVertex,Edge>(layout);
|
---|
139 | vv.setPreferredSize(new Dimension(1100,850)); //Sets the viewing area size
|
---|
140 |
|
---|
141 |
|
---|
142 | final Rectangle rect = new Rectangle(40, 20);
|
---|
143 |
|
---|
144 | Transformer<TrieVertex, Shape> vertexShapeTransformer =
|
---|
145 | new Transformer<TrieVertex, Shape>() {
|
---|
146 | public Shape transform(TrieVertex s) {
|
---|
147 | return rect;
|
---|
148 | }
|
---|
149 | };
|
---|
150 | vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
|
---|
151 | vv.getRenderContext().setVertexShapeTransformer(vertexShapeTransformer);
|
---|
152 | vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<TrieVertex>());
|
---|
153 |
|
---|
154 | JFrame frame = new JFrame("Trie");
|
---|
155 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
---|
156 | frame.getContentPane().add(vv);
|
---|
157 | frame.pack();
|
---|
158 | frame.setVisible(true);
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | public String toString() {
|
---|
163 | return rootNode.toString();
|
---|
164 | }
|
---|
165 | }
|
---|