Changeset 1693 for branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
- Timestamp:
- 08/24/14 21:12:44 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
r1692 r1693 68 68 class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule { 69 69 70 71 70 private int iteration = 0; 72 71 /** … … 92 91 */ 93 92 private TaskHandlingStrategy preparationTaskHandlingStrategy; 94 95 93 96 94 /** … … 139 137 public RuleApplicationResult apply(List<IUserSession> sessions) { 140 138 RuleApplicationData appData = new RuleApplicationData(sessions); 141 139 142 140 harmonizeEventTaskInstancesModel(appData); 143 141 do { 144 142 iteration++; 145 143 146 144 appData.detectedAndReplacedTasks = false; 147 appData.getStopWatch().start("whole loop"); 145 appData.getStopWatch().start("whole loop"); 148 146 detectAndReplaceIterations(appData); 149 appData.getStopWatch().start("task replacement"); 147 appData.getStopWatch().start("task replacement"); 150 148 detectAndReplaceTasks(appData); // 151 appData.getStopWatch().stop("task replacement"); 149 appData.getStopWatch().stop("task replacement"); 152 150 appData.getStopWatch().stop("whole loop"); 153 appData.getStopWatch().dumpStatistics(System.out); 151 appData.getStopWatch().dumpStatistics(System.out); 154 152 appData.getStopWatch().reset(); 155 153 … … 181 179 templist.getSequence()[j] = taskInstance.getTask().getId(); 182 180 } 183 //System.out.println();184 181 // Each NumberSequence is identified by its id, beginning to count 185 182 // at zero … … 207 204 "harmonizing task model of event task instances"); 208 205 appData.getStopWatch().start("harmonizing event tasks"); 209 SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy.createSymbolMap(); 206 SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy 207 .createSymbolMap(); 210 208 211 209 TaskInstanceComparator comparator = preparationTaskHandlingStrategy … … 223 221 224 222 if (task == null) { 225 uniqueTasks.addSymbol(taskInstance, 223 uniqueTasks.addSymbol(taskInstance, taskInstance.getTask()); 224 appData.getUniqueTasks().add(taskInstance.getTask()); 225 appData.getNumber2Task().put( 226 taskInstance.getTask().getId(), 226 227 taskInstance.getTask()); 227 appData.getUniqueTasks().add(taskInstance.getTask());228 appData.getNumber2Task().put(taskInstance.getTask().getId(),taskInstance.getTask());229 228 } else { 230 229 taskBuilder.setTask(taskInstance, task); … … 323 322 324 323 IIteration iteration = taskFactory.createNewIteration(); 325 appData.getUniqueTasks().add( 324 appData.getUniqueTasks().add(iteration); 326 325 appData.getNumber2Task().put(iteration.getId(), iteration); 327 326 iterations.put(iteratedTask, iteration); … … 350 349 .createNewTaskInstance(iteration); 351 350 iterationInstances.get(iteration) 352 .add(iterationInstance);//TODO:: Don't create TaskInstances here, use a set of tasks instead 351 .add(iterationInstance);// TODO:: Don't create 352 // TaskInstances here, 353 // use a set of tasks 354 // instead 353 355 taskBuilder.addTaskInstance(session, index, 354 356 iterationInstance); … … 366 368 } 367 369 } 368 369 for (Map.Entry<IIteration, List<IIterationInstance>> entry : iterationInstances.entrySet()) 370 { 371 harmonizeIterationInstancesModel(entry.getKey(), entry.getValue()); 372 } 373 } 374 375 376 /** 377 * <p> 378 * TODO clarify why this is done 379 * </p> 380 */ 381 private void harmonizeIterationInstancesModel(IIteration iteration, 382 List<IIterationInstance> iterationInstances) 383 { 384 List<ITask> iteratedTaskVariants = new LinkedList<ITask>(); 385 TaskInstanceComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 386 387 // merge the lexically different variants of iterated task to a unique list 388 for (IIterationInstance iterationInstance : iterationInstances) { 389 for (ITaskInstance executionVariant : iterationInstance) { 390 ITask candidate = executionVariant.getTask(); 391 392 boolean found = false; 393 for (ITask taskVariant : iteratedTaskVariants) { 394 if (comparator.areLexicallyEqual(taskVariant, candidate)) { 395 taskBuilder.setTask(executionVariant, taskVariant); 396 found = true; 397 break; 398 } 399 } 400 401 if (!found) { 402 iteratedTaskVariants.add(candidate); 403 } 404 } 405 } 406 407 // if there are more than one lexically different variant of iterated tasks, adapt the 408 // iteration model to be a selection of different variants. In this case also adapt 409 // the generated iteration instances to correctly contain selection instances. If there 410 // is only one variant of an iterated task, simply set this as the marked task of the 411 // iteration. In this case, the instances can be preserved as is 412 if (iteratedTaskVariants.size() > 1) { 413 ISelection selection = taskFactory.createNewSelection(); 414 415 for (ITask variant : iteratedTaskVariants) { 416 taskBuilder.addChild(selection, variant); 417 } 418 419 taskBuilder.setMarkedTask(iteration, selection); 420 421 for (IIterationInstance instance : iterationInstances) { 422 for (int i = 0; i < instance.size(); i++) { 423 ISelectionInstance selectionInstance = 424 taskFactory.createNewTaskInstance(selection); 425 taskBuilder.setChild(selectionInstance, instance.get(i)); 426 taskBuilder.setTaskInstance(instance, i, selectionInstance); 427 } 428 } 429 } 430 else { 431 taskBuilder.setMarkedTask(iteration, iteratedTaskVariants.get(0)); 432 } 433 } 434 435 436 ISequence matchAsSequence(RuleApplicationData appData, Match m) { 437 370 371 for (Map.Entry<IIteration, List<IIterationInstance>> entry : iterationInstances 372 .entrySet()) { 373 harmonizeIterationInstancesModel(entry.getKey(), entry.getValue()); 374 } 375 } 376 377 /** 378 * <p> 379 * TODO clarify why this is done 380 * </p> 381 */ 382 private void harmonizeIterationInstancesModel(IIteration iteration, 383 List<IIterationInstance> iterationInstances) { 384 List<ITask> iteratedTaskVariants = new LinkedList<ITask>(); 385 TaskInstanceComparator comparator = preparationTaskHandlingStrategy 386 .getTaskComparator(); 387 388 // merge the lexically different variants of iterated task to a unique 389 // list 390 for (IIterationInstance iterationInstance : iterationInstances) { 391 for (ITaskInstance executionVariant : iterationInstance) { 392 ITask candidate = executionVariant.getTask(); 393 394 boolean found = false; 395 for (ITask taskVariant : iteratedTaskVariants) { 396 if (comparator.areLexicallyEqual(taskVariant, candidate)) { 397 taskBuilder.setTask(executionVariant, taskVariant); 398 found = true; 399 break; 400 } 401 } 402 403 if (!found) { 404 iteratedTaskVariants.add(candidate); 405 } 406 } 407 } 408 409 // if there are more than one lexically different variant of iterated 410 // tasks, adapt the 411 // iteration model to be a selection of different variants. In this case 412 // also adapt 413 // the generated iteration instances to correctly contain selection 414 // instances. If there 415 // is only one variant of an iterated task, simply set this as the 416 // marked task of the 417 // iteration. In this case, the instances can be preserved as is 418 if (iteratedTaskVariants.size() > 1) { 419 ISelection selection = taskFactory.createNewSelection(); 420 421 for (ITask variant : iteratedTaskVariants) { 422 taskBuilder.addChild(selection, variant); 423 } 424 425 taskBuilder.setMarkedTask(iteration, selection); 426 427 for (IIterationInstance instance : iterationInstances) { 428 for (int i = 0; i < instance.size(); i++) { 429 ISelectionInstance selectionInstance = taskFactory 430 .createNewTaskInstance(selection); 431 taskBuilder.setChild(selectionInstance, instance.get(i)); 432 taskBuilder.setTaskInstance(instance, i, selectionInstance); 433 } 434 } 435 } else { 436 taskBuilder.setMarkedTask(iteration, iteratedTaskVariants.get(0)); 437 } 438 } 439 440 /** 441 * @param appData 442 * @param m 443 * @return 444 */ 445 public ISequence matchAsSequence(RuleApplicationData appData, Match m) { 446 447 System.out.println("DEBUGGING MODEL GENERATION"); 448 System.out.println(); 449 438 450 ISequence sequence = taskFactory.createNewSequence(); 439 451 appData.uniqueTasks.add(sequence); 440 452 appData.number2task.put(sequence.getId(), sequence); 441 442 453 443 454 int[] first = m.getFirstSequence().getSequence(); … … 450 461 // far, just to handle it 451 462 if (first[i] == -1 && second[i] == -1) { 452 // TODO: Do nothing?463 // Do nothing here. 453 464 } 454 465 // Both events are equal, we can simply add the task referring to … … 478 489 taskBuilder.addChild(sequence, optional); 479 490 } 480 // Both tasks are not equal, we need to insert a selection here 491 // Both tasks are not equal, we need to insert a selection here. 492 // Check if the next position is not a selection 493 else if (i < first.length - 1) { 494 495 if ((first[i] != second[i]) 496 && ((first[i + 1] == second[i + 1] 497 || first[i + 1] == -1 || second[i + 1] == -1))) { 498 499 ISelection selection = taskFactory.createNewSelection(); 500 appData.getUniqueTasks().add(selection); 501 appData.number2task.put(selection.getId(), selection); 502 taskBuilder.addChild(selection, appData.getNumber2Task() 503 .get(first[i])); 504 taskBuilder.addChild(selection, appData.getNumber2Task() 505 .get(second[i])); 506 taskBuilder.addChild(sequence, selection); 507 } else { 508 System.out.println("SELECTION OF SEQUENCES FOUND!"); 509 boolean selectionfound = true; 510 ISelection selection = taskFactory.createNewSelection(); 511 appData.getUniqueTasks().add(selection); 512 appData.number2task.put(selection.getId(), selection); 513 514 ISequence subsequence1 = taskFactory.createNewSequence(); 515 appData.uniqueTasks.add(subsequence1); 516 appData.number2task.put(subsequence1.getId(), subsequence1); 517 518 ISequence subsequence2 = taskFactory.createNewSequence(); 519 appData.uniqueTasks.add(subsequence2); 520 appData.number2task.put(subsequence2.getId(), subsequence2); 521 522 taskBuilder.addChild(selection, subsequence1); 523 taskBuilder.addChild(selection, subsequence2); 524 taskBuilder.addChild(sequence,selection); 525 while (i < first.length - 1 && selectionfound) { 526 selectionfound = false; 527 taskBuilder.addChild(subsequence1, appData 528 .getNumber2Task().get(first[i])); 529 taskBuilder.addChild(subsequence2, appData 530 .getNumber2Task().get(second[i])); 531 if (first[i + 1] != second[i + 1] && first[i + 1] != -1 532 && second[i + 1] != -1) { 533 selectionfound = true; 534 } 535 i++; 536 } 537 if(i==first.length-1 && selectionfound) { 538 taskBuilder.addChild(subsequence1, appData 539 .getNumber2Task().get(first[i])); 540 taskBuilder.addChild(subsequence2, appData 541 .getNumber2Task().get(second[i])); 542 } 543 } 544 } 545 481 546 else { 482 ISelection selection = taskFactory.createNewSelection(); 483 appData.getUniqueTasks().add(selection); 484 appData.number2task.put(selection.getId(), selection); 485 taskBuilder.addChild(selection, 486 appData.getNumber2Task().get(first[i])); 487 taskBuilder.addChild(selection, 488 appData.getNumber2Task().get(second[i])); 489 taskBuilder.addChild(sequence, selection); 490 } 491 } 492 493 // TODO: Debug output 494 /* 495 * for (int i =0;i<sequence.getChildren().size();i++) { 496 * System.out.println(sequence.getChildren().get(i)); 497 * 498 * if(sequence.getChildren().get(i).getType() == "selection") { for(int 499 * j=0; j< ((ISelection) 500 * sequence.getChildren().get(i)).getChildren().size();j++) { 501 * System.out.println("\t" +((ISelection) 502 * sequence.getChildren().get(i)).getChildren().get(j)); } } } 503 */ 547 if ((first[i] != second[i])) { 548 549 ISelection selection = taskFactory.createNewSelection(); 550 appData.getUniqueTasks().add(selection); 551 appData.number2task.put(selection.getId(), selection); 552 taskBuilder.addChild(selection, appData.getNumber2Task() 553 .get(first[i])); 554 taskBuilder.addChild(selection, appData.getNumber2Task() 555 .get(second[i])); 556 taskBuilder.addChild(sequence, selection); 557 } 558 } 559 560 } 504 561 return sequence; 505 562 } 506 507 563 508 564 /** … … 546 602 } 547 603 } 548 604 549 605 Console.traceln(Level.FINEST, 550 606 "retrieving significant sequence pieces: 100%"); … … 598 654 // well 599 655 if (matchseqs.get(i).occurenceCount() > 2) { 600 656 601 657 appData.detectedAndReplacedTasks = true; 602 658 ISequence task = matchAsSequence(appData, matchseqs.get(i)); … … 604 660 .get(i).getOccurences().iterator(); it.hasNext();) { 605 661 MatchOccurence oc = it.next(); 606 607 if(iteration==-1) { 608 System.out.println("Trying to replace sequence: "); 609 matchseqs.get(i).getFirstSequence().printSequence(); 610 matchseqs.get(i).getSecondSequence().printSequence(); 611 System.out.println(" in session number: " + 612 (oc.getSequenceId() + 1) + " at position " + 613 (oc.getStartindex()) + "-" + oc.getEndindex()); 614 System.out.println(); 662 663 if (iteration > 0) { 615 664 616 617 System.out.println("Printing session: "); 618 for (int j = 0; j < 619 appData.getSessions().get(oc.getSequenceId()).size(); 620 j++) { 621 System.out.println(j + ": " 622 + appData.getSessions().get(oc.getSequenceId()).get(j)); 623 } 665 System.out.println("Trying to replace sequence: "); 666 matchseqs.get(i).getFirstSequence().printSequence(); 667 matchseqs.get(i).getSecondSequence().printSequence(); 668 System.out.println(" in session number: " 669 + (oc.getSequenceId() + 1) + " at position " 670 + (oc.getStartindex()) + "-" + oc.getEndindex()); 671 System.out.println(); 672 673 System.out.println("Printing session: "); 674 for (int j = 0; j < appData.getSessions() 675 .get(oc.getSequenceId()).size(); j++) { 676 System.out.println(j 677 + ": " 678 + appData.getSessions().get(oc.getSequenceId()) 679 .get(j)); 624 680 } 625 681 } 626 682 // Check if nothing has been replaced in the sequence we 627 683 // want to replace … … 687 743 } 688 744 689 690 691 745 /** 692 746 * … … 696 750 private HashMap<Integer, ITask> number2task; 697 751 698 // TODO: We Actually just need number2task here752 // TODO: We Actually just need number2task here 699 753 private HashSet<ITask> uniqueTasks; 700 754
Note: See TracChangeset
for help on using the changeset viewer.