[13] | 1 | package de.ugoe.cs.eventbench.models;
|
---|
[1] | 2 |
|
---|
[86] | 3 | import java.io.Serializable;
|
---|
[361] | 4 | import java.security.InvalidParameterException;
|
---|
[102] | 5 | import java.util.Collection;
|
---|
[129] | 6 | import java.util.HashSet;
|
---|
[9] | 7 | import java.util.LinkedHashSet;
|
---|
[1] | 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.List;
|
---|
[129] | 10 | import java.util.Set;
|
---|
[1] | 11 |
|
---|
[30] | 12 | import de.ugoe.cs.util.StringTools;
|
---|
| 13 |
|
---|
[5] | 14 | import edu.uci.ics.jung.graph.DelegateTree;
|
---|
[106] | 15 | import edu.uci.ics.jung.graph.Graph;
|
---|
[5] | 16 | import edu.uci.ics.jung.graph.Tree;
|
---|
| 17 |
|
---|
[106] | 18 | /**
|
---|
| 19 | * <p>
|
---|
| 20 | * This class implements a <it>trie</it>, i.e., a tree of sequences that the
|
---|
| 21 | * occurence of subsequences up to a predefined length. This length is the trie
|
---|
| 22 | * order.
|
---|
| 23 | * </p>
|
---|
| 24 | *
|
---|
| 25 | * @author Steffen Herbold
|
---|
| 26 | *
|
---|
| 27 | * @param <T>
|
---|
| 28 | * Type of the symbols that are stored in the trie.
|
---|
| 29 | *
|
---|
| 30 | * @see TrieNode
|
---|
| 31 | */
|
---|
[86] | 32 | public class Trie<T> implements IDotCompatible, Serializable {
|
---|
[106] | 33 |
|
---|
[86] | 34 | /**
|
---|
[106] | 35 | * <p>
|
---|
[86] | 36 | * Id for object serialization.
|
---|
[106] | 37 | * </p>
|
---|
[86] | 38 | */
|
---|
| 39 | private static final long serialVersionUID = 1L;
|
---|
| 40 |
|
---|
[106] | 41 | /**
|
---|
| 42 | * <p>
|
---|
| 43 | * Collection of all symbols occuring in the trie.
|
---|
| 44 | * </p>
|
---|
| 45 | */
|
---|
| 46 | private Collection<T> knownSymbols;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * <p>
|
---|
| 50 | * Reference to the root of the trie.
|
---|
| 51 | * </p>
|
---|
| 52 | */
|
---|
[6] | 53 | private final TrieNode<T> rootNode;
|
---|
[106] | 54 |
|
---|
| 55 | /**
|
---|
| 56 | * <p>
|
---|
| 57 | * Contructor. Creates a new Trie.
|
---|
| 58 | * </p>
|
---|
| 59 | */
|
---|
[6] | 60 | public Trie() {
|
---|
| 61 | rootNode = new TrieNode<T>();
|
---|
[9] | 62 | knownSymbols = new LinkedHashSet<T>();
|
---|
[6] | 63 | }
|
---|
[106] | 64 |
|
---|
| 65 | /**
|
---|
| 66 | * <p>
|
---|
[361] | 67 | * Copy-Constructor. Creates a new Trie as the copy of other. The other trie
|
---|
| 68 | * must noch be null.
|
---|
| 69 | * </p>
|
---|
| 70 | *
|
---|
| 71 | * @param other
|
---|
| 72 | * Trie that is copied
|
---|
| 73 | */
|
---|
| 74 | public Trie(Trie<T> other) {
|
---|
| 75 | if (other == null) {
|
---|
| 76 | throw new InvalidParameterException("other trie must not be null");
|
---|
| 77 | }
|
---|
| 78 | rootNode = new TrieNode<T>(other.rootNode);
|
---|
| 79 | knownSymbols = new LinkedHashSet<T>(other.knownSymbols);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /**
|
---|
| 83 | * <p>
|
---|
[106] | 84 | * Returns a collection of all symbols occuring in the trie.
|
---|
| 85 | * </p>
|
---|
| 86 | *
|
---|
| 87 | * @return symbols occuring in the trie
|
---|
| 88 | */
|
---|
| 89 | public Collection<T> getKnownSymbols() {
|
---|
[9] | 90 | return new LinkedHashSet<T>(knownSymbols);
|
---|
| 91 | }
|
---|
[106] | 92 |
|
---|
| 93 | /**
|
---|
| 94 | * <p>
|
---|
| 95 | * Trains the current trie using the given sequence and adds all subsequence
|
---|
| 96 | * of length {@code maxOrder}.
|
---|
| 97 | * </p>
|
---|
| 98 | *
|
---|
| 99 | * @param sequence
|
---|
| 100 | * sequence whose subsequences are added to the trie
|
---|
| 101 | * @param maxOrder
|
---|
| 102 | * maximum length of the subsequences added to the trie
|
---|
| 103 | */
|
---|
[9] | 104 | public void train(List<T> sequence, int maxOrder) {
|
---|
[258] | 105 | if (maxOrder < 1) {
|
---|
[251] | 106 | return;
|
---|
| 107 | }
|
---|
[9] | 108 | IncompleteMemory<T> latestActions = new IncompleteMemory<T>(maxOrder);
|
---|
[106] | 109 | int i = 0;
|
---|
| 110 | for (T currentEvent : sequence) {
|
---|
[9] | 111 | latestActions.add(currentEvent);
|
---|
| 112 | knownSymbols.add(currentEvent);
|
---|
| 113 | i++;
|
---|
[106] | 114 | if (i >= maxOrder) {
|
---|
[9] | 115 | add(latestActions.getLast(maxOrder));
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | int sequenceLength = sequence.size();
|
---|
[106] | 119 | for (int j = maxOrder - 1; j > 0; j--) {
|
---|
| 120 | add(sequence.subList(sequenceLength - j, sequenceLength));
|
---|
[9] | 121 | }
|
---|
| 122 | }
|
---|
[1] | 123 |
|
---|
[106] | 124 | /**
|
---|
| 125 | * <p>
|
---|
| 126 | * Adds a given subsequence to the trie and increases the counters
|
---|
| 127 | * accordingly.
|
---|
| 128 | * </p>
|
---|
| 129 | *
|
---|
| 130 | * @param subsequence
|
---|
| 131 | * subsequence whose counters are increased
|
---|
| 132 | * @see TrieNode#add(List)
|
---|
| 133 | */
|
---|
| 134 | protected void add(List<T> subsequence) {
|
---|
| 135 | if (subsequence != null && !subsequence.isEmpty()) {
|
---|
[9] | 136 | knownSymbols.addAll(subsequence);
|
---|
[106] | 137 | subsequence = new LinkedList<T>(subsequence); // defensive copy!
|
---|
[1] | 138 | T firstSymbol = subsequence.get(0);
|
---|
[6] | 139 | TrieNode<T> node = getChildCreate(firstSymbol);
|
---|
| 140 | node.add(subsequence);
|
---|
[1] | 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[106] | 144 | /**
|
---|
| 145 | * <p>
|
---|
| 146 | * Returns the child of the root node associated with the given symbol or
|
---|
| 147 | * creates it if it does not exist yet.
|
---|
| 148 | * </p>
|
---|
| 149 | *
|
---|
| 150 | * @param symbol
|
---|
| 151 | * symbol whose node is required
|
---|
| 152 | * @return node associated with the symbol
|
---|
| 153 | * @see TrieNode#getChildCreate(Object)
|
---|
| 154 | */
|
---|
| 155 | protected TrieNode<T> getChildCreate(T symbol) {
|
---|
[6] | 156 | return rootNode.getChildCreate(symbol);
|
---|
[1] | 157 | }
|
---|
[106] | 158 |
|
---|
| 159 | /**
|
---|
| 160 | * <p>
|
---|
| 161 | * Returns the child of the root node associated with the given symbol or
|
---|
| 162 | * null if it does not exist.
|
---|
| 163 | * </p>
|
---|
| 164 | *
|
---|
| 165 | * @param symbol
|
---|
| 166 | * symbol whose node is required
|
---|
| 167 | * @return node associated with the symbol; null if no such node exists
|
---|
| 168 | * @see TrieNode#getChild(Object)
|
---|
| 169 | */
|
---|
[1] | 170 | protected TrieNode<T> getChild(T symbol) {
|
---|
[6] | 171 | return rootNode.getChild(symbol);
|
---|
[1] | 172 | }
|
---|
| 173 |
|
---|
[106] | 174 | /**
|
---|
| 175 | * <p>
|
---|
| 176 | * Returns the number of occurences of the given sequence.
|
---|
| 177 | * </p>
|
---|
| 178 | *
|
---|
| 179 | * @param sequence
|
---|
| 180 | * sequence whose number of occurences is required
|
---|
| 181 | * @return number of occurences of the sequence
|
---|
| 182 | */
|
---|
[1] | 183 | public int getCount(List<T> sequence) {
|
---|
| 184 | int count = 0;
|
---|
| 185 | TrieNode<T> node = find(sequence);
|
---|
[106] | 186 | if (node != null) {
|
---|
[1] | 187 | count = node.getCount();
|
---|
| 188 | }
|
---|
| 189 | return count;
|
---|
| 190 | }
|
---|
[106] | 191 |
|
---|
| 192 | /**
|
---|
| 193 | * <p>
|
---|
| 194 | * Returns the number of occurences of the given prefix and a symbol that
|
---|
| 195 | * follows it.<br>
|
---|
| 196 | * Convenience function to simplify usage of {@link #getCount(List)}.
|
---|
| 197 | * </p>
|
---|
| 198 | *
|
---|
| 199 | * @param sequence
|
---|
| 200 | * prefix of the sequence
|
---|
| 201 | * @param follower
|
---|
| 202 | * suffix of the sequence
|
---|
| 203 | * @return number of occurences of the sequence
|
---|
| 204 | * @see #getCount(List)
|
---|
| 205 | */
|
---|
[1] | 206 | public int getCount(List<T> sequence, T follower) {
|
---|
| 207 | List<T> tmpSequence = new LinkedList<T>(sequence);
|
---|
| 208 | tmpSequence.add(follower);
|
---|
| 209 | return getCount(tmpSequence);
|
---|
[106] | 210 |
|
---|
[1] | 211 | }
|
---|
[106] | 212 |
|
---|
| 213 | /**
|
---|
| 214 | * <p>
|
---|
| 215 | * Searches the trie for a given sequence and returns the node associated
|
---|
| 216 | * with the sequence or null if no such node is found.
|
---|
| 217 | * </p>
|
---|
| 218 | *
|
---|
| 219 | * @param sequence
|
---|
| 220 | * sequence that is searched for
|
---|
| 221 | * @return node associated with the sequence
|
---|
| 222 | * @see TrieNode#find(List)
|
---|
| 223 | */
|
---|
[1] | 224 | public TrieNode<T> find(List<T> sequence) {
|
---|
[106] | 225 | if (sequence == null || sequence.isEmpty()) {
|
---|
[6] | 226 | return rootNode;
|
---|
| 227 | }
|
---|
[5] | 228 | List<T> sequenceCopy = new LinkedList<T>(sequence);
|
---|
[1] | 229 | TrieNode<T> result = null;
|
---|
[6] | 230 | TrieNode<T> node = getChild(sequenceCopy.get(0));
|
---|
[106] | 231 | if (node != null) {
|
---|
[6] | 232 | sequenceCopy.remove(0);
|
---|
| 233 | result = node.find(sequenceCopy);
|
---|
[1] | 234 | }
|
---|
| 235 | return result;
|
---|
| 236 | }
|
---|
[106] | 237 |
|
---|
| 238 | /**
|
---|
| 239 | * <p>
|
---|
| 240 | * Returns a collection of all symbols that follow a given sequence in the
|
---|
| 241 | * trie. In case the sequence is not found or no symbols follow the sequence
|
---|
| 242 | * the result will be empty.
|
---|
| 243 | * </p>
|
---|
| 244 | *
|
---|
| 245 | * @param sequence
|
---|
| 246 | * sequence whose followers are returned
|
---|
| 247 | * @return symbols following the given sequence
|
---|
| 248 | * @see TrieNode#getFollowingSymbols()
|
---|
| 249 | */
|
---|
[102] | 250 | public Collection<T> getFollowingSymbols(List<T> sequence) {
|
---|
| 251 | Collection<T> result = new LinkedList<T>();
|
---|
[6] | 252 | TrieNode<T> node = find(sequence);
|
---|
[106] | 253 | if (node != null) {
|
---|
[6] | 254 | result = node.getFollowingSymbols();
|
---|
[1] | 255 | }
|
---|
| 256 | return result;
|
---|
| 257 | }
|
---|
[106] | 258 |
|
---|
| 259 | /**
|
---|
| 260 | * <p>
|
---|
| 261 | * Returns the longest suffix of the given context that is contained in the
|
---|
| 262 | * tree and whose children are leaves.
|
---|
| 263 | * </p>
|
---|
| 264 | *
|
---|
| 265 | * @param context
|
---|
| 266 | * context whose suffix is searched for
|
---|
| 267 | * @return longest suffix of the context
|
---|
| 268 | */
|
---|
[1] | 269 | public List<T> getContextSuffix(List<T> context) {
|
---|
[316] | 270 | List<T> contextSuffix;
|
---|
[361] | 271 | if (context != null) {
|
---|
[316] | 272 | contextSuffix = new LinkedList<T>(context); // defensive copy
|
---|
| 273 | } else {
|
---|
| 274 | contextSuffix = new LinkedList<T>();
|
---|
| 275 | }
|
---|
[1] | 276 | boolean suffixFound = false;
|
---|
[106] | 277 |
|
---|
| 278 | while (!suffixFound) {
|
---|
| 279 | if (contextSuffix.isEmpty()) {
|
---|
[1] | 280 | suffixFound = true; // suffix is the empty word
|
---|
| 281 | } else {
|
---|
| 282 | TrieNode<T> node = find(contextSuffix);
|
---|
[106] | 283 | if (node != null) {
|
---|
| 284 | if (!node.getFollowingSymbols().isEmpty()) {
|
---|
[1] | 285 | suffixFound = true;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
[106] | 288 | if (!suffixFound) {
|
---|
[5] | 289 | contextSuffix.remove(0);
|
---|
| 290 | }
|
---|
[1] | 291 | }
|
---|
| 292 | }
|
---|
[106] | 293 |
|
---|
[1] | 294 | return contextSuffix;
|
---|
| 295 | }
|
---|
[106] | 296 |
|
---|
| 297 | /**
|
---|
| 298 | * <p>
|
---|
| 299 | * Helper class for graph visualization of a trie.
|
---|
| 300 | * </p>
|
---|
| 301 | *
|
---|
| 302 | * @author Steffen Herbold
|
---|
| 303 | * @version 1.0
|
---|
| 304 | */
|
---|
| 305 | static public class Edge {
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /**
|
---|
| 309 | * <p>
|
---|
| 310 | * Helper class for graph visualization of a trie.
|
---|
| 311 | * </p>
|
---|
| 312 | *
|
---|
| 313 | * @author Steffen Herbold
|
---|
| 314 | * @version 1.0
|
---|
| 315 | */
|
---|
[5] | 316 | static public class TrieVertex {
|
---|
[106] | 317 |
|
---|
| 318 | /**
|
---|
| 319 | * <p>
|
---|
| 320 | * Id of the vertex.
|
---|
| 321 | * </p>
|
---|
| 322 | */
|
---|
[5] | 323 | private String id;
|
---|
[106] | 324 |
|
---|
| 325 | /**
|
---|
| 326 | * <p>
|
---|
| 327 | * Contructor. Creates a new TrieVertex.
|
---|
| 328 | * </p>
|
---|
| 329 | *
|
---|
| 330 | * @param id
|
---|
| 331 | * id of the vertex
|
---|
| 332 | */
|
---|
[5] | 333 | protected TrieVertex(String id) {
|
---|
| 334 | this.id = id;
|
---|
| 335 | }
|
---|
[106] | 336 |
|
---|
| 337 | /**
|
---|
| 338 | * <p>
|
---|
| 339 | * Returns the id of the vertex.
|
---|
| 340 | * </p>
|
---|
| 341 | *
|
---|
| 342 | * @see java.lang.Object#toString()
|
---|
| 343 | */
|
---|
| 344 | @Override
|
---|
[5] | 345 | public String toString() {
|
---|
| 346 | return id;
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
[106] | 349 |
|
---|
| 350 | /**
|
---|
| 351 | * <p>
|
---|
| 352 | * Returns a {@link Graph} representation of the trie.
|
---|
| 353 | * </p>
|
---|
| 354 | *
|
---|
| 355 | * @return {@link Graph} representation of the trie
|
---|
| 356 | */
|
---|
[23] | 357 | protected Tree<TrieVertex, Edge> getGraph() {
|
---|
[5] | 358 | DelegateTree<TrieVertex, Edge> graph = new DelegateTree<TrieVertex, Edge>();
|
---|
[6] | 359 | rootNode.getGraph(null, graph);
|
---|
[5] | 360 | return graph;
|
---|
| 361 | }
|
---|
[106] | 362 |
|
---|
| 363 | /*
|
---|
| 364 | * (non-Javadoc)
|
---|
| 365 | *
|
---|
| 366 | * @see de.ugoe.cs.eventbench.models.IDotCompatible#getDotRepresentation()
|
---|
| 367 | */
|
---|
[30] | 368 | public String getDotRepresentation() {
|
---|
| 369 | StringBuilder stringBuilder = new StringBuilder();
|
---|
| 370 | stringBuilder.append("digraph model {" + StringTools.ENDLINE);
|
---|
| 371 | rootNode.appendDotRepresentation(stringBuilder);
|
---|
| 372 | stringBuilder.append('}' + StringTools.ENDLINE);
|
---|
| 373 | return stringBuilder.toString();
|
---|
| 374 | }
|
---|
[106] | 375 |
|
---|
| 376 | /**
|
---|
| 377 | * <p>
|
---|
| 378 | * Returns the string representation of the root node.
|
---|
| 379 | * </p>
|
---|
| 380 | *
|
---|
| 381 | * @see TrieNode#toString()
|
---|
| 382 | * @see java.lang.Object#toString()
|
---|
| 383 | */
|
---|
[1] | 384 | @Override
|
---|
| 385 | public String toString() {
|
---|
[6] | 386 | return rootNode.toString();
|
---|
[1] | 387 | }
|
---|
[106] | 388 |
|
---|
| 389 | /**
|
---|
| 390 | * <p>
|
---|
| 391 | * Returns the number of symbols contained in the trie.
|
---|
| 392 | * </p>
|
---|
| 393 | *
|
---|
[129] | 394 | * @return number of symbols contained in the trie
|
---|
[106] | 395 | */
|
---|
[66] | 396 | public int getNumSymbols() {
|
---|
| 397 | return knownSymbols.size();
|
---|
| 398 | }
|
---|
[129] | 399 |
|
---|
| 400 | /**
|
---|
| 401 | * <p>
|
---|
| 402 | * Returns the number of trie nodes that are ancestors of a leaf. This is
|
---|
| 403 | * the equivalent to the number of states a first-order markov model would
|
---|
| 404 | * have.
|
---|
| 405 | * <p>
|
---|
| 406 | *
|
---|
| 407 | * @return number of trie nodes that are ancestors of leafs.
|
---|
| 408 | */
|
---|
| 409 | public int getNumLeafAncestors() {
|
---|
| 410 | Set<TrieNode<T>> ancestors = new HashSet<TrieNode<T>>();
|
---|
| 411 | rootNode.getLeafAncestors(ancestors);
|
---|
| 412 | return ancestors.size();
|
---|
| 413 | }
|
---|
[248] | 414 |
|
---|
| 415 | /**
|
---|
| 416 | * <p>
|
---|
| 417 | * Returns the number of trie nodes that are leafs.
|
---|
| 418 | * </p>
|
---|
| 419 | *
|
---|
| 420 | * @return number of leafs in the trie
|
---|
| 421 | */
|
---|
| 422 | public int getNumLeafs() {
|
---|
| 423 | return rootNode.getNumLeafs();
|
---|
| 424 | }
|
---|
[258] | 425 |
|
---|
| 426 | /**
|
---|
| 427 | * <p>
|
---|
| 428 | * Updates the list of known symbols by replacing it with all symbols that
|
---|
| 429 | * are found in the child nodes of the root node. This should be the same as
|
---|
| 430 | * all symbols that are contained in the trie.
|
---|
| 431 | * </p>
|
---|
| 432 | */
|
---|
| 433 | public void updateKnownSymbols() {
|
---|
| 434 | knownSymbols = new HashSet<T>();
|
---|
| 435 | for (TrieNode<T> node : rootNode.getChildren()) {
|
---|
| 436 | knownSymbols.add(node.getSymbol());
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
[397] | 439 |
|
---|
| 440 | /**
|
---|
| 441 | * <p>
|
---|
| 442 | * Two Tries are defined as equal, if their {@link #rootNode} are equal.
|
---|
| 443 | * </p>
|
---|
| 444 | *
|
---|
| 445 | * @see java.lang.Object#equals(java.lang.Object)
|
---|
| 446 | */
|
---|
| 447 | @SuppressWarnings("rawtypes")
|
---|
| 448 | @Override
|
---|
| 449 | public boolean equals(Object other) {
|
---|
| 450 | if (other == this) {
|
---|
| 451 | return true;
|
---|
| 452 | }
|
---|
| 453 | if (other instanceof Trie) {
|
---|
| 454 | return rootNode.equals(((Trie) other).rootNode);
|
---|
| 455 | }
|
---|
| 456 | return false;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | /*
|
---|
| 460 | * (non-Javadoc)
|
---|
| 461 | *
|
---|
| 462 | * @see java.lang.Object#hashCode()
|
---|
| 463 | */
|
---|
| 464 | @Override
|
---|
| 465 | public int hashCode() {
|
---|
| 466 | int multiplier = 17;
|
---|
| 467 | int hash = 42;
|
---|
| 468 | if (rootNode != null) {
|
---|
| 469 | hash = multiplier * hash + rootNode.hashCode();
|
---|
| 470 | }
|
---|
| 471 | return hash;
|
---|
| 472 | }
|
---|
[1] | 473 | }
|
---|