- Timestamp:
- 07/05/11 15:18:56 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/TrieNode.java
r102 r106 11 11 import de.ugoe.cs.util.StringTools; 12 12 import edu.uci.ics.jung.graph.DelegateTree; 13 14 13 import edu.uci.ics.jung.graph.Tree; 14 15 /** 16 * <p> 17 * This class implements a node of a trie. Each node is associated with a symbol 18 * and has a counter. The counter marks the number of occurences of the sequence 19 * defined by the path from the root of the trie to this node. 20 * </p> 21 * 22 * @author Steffen Herbold 23 * 24 * @param <T> 25 * Type of the symbols that are stored in the trie. 26 * @see Trie 27 */ 15 28 class TrieNode<T> implements Serializable { 16 17 /** 29 30 /** 31 * <p> 18 32 * Id for object serialization. 33 * </p> 19 34 */ 20 35 private static final long serialVersionUID = 1L; 21 36 37 /** 38 * <p> 39 * Counter for the number of occurences of the sequence. 40 * </p> 41 */ 22 42 private int count; 43 44 /** 45 * <p> 46 * Symbol associated with the node. 47 * </p> 48 */ 23 49 private final T symbol; 24 50 51 /** 52 * <p> 53 * Child nodes of this node. If the node is a leaf this collection is empty. 54 * </p> 55 */ 25 56 private Collection<TrieNode<T>> children; 26 57 58 /** 59 * <p> 60 * Constructor. Creates a new TrieNode without a symbol associated.<br> 61 * <b>This constructor should only be used to create the root node of the 62 * trie!</b> 63 * </p> 64 */ 27 65 TrieNode() { 28 66 this.symbol = null; … … 30 68 children = new LinkedList<TrieNode<T>>(); 31 69 } 32 70 71 /** 72 * <p> 73 * Constructor. Creates a new TrieNode. The symbol must not be null. 74 * </p> 75 * 76 * @param symbol 77 * symbol associated with the trie node 78 */ 33 79 public TrieNode(T symbol) { 34 if( symbol==null ) { 35 throw new InvalidParameterException("symbol must not be null. null is reserved for root node!"); 80 if (symbol == null) { 81 throw new InvalidParameterException( 82 "symbol must not be null. null is reserved for root node!"); 36 83 } 37 84 this.symbol = symbol; … … 40 87 } 41 88 89 /** 90 * <p> 91 * Adds a given subsequence to the trie and increases the counters 92 * accordingly. 93 * </p> 94 * 95 * @param subsequence 96 * subsequence whose counters are increased 97 * @see Trie#add(List) 98 */ 42 99 public void add(List<T> subsequence) { 43 if( !subsequence.isEmpty() ) { 44 if( !symbol.equals(subsequence.get(0)) ) { // should be guaranteed by the recursion/TrieRoot! 100 if (!subsequence.isEmpty()) { 101 if (!symbol.equals(subsequence.get(0))) { // should be guaranteed by 102 // the 103 // recursion/TrieRoot! 45 104 throw new AssertionError("Invalid trie operation!"); 46 105 } 47 106 count++; 48 107 subsequence.remove(0); 49 if ( !subsequence.isEmpty()) {108 if (!subsequence.isEmpty()) { 50 109 T nextSymbol = subsequence.get(0); 51 110 getChildCreate(nextSymbol).add(subsequence); … … 53 112 } 54 113 } 55 114 115 /** 116 * <p> 117 * Returns the symbol associated with the node. 118 * </p> 119 * 120 * @return symbol associated with the node 121 */ 56 122 public T getSymbol() { 57 123 return symbol; 58 124 } 59 125 126 /** 127 * <p> 128 * Returns the number of occurences of the sequence represented by the node. 129 * </p> 130 * 131 * @return number of occurences of the sequence represented by the node 132 */ 60 133 public int getCount() { 61 134 return count; 62 135 } 63 64 protected TrieNode<T> getChildCreate(T symbol) { 136 137 /** 138 * <p> 139 * Returns the child of the node associated with the given symbol or creates 140 * it if it does not exist yet. 141 * </p> 142 * 143 * @param symbol 144 * symbol whose node is required 145 * @return node associated with the symbol 146 * @see Trie#getChildCreate(Object) 147 */ 148 protected TrieNode<T> getChildCreate(T symbol) { 65 149 TrieNode<T> node = getChild(symbol); 66 if ( node==null) {150 if (node == null) { 67 151 node = new TrieNode<T>(symbol); 68 152 children.add(node); … … 70 154 return node; 71 155 } 72 156 157 /** 158 * <p> 159 * Returns the child of the node associated with the given symbol or null if 160 * it does not exist. 161 * </p> 162 * 163 * @param symbol 164 * symbol whose node is required 165 * @return node associated with the symbol; null if no such node exists 166 * @see Trie#getChild(Object) 167 */ 73 168 protected TrieNode<T> getChild(T symbol) { 74 for ( TrieNode<T> child : children) {75 if ( child.getSymbol().equals(symbol)) {169 for (TrieNode<T> child : children) { 170 if (child.getSymbol().equals(symbol)) { 76 171 return child; 77 172 } … … 79 174 return null; 80 175 } 81 82 83 176 177 /** 178 * <p> 179 * Searches the sub-trie of this trie node for a given sequence and returns 180 * the node associated with the sequence or null if no such node is found. 181 * </p> 182 * 183 * @param sequence 184 * sequence that is searched for 185 * @return node associated with the sequence 186 * @see Trie#find(List) 187 */ 84 188 public TrieNode<T> find(List<T> subsequence) { 85 189 TrieNode<T> result = null; 86 if ( subsequence.isEmpty()) {190 if (subsequence.isEmpty()) { 87 191 result = this; 88 192 } else { 89 193 TrieNode<T> node = getChild(subsequence.get(0)); 90 if ( node!=null) {194 if (node != null) { 91 195 subsequence.remove(0); 92 196 result = node.find(subsequence); … … 95 199 return result; 96 200 } 97 98 // returns all symbols that follow this node 201 202 /** 203 * <p> 204 * Returns a collection of all symbols that follow a this node, i.e., the 205 * symbols associated with the children of this node. 206 * </p> 207 * 208 * @return symbols follow this node 209 * @see TrieNode#getFollowingSymbols() 210 */ 99 211 public Collection<T> getFollowingSymbols() { 100 212 Collection<T> followingSymbols = new LinkedList<T>(); 101 for ( TrieNode<T> child : children) {213 for (TrieNode<T> child : children) { 102 214 followingSymbols.add(child.getSymbol()); 103 215 } 104 216 return followingSymbols; 105 217 } 106 218 219 /** 220 * <p> 221 * The string representation of a node is {@code symbol.toString()#count} 222 * </p> 223 * 224 * @see java.lang.Object#toString() 225 */ 107 226 @Override 108 227 public String toString() { 109 String str = symbol.toString() +" #"+count;110 if ( !children.isEmpty()) {228 String str = symbol.toString() + " #" + count; 229 if (!children.isEmpty()) { 111 230 str += StringTools.ENDLINE + children.toString(); 112 231 } 113 return str; 114 } 115 116 public void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) { 232 return str; 233 } 234 235 /** 236 * <p> 237 * Generates a {@link Tree} represenation of the trie. 238 * </p> 239 * 240 * @param parent 241 * parent vertex in the generated tree 242 * @param graph 243 * complete tree 244 */ 245 void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) { 117 246 TrieVertex currentVertex; 118 if ( symbol==null ){247 if (symbol == null) { 119 248 currentVertex = new TrieVertex("root"); 120 249 graph.addVertex(currentVertex); 121 250 } else { 122 currentVertex = new TrieVertex(getSymbol().toString()+"#"+getCount()); 123 graph.addChild( new Edge() , parent, currentVertex ); 124 } 125 for( TrieNode<T> node : children ) { 251 currentVertex = new TrieVertex(getSymbol().toString() + "#" 252 + getCount()); 253 graph.addChild(new Edge(), parent, currentVertex); 254 } 255 for (TrieNode<T> node : children) { 126 256 node.getGraph(currentVertex, graph); 127 } 128 } 129 257 } 258 } 259 260 /** 261 * <p> 262 * Appends the current node to the dot representation of the trie. 263 * </p> 264 * 265 * @param stringBuilder 266 * {@link StringBuilder} to which the dot representation is 267 * appended 268 */ 130 269 void appendDotRepresentation(StringBuilder stringBuilder) { 131 270 String thisSaneId; 132 if ( symbol==null) {271 if (symbol == null) { 133 272 thisSaneId = "root"; 134 273 } else { 135 thisSaneId = symbol.toString().replace("\"", "\\\"").replaceAll("[\r\n]","")+"#"+count; 136 } 137 stringBuilder.append(" " + hashCode() + " [label=\""+thisSaneId+"\"];" + StringTools.ENDLINE); 138 for( TrieNode<T> childNode : children ) { 139 stringBuilder.append(" "+hashCode()+" -> " + childNode.hashCode() + ";" + StringTools.ENDLINE); 140 } 141 for( TrieNode<T> childNode : children ) { 274 thisSaneId = symbol.toString().replace("\"", "\\\"") 275 .replaceAll("[\r\n]", "") 276 + "#" + count; 277 } 278 stringBuilder.append(" " + hashCode() + " [label=\"" + thisSaneId 279 + "\"];" + StringTools.ENDLINE); 280 for (TrieNode<T> childNode : children) { 281 stringBuilder.append(" " + hashCode() + " -> " 282 + childNode.hashCode() + ";" + StringTools.ENDLINE); 283 } 284 for (TrieNode<T> childNode : children) { 142 285 childNode.appendDotRepresentation(stringBuilder); 143 286 }
Note: See TracChangeset
for help on using the changeset viewer.