[13] | 1 | package de.ugoe.cs.eventbench.models;
|
---|
[1] | 2 |
|
---|
[86] | 3 | import java.io.Serializable;
|
---|
[1] | 4 | import java.security.InvalidParameterException;
|
---|
[102] | 5 | import java.util.Collection;
|
---|
[1] | 6 | import java.util.LinkedList;
|
---|
| 7 | import java.util.List;
|
---|
[129] | 8 | import java.util.Set;
|
---|
[1] | 9 |
|
---|
[13] | 10 | import de.ugoe.cs.eventbench.models.Trie.Edge;
|
---|
| 11 | import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
|
---|
[1] | 12 | import de.ugoe.cs.util.StringTools;
|
---|
[5] | 13 | import edu.uci.ics.jung.graph.DelegateTree;
|
---|
[106] | 14 | import edu.uci.ics.jung.graph.Tree;
|
---|
[1] | 15 |
|
---|
[106] | 16 | /**
|
---|
| 17 | * <p>
|
---|
| 18 | * This class implements a node of a trie. Each node is associated with a symbol
|
---|
| 19 | * and has a counter. The counter marks the number of occurences of the sequence
|
---|
| 20 | * defined by the path from the root of the trie to this node.
|
---|
| 21 | * </p>
|
---|
| 22 | *
|
---|
| 23 | * @author Steffen Herbold
|
---|
| 24 | *
|
---|
| 25 | * @param <T>
|
---|
| 26 | * Type of the symbols that are stored in the trie.
|
---|
| 27 | * @see Trie
|
---|
| 28 | */
|
---|
| 29 | class TrieNode<T> implements Serializable {
|
---|
[1] | 30 |
|
---|
[86] | 31 | /**
|
---|
[106] | 32 | * <p>
|
---|
[86] | 33 | * Id for object serialization.
|
---|
[106] | 34 | * </p>
|
---|
[86] | 35 | */
|
---|
| 36 | private static final long serialVersionUID = 1L;
|
---|
[106] | 37 |
|
---|
| 38 | /**
|
---|
| 39 | * <p>
|
---|
| 40 | * Counter for the number of occurences of the sequence.
|
---|
| 41 | * </p>
|
---|
| 42 | */
|
---|
[1] | 43 | private int count;
|
---|
[106] | 44 |
|
---|
| 45 | /**
|
---|
| 46 | * <p>
|
---|
| 47 | * Symbol associated with the node.
|
---|
| 48 | * </p>
|
---|
| 49 | */
|
---|
[1] | 50 | private final T symbol;
|
---|
[106] | 51 |
|
---|
| 52 | /**
|
---|
| 53 | * <p>
|
---|
| 54 | * Child nodes of this node. If the node is a leaf this collection is empty.
|
---|
| 55 | * </p>
|
---|
| 56 | */
|
---|
[102] | 57 | private Collection<TrieNode<T>> children;
|
---|
[106] | 58 |
|
---|
| 59 | /**
|
---|
| 60 | * <p>
|
---|
| 61 | * Constructor. Creates a new TrieNode without a symbol associated.<br>
|
---|
| 62 | * <b>This constructor should only be used to create the root node of the
|
---|
| 63 | * trie!</b>
|
---|
| 64 | * </p>
|
---|
| 65 | */
|
---|
[6] | 66 | TrieNode() {
|
---|
| 67 | this.symbol = null;
|
---|
| 68 | count = 0;
|
---|
| 69 | children = new LinkedList<TrieNode<T>>();
|
---|
| 70 | }
|
---|
[106] | 71 |
|
---|
| 72 | /**
|
---|
| 73 | * <p>
|
---|
| 74 | * Constructor. Creates a new TrieNode. The symbol must not be null.
|
---|
| 75 | * </p>
|
---|
| 76 | *
|
---|
| 77 | * @param symbol
|
---|
| 78 | * symbol associated with the trie node
|
---|
| 79 | */
|
---|
[361] | 80 | TrieNode(T symbol) {
|
---|
[106] | 81 | if (symbol == null) {
|
---|
| 82 | throw new InvalidParameterException(
|
---|
| 83 | "symbol must not be null. null is reserved for root node!");
|
---|
[1] | 84 | }
|
---|
| 85 | this.symbol = symbol;
|
---|
| 86 | count = 0;
|
---|
| 87 | children = new LinkedList<TrieNode<T>>();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[106] | 90 | /**
|
---|
| 91 | * <p>
|
---|
[361] | 92 | * Copy-Constructor. Creates a new TrieNode as copy of other. Other must not
|
---|
| 93 | * be null.
|
---|
| 94 | * </p>
|
---|
| 95 | *
|
---|
| 96 | * @param other
|
---|
| 97 | */
|
---|
| 98 | TrieNode(TrieNode<T> other) {
|
---|
| 99 | if (other == null) {
|
---|
| 100 | throw new InvalidParameterException("other must not be null");
|
---|
| 101 | }
|
---|
| 102 | symbol = other.symbol;
|
---|
| 103 | count = other.count;
|
---|
| 104 | children = new LinkedList<TrieNode<T>>();
|
---|
| 105 | for (TrieNode<T> child : other.children) {
|
---|
| 106 | children.add(new TrieNode<T>(child));
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * <p>
|
---|
[106] | 112 | * Adds a given subsequence to the trie and increases the counters
|
---|
| 113 | * accordingly.
|
---|
| 114 | * </p>
|
---|
| 115 | *
|
---|
| 116 | * @param subsequence
|
---|
| 117 | * subsequence whose counters are increased
|
---|
| 118 | * @see Trie#add(List)
|
---|
| 119 | */
|
---|
[1] | 120 | public void add(List<T> subsequence) {
|
---|
[106] | 121 | if (!subsequence.isEmpty()) {
|
---|
| 122 | if (!symbol.equals(subsequence.get(0))) { // should be guaranteed by
|
---|
| 123 | // the
|
---|
| 124 | // recursion/TrieRoot!
|
---|
[1] | 125 | throw new AssertionError("Invalid trie operation!");
|
---|
| 126 | }
|
---|
| 127 | count++;
|
---|
| 128 | subsequence.remove(0);
|
---|
[106] | 129 | if (!subsequence.isEmpty()) {
|
---|
[1] | 130 | T nextSymbol = subsequence.get(0);
|
---|
| 131 | getChildCreate(nextSymbol).add(subsequence);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
[106] | 135 |
|
---|
| 136 | /**
|
---|
| 137 | * <p>
|
---|
| 138 | * Returns the symbol associated with the node.
|
---|
| 139 | * </p>
|
---|
| 140 | *
|
---|
| 141 | * @return symbol associated with the node
|
---|
| 142 | */
|
---|
[1] | 143 | public T getSymbol() {
|
---|
| 144 | return symbol;
|
---|
| 145 | }
|
---|
[106] | 146 |
|
---|
| 147 | /**
|
---|
| 148 | * <p>
|
---|
| 149 | * Returns the number of occurences of the sequence represented by the node.
|
---|
| 150 | * </p>
|
---|
| 151 | *
|
---|
| 152 | * @return number of occurences of the sequence represented by the node
|
---|
| 153 | */
|
---|
[1] | 154 | public int getCount() {
|
---|
| 155 | return count;
|
---|
| 156 | }
|
---|
[106] | 157 |
|
---|
| 158 | /**
|
---|
| 159 | * <p>
|
---|
| 160 | * Returns the child of the node associated with the given symbol or creates
|
---|
| 161 | * it if it does not exist yet.
|
---|
| 162 | * </p>
|
---|
| 163 | *
|
---|
| 164 | * @param symbol
|
---|
| 165 | * symbol whose node is required
|
---|
| 166 | * @return node associated with the symbol
|
---|
| 167 | * @see Trie#getChildCreate(Object)
|
---|
| 168 | */
|
---|
| 169 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
[1] | 170 | TrieNode<T> node = getChild(symbol);
|
---|
[106] | 171 | if (node == null) {
|
---|
[1] | 172 | node = new TrieNode<T>(symbol);
|
---|
| 173 | children.add(node);
|
---|
| 174 | }
|
---|
| 175 | return node;
|
---|
| 176 | }
|
---|
[106] | 177 |
|
---|
| 178 | /**
|
---|
| 179 | * <p>
|
---|
| 180 | * Returns the child of the node associated with the given symbol or null if
|
---|
| 181 | * it does not exist.
|
---|
| 182 | * </p>
|
---|
| 183 | *
|
---|
| 184 | * @param symbol
|
---|
| 185 | * symbol whose node is required
|
---|
| 186 | * @return node associated with the symbol; null if no such node exists
|
---|
| 187 | * @see Trie#getChild(Object)
|
---|
| 188 | */
|
---|
[1] | 189 | protected TrieNode<T> getChild(T symbol) {
|
---|
[106] | 190 | for (TrieNode<T> child : children) {
|
---|
| 191 | if (child.getSymbol().equals(symbol)) {
|
---|
[1] | 192 | return child;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | return null;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[106] | 198 | /**
|
---|
| 199 | * <p>
|
---|
[258] | 200 | * Returns all children of this node.
|
---|
| 201 | * </p>
|
---|
| 202 | *
|
---|
| 203 | * @return children of this node
|
---|
| 204 | */
|
---|
| 205 | protected Collection<TrieNode<T>> getChildren() {
|
---|
| 206 | return children;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | /**
|
---|
| 210 | * <p>
|
---|
[106] | 211 | * Searches the sub-trie of this trie node for a given sequence and returns
|
---|
| 212 | * the node associated with the sequence or null if no such node is found.
|
---|
| 213 | * </p>
|
---|
| 214 | *
|
---|
| 215 | * @param sequence
|
---|
| 216 | * sequence that is searched for
|
---|
| 217 | * @return node associated with the sequence
|
---|
| 218 | * @see Trie#find(List)
|
---|
| 219 | */
|
---|
[1] | 220 | public TrieNode<T> find(List<T> subsequence) {
|
---|
| 221 | TrieNode<T> result = null;
|
---|
[106] | 222 | if (subsequence.isEmpty()) {
|
---|
[1] | 223 | result = this;
|
---|
| 224 | } else {
|
---|
| 225 | TrieNode<T> node = getChild(subsequence.get(0));
|
---|
[106] | 226 | if (node != null) {
|
---|
[1] | 227 | subsequence.remove(0);
|
---|
| 228 | result = node.find(subsequence);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | return result;
|
---|
| 232 | }
|
---|
[106] | 233 |
|
---|
| 234 | /**
|
---|
| 235 | * <p>
|
---|
| 236 | * Returns a collection of all symbols that follow a this node, i.e., the
|
---|
| 237 | * symbols associated with the children of this node.
|
---|
| 238 | * </p>
|
---|
| 239 | *
|
---|
| 240 | * @return symbols follow this node
|
---|
| 241 | * @see TrieNode#getFollowingSymbols()
|
---|
| 242 | */
|
---|
[102] | 243 | public Collection<T> getFollowingSymbols() {
|
---|
| 244 | Collection<T> followingSymbols = new LinkedList<T>();
|
---|
[106] | 245 | for (TrieNode<T> child : children) {
|
---|
[1] | 246 | followingSymbols.add(child.getSymbol());
|
---|
| 247 | }
|
---|
| 248 | return followingSymbols;
|
---|
| 249 | }
|
---|
[106] | 250 |
|
---|
| 251 | /**
|
---|
| 252 | * <p>
|
---|
| 253 | * The string representation of a node is {@code symbol.toString()#count}
|
---|
| 254 | * </p>
|
---|
| 255 | *
|
---|
| 256 | * @see java.lang.Object#toString()
|
---|
| 257 | */
|
---|
[1] | 258 | @Override
|
---|
| 259 | public String toString() {
|
---|
[106] | 260 | String str = symbol.toString() + " #" + count;
|
---|
| 261 | if (!children.isEmpty()) {
|
---|
[1] | 262 | str += StringTools.ENDLINE + children.toString();
|
---|
| 263 | }
|
---|
[106] | 264 | return str;
|
---|
[1] | 265 | }
|
---|
| 266 |
|
---|
[106] | 267 | /**
|
---|
| 268 | * <p>
|
---|
| 269 | * Generates a {@link Tree} represenation of the trie.
|
---|
| 270 | * </p>
|
---|
| 271 | *
|
---|
| 272 | * @param parent
|
---|
| 273 | * parent vertex in the generated tree
|
---|
| 274 | * @param graph
|
---|
| 275 | * complete tree
|
---|
| 276 | */
|
---|
| 277 | void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) {
|
---|
[6] | 278 | TrieVertex currentVertex;
|
---|
[251] | 279 | if (isRoot()) {
|
---|
[6] | 280 | currentVertex = new TrieVertex("root");
|
---|
| 281 | graph.addVertex(currentVertex);
|
---|
| 282 | } else {
|
---|
[106] | 283 | currentVertex = new TrieVertex(getSymbol().toString() + "#"
|
---|
| 284 | + getCount());
|
---|
| 285 | graph.addChild(new Edge(), parent, currentVertex);
|
---|
[6] | 286 | }
|
---|
[106] | 287 | for (TrieNode<T> node : children) {
|
---|
[6] | 288 | node.getGraph(currentVertex, graph);
|
---|
[106] | 289 | }
|
---|
[5] | 290 | }
|
---|
[106] | 291 |
|
---|
| 292 | /**
|
---|
| 293 | * <p>
|
---|
| 294 | * Appends the current node to the dot representation of the trie.
|
---|
| 295 | * </p>
|
---|
| 296 | *
|
---|
| 297 | * @param stringBuilder
|
---|
| 298 | * {@link StringBuilder} to which the dot representation is
|
---|
| 299 | * appended
|
---|
| 300 | */
|
---|
[30] | 301 | void appendDotRepresentation(StringBuilder stringBuilder) {
|
---|
| 302 | String thisSaneId;
|
---|
[251] | 303 | if (isRoot()) {
|
---|
[30] | 304 | thisSaneId = "root";
|
---|
| 305 | } else {
|
---|
[106] | 306 | thisSaneId = symbol.toString().replace("\"", "\\\"")
|
---|
| 307 | .replaceAll("[\r\n]", "")
|
---|
| 308 | + "#" + count;
|
---|
[30] | 309 | }
|
---|
[106] | 310 | stringBuilder.append(" " + hashCode() + " [label=\"" + thisSaneId
|
---|
| 311 | + "\"];" + StringTools.ENDLINE);
|
---|
| 312 | for (TrieNode<T> childNode : children) {
|
---|
| 313 | stringBuilder.append(" " + hashCode() + " -> "
|
---|
| 314 | + childNode.hashCode() + ";" + StringTools.ENDLINE);
|
---|
[30] | 315 | }
|
---|
[106] | 316 | for (TrieNode<T> childNode : children) {
|
---|
[30] | 317 | childNode.appendDotRepresentation(stringBuilder);
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
[129] | 320 |
|
---|
| 321 | /**
|
---|
| 322 | * <p>
|
---|
| 323 | * Checks if the node is a leaf.
|
---|
| 324 | * </p>
|
---|
| 325 | *
|
---|
| 326 | * @return true if the node is a leaf, false otherwise.
|
---|
| 327 | */
|
---|
| 328 | protected boolean isLeaf() {
|
---|
| 329 | return children.isEmpty();
|
---|
| 330 | }
|
---|
[258] | 331 |
|
---|
[251] | 332 | /**
|
---|
| 333 | * <p>
|
---|
| 334 | * Checks if the node is the root.
|
---|
| 335 | * </p>
|
---|
| 336 | *
|
---|
| 337 | * @return true if the node is the root of the trie, false otherwise
|
---|
| 338 | */
|
---|
| 339 | protected boolean isRoot() {
|
---|
[258] | 340 | return symbol == null;
|
---|
[251] | 341 | }
|
---|
[129] | 342 |
|
---|
| 343 | /**
|
---|
| 344 | * <p>
|
---|
| 345 | * Recursive methods that collects all nodes that are ancestors of leafs and
|
---|
| 346 | * stores them in the set.
|
---|
| 347 | * </p>
|
---|
| 348 | *
|
---|
| 349 | * @param ancestors
|
---|
| 350 | * set of all ancestors of leafs
|
---|
| 351 | */
|
---|
| 352 | protected void getLeafAncestors(Set<TrieNode<T>> ancestors) {
|
---|
| 353 | boolean isAncestor = false;
|
---|
| 354 | for (TrieNode<T> child : children) {
|
---|
| 355 | child.getLeafAncestors(ancestors);
|
---|
| 356 | isAncestor |= child.isLeaf();
|
---|
| 357 | }
|
---|
| 358 | if (isAncestor) {
|
---|
| 359 | ancestors.add(this);
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
[258] | 362 |
|
---|
[248] | 363 | /**
|
---|
[258] | 364 | * <p>
|
---|
| 365 | * Returns the number of descendants of this node that are leafs. This does
|
---|
| 366 | * not only include direct children of this node, but all leafs in the
|
---|
| 367 | * sub-trie with this node as root.
|
---|
[248] | 368 | * </p>
|
---|
[258] | 369 | *
|
---|
| 370 | * @return number of leafs in this sub-trie
|
---|
[248] | 371 | */
|
---|
| 372 | protected int getNumLeafs() {
|
---|
| 373 | int numLeafs = 0;
|
---|
[258] | 374 | for (TrieNode<T> child : children) {
|
---|
| 375 | if (child.isLeaf()) {
|
---|
[248] | 376 | numLeafs++;
|
---|
| 377 | } else {
|
---|
| 378 | numLeafs += child.getNumLeafs();
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
| 381 | return numLeafs;
|
---|
| 382 | }
|
---|
[258] | 383 |
|
---|
| 384 | /**
|
---|
| 385 | * <p>
|
---|
| 386 | * Sets the {@link #count} of this node.
|
---|
| 387 | * </p>
|
---|
| 388 | * <p>
|
---|
| 389 | * This function should only be used sparingly and very carefully! The count
|
---|
| 390 | * is usually maintained automatically by the training procedures.
|
---|
| 391 | * </p>
|
---|
| 392 | *
|
---|
| 393 | * @param count
|
---|
| 394 | * new count
|
---|
| 395 | */
|
---|
| 396 | protected void setCount(int count) {
|
---|
| 397 | this.count = count;
|
---|
| 398 | }
|
---|
[1] | 399 | }
|
---|