Changeset 1730 for branches/autoquest-core-tasktrees-alignment/src
- Timestamp:
- 09/05/14 17:32:57 (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
r1729 r1730 232 232 detectAndReplaceIterations(appData); 233 233 appData.getStopWatch().start("task replacement"); 234 appData.updateSubstitutionMatrix();234 //appData.updateSubstitutionMatrix(); 235 235 detectAndReplaceTasks(appData); // 236 236 appData.getStopWatch().stop("task replacement"); … … 670 670 // Replace matches in the sessions 671 671 Console.traceln(Level.INFO, "Replacing matches in sessions"); 672 replaceMatches(appData); 672 673 appData.getStopWatch().start("replacing tasks"); 673 HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>(); 674 for (int i = 0; i < appData.getMatchseqs().size(); i++) { 675 676 // Every pattern consists of 2 sequences, therefore the minimum 677 // occurrences here is 2. 678 // We just need the sequences also occurring in other sequences as 679 // well 680 if (appData.getMatchseqs().get(i).occurenceCount() > 2) { 681 appData.detectedAndReplacedTasks = true; 682 ISequence task = matchAsSequence(appData, appData 683 .getMatchseqs().get(i)); 684 invalidOccurence: for (Iterator<MatchOccurence> it = appData 685 .getMatchseqs().get(i).getOccurences().iterator(); it 686 .hasNext();) { 687 MatchOccurence oc = it.next(); 688 689 // Check if nothing has been replaced in the sequence we 690 // want to replace 691 if (replacedOccurences.get(oc.getSequenceId()) == null) { 692 replacedOccurences.put(oc.getSequenceId(), 693 new LinkedList<MatchOccurence>()); 694 } else { 695 // check if we have any replaced occurence with indexes 696 // smaller than ours. If so, we need to adjust our start 697 // and endpoints 698 // of the replacement. 699 // Also do a check if we have replaced this specific 700 // MatchOccurence in this sequence already. Jump to the 701 // next occurence if this is the case. 702 // This is no more neccessary once the matches are 703 // harmonized. 704 for (Iterator<MatchOccurence> jt = replacedOccurences 705 .get(oc.getSequenceId()).iterator(); jt 706 .hasNext();) { 707 MatchOccurence tmpOC = jt.next(); 708 709 if (oc.getStartindex() >= tmpOC.getStartindex() 710 && oc.getStartindex() <= tmpOC 711 .getEndindex()) { 712 continue invalidOccurence; 713 } 714 if (oc.getEndindex() >= tmpOC.getStartindex()) { 715 continue invalidOccurence; 716 717 } else if (oc.getStartindex() > tmpOC.getEndindex()) { 718 int diff = tmpOC.getEndindex() 719 - tmpOC.getStartindex(); 720 // Just to be sure. 721 if (diff > 0) { 722 oc.setStartindex(oc.getStartindex() - diff 723 + 1); 724 oc.setEndindex(oc.getEndindex() - diff + 1); 725 } else { 726 Console.traceln(Level.WARNING, 727 "End index of a Match before start. This should never happen"); 728 } 729 } 730 } 731 } 732 System.out.println("Replacing in sequence" 733 + oc.getSequenceId()); 734 ISequenceInstance sequenceInstances = RuleUtils 735 .createNewSubSequenceInRange(appData.getSessions() 736 .get(oc.getSequenceId()), oc 737 .getStartindex(), oc.getEndindex(), task, 738 taskFactory, taskBuilder); 739 // Adjust the length of the match regarding to the length of 740 // instance. (OptionalInstances may be shorter) 741 oc.setEndindex(oc.getStartindex() 742 + sequenceInstances.size() 743 - RuleUtils.missedOptionals); 744 replacedOccurences.get(oc.getSequenceId()).add(oc); 745 } 746 } 747 } 674 748 675 // appData.setMatchseqs(null); 749 676 appData.getStopWatch().stop("replacing tasks"); 750 677 } 751 678 752 private void replaceMatches() { 753 754 } 755 756 private void searchMatchesInAllSessions(RuleApplicationData appData) { 757 Console.traceln(Level.INFO, 758 "searching for patterns occuring most with " + nThreads 759 + " threads"); 760 // Prepare parallel search of matchseqs 761 ExecutorService executor = Executors.newFixedThreadPool(nThreads); 679 private void replaceMatches(RuleApplicationData appData) { 680 appData.replacedOccurences = new HashMap<Integer, List<MatchOccurence>>(); 681 762 682 int matchSeqSize = appData.getMatchseqs().size(); 763 int interval = matchSeqSize / nThreads; 764 int rest = matchSeqSize % nThreads; 765 683 int newThreads = nThreads; 684 if (matchSeqSize < nThreads) { 685 newThreads = matchSeqSize; 686 } 687 ExecutorService executor = Executors.newFixedThreadPool(newThreads); 688 int interval = matchSeqSize / newThreads; 689 int rest = matchSeqSize % newThreads; 690 766 691 for (int i = 0; i < matchSeqSize - interval; i += interval) { 767 692 int offset = 0; … … 772 697 int from = i; 773 698 int to = i + interval + offset; 774 System.out.println(" Creating thread with matches from " + from699 System.out.println("Replacement: Creating thread with matches from " + from 775 700 + " to " + to); 776 701 // search each match in every other sequence 777 ParallelMatch OcurrencesFinder finder = new ParallelMatchOcurrencesFinder(702 ParallelMatchReplacer replacer = new ParallelMatchReplacer( 778 703 appData, from, to); 779 executor.execute( finder);704 executor.execute(replacer); 780 705 } 781 706 executor.shutdown(); … … 786 711 e.printStackTrace(); 787 712 } 713 } 714 715 private void searchMatchesInAllSessions(RuleApplicationData appData) { 716 Console.traceln(Level.INFO, 717 "searching for patterns occuring most with " + nThreads 718 + " threads"); 719 // Prepare parallel search of matchseqs 720 721 int matchSeqSize = appData.getMatchseqs().size(); 722 int newThreads = nThreads; 723 if (matchSeqSize < nThreads) { 724 newThreads = matchSeqSize; 725 } 726 int interval = matchSeqSize / newThreads; 727 int rest = matchSeqSize % newThreads; 728 ExecutorService executor = Executors.newFixedThreadPool(nThreads); 729 730 for (int i = 0; i < matchSeqSize - interval; i += interval) { 731 int offset = 0; 732 if (rest != 0) { 733 offset = 1; 734 rest--; 735 } 736 int from = i; 737 int to = i + interval + offset; 738 System.out.println("Match finding: Creating thread with matches from " + from 739 + " to " + to); 740 // search each match in every other sequence 741 ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder( 742 appData, from, to); 743 executor.execute(finder); 744 } 745 executor.shutdown(); 746 try { 747 executor.awaitTermination(2, TimeUnit.HOURS); 748 } catch (InterruptedException e) { 749 // TODO Auto-generated catch block 750 e.printStackTrace(); 751 } 788 752 789 753 } 790 754 791 755 // Print out the progress 792 private static void printProgressPercentage( int count, int size) {756 private static void printProgressPercentage(String message,int count, int size) { 793 757 if (size > 100) { 794 758 if ((count % (size / 100) == 0)) { … … 796 760 // Thread.currentThread().getName() + ": " + Math.round((float) 797 761 // count/size*100))+ "%"); 798 System.out.println( "Thread" + Thread.currentThread().getName()762 System.out.println(message + " in thread" + Thread.currentThread().getName() 799 763 + ": " + Math.round((float) count / size * 100) + "%"); 800 764 } … … 803 767 // Thread.currentThread().getName() + ": " +Math.round((float) 804 768 // count/size*100))+ "%"); 805 System.out.println( "Thread" + Thread.currentThread().getName()769 System.out.println(message + " in thread" + Thread.currentThread().getName() 806 770 + ": " + Math.round((float) count / size * 100) + "%"); 807 771 … … 811 775 private class ParallelMatchReplacer implements Runnable { 812 776 777 private final RuleApplicationData appData; 778 private final int from; 779 private final int to; 780 781 ParallelMatchReplacer(RuleApplicationData appData, int from, 782 int to) { 783 this.appData = appData; 784 this.from = from; 785 this.to = to; 786 } 813 787 @Override 814 788 public void run() { 815 816 } 817 818 } 789 int count = 0; 790 int size = to - from; 791 for (int i = from; i < to; i++) { 792 count++; 793 // Every pattern consists of 2 sequences, therefore the minimum 794 // occurrences here is 2. 795 // We just need the sequences also occurring in other sequences as well 796 if (appData.getMatchseqs().get(i).occurenceCount() > 2) { 797 798 ISequence task = matchAsSequence(appData, appData.getMatchseqs().get(i)); 799 invalidOccurence: for (Iterator<MatchOccurence> it = appData 800 .getMatchseqs().get(i).getOccurences().iterator(); it 801 .hasNext();) { 802 MatchOccurence oc = it.next(); 803 804 // Check if nothing has been replaced in the sequence we 805 // want to replace now 806 807 synchronized (appData.getReplacedOccurences()) { 808 if (appData.getReplacedOccurences().get(oc.getSequenceId()) == null) { 809 appData.getReplacedOccurences().put(oc.getSequenceId(), 810 new LinkedList<MatchOccurence>()); 811 } else { 812 // check if we have any replaced occurence with indexes 813 // smaller than ours. If so, we need to adjust our start 814 // and endpoints 815 // of the replacement. 816 // Also do a check if we have replaced this specific 817 // MatchOccurence in this sequence already. Jump to the 818 // next occurence if this is the case. 819 // This is no more neccessary once the matches are 820 // harmonized. 821 for (Iterator<MatchOccurence> jt = appData.getReplacedOccurences() 822 .get(oc.getSequenceId()).iterator(); jt 823 .hasNext();) { 824 MatchOccurence tmpOC = jt.next(); 825 826 if (oc.getStartindex() >= tmpOC.getStartindex() 827 && oc.getStartindex() <= tmpOC 828 .getEndindex()) { 829 continue invalidOccurence; 830 } 831 if (oc.getEndindex() >= tmpOC.getStartindex()) { 832 continue invalidOccurence; 833 834 } else if (oc.getStartindex() > tmpOC.getEndindex()) { 835 int diff = tmpOC.getEndindex() 836 - tmpOC.getStartindex(); 837 // Just to be sure. 838 if (diff > 0) { 839 oc.setStartindex(oc.getStartindex() - diff 840 + 1); 841 oc.setEndindex(oc.getEndindex() - diff + 1); 842 } else { 843 Console.traceln(Level.WARNING, 844 "End index of a Match before start. This should never happen"); 845 } 846 } 847 } 848 } 849 System.out.println("Replacing in sequence" 850 + oc.getSequenceId()); 851 synchronized(appData) { 852 appData.detectedAndReplacedTasks = true; 853 } 854 synchronized (appData.getSessions().get(oc.getSequenceId())) { 855 ISequenceInstance sequenceInstances = RuleUtils 856 .createNewSubSequenceInRange(appData.getSessions() 857 .get(oc.getSequenceId()), oc 858 .getStartindex(), oc.getEndindex(), task, 859 taskFactory, taskBuilder); 860 oc.setEndindex(oc.getStartindex() 861 + sequenceInstances.size() 862 - RuleUtils.missedOptionals); 863 } 864 } 865 // Adjust the length of the match regarding to the length of 866 // instance. (OptionalInstances may be shorter) 867 868 synchronized(appData.getReplacedOccurences().get(oc.getSequenceId())) { 869 appData.getReplacedOccurences().get(oc.getSequenceId()).add(oc); 870 } 871 } 872 } 873 } 874 } 875 } 876 877 819 878 820 879 private class ParallelMatchOcurrencesFinder implements Runnable { … … 838 897 Match pattern = appData.getMatchseqs().get(i); 839 898 count++; 840 printProgressPercentage( count, size);899 printProgressPercentage("Match finding progress",count, size); 841 900 // Skip sequences with more 0 events (scrolls) than other 842 901 // events. … … 885 944 NumberSequence ns1 = appData.getNumberSequences().get(i); 886 945 count++; 887 printProgressPercentage( count, size);946 printProgressPercentage("Aligning Progress",count, size); 888 947 for (int j = 0; j < appData.getNumberSequences().size(); j++) { 889 948 NumberSequence ns2 = appData.getNumberSequences().get(j); … … 908 967 Console.traceln(Level.INFO, "generating pairwise alignments from " 909 968 + numberSeqSize + " sessions with " + nThreads + " threads"); 910 ExecutorService executor = Executors.newFixedThreadPool(nThreads); 911 int interval = numberSeqSize / nThreads; 912 int rest = numberSeqSize % nThreads; 969 970 int newThreads = nThreads; 971 if (numberSeqSize < nThreads) { 972 newThreads = numberSeqSize; 973 } 974 975 ExecutorService executor = Executors.newFixedThreadPool(newThreads); 976 int interval = numberSeqSize / newThreads; 977 int rest = numberSeqSize % newThreads; 913 978 914 979 for (int i = 0; i < numberSeqSize - interval; i += interval) { … … 933 998 e.printStackTrace(); 934 999 } 935 936 1000 } 937 1001 … … 952 1016 private HashSet<ITask> uniqueTasks; 953 1017 954 ObjectDistanceSubstitionMatrix submat;1018 private ObjectDistanceSubstitionMatrix submat; 955 1019 1020 public HashMap<Integer, List<MatchOccurence>> replacedOccurences; 956 1021 957 958 LinkedList<Match> matchseqs;1022 1023 public LinkedList<Match> matchseqs; 959 1024 960 1025 private ArrayList<NumberSequence> numberseqs; … … 1005 1070 } 1006 1071 1007 private void resetNewlyCreatedTasks() {1072 synchronized private void resetNewlyCreatedTasks() { 1008 1073 uniqueTasks.addAll(newTasks); 1009 1074 newTasks.clear(); … … 1021 1086 return sessions; 1022 1087 } 1088 1089 1090 public HashMap<Integer, List<MatchOccurence>> getReplacedOccurences() { 1091 return replacedOccurences; 1092 } 1093 1094 public void setReplacedOccurences( 1095 HashMap<Integer, List<MatchOccurence>> replacedOccurences) { 1096 this.replacedOccurences = replacedOccurences; 1097 } 1098 1023 1099 1024 1100 private HashSet<ITask> getUniqueTasks() {
Note: See TracChangeset
for help on using the changeset viewer.