Changeset 1657 for branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
- Timestamp:
- 08/11/14 08:22:13 (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
r1656 r1657 95 95 * <p> 96 96 * the task handling strategy to be used for comparing tasks during 97 * iteration detection i.e., after the tasks are 98 * harmonized 97 * iteration detection i.e., after the tasks are harmonized 99 98 * </p> 100 99 */ 101 100 private TaskHandlingStrategy identityTaskHandlingStrategy; 102 103 104 101 105 102 /** … … 166 163 .generate(appData.getNumberSequences(), submat, 9); 167 164 168 169 170 165 // Retrieve all matches reached a specific threshold 171 166 Console.traceln(Level.INFO, "retrieving significant sequence pieces"); … … 174 169 Level.FINEST, 175 170 "retrieving significant sequence pieces: " 176 + Math.round((float) i / (float) appData.getNumberSequences().size() 177 * 100) + "%"); 171 + Math.round((float) i 172 / (float) appData.getNumberSequences() 173 .size() * 100) + "%"); 178 174 for (int j = 0; j < appData.getNumberSequences().size(); j++) { 179 175 if (i != j) { … … 197 193 .getFirstSequence().size()) 198 194 continue; 199 195 200 196 for (int j = 0; j < appData.getNumberSequences().size(); j++) { 201 LinkedList<Integer> startpositions = appData .getNumberSequences().get(j)202 . containsPattern(pattern);197 LinkedList<Integer> startpositions = appData 198 .getNumberSequences().get(j).containsPattern(pattern); 203 199 if (startpositions.size() > 0) { 204 200 NumberSequence tempns = appData.getNumberSequences().get(j); 205 201 206 202 for (Iterator<Integer> jt = startpositions.iterator(); jt 207 203 .hasNext();) { 208 204 int start = jt.next(); 209 // TODO: Debug Output205 // TODO: Debug Output 210 206 /* 211 System.out.println("Found match "); 212 pattern.getFirstSequence().printSequence(); 213 pattern.getSecondSequence().printSequence(); 214 System.out.println("in sequence " + (j+1) + " at position " + start); 215 for(int k=0;k<tempns.getSequence().length;k++) { 216 System.out.print(k + ": " + tempns.getSequence()[k] + " "); 217 System.out.println(appData.getNumber2Task().get(tempns.getSequence()[k])); 218 } 219 */ 220 pattern.addOccurence( 221 new MatchOccurence(start, j)); 207 * System.out.println("Found match "); 208 * pattern.getFirstSequence().printSequence(); 209 * pattern.getSecondSequence().printSequence(); 210 * System.out.println("in sequence " + (j+1) + 211 * " at position " + start); for(int 212 * k=0;k<tempns.getSequence().length;k++) { 213 * System.out.print(k + ": " + tempns.getSequence()[k] + 214 * " "); 215 * System.out.println(appData.getNumber2Task().get( 216 * tempns.getSequence()[k])); } 217 */ 218 pattern.addOccurence(new MatchOccurence(start, start 219 + pattern.size(), j)); 222 220 } 223 221 … … 230 228 Comparator<Match> comparator = new Comparator<Match>() { 231 229 public int compare(Match m1, Match m2) { 232 return m2.occurenceCount() - m1.occurenceCount(); 233 230 return m2.occurenceCount() - m1.occurenceCount(); 231 234 232 } 235 233 }; 236 //Collections.sort(matchseqs, comparator); 237 234 // Collections.sort(matchseqs, comparator); 238 235 239 236 // TODO: Harmonize matches: finding double matches and merge them 240 237 /* 241 Console.traceln(Level.INFO, "harmonizing matches"); 242 int i=0; 243 244 while(i<matchseqs.size()) { 245 int j=i; 246 while(j<matchseqs.size()) { 247 if(i!=j) { 248 if(matchseqs.get(i).equals(matchseqs.get(j))) { 249 //matchseqs.get(i).addOccurencesOf(matchseqs.get(j)); 250 matchseqs.remove(j); 251 252 //System.out.println("Sequence " + i); 253 //matchseqs.get(i).getFirstSequence().printSequence(); 254 //matchseqs.get(i).getSecondSequence().printSequence(); 255 //System.out.println("is equal to sequence " + j); 256 //matchseqs.get(j).getFirstSequence().printSequence(); 257 //matchseqs.get(j).getSecondSequence().printSequence(); 258 continue; 259 } 260 } 261 j++; 262 } 263 i++; 264 } 265 Collections.sort(matchseqs, comparator); 266 */ 267 268 269 // Just printing the results out 238 * Console.traceln(Level.INFO, "harmonizing matches"); int i=0; 239 * 240 * while(i<matchseqs.size()) { int j=i; while(j<matchseqs.size()) { 241 * if(i!=j) { if(matchseqs.get(i).equals(matchseqs.get(j))) { 242 * //matchseqs.get(i).addOccurencesOf(matchseqs.get(j)); 243 * matchseqs.remove(j); 244 * 245 * //System.out.println("Sequence " + i); 246 * //matchseqs.get(i).getFirstSequence().printSequence(); 247 * //matchseqs.get(i).getSecondSequence().printSequence(); 248 * //System.out.println("is equal to sequence " + j); 249 * //matchseqs.get(j).getFirstSequence().printSequence(); 250 * //matchseqs.get(j).getSecondSequence().printSequence(); continue; } } 251 * j++; } i++; } Collections.sort(matchseqs, comparator); 252 */ 253 254 HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>(); 255 // Replace matches in the sessions 270 256 for (int i = 0; i < matchseqs.size(); i++) { 271 257 // Every pattern consists of 2 sequences, therefore the minimum … … 274 260 // well 275 261 if (matchseqs.get(i).occurenceCount() > 2) { 276 277 ISequence task = matchAsSequence(appData,matchseqs.get(i)); 278 for(Iterator<MatchOccurence> it = matchseqs.get(i).getOccurences().iterator();it.hasNext();) { 262 263 ISequence task = matchAsSequence(appData, matchseqs.get(i)); 264 invalidOccurence: for (Iterator<MatchOccurence> it = matchseqs 265 .get(i).getOccurences().iterator(); it.hasNext();) { 279 266 MatchOccurence oc = it.next(); 280 267 System.out.println("Trying to replace sequence: "); 281 268 matchseqs.get(i).getFirstSequence().printSequence(); 282 269 matchseqs.get(i).getSecondSequence().printSequence(); 283 System.out.println(" in session number: " + (oc.getSequenceId()+1) + " at position " + (oc.getStartindex()) + "-" + (oc.getStartindex()+matchseqs.get(i).getFirstSequence().size())); 270 System.out.println(" in session number: " 271 + (oc.getSequenceId() + 1) 272 + " at position " 273 + (oc.getStartindex()) 274 + "-" 275 + (oc.getStartindex() + matchseqs.get(i) 276 .getFirstSequence().size())); 284 277 System.out.println(); 285 /* 286 System.out.println("Printing session: "); 287 for(int j=0;j<sessions.get(oc.getSequenceId()).size();j++) { 288 System.out.println(j+ ": " + sessions.get(oc.getSequenceId()).get(j)); 289 }*/ 290 List<ISequenceInstance> sequenceInstances = new LinkedList<ISequenceInstance>(); 291 RuleUtils.createNewSubSequenceInRange(sessions.get(oc.getSequenceId()), oc.getStartindex(), oc.getStartindex()+matchseqs.get(i).getFirstSequence().size(), task, taskFactory, taskBuilder); 292 } 293 //System.out.println(task); 294 //matchseqs.get(i).getFirstSequence().printSequence(); 295 //matchseqs.get(i).getSecondSequence().printSequence(); 296 //System.out.println("Found pattern " 297 // + matchseqs.get(i).occurenceCount() + " times"); 278 279 //System.out.println("Printing session: "); 280 for (int j = 0; j < sessions.get(oc.getSequenceId()).size(); j++) { 281 System.out.println(j + ": "+ sessions.get(oc.getSequenceId()).get(j)); 282 } 283 284 // Check if nothing has been replaced in the sequence we 285 // want to replace 286 if (replacedOccurences.get(oc.getSequenceId()) == null) { 287 replacedOccurences.put(oc.getSequenceId(), 288 new LinkedList<MatchOccurence>()); 289 } else { 290 // check if we have any replaced occurence with indexes 291 // smaller than ours. If so, we need to adjust our start 292 // and endpoints 293 // of the replacement. 294 // Also do a check if we have replaced this specific 295 // MatchOccurence in this sequence already. Jump to the 296 // next occurence if this is the case. 297 // This is no more neccessary once the matches are 298 // harmonized. 299 for (Iterator<MatchOccurence> jt = replacedOccurences 300 .get(oc.getSequenceId()).iterator(); jt 301 .hasNext();) { 302 MatchOccurence tmpOC = jt.next(); 303 if (tmpOC.getStartindex() == oc.getStartindex()) { 304 System.out.println("This position has already been replaced in this iteration"); 305 continue invalidOccurence; 306 307 } 308 if (tmpOC.getStartindex() < oc 309 .getStartindex()) { 310 int diff = tmpOC.getEndindex() 311 - tmpOC.getStartindex(); 312 // Just to be sure. 313 if (diff > 0) { 314 System.out.println("Old index: Start: " 315 + oc.getStartindex() + " End " 316 + oc.getEndindex()); 317 oc.setStartindex(oc.getStartindex() - diff+1); 318 oc.setEndindex(oc.getEndindex() - diff+1); 319 System.out.println("Updated index: Start: " 320 + oc.getStartindex() + " End " 321 + oc.getEndindex()); 322 } else { 323 Console.traceln(Level.WARNING, 324 "End index of a Match before start. This should never happen"); 325 } 326 } 327 } 328 } 329 330 331 ISequenceInstance sequenceInstances = RuleUtils.createNewSubSequenceInRange( 332 sessions.get(oc.getSequenceId()), 333 oc.getStartindex(), oc.getEndindex(), task, 334 taskFactory, taskBuilder); 335 336 //Adjust the length of the match regarding to the length of instance. (OptionalInstances may be shorter) 337 oc.setEndindex(oc.getStartindex()+sequenceInstances.size()-RuleUtils.missedOptionals); 338 replacedOccurences.get(oc.getSequenceId()).add(oc); 339 } 340 298 341 System.out.println(); 299 342 } 300 343 } 301 302 344 303 345 alignments = null; 304 346 305 347 do { 306 307 appData.getStopWatch().start("whole loop"); // 308 detectAndReplaceIterations(appData); 309 310 appData.getStopWatch().start("task replacement"); // 311 //detectAndReplaceTasks(appData); // 312 appData.getStopWatch().stop("task replacement"); // 313 appData.getStopWatch().stop("whole loop"); 314 315 appData.getStopWatch().dumpStatistics(System.out); // 316 appData.getStopWatch().reset(); 317 318 } while (appData.detectedAndReplacedTasks()); 319 348 349 appData.getStopWatch().start("whole loop"); // 350 detectAndReplaceIterations(appData); 351 352 appData.getStopWatch().start("task replacement"); // 353 // detectAndReplaceTasks(appData); // 354 appData.getStopWatch().stop("task replacement"); // 355 appData.getStopWatch().stop("whole loop"); 356 357 appData.getStopWatch().dumpStatistics(System.out); // 358 appData.getStopWatch().reset(); 359 360 } while (appData.detectedAndReplacedTasks()); 320 361 321 362 Console.println("created " … … 334 375 } 335 376 336 337 338 377 /** 339 378 * <p> … … 363 402 ITask task; 364 403 List<IUserSession> sessions = appData.getSessions(); 365 for (int j = 0; j < sessions.size();j++) {404 for (int j = 0; j < sessions.size(); j++) { 366 405 IUserSession session = sessions.get(j); 367 406 368 407 NumberSequence templist = new NumberSequence(session.size()); 369 408 … … 375 414 uniqueTasks.addSymbol(taskInstance, taskInstance.getTask()); 376 415 templist.getSequence()[i] = taskInstance.getTask().getId(); 377 416 378 417 } else { 379 418 taskBuilder.setTask(taskInstance, task); … … 381 420 unifiedTasks++; 382 421 } 383 appData.getNumber2Task().put(templist.getSequence()[i], taskInstance.getTask()); 422 appData.getNumber2Task().put(templist.getSequence()[i], 423 taskInstance.getTask()); 384 424 385 //System.out.println("TaskID: " + taskInstance.getTask().getId()+ " Numbersequence: " + templist.getSequence()[i]); 386 } 387 //Each NumberSequence is identified by its id, beginning to count at zero 425 //if(j==1) { 426 // System.out.println(i + ": TaskID: " + 427 // taskInstance.getTask().getId()+ " Numbersequence: " + 428 // templist.getSequence()[i]); 429 //} 430 431 } 432 // Each NumberSequence is identified by its id, beginning to count 433 // at zero 388 434 templist.setId(j); 389 435 appData.getNumberSequences().add(templist); … … 528 574 } 529 575 530 531 ISequence matchAsSequence(RuleApplicationData appData,Match m) { 532 533 ISequence sequence = taskFactory.createNewSequence(); 534 535 int[] first = m.getFirstSequence().getSequence(); 536 int[] second = m.getSecondSequence().getSequence(); 537 538 539 //Both sequences of a match are equally long 540 for(int i=0;i<m.getFirstSequence().size();i++) { 541 542 //Two gaps aligned to each other: Have not seen it happening so far, just to handle it 543 if(first[i]==-1 && second[i]==-1) { 544 //TODO: Do nothing? 545 } 546 //Both events are equal, we can simply add the task referring to the number 547 else if(first[i]==second[i]) { 548 taskBuilder.addChild(sequence, appData.getNumber2Task().get(first[i])); 549 } 550 //We have a gap in the first sequence, we need to add the task of the second sequence as optional 551 else if(first[i]==-1 && second[i]!=-1) { 552 IOptional optional = taskFactory.createNewOptional(); 553 taskBuilder.setMarkedTask(optional, appData.getNumber2Task().get(second[i])); 554 taskBuilder.addChild(sequence,optional); 555 } 556 //We have a gap in the second sequence, we need to add the task of the first sequence as optional 557 else if(first[i]!=-1 && second[i]==-1) { 558 IOptional optional = taskFactory.createNewOptional(); 559 taskBuilder.setMarkedTask(optional, appData.getNumber2Task().get(first[i])); 560 taskBuilder.addChild(sequence,optional); 561 } 562 //Both tasks are unequal, we need to insert a selection here 563 else { 564 ISelection selection = taskFactory.createNewSelection(); 565 taskBuilder.addChild(selection, appData.getNumber2Task().get(first[i])); 566 taskBuilder.addChild(selection, appData.getNumber2Task().get(second[i])); 567 taskBuilder.addChild(sequence,selection); 568 } 569 } 570 571 //TODO: Debug output 572 /* 573 for (int i =0;i<sequence.getChildren().size();i++) { 574 System.out.println(sequence.getChildren().get(i)); 575 576 if(sequence.getChildren().get(i).getType() == "selection") { 577 for(int j=0; j< ((ISelection) sequence.getChildren().get(i)).getChildren().size();j++) { 578 System.out.println("\t" +((ISelection) sequence.getChildren().get(i)).getChildren().get(j)); 579 } 580 } 581 } 582 */ 583 return sequence; 584 } 585 586 576 ISequence matchAsSequence(RuleApplicationData appData, Match m) { 577 578 ISequence sequence = taskFactory.createNewSequence(); 579 580 int[] first = m.getFirstSequence().getSequence(); 581 int[] second = m.getSecondSequence().getSequence(); 582 583 // Both sequences of a match are equally long 584 for (int i = 0; i < m.getFirstSequence().size(); i++) { 585 586 // Two gaps aligned to each other: Have not seen it happening so 587 // far, just to handle it 588 if (first[i] == -1 && second[i] == -1) { 589 // TODO: Do nothing? 590 } 591 // Both events are equal, we can simply add the task referring to 592 // the number 593 else if (first[i] == second[i]) { 594 taskBuilder.addChild(sequence, 595 appData.getNumber2Task().get(first[i])); 596 } 597 // We have a gap in the first sequence, we need to add the task of 598 // the second sequence as optional 599 else if (first[i] == -1 && second[i] != -1) { 600 IOptional optional = taskFactory.createNewOptional(); 601 taskBuilder.setMarkedTask(optional, appData.getNumber2Task() 602 .get(second[i])); 603 taskBuilder.addChild(sequence, optional); 604 } 605 // We have a gap in the second sequence, we need to add the task of 606 // the first sequence as optional 607 else if (first[i] != -1 && second[i] == -1) { 608 IOptional optional = taskFactory.createNewOptional(); 609 taskBuilder.setMarkedTask(optional, appData.getNumber2Task() 610 .get(first[i])); 611 taskBuilder.addChild(sequence, optional); 612 } 613 // Both tasks are unequal, we need to insert a selection here 614 else { 615 ISelection selection = taskFactory.createNewSelection(); 616 taskBuilder.addChild(selection, 617 appData.getNumber2Task().get(first[i])); 618 taskBuilder.addChild(selection, 619 appData.getNumber2Task().get(second[i])); 620 taskBuilder.addChild(sequence, selection); 621 } 622 } 623 624 // TODO: Debug output 625 /* 626 * for (int i =0;i<sequence.getChildren().size();i++) { 627 * System.out.println(sequence.getChildren().get(i)); 628 * 629 * if(sequence.getChildren().get(i).getType() == "selection") { for(int 630 * j=0; j< ((ISelection) 631 * sequence.getChildren().get(i)).getChildren().size();j++) { 632 * System.out.println("\t" +((ISelection) 633 * sequence.getChildren().get(i)).getChildren().get(j)); } } } 634 */ 635 return sequence; 636 } 637 587 638 /** 588 639 * <p> … … 659 710 appData.getStopWatch().start("detecting tasks"); 660 711 661 // getSequencesOccuringMostOften(appData);712 // getSequencesOccuringMostOften(appData); 662 713 663 714 appData.getStopWatch().stop("detecting tasks"); … … 667 718 668 719 appData.getStopWatch().stop("replacing tasks"); 669 670 //Console.traceln(Level.INFO, "detected and replaced " 671 // + appData.getLastFoundTasks().size() + " tasks occuring " 672 // + appData.getLastFoundTasks().getOccurrenceCount() + " times"); 673 } 674 675 676 720 721 // Console.traceln(Level.INFO, "detected and replaced " 722 // + appData.getLastFoundTasks().size() + " tasks occuring " 723 // + appData.getLastFoundTasks().getOccurrenceCount() + " times"); 724 } 677 725 678 726 /** … … 684 732 appData.detectedAndReplacedTasks(false); 685 733 686 /* 687 Console.traceln(Level.FINER, "replacing tasks occurrences"); 688 689 for (List<ITaskInstance> task : appData.getLastFoundTasks()) { 690 ISequence sequence = taskFactory.createNewSequence(); 691 692 Console.traceln(Level.FINEST, "replacing " + sequence.getId() 693 + ": " + task); 694 695 List<ISequenceInstance> sequenceInstances = replaceTaskOccurrences( 696 task, appData.getSessions(), sequence); 697 698 harmonizeSequenceInstancesModel(sequence, sequenceInstances, 699 task.size()); 700 appData.detectedAndReplacedTasks(appData 701 .detectedAndReplacedTasks() 702 || (sequenceInstances.size() > 0)); 703 704 if (sequenceInstances.size() < appData.getLastFoundTasks() 705 .getOccurrenceCount()) { 706 Console.traceln(Level.FINE, 707 sequence.getId() 708 + ": replaced task only " 709 + sequenceInstances.size() 710 + " times instead of expected " 711 + appData.getLastFoundTasks() 712 .getOccurrenceCount()); 713 } 714 } 715 */ 716 } 717 734 /* 735 * Console.traceln(Level.FINER, "replacing tasks occurrences"); 736 * 737 * for (List<ITaskInstance> task : appData.getLastFoundTasks()) { 738 * ISequence sequence = taskFactory.createNewSequence(); 739 * 740 * Console.traceln(Level.FINEST, "replacing " + sequence.getId() + ": " 741 * + task); 742 * 743 * List<ISequenceInstance> sequenceInstances = replaceTaskOccurrences( 744 * task, appData.getSessions(), sequence); 745 * 746 * harmonizeSequenceInstancesModel(sequence, sequenceInstances, 747 * task.size()); appData.detectedAndReplacedTasks(appData 748 * .detectedAndReplacedTasks() || (sequenceInstances.size() > 0)); 749 * 750 * if (sequenceInstances.size() < appData.getLastFoundTasks() 751 * .getOccurrenceCount()) { Console.traceln(Level.FINE, sequence.getId() 752 * + ": replaced task only " + sequenceInstances.size() + 753 * " times instead of expected " + appData.getLastFoundTasks() 754 * .getOccurrenceCount()); } } 755 */ 756 } 718 757 719 758 /** … … 847 886 } 848 887 849 850 888 /** 851 889 * … … 853 891 private static class RuleApplicationData { 854 892 855 856 private HashMap<Integer,ITask> number2task; 857 858 893 private HashMap<Integer, ITask> number2task; 894 859 895 private ArrayList<NumberSequence> numberseqs; 860 896 861 897 /** 862 898 * 863 899 */ 864 900 private List<IUserSession> sessions; 865 866 867 901 868 902 /** … … 887 921 this.sessions = sessions; 888 922 numberseqs = new ArrayList<NumberSequence>(); 889 number2task = new HashMap<Integer, ITask>();890 stopWatch = new StopWatch();891 result = 923 number2task = new HashMap<Integer, ITask>(); 924 stopWatch = new StopWatch(); 925 result = new RuleApplicationResult(); 892 926 } 893 927 … … 899 933 } 900 934 901 902 935 private ArrayList<NumberSequence> getNumberSequences() { 903 936 return numberseqs; 904 937 } 905 906 907 938 908 939 /** … … 934 965 } 935 966 936 private HashMap<Integer, ITask> getNumber2Task() {967 private HashMap<Integer, ITask> getNumber2Task() { 937 968 return number2task; 938 969 } … … 940 971 } 941 972 942 943 944 945 973 }
Note: See TracChangeset
for help on using the changeset viewer.