Changeset 1256 for trunk/autoquest-core-tasktrees/src/main/java/de
- Timestamp:
- 07/24/13 07:45:47 (11 years ago)
- Location:
- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRule.java
r1196 r1256 15 15 package de.ugoe.cs.autoquest.tasktrees.temporalrelation; 16 16 17 import java.io.IOException;18 import java.io.ObjectInputStream;19 import java.util.HashMap;20 17 import java.util.Iterator; 21 18 import java.util.LinkedList; … … 23 20 24 21 import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; 25 import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEqualityRuleManager;26 import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;27 22 import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; 28 23 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; … … 34 29 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; 35 30 import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; 36 import de.ugoe.cs.autoquest.usageprofiles.SymbolComparator; 37 import de.ugoe.cs.autoquest.usageprofiles.Trie; 31 import de.ugoe.cs.autoquest.usageprofiles.SymbolMap; 38 32 import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor; 39 33 import de.ugoe.cs.util.StopWatch; … … 102 96 103 97 // this is the real rule application. Loop while something is replaced. 98 harmonizeEventTaskInstancesModel(appData); 104 99 do { 105 100 System.out.println(); … … 134 129 135 130 return appData.getResult(); 131 } 132 133 /** 134 * <p> 135 * TODO: comment 136 * </p> 137 * 138 * @param appData 139 */ 140 private void harmonizeEventTaskInstancesModel(RuleApplicationData appData) { 141 System.out.print("harmonizing task model of event task instances"); 142 appData.getStopWatch().start("harmonizing event tasks"); 143 144 SymbolMap<ITaskInstance, ITask> unifiedTaskMap = 145 new SymbolMap<ITaskInstance, ITask>(taskComparator); 146 147 int unifiedTasks = 0; 148 149 List<IUserSession> sessions = appData.getSessions(); 150 for (IUserSession session : sessions) { 151 for (ITaskInstance taskInstance : session) { 152 ITask task = unifiedTaskMap.getValue(taskInstance); 153 154 if (task == null) { 155 unifiedTaskMap.addSymbol(taskInstance, taskInstance.getTask()); 156 } 157 else { 158 taskBuilder.setTask(taskInstance, task); 159 unifiedTasks++; 160 } 161 } 162 } 163 164 165 appData.getStopWatch().stop("harmonizing event tasks"); 166 System.out.println(" --> harmonized " + unifiedTasks + " task occurrences (still " + 167 unifiedTaskMap.size() + " different tasks)"); 168 169 appData.getStopWatch().dumpStatistics(System.out); 136 170 } 137 171 … … 373 407 while (createNewTrie); 374 408 409 // create a normal trie for comparison 410 /*System.out.println("creating test trie for comparison"); 411 412 appData.getStopWatch().start("testtrie"); 413 Trie<ITaskInstance> trie = new Trie<ITaskInstance>(taskComparator); 414 415 for (IUserSession session : appData.getSessions()) { 416 trie.train(session.getExecutedTasks(), appData.getTrainedSequenceLength()); 417 } 418 419 appData.getStopWatch().stop("testtrie"); 420 421 MaxCountAndLongestTasksFinder finder = new MaxCountAndLongestTasksFinder(); 422 trie.process(finder); 423 424 boolean allTasksEqual = finder.getFoundTasks().size() == tasks.size(); 425 426 allTasksEqual &= finder.getFoundTasks().occurrenceCount == tasks.occurrenceCount; 427 428 for (List<ITaskInstance> task1 : finder.getFoundTasks()) { 429 boolean foundTask = false; 430 for (List<ITaskInstance> task2 : tasks) { 431 boolean tasksEqual = false; 432 if (task1.size() == task2.size()) { 433 tasksEqual = true; 434 for (int i = 0; i < task1.size(); i++) { 435 if (!taskComparator.equals(task1.get(i).getTask(), task2.get(i).getTask())) 436 { 437 tasksEqual = false; 438 } 439 } 440 } 441 442 if (tasksEqual) { 443 foundTask = true; 444 break; 445 } 446 } 447 448 if (!foundTask) { 449 System.out.println("different is " + task1); 450 allTasksEqual = false; 451 break; 452 } 453 } 454 455 if (!allTasksEqual) { 456 System.out.println(finder.getFoundTasks()); 457 System.out.println(tasks); 458 459 throw new IllegalArgumentException("both tries calculated different subsequences"); 460 }*/ 461 462 /*TrieStatisticsDumper dumper = new TrieStatisticsDumper(); 463 appData.getLastTrie().process(dumper); 464 dumper.dumpCounters();*/ 465 375 466 appData.setLastFoundTasks(tasks); 376 467 … … 388 479 389 480 appData.getStopWatch().start("training trie"); 390 appData.setLastTrie(new T rie<ITaskInstance>(taskComparator));481 appData.setLastTrie(new TaskInstanceTrie(taskComparator)); 391 482 392 List<IUserSession> sessions = appData.getSessions(); 393 394 for (IUserSession session : sessions) { 395 trainTrie(session, appData); 396 } 483 appData.getLastTrie().trainSessions 484 (appData.getSessions(), appData.getTrainedSequenceLength()); 397 485 398 486 appData.getStopWatch().stop("training trie"); 399 }400 401 /**402 * @param trie403 * @param parent404 */405 private void trainTrie(IUserSession session, RuleApplicationData appData) {406 List<ITaskInstance> children = session.getExecutedTasks();407 408 if ((children != null) && (children.size() > 0)) {409 appData.getLastTrie().train(children, appData.getTrainedSequenceLength());410 }411 487 } 412 488 … … 704 780 } 705 781 782 // /** 783 // * @author Patrick Harms 784 // */ 785 // private class TrieStatisticsDumper implements TrieProcessor<ITaskInstance> { 786 // 787 // /** 788 // * 789 // */ 790 // private Map<Integer, Integer> counters = new HashMap<Integer, Integer>(); 791 // 792 // /* (non-Javadoc) 793 // * @see de.ugoe.cs.autoquest.usageprofiles.TrieProcessor#process(java.util.List, int) 794 // */ 795 // @Override 796 // public TrieProcessor.Result process(List<ITaskInstance> subsequence, int count) { 797 // if (subsequence.size() == 1) { 798 // Integer value = counters.get(count); 799 // 800 // if (value == null) { 801 // value = 0; 802 // } 803 // 804 // counters.put(count, value + 1); 805 // 806 // return TrieProcessor.Result.CONTINUE; 807 // } 808 // else { 809 // // ignore singular occurrences 810 // return TrieProcessor.Result.SKIP_NODE; 811 // } 812 // } 813 // 814 // /** 815 // * @return 816 // */ 817 // public void dumpCounters() { 818 // int dumpedCounters = 0; 819 // 820 // int count = 1; 821 // while (dumpedCounters < counters.size()) { 822 // Integer value = counters.get(count++); 823 // if (value != null) { 824 // System.out.println(value + " symbols occurred " + count + " times"); 825 // dumpedCounters++; 826 // } 827 // } 828 // } 829 // 830 // } 831 706 832 /** 707 833 * … … 717 843 * 718 844 */ 719 private T rie<ITaskInstance>lastTrie;845 private TaskInstanceTrie lastTrie; 720 846 721 847 /** … … 761 887 * @param lastTrie the lastTrie to set 762 888 */ 763 private void setLastTrie(T rie<ITaskInstance>lastTrie) {889 private void setLastTrie(TaskInstanceTrie lastTrie) { 764 890 this.lastTrie = lastTrie; 765 891 } … … 768 894 * @return the lastTrie 769 895 */ 770 private T rie<ITaskInstance>getLastTrie() {896 private TaskInstanceTrie getLastTrie() { 771 897 return lastTrie; 772 898 } … … 882 1008 } 883 1009 884 }885 886 /**887 *888 */889 private static class TaskComparator implements SymbolComparator<ITaskInstance> {890 891 /** */892 private static final long serialVersionUID = 1L;893 894 /** */895 private TaskEquality minimalNodeEquality;896 897 /** */898 private transient Comparer comparer;899 900 /** */901 private transient Comparer lexicalComparer;902 903 /** */904 private transient HashMap<Long, Boolean> equalityBuffer = new HashMap<Long, Boolean>();905 906 /** */907 private transient HashMap<Long, Boolean> lexicalEqualityBuffer;908 909 /**910 *911 */912 public TaskComparator(TaskEquality minimalNodeEquality) {913 this.minimalNodeEquality = minimalNodeEquality;914 init();915 }916 917 1010 /* (non-Javadoc) 918 * @see de.ugoe.cs.autoquest.tasktrees.temporalrelation.SymbolComparator#equals(java.lang.Object, java.lang.Object)1011 * @see java.lang.Object#toString() 919 1012 */ 920 1013 @Override 921 public boolean equals(ITaskInstance taskInstance1, ITaskInstance taskInstance2) { 922 return equals(taskInstance1.getTask(), taskInstance2.getTask()); 923 } 924 925 /** 926 * 927 */ 928 public boolean equals(ITask task1, ITask task2) { 929 Boolean result; 930 931 if (task1 != task2) { 932 if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) { 933 long key = ((long) System.identityHashCode(task1)) << 32; 934 key += System.identityHashCode(task2); 935 936 result = equalityBuffer.get(key); 937 938 if (result == null) { 939 result = comparer.compare(task1, task2); 940 equalityBuffer.put(key, result); 941 } 942 } 943 else { 944 result = false; 945 } 946 } 947 else { 948 result = true; 949 } 950 951 return result; 952 } 953 954 /** 955 * 956 */ 957 public boolean areLexicallyEqual(ITask task1, ITask task2) { 958 Boolean result; 959 960 if (task1 != task2) { 961 long key = ((long) System.identityHashCode(task1)) << 32; 962 key += System.identityHashCode(task2); 963 964 result = lexicalEqualityBuffer.get(key); 965 966 if (result == null) { 967 result = lexicalComparer.compare(task1, task2); 968 lexicalEqualityBuffer.put(key, result); 969 } 970 } 971 else { 972 result = true; 973 } 974 975 return result; 976 } 977 978 /** 979 * 980 */ 981 private void init() { 982 if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) { 983 comparer = new LexicalComparer(); 984 } 985 else if (minimalNodeEquality == TaskEquality.SYNTACTICALLY_EQUAL) { 986 comparer = new SyntacticalComparer(); 987 } 988 else if (minimalNodeEquality == TaskEquality.SEMANTICALLY_EQUAL) { 989 comparer = new SemanticalComparer(); 990 } 991 else { 992 comparer = new DefaultComparer(this.minimalNodeEquality); 993 } 994 995 if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) { 996 lexicalComparer = comparer; 997 lexicalEqualityBuffer = equalityBuffer; 998 } 999 else { 1000 lexicalComparer = new LexicalComparer(); 1001 lexicalEqualityBuffer = new HashMap<Long, Boolean>(); 1002 } 1003 } 1004 1005 /** 1006 * <p> 1007 * deserialize this object and reinitialize the buffers 1008 * </p> 1009 */ 1010 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 1011 in.defaultReadObject(); 1012 init(); 1013 } 1014 } 1015 1016 /** 1017 * 1018 */ 1019 private static interface Comparer { 1020 1021 /** 1022 * 1023 */ 1024 boolean compare(ITask task1, ITask task2); 1025 } 1026 1027 /** 1028 * 1029 */ 1030 private static class LexicalComparer implements Comparer { 1031 1032 /** 1033 * 1034 */ 1035 public boolean compare(ITask task1, ITask task2) { 1036 return TaskEqualityRuleManager.getInstance().areLexicallyEqual(task1, task2); 1037 } 1038 } 1039 1040 /** 1041 * 1042 */ 1043 private static class SyntacticalComparer implements Comparer { 1044 1045 /** 1046 * 1047 */ 1048 public boolean compare(ITask task1, ITask task2) { 1049 return TaskEqualityRuleManager.getInstance().areSyntacticallyEqual(task1, task2); 1050 } 1051 } 1052 1053 /** 1054 * 1055 */ 1056 private static class SemanticalComparer implements Comparer { 1057 1058 /** 1059 * 1060 */ 1061 public boolean compare(ITask task1, ITask task2) { 1062 return TaskEqualityRuleManager.getInstance().areSemanticallyEqual(task1, task2); 1063 } 1064 } 1065 1066 /** 1067 * 1068 */ 1069 private static class DefaultComparer implements Comparer { 1070 1071 /** 1072 * <p> 1073 * the minimal task equality two identified sublists need to have to consider them as equal 1074 * </p> 1075 */ 1076 private TaskEquality minimalNodeEquality; 1077 1078 /** 1079 * 1080 */ 1081 public DefaultComparer(TaskEquality minimalNodeEquality) { 1082 this.minimalNodeEquality = minimalNodeEquality; 1083 } 1084 1085 /** 1086 * 1087 */ 1088 public boolean compare(ITask task1, ITask task2) { 1089 return TaskEqualityRuleManager.getInstance().areAtLeastEqual 1090 (task1, task2, minimalNodeEquality); 1091 } 1014 public String toString() { 1015 StringBuffer result = new StringBuffer(); 1016 result.append(this.occurrenceCount); 1017 result.append(" occurrences:\n"); 1018 1019 for (List<ITaskInstance> task : sequences) { 1020 result.append(task); 1021 result.append("\n"); 1022 } 1023 1024 return result.toString(); 1025 } 1026 1092 1027 } 1093 1028 }
Note: See TracChangeset
for help on using the changeset viewer.