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