[1113] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[1107] | 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
| 16 | |
---|
| 17 | import java.util.Iterator; |
---|
| 18 | import java.util.LinkedList; |
---|
| 19 | import java.util.List; |
---|
| 20 | |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
[1127] | 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1107] | 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
| 29 | import de.ugoe.cs.autoquest.usageprofiles.Trie; |
---|
[1119] | 30 | import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor; |
---|
[1129] | 31 | import de.ugoe.cs.util.StopWatch; |
---|
[1127] | 32 | import de.ugoe.cs.util.console.Console; |
---|
[1107] | 33 | |
---|
| 34 | /** |
---|
| 35 | * <p> |
---|
| 36 | * TODO comment |
---|
| 37 | * </p> |
---|
| 38 | * |
---|
| 39 | * @author Patrick Harms |
---|
| 40 | */ |
---|
[1127] | 41 | class SequenceForTaskDetectionRule implements TemporalRelationshipRule { |
---|
[1107] | 42 | |
---|
| 43 | /** |
---|
| 44 | * <p> |
---|
| 45 | * the task tree node factory to be used for creating substructures for the temporal |
---|
| 46 | * relationships identified during rule |
---|
| 47 | * </p> |
---|
| 48 | */ |
---|
| 49 | private ITaskTreeNodeFactory taskTreeNodeFactory; |
---|
| 50 | /** |
---|
| 51 | * <p> |
---|
| 52 | * the task tree builder to be used for creating substructures for the temporal relationships |
---|
| 53 | * identified during rule application |
---|
| 54 | * </p> |
---|
| 55 | */ |
---|
| 56 | private ITaskTreeBuilder taskTreeBuilder; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * <p> |
---|
| 60 | * the node comparator to be used for comparing task tree nodes |
---|
| 61 | * </p> |
---|
| 62 | */ |
---|
[1127] | 63 | private TaskTreeNodeComparator nodeComparator; |
---|
[1107] | 64 | |
---|
| 65 | /** |
---|
| 66 | * <p> |
---|
| 67 | * instantiates the rule and initializes it with a node equality rule manager and the minimal |
---|
| 68 | * node equality identified sublist must have to consider them as iterated. |
---|
| 69 | * </p> |
---|
| 70 | */ |
---|
[1127] | 71 | SequenceForTaskDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager, |
---|
| 72 | NodeEquality minimalNodeEquality, |
---|
| 73 | ITaskTreeNodeFactory taskTreeNodeFactory, |
---|
| 74 | ITaskTreeBuilder taskTreeBuilder) |
---|
[1107] | 75 | { |
---|
| 76 | this.taskTreeNodeFactory = taskTreeNodeFactory; |
---|
| 77 | this.taskTreeBuilder = taskTreeBuilder; |
---|
| 78 | |
---|
| 79 | this.nodeComparator = |
---|
| 80 | new TaskTreeNodeComparator(nodeEqualityRuleManager, minimalNodeEquality); |
---|
| 81 | } |
---|
| 82 | |
---|
[1119] | 83 | /* (non-Javadoc) |
---|
| 84 | * @see java.lang.Object#toString() |
---|
| 85 | */ |
---|
| 86 | @Override |
---|
| 87 | public String toString() { |
---|
[1127] | 88 | return "SequenceForTaskDetectionRule"; |
---|
[1119] | 89 | } |
---|
| 90 | |
---|
[1107] | 91 | /* |
---|
| 92 | * (non-Javadoc) |
---|
| 93 | * |
---|
| 94 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
| 95 | * boolean) |
---|
| 96 | */ |
---|
| 97 | @Override |
---|
| 98 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
| 99 | if (!(parent instanceof ISequence)) { |
---|
| 100 | return null; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | if (!finalize) { |
---|
| 104 | // the rule is always feasible as tasks may occur at any time |
---|
| 105 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
[1127] | 106 | result.setRuleApplicationStatus(RuleApplicationStatus.FEASIBLE); |
---|
[1107] | 107 | return result; |
---|
| 108 | } |
---|
| 109 | |
---|
[1127] | 110 | List<ITaskTreeNode> children = parent.getChildren(); |
---|
| 111 | List<ISequence> sessions = new LinkedList<ISequence>(); |
---|
[1107] | 112 | |
---|
[1127] | 113 | for (ITaskTreeNode child : children) { |
---|
| 114 | if (child instanceof ISequence) { |
---|
| 115 | sessions.add((ISequence) child); |
---|
| 116 | } |
---|
| 117 | else { |
---|
| 118 | Console.println("provided parent is no parent of sessions"); |
---|
| 119 | return null; |
---|
| 120 | } |
---|
| 121 | } |
---|
[1107] | 122 | |
---|
[1127] | 123 | RuleApplicationData appData = new RuleApplicationData(sessions); |
---|
| 124 | |
---|
| 125 | boolean finished = false; |
---|
| 126 | |
---|
| 127 | // this is the real rule application. Loop while something is replaced. |
---|
[1107] | 128 | do { |
---|
[1127] | 129 | System.out.println(); |
---|
[1107] | 130 | |
---|
[1127] | 131 | appData.getStopWatch().start("whole loop"); |
---|
| 132 | detectAndReplaceIterations(appData); |
---|
| 133 | //mergeEqualTasks(appData); |
---|
| 134 | appData.getStopWatch().start("task replacement"); |
---|
| 135 | detectAndReplaceTasks(appData); |
---|
| 136 | appData.getStopWatch().stop("task replacement"); |
---|
| 137 | appData.getStopWatch().stop("whole loop"); |
---|
[1107] | 138 | |
---|
[1127] | 139 | //((TaskTreeNodeComparator) nodeComparator).getStopWatch().dumpStatistics(System.out); |
---|
| 140 | //((TaskTreeNodeComparator) nodeComparator).getStopWatch().reset(); |
---|
| 141 | |
---|
| 142 | appData.getStopWatch().dumpStatistics(System.out); |
---|
| 143 | appData.getStopWatch().reset(); |
---|
| 144 | |
---|
| 145 | finished = (appData.getReplacementCounter() == 0); |
---|
[1107] | 146 | } |
---|
[1127] | 147 | while (!finished); |
---|
[1107] | 148 | |
---|
[1127] | 149 | System.out.println("created " + appData.getResult().getNewlyCreatedParentNodes().size() + |
---|
| 150 | " new parent nodes\n"); |
---|
[1107] | 151 | |
---|
[1119] | 152 | if (appData.getResult().getNewlyCreatedParentNodes().size() > 0) { |
---|
[1127] | 153 | appData.getResult().setRuleApplicationStatus(RuleApplicationStatus.FINISHED); |
---|
[1119] | 154 | } |
---|
| 155 | |
---|
[1127] | 156 | return appData.getResult(); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * <p> |
---|
| 161 | * TODO: comment |
---|
| 162 | * </p> |
---|
| 163 | * |
---|
| 164 | * @param appData |
---|
| 165 | */ |
---|
| 166 | private void detectAndReplaceIterations(RuleApplicationData appData) { |
---|
| 167 | System.out.print("detecting iterations"); |
---|
| 168 | appData.getStopWatch().start("detecting iterations"); |
---|
[1119] | 169 | |
---|
[1127] | 170 | List<ISequence> sessions = appData.getSessions(); |
---|
| 171 | int foundIterations = 0; |
---|
| 172 | |
---|
| 173 | for (ISequence session : sessions) { |
---|
| 174 | foundIterations += detectAndReplaceIterations(session, appData); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | appData.getStopWatch().stop("detecting iterations"); |
---|
| 178 | System.out.println(" --> found " + foundIterations); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * <p> |
---|
| 183 | * TODO: comment |
---|
| 184 | * </p> |
---|
| 185 | * |
---|
| 186 | * @param appData |
---|
| 187 | */ |
---|
| 188 | private int detectAndReplaceIterations(ISequence session, |
---|
| 189 | RuleApplicationData appData) |
---|
| 190 | { |
---|
| 191 | int count = 0; |
---|
| 192 | |
---|
| 193 | TemporalRelationshipRule rule = new SimpleIterationDetectionRule |
---|
| 194 | (nodeComparator, taskTreeNodeFactory, taskTreeBuilder); |
---|
| 195 | |
---|
| 196 | RuleApplicationResult result = rule.apply(session, true); |
---|
| 197 | |
---|
| 198 | if ((result != null) && (result.getNewlyCreatedParentNodes() != null)) { |
---|
| 199 | for (ITaskTreeNode newParent : result.getNewlyCreatedParentNodes()) { |
---|
| 200 | appData.getResult().addNewlyCreatedParentNode(newParent); |
---|
| 201 | if (newParent instanceof IIteration) { |
---|
| 202 | count++; |
---|
| 203 | } |
---|
[1107] | 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
[1127] | 207 | return count; |
---|
[1107] | 208 | } |
---|
| 209 | |
---|
[1127] | 210 | // /** |
---|
| 211 | // * <p> |
---|
| 212 | // * TODO: comment |
---|
| 213 | // * </p> |
---|
| 214 | // * |
---|
| 215 | // * @param appData |
---|
| 216 | // */ |
---|
| 217 | // private void mergeEqualTasks(RuleApplicationData appData) { |
---|
| 218 | // System.out.println("merging equal tasks"); |
---|
| 219 | // appData.getStopWatch().start("merging equal tasks"); |
---|
| 220 | // |
---|
| 221 | // int replacements = 0; |
---|
| 222 | // List<ISequence> sessions = appData.getSessions(); |
---|
| 223 | // |
---|
| 224 | // IdentityHashMap<ITaskTreeNode, ITaskTreeNode> replacedChildren = |
---|
| 225 | // new IdentityHashMap<ITaskTreeNode, ITaskTreeNode>(); |
---|
| 226 | // |
---|
| 227 | // for (int sessionIdx1 = 0; sessionIdx1 < sessions.size(); sessionIdx1++) { |
---|
| 228 | // List<ITaskTreeNode> children1 = appData.getSessions().get(sessionIdx1).getChildren(); |
---|
| 229 | // for (int childIdx1 = 0; childIdx1 < children1.size(); childIdx1++) { |
---|
| 230 | // // this is the child of which we search equal other children to merge and to |
---|
| 231 | // // replace with one single unique node |
---|
| 232 | // ITaskTreeNode child1 = children1.get(childIdx1); |
---|
| 233 | // |
---|
| 234 | // if (replacedChildren.containsKey(child1)) { |
---|
| 235 | // continue; |
---|
| 236 | // } |
---|
| 237 | // |
---|
| 238 | // // now search for all other children that are equal. Also record the session they |
---|
| 239 | // // belong to as well as the index in that session |
---|
| 240 | // List<ITaskTreeNode> equalChildren = new LinkedList<ITaskTreeNode>(); |
---|
| 241 | // List<Integer> sessionIndexes = new LinkedList<Integer>(); |
---|
| 242 | // List<Integer> childIndexes = new LinkedList<Integer>(); |
---|
| 243 | // |
---|
| 244 | // // add all information about the current child |
---|
| 245 | // equalChildren.add(child1); |
---|
| 246 | // sessionIndexes.add(sessionIdx1); |
---|
| 247 | // childIndexes.add(childIdx1); |
---|
| 248 | // |
---|
| 249 | // for (int sessionIdx2 = sessionIdx1; sessionIdx2 < sessions.size(); sessionIdx2++) { |
---|
| 250 | // List<ITaskTreeNode> children2 = |
---|
| 251 | // appData.getSessions().get(sessionIdx2).getChildren(); |
---|
| 252 | // |
---|
| 253 | // int startIndex = (sessionIdx1 == sessionIdx2) ? childIdx1 + 1 : 0; |
---|
| 254 | // |
---|
| 255 | // for (int childIdx2 = startIndex; childIdx2 < children2.size(); childIdx2++) { |
---|
| 256 | // ITaskTreeNode child2 = children2.get(childIdx2); |
---|
| 257 | // |
---|
| 258 | // if ((child1 != child2) && (nodeComparator.equals(child1, child2))) { |
---|
| 259 | // // this is an equal child --> record its occurrence |
---|
| 260 | // equalChildren.add(child2); |
---|
| 261 | // sessionIndexes.add(sessionIdx2); |
---|
| 262 | // childIndexes.add(childIdx2); |
---|
| 263 | // } |
---|
| 264 | // } |
---|
| 265 | // } |
---|
| 266 | // |
---|
| 267 | // // now merge the found children |
---|
| 268 | // if (equalChildren.size() > 1) { |
---|
| 269 | // ITaskTreeNode replacement = |
---|
| 270 | // mergeVariantsOfTasks(child1.toString(), equalChildren); |
---|
| 271 | // |
---|
| 272 | // for (int i = 0; i < sessionIndexes.size(); i++) { |
---|
| 273 | // taskTreeBuilder.setChild(appData.getSessions().get(sessionIndexes.get(i)), |
---|
| 274 | // childIndexes.get(i), replacement); |
---|
| 275 | // |
---|
| 276 | // replacements++; |
---|
| 277 | // } |
---|
| 278 | // |
---|
| 279 | // // remember the replacement to prevent comparison of merged nodes |
---|
| 280 | // replacedChildren.put(replacement, replacement); |
---|
| 281 | // |
---|
| 282 | // System.out.println |
---|
| 283 | // ("replaced " + sessionIndexes.size() + " occurrences of " + child1); |
---|
| 284 | // } |
---|
| 285 | // } |
---|
| 286 | // } |
---|
| 287 | // |
---|
| 288 | // appData.getStopWatch().stop("merging equal tasks"); |
---|
| 289 | // |
---|
| 290 | // System.out.println("replaced " + replacements + " equal tasks with unique replacements"); |
---|
| 291 | // } |
---|
| 292 | // |
---|
[1107] | 293 | /** |
---|
| 294 | * <p> |
---|
| 295 | * TODO: comment |
---|
| 296 | * </p> |
---|
| 297 | * |
---|
[1127] | 298 | * @param appData |
---|
| 299 | */ |
---|
| 300 | private void detectAndReplaceTasks(RuleApplicationData appData) { |
---|
| 301 | System.out.println("detecting and replacing tasks"); |
---|
| 302 | appData.getStopWatch().start("detecting tasks"); |
---|
| 303 | |
---|
| 304 | getSequencesOccuringMostOften(appData); |
---|
| 305 | |
---|
| 306 | appData.getStopWatch().stop("detecting tasks"); |
---|
| 307 | appData.getStopWatch().start("replacing tasks"); |
---|
| 308 | |
---|
| 309 | replaceSequencesOccurringMostOften(appData); |
---|
| 310 | |
---|
| 311 | appData.getStopWatch().stop("replacing tasks"); |
---|
| 312 | System.out.println("detected and replaced " + appData.getLastFoundTasks().size() + " tasks"); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
| 316 | * <p> |
---|
| 317 | * TODO: comment |
---|
| 318 | * </p> |
---|
| 319 | * |
---|
[1119] | 320 | * @param i |
---|
| 321 | * @return |
---|
[1107] | 322 | */ |
---|
[1127] | 323 | private void getSequencesOccuringMostOften(RuleApplicationData appData) { |
---|
| 324 | System.out.println("determining most prominent tasks"); |
---|
[1119] | 325 | |
---|
[1127] | 326 | Tasks tasks; |
---|
[1119] | 327 | boolean createNewTrie = (appData.getLastTrie() == null) || |
---|
| 328 | appData.getReplacementCounter() > 0; // tree has changed |
---|
[1107] | 329 | |
---|
[1119] | 330 | do { |
---|
| 331 | if (createNewTrie) { |
---|
| 332 | createNewTrie(appData); |
---|
| 333 | } |
---|
[1107] | 334 | |
---|
[1127] | 335 | MaxCountAndLongestTasksFinder finder = new MaxCountAndLongestTasksFinder(); |
---|
[1119] | 336 | appData.getLastTrie().process(finder); |
---|
| 337 | |
---|
[1127] | 338 | tasks = finder.getFoundTasks(); |
---|
[1119] | 339 | |
---|
| 340 | createNewTrie = false; |
---|
| 341 | |
---|
[1127] | 342 | for (List<ITaskTreeNode> task : tasks) { |
---|
[1119] | 343 | if (task.size() >= appData.getTrainedSequenceLength()) { |
---|
| 344 | // Trie must be recreated with a longer sequence length to be sure that |
---|
| 345 | // the found sequences occurring most often are found in their whole length |
---|
| 346 | appData.setTrainedSequenceLength(appData.getTrainedSequenceLength() + 1); |
---|
| 347 | createNewTrie = true; |
---|
| 348 | break; |
---|
| 349 | } |
---|
[1107] | 350 | } |
---|
| 351 | } |
---|
[1119] | 352 | while (createNewTrie); |
---|
| 353 | |
---|
[1127] | 354 | appData.setLastFoundTasks(tasks); |
---|
| 355 | |
---|
| 356 | System.out.println("found " + appData.getLastFoundTasks().size() + " tasks occurring " + |
---|
| 357 | appData.getLastFoundTasks().getOccurrenceCount() + " times"); |
---|
[1107] | 358 | } |
---|
| 359 | |
---|
| 360 | /** |
---|
[1119] | 361 | * <p> |
---|
| 362 | * TODO: comment |
---|
| 363 | * </p> |
---|
[1107] | 364 | * |
---|
[1119] | 365 | * @param parent |
---|
| 366 | * @return |
---|
[1107] | 367 | */ |
---|
[1127] | 368 | private void createNewTrie(RuleApplicationData appData) { |
---|
[1119] | 369 | System.out.println("training trie with a maximum sequence length of " + |
---|
| 370 | appData.getTrainedSequenceLength()); |
---|
| 371 | |
---|
| 372 | appData.getStopWatch().start("training trie"); |
---|
| 373 | appData.setLastTrie(new Trie<ITaskTreeNode>(nodeComparator)); |
---|
| 374 | |
---|
[1127] | 375 | List<ISequence> sessions = appData.getSessions(); |
---|
| 376 | |
---|
| 377 | for (ISequence session : sessions) { |
---|
| 378 | trainTrie(session, appData); |
---|
| 379 | } |
---|
| 380 | |
---|
[1119] | 381 | appData.getStopWatch().stop("training trie"); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | * <p> |
---|
| 386 | * TODO: comment |
---|
| 387 | * </p> |
---|
| 388 | * |
---|
| 389 | * @param trie |
---|
| 390 | * @param parent |
---|
| 391 | */ |
---|
[1127] | 392 | private void trainTrie(ISequence session, RuleApplicationData appData) { |
---|
| 393 | List<ITaskTreeNode> children = session.getChildren(); |
---|
[1107] | 394 | |
---|
[1127] | 395 | if ((children != null) && (children.size() > 0)) { |
---|
| 396 | appData.getLastTrie().train(children, appData.getTrainedSequenceLength()); |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | /** |
---|
| 401 | * <p> |
---|
| 402 | * TODO: comment |
---|
| 403 | * </p> |
---|
| 404 | * |
---|
| 405 | * @param appData |
---|
| 406 | */ |
---|
| 407 | private void replaceSequencesOccurringMostOften(RuleApplicationData appData) { |
---|
| 408 | appData.resetReplacementCounter(); |
---|
| 409 | |
---|
| 410 | if ((appData.getLastFoundTasks().size() > 0) && |
---|
| 411 | (appData.getLastFoundTasks().getOccurrenceCount() > 1)) |
---|
| 412 | { |
---|
| 413 | System.out.println("replacing tasks occurrences with merged variants of all versions"); |
---|
| 414 | |
---|
| 415 | for (List<ITaskTreeNode> task : appData.getLastFoundTasks()) { |
---|
| 416 | String taskId = "task " + RuleUtils.getNewId(); |
---|
| 417 | System.out.println("replacing " + taskId + ": " + task); |
---|
| 418 | |
---|
| 419 | appData.clearTaskOccurrences(); |
---|
| 420 | determineVariantsOfTaskOccurrences(task, appData.getSessions(), appData); |
---|
[1119] | 421 | |
---|
[1127] | 422 | appData.getStopWatch().start("merging task nodes"); |
---|
| 423 | ITaskTreeNode taskReplacement = mergeVariantsOfTaskOccurrence(taskId, appData); |
---|
| 424 | appData.getStopWatch().stop("merging task nodes"); |
---|
| 425 | |
---|
| 426 | appData.resetReplacementCounter(); |
---|
| 427 | replaceTaskOccurrences(task, taskReplacement, appData.getSessions(), appData); |
---|
| 428 | |
---|
| 429 | if (appData.getReplacementCounter() > 0) { |
---|
| 430 | appData.getResult().addNewlyCreatedParentNode(taskReplacement); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if (appData.getReplacementCounter() < |
---|
| 434 | appData.getLastFoundTasks().getOccurrenceCount()) |
---|
| 435 | { |
---|
| 436 | System.out.println(taskId + ": replaced task only " + |
---|
| 437 | appData.getReplacementCounter() + |
---|
| 438 | " times instead of expected " + |
---|
| 439 | appData.getLastFoundTasks().getOccurrenceCount()); |
---|
| 440 | } |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | /** |
---|
| 447 | * <p> |
---|
| 448 | * TODO: comment |
---|
| 449 | * </p> |
---|
| 450 | * |
---|
| 451 | * @param tree |
---|
| 452 | */ |
---|
| 453 | private void determineVariantsOfTaskOccurrences(List<ITaskTreeNode> task, |
---|
| 454 | List<ISequence> sessions, |
---|
| 455 | RuleApplicationData appData) |
---|
| 456 | { |
---|
| 457 | for (ISequence session : sessions) { |
---|
| 458 | int index = -1; |
---|
[1119] | 459 | |
---|
[1127] | 460 | List<ITaskTreeNode> children = session.getChildren(); |
---|
| 461 | |
---|
| 462 | do { |
---|
| 463 | index = getSubListIndex(children, task, ++index); |
---|
| 464 | |
---|
| 465 | if (index > -1) { |
---|
| 466 | ISequence taskOccurrence = RuleUtils.getSubSequenceInRange |
---|
| 467 | (session, index, index + task.size() - 1, null, |
---|
| 468 | taskTreeNodeFactory, taskTreeBuilder); |
---|
| 469 | |
---|
| 470 | appData.addTaskOccurrence(taskOccurrence); |
---|
| 471 | |
---|
| 472 | // let the index point to the last element the belongs the identified occurrence |
---|
| 473 | index += task.size() - 1; |
---|
[1107] | 474 | } |
---|
| 475 | } |
---|
[1127] | 476 | while (index > -1); |
---|
[1107] | 477 | } |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | /** |
---|
| 481 | * <p> |
---|
| 482 | * TODO: comment |
---|
| 483 | * </p> |
---|
| 484 | * |
---|
[1127] | 485 | * @param appData |
---|
| 486 | * @return |
---|
| 487 | */ |
---|
| 488 | private ITaskTreeNode mergeVariantsOfTaskOccurrence(String taskId, |
---|
| 489 | RuleApplicationData appData) |
---|
| 490 | { |
---|
| 491 | return mergeVariantsOfTasks(taskId, appData.getTaskOccurrences()); |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | /** |
---|
| 495 | * <p> |
---|
| 496 | * TODO: comment |
---|
| 497 | * </p> |
---|
| 498 | * |
---|
| 499 | * @param appData |
---|
| 500 | * @return |
---|
| 501 | */ |
---|
| 502 | private ITaskTreeNode mergeVariantsOfTasks(String description, List<ITaskTreeNode> variants) { |
---|
| 503 | // merge but preserve lexically distinct variants |
---|
| 504 | TaskTreeNodeMerger merger = new TaskTreeNodeMerger |
---|
| 505 | (taskTreeNodeFactory, taskTreeBuilder, nodeComparator); |
---|
| 506 | |
---|
| 507 | merger.mergeTaskNodes(variants); |
---|
| 508 | |
---|
| 509 | if (variants.size() == 1) { |
---|
| 510 | ITaskTreeNode replacement = variants.get(0); |
---|
| 511 | taskTreeBuilder.setDescription(replacement, description); |
---|
| 512 | return replacement; |
---|
| 513 | } |
---|
| 514 | else { |
---|
| 515 | ISelection selection = taskTreeNodeFactory.createNewSelection(); |
---|
| 516 | taskTreeBuilder.setDescription(selection, "variants of task " + description); |
---|
| 517 | |
---|
| 518 | for (ITaskTreeNode variant : variants) { |
---|
| 519 | taskTreeBuilder.addChild(selection, variant); |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | return selection; |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /** |
---|
| 527 | * <p> |
---|
| 528 | * TODO: comment |
---|
| 529 | * </p> |
---|
| 530 | * |
---|
[1107] | 531 | * @param task |
---|
| 532 | * @param parent |
---|
| 533 | * @param treeBuilder |
---|
| 534 | * @param nodeFactory |
---|
| 535 | * @param result |
---|
| 536 | */ |
---|
[1127] | 537 | private void replaceTaskOccurrences(List<ITaskTreeNode> task, |
---|
| 538 | ITaskTreeNode replacement, |
---|
| 539 | List<ISequence> sessions, |
---|
| 540 | RuleApplicationData appData) |
---|
[1107] | 541 | { |
---|
| 542 | // now check the children themselves for an occurrence of the task |
---|
[1127] | 543 | for (int i = 0; i < sessions.size(); i++) { |
---|
| 544 | ISequence session = sessions.get(i); |
---|
| 545 | |
---|
| 546 | int index = -1; |
---|
[1107] | 547 | |
---|
[1127] | 548 | List<ITaskTreeNode> children = session.getChildren(); |
---|
| 549 | |
---|
| 550 | do { |
---|
| 551 | index = getSubListIndex(children, task, ++index); |
---|
| 552 | |
---|
| 553 | if (index > -1) { |
---|
| 554 | if ((!(replacement instanceof ISequence)) || |
---|
| 555 | (task.size() < children.size())) |
---|
| 556 | { |
---|
| 557 | for (int j = index; j < index + task.size(); j++) { |
---|
| 558 | taskTreeBuilder.removeChild(session, index); |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | taskTreeBuilder.addChild(session, index, replacement); |
---|
| 562 | appData.incrementReplacementCounter(); |
---|
| 563 | |
---|
| 564 | children = session.getChildren(); |
---|
[1107] | 565 | } |
---|
| 566 | else { |
---|
[1127] | 567 | // the whole list of children is an occurrence of this task. So ask the |
---|
| 568 | // caller of the method to replace the whole node |
---|
| 569 | sessions.set(i, (ISequence) replacement); |
---|
| 570 | appData.incrementReplacementCounter(); |
---|
| 571 | break; |
---|
[1107] | 572 | } |
---|
| 573 | } |
---|
| 574 | } |
---|
[1127] | 575 | while (index > -1); |
---|
[1107] | 576 | } |
---|
| 577 | } |
---|
| 578 | |
---|
| 579 | /** |
---|
| 580 | * <p> |
---|
| 581 | * TODO: comment |
---|
| 582 | * </p> |
---|
| 583 | * |
---|
| 584 | * @param trie |
---|
| 585 | * @param object |
---|
| 586 | * @return |
---|
| 587 | */ |
---|
| 588 | private int getSubListIndex(List<ITaskTreeNode> list, |
---|
| 589 | List<ITaskTreeNode> subList, |
---|
| 590 | int startIndex) |
---|
| 591 | { |
---|
| 592 | boolean matchFound; |
---|
| 593 | int result = -1; |
---|
| 594 | |
---|
| 595 | for (int i = startIndex; i <= list.size() - subList.size(); i++) { |
---|
| 596 | matchFound = true; |
---|
| 597 | |
---|
| 598 | for (int j = 0; j < subList.size(); j++) { |
---|
| 599 | if (!nodeComparator.equals(list.get(i + j), subList.get(j))) { |
---|
| 600 | matchFound = false; |
---|
| 601 | break; |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | if (matchFound) { |
---|
| 606 | result = i; |
---|
| 607 | break; |
---|
| 608 | } |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | return result; |
---|
| 612 | } |
---|
| 613 | |
---|
[1119] | 614 | /** |
---|
| 615 | * <p> |
---|
| 616 | * TODO comment |
---|
| 617 | * </p> |
---|
| 618 | * |
---|
| 619 | * @author Patrick Harms |
---|
| 620 | */ |
---|
[1127] | 621 | private class MaxCountAndLongestTasksFinder implements TrieProcessor<ITaskTreeNode> { |
---|
[1119] | 622 | |
---|
| 623 | /** |
---|
| 624 | * |
---|
| 625 | */ |
---|
| 626 | private int currentCount; |
---|
| 627 | |
---|
| 628 | /** |
---|
| 629 | * |
---|
| 630 | */ |
---|
[1127] | 631 | private List<List<ITaskTreeNode>> foundTasks = new LinkedList<List<ITaskTreeNode>>(); |
---|
[1119] | 632 | |
---|
| 633 | /** |
---|
| 634 | * <p> |
---|
| 635 | * TODO: comment |
---|
| 636 | * </p> |
---|
| 637 | * |
---|
| 638 | * @param maxCount |
---|
| 639 | */ |
---|
[1127] | 640 | public MaxCountAndLongestTasksFinder() { |
---|
[1119] | 641 | super(); |
---|
| 642 | this.currentCount = 0; |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | /* (non-Javadoc) |
---|
| 646 | * @see de.ugoe.cs.autoquest.usageprofiles.TrieProcessor#process(java.util.List, int) |
---|
| 647 | */ |
---|
| 648 | @Override |
---|
[1127] | 649 | public TrieProcessor.Result process(List<ITaskTreeNode> task, int count) { |
---|
| 650 | if (task.size() < 2) { |
---|
[1119] | 651 | // ignore single nodes |
---|
| 652 | return TrieProcessor.Result.CONTINUE; |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | if (count < 2) { |
---|
| 656 | // ignore singular occurrences |
---|
| 657 | return TrieProcessor.Result.SKIP_NODE; |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | if (this.currentCount > count) { |
---|
| 661 | // ignore this children of this node, as they may have only smaller counts than |
---|
[1127] | 662 | // the already found tasks |
---|
[1119] | 663 | return TrieProcessor.Result.SKIP_NODE; |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | if (this.currentCount < count) { |
---|
[1127] | 667 | // the provided task occurs more often that all tasks found so far. |
---|
| 668 | // clear all found tasks and use the new count as the one searched for |
---|
| 669 | foundTasks.clear(); |
---|
[1119] | 670 | this.currentCount = count; |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | if (this.currentCount == count) { |
---|
[1127] | 674 | // the task is of interest. Sort it into the other found tasks so that |
---|
[1119] | 675 | // the longest come first |
---|
| 676 | boolean added = false; |
---|
[1127] | 677 | for (int i = 0; i < foundTasks.size(); i++) { |
---|
| 678 | if (foundTasks.get(i).size() < task.size()) { |
---|
[1119] | 679 | // defensive copy |
---|
[1127] | 680 | foundTasks.add(i, new LinkedList<ITaskTreeNode>(task)); // defensive copy |
---|
[1119] | 681 | added = true; |
---|
| 682 | break; |
---|
| 683 | } |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | if (!added) { |
---|
[1127] | 687 | foundTasks.add(new LinkedList<ITaskTreeNode>(task)); // defensive copy |
---|
[1119] | 688 | } |
---|
| 689 | } |
---|
| 690 | |
---|
| 691 | return TrieProcessor.Result.CONTINUE; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | /** |
---|
| 695 | * <p> |
---|
| 696 | * TODO: comment |
---|
| 697 | * </p> |
---|
| 698 | * |
---|
| 699 | * @return |
---|
| 700 | */ |
---|
[1127] | 701 | public Tasks getFoundTasks() { |
---|
[1119] | 702 | removePermutationsOfShorterTasks(); |
---|
[1127] | 703 | return new Tasks(currentCount, foundTasks); |
---|
[1119] | 704 | } |
---|
| 705 | |
---|
| 706 | /** |
---|
| 707 | * <p> |
---|
| 708 | * TODO: comment |
---|
| 709 | * </p> |
---|
| 710 | * |
---|
| 711 | */ |
---|
| 712 | private void removePermutationsOfShorterTasks() { |
---|
| 713 | // now iterate the sorted list and for each task remove all other tasks that are shorter |
---|
| 714 | // (i.e. come later in the sorted list) and that represent a subpart of the task |
---|
[1127] | 715 | for (int i = 0; i < foundTasks.size(); i++) { |
---|
| 716 | for (int j = i + 1; j < foundTasks.size();) { |
---|
| 717 | if (foundTasks.get(j).size() < foundTasks.get(i).size()) { |
---|
[1119] | 718 | // found a task that is a potential subtask. Check for this and remove the |
---|
| 719 | // subtask if needed |
---|
[1127] | 720 | List<ITaskTreeNode> longTask = foundTasks.get(i); |
---|
| 721 | List<ITaskTreeNode> shortTask = foundTasks.get(j); |
---|
[1119] | 722 | |
---|
| 723 | if (getSubListIndex(longTask, shortTask, 0) > -1) { |
---|
[1127] | 724 | foundTasks.remove(j); |
---|
[1119] | 725 | } |
---|
| 726 | else { |
---|
| 727 | j++; |
---|
| 728 | } |
---|
| 729 | } |
---|
| 730 | else { |
---|
| 731 | j++; |
---|
| 732 | } |
---|
| 733 | } |
---|
| 734 | } |
---|
| 735 | } |
---|
| 736 | |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | /** |
---|
| 740 | * |
---|
| 741 | */ |
---|
[1127] | 742 | private class RuleApplicationData { |
---|
[1119] | 743 | |
---|
| 744 | /** |
---|
| 745 | * |
---|
| 746 | */ |
---|
[1127] | 747 | private List<ISequence> sessions; |
---|
[1119] | 748 | |
---|
| 749 | /** |
---|
| 750 | * |
---|
| 751 | */ |
---|
[1127] | 752 | private Trie<ITaskTreeNode> lastTrie; |
---|
[1119] | 753 | |
---|
| 754 | /** |
---|
[1127] | 755 | * default and minimum trained sequence length is 3 |
---|
[1119] | 756 | */ |
---|
[1127] | 757 | private int trainedSequenceLength = 3; |
---|
[1119] | 758 | |
---|
| 759 | /** |
---|
| 760 | * |
---|
| 761 | */ |
---|
[1127] | 762 | private Tasks lastFoundTasks = new Tasks(Integer.MAX_VALUE, null); |
---|
[1119] | 763 | |
---|
| 764 | /** |
---|
[1127] | 765 | * |
---|
[1119] | 766 | */ |
---|
[1127] | 767 | private List<ITaskTreeNode> taskOccurrences = new LinkedList<ITaskTreeNode>(); |
---|
[1119] | 768 | |
---|
| 769 | /** |
---|
| 770 | * |
---|
| 771 | */ |
---|
| 772 | private int replacementCounter; |
---|
| 773 | |
---|
| 774 | /** |
---|
| 775 | * |
---|
| 776 | */ |
---|
[1127] | 777 | private RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 778 | |
---|
| 779 | /** |
---|
| 780 | * |
---|
| 781 | */ |
---|
[1119] | 782 | private StopWatch stopWatch = new StopWatch(); |
---|
| 783 | |
---|
| 784 | /** |
---|
| 785 | * |
---|
| 786 | */ |
---|
[1127] | 787 | private RuleApplicationData(List<ISequence> sessions) { |
---|
| 788 | this.sessions = sessions; |
---|
[1119] | 789 | } |
---|
| 790 | |
---|
| 791 | /** |
---|
[1127] | 792 | * @return the tree |
---|
[1119] | 793 | */ |
---|
[1127] | 794 | private List<ISequence> getSessions() { |
---|
| 795 | return sessions; |
---|
[1119] | 796 | } |
---|
| 797 | |
---|
| 798 | /** |
---|
[1127] | 799 | * @param lastTrie the lastTrie to set |
---|
[1119] | 800 | */ |
---|
[1127] | 801 | private void setLastTrie(Trie<ITaskTreeNode> lastTrie) { |
---|
| 802 | this.lastTrie = lastTrie; |
---|
[1119] | 803 | } |
---|
| 804 | |
---|
| 805 | /** |
---|
| 806 | * @return the lastTrie |
---|
| 807 | */ |
---|
[1127] | 808 | private Trie<ITaskTreeNode> getLastTrie() { |
---|
[1119] | 809 | return lastTrie; |
---|
| 810 | } |
---|
| 811 | |
---|
| 812 | /** |
---|
[1127] | 813 | * @param trainedSequenceLength the trainedSequenceLength to set |
---|
| 814 | */ |
---|
| 815 | private void setTrainedSequenceLength(int trainedSequenceLength) { |
---|
| 816 | this.trainedSequenceLength = trainedSequenceLength; |
---|
| 817 | } |
---|
| 818 | |
---|
| 819 | /** |
---|
[1119] | 820 | * @return the trainedSequenceLength |
---|
| 821 | */ |
---|
| 822 | private int getTrainedSequenceLength() { |
---|
| 823 | return trainedSequenceLength; |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | /** |
---|
[1127] | 827 | * @param lastFoundSequences the lastFoundSequences to set |
---|
[1119] | 828 | */ |
---|
[1127] | 829 | private void setLastFoundTasks(Tasks lastFoundSequences) { |
---|
| 830 | this.lastFoundTasks = lastFoundSequences; |
---|
[1119] | 831 | } |
---|
| 832 | |
---|
| 833 | /** |
---|
[1127] | 834 | * @return the lastFoundSequences |
---|
[1119] | 835 | */ |
---|
[1127] | 836 | private Tasks getLastFoundTasks() { |
---|
| 837 | return lastFoundTasks; |
---|
[1119] | 838 | } |
---|
| 839 | |
---|
| 840 | /** |
---|
[1127] | 841 | * @return the taskOccurrences |
---|
[1119] | 842 | */ |
---|
[1127] | 843 | private void clearTaskOccurrences() { |
---|
| 844 | taskOccurrences.clear(); |
---|
[1119] | 845 | } |
---|
| 846 | |
---|
| 847 | /** |
---|
[1127] | 848 | * @return the taskOccurrences |
---|
[1119] | 849 | */ |
---|
[1127] | 850 | private void addTaskOccurrence(ITaskTreeNode taskOccurrence) { |
---|
| 851 | taskOccurrences.add(taskOccurrence); |
---|
[1119] | 852 | } |
---|
| 853 | |
---|
| 854 | /** |
---|
[1127] | 855 | * @return the taskOccurrences |
---|
[1119] | 856 | */ |
---|
[1127] | 857 | private List<ITaskTreeNode> getTaskOccurrences() { |
---|
| 858 | return taskOccurrences; |
---|
[1119] | 859 | } |
---|
| 860 | |
---|
| 861 | /** |
---|
| 862 | * |
---|
| 863 | */ |
---|
| 864 | private void resetReplacementCounter() { |
---|
| 865 | replacementCounter = 0; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | /** |
---|
| 869 | * |
---|
| 870 | */ |
---|
| 871 | private void incrementReplacementCounter() { |
---|
| 872 | replacementCounter++; |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | /** |
---|
| 876 | * |
---|
| 877 | */ |
---|
| 878 | private int getReplacementCounter() { |
---|
| 879 | return replacementCounter; |
---|
| 880 | } |
---|
[1127] | 881 | |
---|
| 882 | /** |
---|
| 883 | * @return the result |
---|
| 884 | */ |
---|
| 885 | private RuleApplicationResult getResult() { |
---|
| 886 | return result; |
---|
| 887 | } |
---|
| 888 | |
---|
| 889 | /** |
---|
| 890 | * @return the stopWatch |
---|
| 891 | */ |
---|
| 892 | private StopWatch getStopWatch() { |
---|
| 893 | return stopWatch; |
---|
| 894 | } |
---|
| 895 | |
---|
[1119] | 896 | } |
---|
| 897 | |
---|
| 898 | |
---|
| 899 | /** |
---|
| 900 | * <p> |
---|
| 901 | * TODO comment |
---|
| 902 | * </p> |
---|
| 903 | * |
---|
| 904 | * @author Patrick Harms |
---|
| 905 | */ |
---|
[1127] | 906 | private class Tasks implements Iterable<List<ITaskTreeNode>> { |
---|
[1119] | 907 | |
---|
| 908 | /** |
---|
| 909 | * |
---|
| 910 | */ |
---|
| 911 | private int occurrenceCount; |
---|
| 912 | |
---|
| 913 | /** |
---|
| 914 | * |
---|
| 915 | */ |
---|
[1127] | 916 | private List<List<ITaskTreeNode>> sequences; |
---|
[1119] | 917 | |
---|
| 918 | /** |
---|
| 919 | * <p> |
---|
| 920 | * TODO: comment |
---|
| 921 | * </p> |
---|
| 922 | * |
---|
| 923 | * @param occurrenceCount |
---|
| 924 | * @param sequences |
---|
| 925 | */ |
---|
[1127] | 926 | private Tasks(int occurrenceCount, List<List<ITaskTreeNode>> sequences) { |
---|
[1119] | 927 | super(); |
---|
| 928 | this.occurrenceCount = occurrenceCount; |
---|
| 929 | this.sequences = sequences; |
---|
| 930 | } |
---|
| 931 | |
---|
| 932 | /** |
---|
| 933 | * <p> |
---|
| 934 | * TODO: comment |
---|
| 935 | * </p> |
---|
| 936 | * |
---|
| 937 | * @return |
---|
| 938 | */ |
---|
| 939 | private int getOccurrenceCount() { |
---|
| 940 | return occurrenceCount; |
---|
| 941 | } |
---|
| 942 | |
---|
| 943 | /** |
---|
| 944 | * <p> |
---|
| 945 | * TODO: comment |
---|
| 946 | * </p> |
---|
| 947 | * |
---|
| 948 | * @return |
---|
| 949 | */ |
---|
| 950 | private int size() { |
---|
| 951 | return this.sequences.size(); |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | /** |
---|
| 955 | * |
---|
| 956 | */ |
---|
| 957 | |
---|
| 958 | /* (non-Javadoc) |
---|
| 959 | * @see java.lang.Iterable#iterator() |
---|
| 960 | */ |
---|
| 961 | @Override |
---|
[1127] | 962 | public Iterator<List<ITaskTreeNode>> iterator() { |
---|
[1119] | 963 | return this.sequences.iterator(); |
---|
| 964 | } |
---|
| 965 | |
---|
| 966 | } |
---|
| 967 | |
---|
[1107] | 968 | } |
---|