Changeset 1742 for branches/autoquest-core-tasktrees-alignment/src/main
- Timestamp:
- 09/12/14 14:30:21 (10 years ago)
- Location:
- branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/algorithms/Match.java
r1741 r1742 13 13 * The Class Match. 14 14 */ 15 public class Match implements C omparable<Match>,Serializable {15 public class Match implements Cloneable,Comparable<Match>,Serializable { 16 16 17 17 /** The Constant serialVersionUID. */ … … 133 133 134 134 /** 135 * Oc urrence id sum. Used for comparing and sorting matches135 * Occurrence id sum. Used for comparing and sorting matches 136 136 * 137 * @return th 137 * @return the sum of all occurrence ids. 138 138 */ 139 139 public int ocurrenceIDSum() { … … 168 168 } 169 169 170 171 170 172 /** 171 173 * Size. -
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java
r1741 r1742 59 59 * @return the replacement for the range 60 60 */ 61 static ISequenceInstance createNewSubSequenceInRange(61 static synchronized ISequenceInstance createNewSubSequenceInRange( 62 62 ITaskInstanceList parent, int startIndex, int endIndex, 63 63 ISequence model, ITaskFactory taskFactory, ITaskBuilder taskBuilder) { -
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
r1741 r1742 74 74 public class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule { 75 75 76 77 76 /** The n threads. */ 78 77 public static int nThreads = Runtime.getRuntime().availableProcessors() - 1; … … 158 157 detectAndReplaceIterations(appData); 159 158 appData.getStopWatch().start("task replacement"); 160 //Just does anything if the substitution matrix is created with the option to do so 159 // Just does anything if the substitution matrix is created with the 160 // option to do so 161 161 appData.updateSubstitutionMatrix(); 162 162 detectAndReplaceTasks(appData); // 163 163 appData.getStopWatch().stop("task replacement"); 164 164 appData.getStopWatch().stop("whole loop"); 165 // appData.getStopWatch().dumpStatistics(System.out);165 // appData.getStopWatch().dumpStatistics(System.out); 166 166 appData.getStopWatch().reset(); 167 167 … … 179 179 RuleApplicationStatus.FINISHED); 180 180 } 181 // new TaskTreeValidator().validate(appData.getSessions());181 // new TaskTreeValidator().validate(appData.getSessions()); 182 182 return appData.getResult(); 183 183 } … … 257 257 Console.traceln(Level.FINE, "Found " + appData.getMatchseqs().size() 258 258 + " results"); 259 259 260 //Nothing found, we can end here 261 if(appData.getMatchseqs().size()==0) { 262 appData.detectedAndReplacedTasks=false; 263 return; 264 } 265 260 266 // Searching each match in all other sessions, counting its occurences 261 267 searchMatchesInAllSessions(appData); … … 268 274 public int compare(Match m1, Match m2) { 269 275 int cmp = m2.occurenceCount() - m1.occurenceCount(); 270 if (cmp != 0) {276 if (cmp != 0) { 271 277 return cmp; 272 } 273 else { 274 cmp = m2.size()-m1.size(); 275 if(cmp != 0) { 278 } else { 279 cmp = m2.size() - m1.size(); 280 if (cmp != 0) { 276 281 return cmp; 277 } 278 else { 279 //This should rarely happen 280 cmp = m2.ocurrenceIDSum()-m1.ocurrenceIDSum(); 281 if(cmp !=0) { 282 } else { 283 // This should rarely happen 284 cmp = m2.ocurrenceIDSum() - m1.ocurrenceIDSum(); 285 if (cmp != 0) { 282 286 return cmp; 283 } 284 else { 285 cmp = m2.taskIdSum()-m1.taskIdSum(); 286 287 } else { 288 cmp = m2.taskIdSum() - m1.taskIdSum(); 289 287 290 return cmp; 288 291 } … … 291 294 } 292 295 }; 293 296 294 297 Collections.sort(appData.getMatchseqs(), comparator); 295 298 appData.getStopWatch().stop("detecting tasks"); 296 297 298 // Replace matches in the sessions299 300 Console.traceln(Level.INFO, "Preparing replacments"); 301 prepareReplacements(appData); 299 302 Console.traceln(Level.INFO, "Replacing matches in sessions"); 300 303 appData.getStopWatch().start("replacing tasks"); … … 303 306 } 304 307 305 306 //TODO: DEBUG METHOD 308 // TODO: DEBUG METHOD 307 309 @SuppressWarnings("unused") 308 310 private void printMatches(RuleApplicationData appData) { 309 311 LinkedList<Match> matchseqs = appData.getMatchseqs(); 310 if (iteration>1) {311 System.out.println("PRINTING MATCHES");312 for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) {313 Match m = it.next();314 m.getFirstSequence().printSequence();315 m.getSecondSequence().printSequence();316 for(Iterator<MatchOccurrence> jt = m.getOccurences().iterator();jt.hasNext();) {317 MatchOccurrence mo = jt.next();318 System.out.print(mo.getSequenceId() + " ");319 }320 System.out.println();321 System.out.println();322 } 323 }324 }325 326 312 if (iteration > 1) { 313 System.out.println("PRINTING MATCHES"); 314 for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) { 315 Match m = it.next(); 316 m.getFirstSequence().printSequence(); 317 m.getSecondSequence().printSequence(); 318 for (Iterator<MatchOccurrence> jt = m.getOccurences() 319 .iterator(); jt.hasNext();) { 320 MatchOccurrence mo = jt.next(); 321 System.out.print(mo.getSequenceId() + " "); 322 } 323 System.out.println(); 324 System.out.println(); 325 } 326 } 327 } 328 327 329 /** 328 330 * <p> … … 557 559 i++; 558 560 } 559 560 561 562 563 564 561 if ((i == (first.length - 1)) && selectionfound) { 562 taskBuilder.addChild(subsequence1, appData 563 .getNumber2Task().get(first[i])); 564 taskBuilder.addChild(subsequence2, appData 565 .getNumber2Task().get(second[i])); 566 } 565 567 } 566 568 } else { … … 659 661 private void prepareReplacements(RuleApplicationData appData) { 660 662 appData.initializeQueues(appData.getSessions().size()); 661 final int matchSeqSize = appData.getMatchseqs().size();662 int count = 0;663 for (Iterator<Match> it = appData.getMatchseqs().iterator(); it.hasNext();) {663 664 for (Iterator<Match> it = appData.getMatchseqs().iterator(); it 665 .hasNext();) { 664 666 Match m = it.next(); 665 for(Iterator<MatchOccurrence> jt=m.getOccurences().iterator();jt.hasNext();) { 667 for (Iterator<MatchOccurrence> jt = m.getOccurences().iterator(); jt 668 .hasNext();) { 666 669 MatchOccurrence mo = jt.next(); 667 670 668 671 Match emptyMatch = null; 669 672 try { … … 675 678 appData.getPlannedReplacements()[mo.getSequenceId()].add(m); 676 679 } 677 count++; 678 679 } 680 } 681 680 } 681 } 682 682 683 /** 683 684 * Replace matches. … … 687 688 */ 688 689 private void replaceMatches(RuleApplicationData appData) { 689 appData.setReplacedOccurences(new HashMap<Integer, List<MatchOccurrence>>()); 690 691 final int matchSeqSize = appData.getMatchseqs().size(); 692 int count = 0; 693 for (int i = 0; i < matchSeqSize; i++) { 694 count++; 695 // Every pattern consists of 2 sequences, therefore the minimum 696 // occurrences here is 2. 697 // We just need the sequences also occurring in other sequences 698 // as well 699 RuleUtils.printProgressPercentage("Replacement progress", count, matchSeqSize); 700 if (appData.getMatchseqs().get(i).occurenceCount() > 2) { 701 702 final ISequence task = matchAsSequence(appData, appData 703 .getMatchseqs().get(i)); 704 invalidOccurence: for (final Iterator<MatchOccurrence> it = appData 705 .getMatchseqs().get(i).getOccurences().iterator(); it 706 .hasNext();) { 707 final MatchOccurrence oc = it.next(); 708 709 // Check if nothing has been replaced in the sequence we 710 // want to replace now 711 if (appData.getReplacedOccurrences() 712 .get(oc.getSequenceId()) == null) { 713 appData.getReplacedOccurrences().put( 714 oc.getSequenceId(), 715 new LinkedList<MatchOccurrence>()); 716 } else { 717 // check if we have any replaced occurence with 718 // indexes 719 // smaller than ours. If so, we need to adjust 720 // our start 721 // and endpoints 722 // of the replacement. 723 // Also do a check if we have replaced this 724 // specific 725 // MatchOccurence in this sequence already. Jump 726 // to the 727 // next occurence if this is the case. 728 // This is no more neccessary once the matches 729 // are 730 // harmonized. 731 732 for (final Iterator<MatchOccurrence> jt = appData 733 .getReplacedOccurrences() 734 .get(oc.getSequenceId()).iterator(); jt 735 .hasNext();) { 736 final MatchOccurrence tmpOC = jt.next(); 737 738 if ((oc.getStartindex() >= tmpOC.getStartindex()) 739 && (oc.getStartindex() <= tmpOC 740 .getEndindex())) { 741 continue invalidOccurence; 742 } 743 if (oc.getEndindex() >= tmpOC.getStartindex()) { 744 continue invalidOccurence; 745 746 } else if (oc.getStartindex() > tmpOC.getEndindex()) { 747 final int diff = tmpOC.getEndindex() 748 - tmpOC.getStartindex(); 749 // Just to be sure. 750 if (diff > 0) { 751 oc.setStartindex((oc.getStartindex() - diff) + 1); 752 oc.setEndindex((oc.getEndindex() - diff) + 1); 753 } else { 754 Console.traceln(Level.WARNING, 755 "End index of a Match before start. This should never happen"); 756 } 757 } 758 } 759 } 760 appData.detectedAndReplacedTasks = true; 761 final ISequenceInstance sequenceInstances = RuleUtils 762 .createNewSubSequenceInRange(appData.getSessions() 763 .get(oc.getSequenceId()), oc 764 .getStartindex(), oc.getEndindex(), task, 765 taskFactory, taskBuilder); 766 oc.setEndindex((oc.getStartindex() + sequenceInstances 767 .size()) - RuleUtils.missedOptionals); 768 769 // Adjust the length of the match regarding to the 770 // length of 771 // instance. (OptionalInstances may be shorter) 772 773 appData.getReplacedOccurrences().get(oc.getSequenceId()) 774 .add(oc); 775 } 776 } 690 691 final int numberSeqSize = appData.getNumberSequences().size(); 692 Console.traceln(Level.INFO, "replacing matches with " + nThreads + " threads"); 693 int newThreads = nThreads; 694 if (numberSeqSize < nThreads) { 695 newThreads = numberSeqSize; 696 } 697 final int interval = numberSeqSize / newThreads; 698 int rest = numberSeqSize % newThreads; 699 final ExecutorService executor = Executors.newFixedThreadPool(nThreads); 700 for (int i = 0; i <= (numberSeqSize - interval); i += interval) { 701 int offset = 0; 702 if (rest != 0) { 703 offset = 1; 704 rest--; 705 } 706 final int from = i; 707 final int to = i + interval + offset - 1; 708 Console.traceln(Level.FINE, 709 "Match replaceing: Creating thread with matches from " + from 710 + " to " + to); 711 // search each match in every other sequence 712 final ParallelMatchReplacer replacer = new ParallelMatchReplacer( 713 appData, from, to); 714 executor.execute(replacer); 715 i += offset; 716 } 717 executor.shutdown(); 718 try { 719 executor.awaitTermination(2, TimeUnit.HOURS); 720 } catch (final InterruptedException e) { 721 e.printStackTrace(); 777 722 } 778 723 } … … 833 778 rest--; 834 779 } 835 780 836 781 final int from = i; 837 final int to = i + interval+offset-1; 838 Console.traceln(Level.FINER, "Aligning: Creating thread for sessions " + from + " till " + to); 782 final int to = i + interval + offset - 1; 783 Console.traceln(Level.FINER, 784 "Aligning: Creating thread for sessions " + from + " till " 785 + to); 839 786 final ParallelPairwiseAligner aligner = new ParallelPairwiseAligner( 840 787 appData, from, to); 841 788 executor.execute(aligner); 842 i +=offset;789 i += offset; 843 790 } 844 791 executor.shutdown(); … … 850 797 } 851 798 852 853 854 855 799 /** 856 800 * Search matches in all sessions. … … 860 804 */ 861 805 private void searchMatchesInAllSessions(RuleApplicationData appData) { 862 806 863 807 // Prepare parallel search of matchseqs 864 808 final int matchSeqSize = appData.getMatchseqs().size(); 865 Console.traceln(Level.INFO, 866 "searching for patterns ("+ matchSeqSize+") occuring most with " + nThreads 867 + " threads"); 809 Console.traceln(Level.INFO, "searching for patterns (" + matchSeqSize 810 + ") occuring most with " + nThreads + " threads"); 868 811 int newThreads = nThreads; 869 812 if (matchSeqSize < nThreads) { … … 874 817 final ExecutorService executor = Executors.newFixedThreadPool(nThreads); 875 818 Console.traceln(Level.FINER, "Interval: " + interval + " Rest: " + rest); 876 for (int i = 0; i <= (matchSeqSize -interval); i += interval) {819 for (int i = 0; i <= (matchSeqSize - interval); i += interval) { 877 820 int offset = 0; 878 821 if (rest != 0) { … … 881 824 } 882 825 final int from = i; 883 final int to = i + interval + offset-1; 884 Console.traceln(Level.FINE, "Match finding: Creating thread with matches from " 885 + from + " to " + to); 826 final int to = i + interval + offset - 1; 827 Console.traceln(Level.FINE, 828 "Match finding: Creating thread with matches from " + from 829 + " to " + to); 886 830 // search each match in every other sequence 887 831 final ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder( 888 832 appData, from, to); 889 833 executor.execute(finder); 890 i +=offset;834 i += offset; 891 835 } 892 836 executor.shutdown(); … … 939 883 this.to = to; 940 884 } 885 941 886 /* 942 887 * (non-Javadoc) … … 947 892 public void run() { 948 893 int count = 0; 949 final int size = to - from;894 final int size = to + 1 - from; 950 895 951 896 for (int i = from; i <= to; i++) { … … 981 926 } 982 927 } 983 984 985 928 986 929 /** … … 1022 965 public void run() { 1023 966 int count = 0; 1024 final int size = to - from;967 final int size = to +1 - from; 1025 968 1026 969 for (int i = from; i <= to; i++) { … … 1044 987 } 1045 988 } 1046 989 990 /** 991 * The Class ParallelPairwiseAligner. 992 */ 993 private class ParallelMatchReplacer implements Runnable { 994 995 /** The app data. */ 996 private final RuleApplicationData appData; 997 998 /** The from. */ 999 private final int from; 1000 1001 /** The to. */ 1002 private final int to; 1003 1004 /** 1005 * Instantiates a new parallel pairwise aligner. 1006 * 1007 * @param appData 1008 * the app data 1009 * @param from 1010 * the from 1011 * @param to 1012 * the to 1013 */ 1014 ParallelMatchReplacer(RuleApplicationData appData, int from, int to) { 1015 this.appData = appData; 1016 this.from = from; 1017 this.to = to; 1018 } 1019 1020 /* 1021 * (non-Javadoc) 1022 * 1023 * @see java.lang.Runnable#run() 1024 */ 1025 @Override 1026 public void run() { 1027 for (int i = from; i <= to; i++) { 1028 1029 /* 1030 * HashMap for keeping track in which sequence which replacement has 1031 * been performed. Neccessary for updating the indices of other 1032 * occurrences accordingly 1033 */ 1034 LinkedList<MatchOccurrence> replacedOccurrences = new LinkedList<MatchOccurrence>(); 1035 invalidOccurence: while (!appData.getPlannedReplacements()[i] 1036 .isEmpty()) { 1037 1038 Match m = appData.getPlannedReplacements()[i].remove(); 1039 // Occurrences list has just one child 1040 MatchOccurrence oc = m.getOccurences().getFirst(); 1041 // check if we have any replaced occurrence with 1042 // indexes 1043 // smaller than ours. If so, we need to adjust 1044 // our start and end points of the replacement. 1045 // Also do a check if we have replaced this 1046 // specific MatchOccurence in this sequence already. 1047 // Jump to the next occurrence if this is the case. 1048 // This is no more necessary once the matches 1049 // are harmonized. 1050 1051 for (final Iterator<MatchOccurrence> jt = replacedOccurrences 1052 .iterator(); jt.hasNext();) { 1053 final MatchOccurrence tmpOC = jt.next(); 1054 1055 if ((oc.getStartindex() >= tmpOC.getStartindex()) 1056 && (oc.getStartindex() <= tmpOC.getEndindex())) { 1057 continue invalidOccurence; 1058 } 1059 if (oc.getEndindex() >= tmpOC.getStartindex()) { 1060 continue invalidOccurence; 1061 1062 } else if (oc.getStartindex() > tmpOC.getEndindex()) { 1063 final int diff = tmpOC.getEndindex() 1064 - tmpOC.getStartindex(); 1065 // Just to be sure. 1066 if (diff > 0) { 1067 oc.setStartindex((oc.getStartindex() - diff) + 1); 1068 oc.setEndindex((oc.getEndindex() - diff) + 1); 1069 } else { 1070 Console.traceln(Level.WARNING, 1071 "End index of a Match before start. This should never happen"); 1072 } 1073 } 1074 } 1075 synchronized (appData) { 1076 appData.detectedAndReplacedTasks = true; 1077 } 1078 final ISequence task = matchAsSequence(appData, m); 1079 final ISequenceInstance sequenceInstances = RuleUtils 1080 .createNewSubSequenceInRange(appData.getSessions() 1081 .get(oc.getSequenceId()), oc 1082 .getStartindex(), oc.getEndindex(), task, 1083 taskFactory, taskBuilder); 1084 1085 // Adjust the length of the match regarding to the 1086 // length of 1087 // instance. (OptionalInstances may be shorter) 1088 oc.setEndindex((oc.getStartindex() + sequenceInstances 1089 .size()) - RuleUtils.missedOptionals); 1090 1091 replacedOccurrences.add(oc); 1092 } 1093 } 1094 } 1095 } 1096 1047 1097 /** 1048 1098 * The Class RuleApplicationData. … … 1072 1122 private final ObjectDistanceSubstitionMatrix submat; 1073 1123 1074 /** 1075 * HashMap for keeping track in which sequence which replacement has 1076 * been performed. Neccessary for updating the indices of other 1077 * occurrences accordingly 1078 */ 1079 private HashMap<Integer, List<MatchOccurrence>> replacedOccurences; 1080 1081 private Queue<Match>[] plannedReplacements; 1082 1124 private PriorityQueue<Match>[] plannedReplacements; 1083 1125 1084 1126 /** The list of all found matches */ … … 1105 1147 /** Stop Watch to measure performance */ 1106 1148 private final StopWatch stopWatch; 1107 1108 1109 1149 1110 1150 /** … … 1125 1165 this.detectedAndReplacedTasks = true; 1126 1166 } 1127 1167 1128 1168 private void initializeQueues(int size) { 1129 plannedReplacements = new Queue[size];1130 for (int i=0;i<size;i++) {1169 plannedReplacements = new PriorityQueue[size]; 1170 for (int i = 0; i < size; i++) { 1131 1171 plannedReplacements[i] = new PriorityQueue<Match>(); 1132 1172 } … … 1137 1177 } 1138 1178 1139 public void setPlannedReplacements(Queue<Match>[] plannedReplacements) {1140 this.plannedReplacements = plannedReplacements;1141 }1142 1143 1179 /** 1144 1180 * Detected and replaced tasks. … … 1151 1187 1152 1188 /** 1153 * Gets the match seqs.1154 * 1155 * @return the match seqs1189 * Gets the match sequences. 1190 * 1191 * @return the match sequences 1156 1192 */ 1157 1193 public LinkedList<Match> getMatchseqs() { … … 1187 1223 1188 1224 /** 1189 * Gets the replaced occurrences.1190 *1191 * @return the replaced occurences1192 */1193 public HashMap<Integer, List<MatchOccurrence>> getReplacedOccurrences() {1194 return replacedOccurences;1195 }1196 1197 /**1198 1225 * Gets the result. 1199 1226 * … … 1223 1250 1224 1251 /** 1225 * Gets the sub mat.1226 * 1227 * @return the sub mat1252 * Gets the substitution matrix. 1253 * 1254 * @return the substitution matrix 1228 1255 */ 1229 1256 private ObjectDistanceSubstitionMatrix getSubmat() { … … 1244 1271 * 1245 1272 * @param task 1246 * can be called when new tasks are created to keep track of all newly created tasks 1273 * can be called when new tasks are created to keep track of 1274 * all newly created tasks 1247 1275 */ 1248 1276 private void newTaskCreated(ITask task) { … … 1270 1298 1271 1299 /** 1272 * Sets the replaced occurences.1273 *1274 * @param replacedOccurences1275 * the replaced occurences1276 */1277 public void setReplacedOccurences(1278 HashMap<Integer, List<MatchOccurrence>> replacedOccurences) {1279 this.replacedOccurences = replacedOccurences;1280 }1281 1282 /**1283 1300 * Update substitution matrix. 1284 1301 */ … … 1290 1307 } 1291 1308 1292 1293 1294 1309 }
Note: See TracChangeset
for help on using the changeset viewer.