- Timestamp:
- 04/11/11 14:43:03 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/ppm/Trie.java
r1 r5 1 1 package de.ugoe.cs.eventbench.ppm; 2 2 3 import java.awt.Dimension; 4 import java.awt.Rectangle; 5 import java.awt.Shape; 3 6 import java.util.LinkedList; 4 7 import java.util.List; 5 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 6 21 public class Trie<T> { 7 22 23 // Children of the Trie root 24 // should contain counts of all elements 8 25 private List<TrieNode<T>> children = new LinkedList<TrieNode<T>>(); 9 26 … … 11 28 public void add(List<T> subsequence) { 12 29 if( !subsequence.isEmpty() ) { 13 subsequence = new LinkedList<T>(subsequence); // copy list!30 subsequence = new LinkedList<T>(subsequence); // defensive copy! 14 31 T firstSymbol = subsequence.get(0); 15 32 getChildCreate(firstSymbol).add(subsequence); … … 37 54 } 38 55 56 // get the count of "sequence" 39 57 public int getCount(List<T> sequence) { 40 58 int count = 0; … … 46 64 } 47 65 66 // get the count of "sequence,follower" 48 67 public int getCount(List<T> sequence, T follower) { 49 68 List<T> tmpSequence = new LinkedList<T>(sequence); … … 54 73 55 74 public TrieNode<T> find(List<T> sequence) { 75 List<T> sequenceCopy = new LinkedList<T>(sequence); 56 76 TrieNode<T> result = null; 57 if( !sequence .isEmpty() ) {58 TrieNode<T> node = getChild(sequence .get(0));77 if( !sequenceCopy.isEmpty() ) { 78 TrieNode<T> node = getChild(sequenceCopy.get(0)); 59 79 if( node!=null ) { 60 result = node.find(sequence); 80 sequenceCopy.remove(0); 81 result = node.find(sequenceCopy); 61 82 } 62 83 } … … 64 85 } 65 86 87 // returns all symbols that follow the defined sequence 66 88 public List<T> getFollowingSymbols(List<T> sequence) { 67 List<T> result = null; 68 TrieNode<T> node = find(sequence); 69 if( node!=null ) { 70 result = node.getFollowingSymbols(); 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 } 71 99 } 72 100 return result; 73 101 } 74 102 103 // longest suffix of context, that is contained in the tree and whose children are leaves 75 104 public List<T> getContextSuffix(List<T> context) { 76 List<T> contextSuffix = new LinkedList<T>(context); 105 List<T> contextSuffix = new LinkedList<T>(context); // defensive copy 77 106 boolean suffixFound = false; 78 107 … … 87 116 } 88 117 } 89 contextSuffix.remove(0); 118 if( !suffixFound ) { 119 contextSuffix.remove(0); 120 } 90 121 } 91 122 } 92 123 93 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); 94 179 } 95 180
Note: See TracChangeset
for help on using the changeset viewer.