Changeset 1285 for trunk/autoquest-core-tasktrees/src/main/java/de/ugoe
- Timestamp:
- 07/30/13 09:44:39 (11 years ago)
- Location:
- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation
- Files:
-
- 4 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRule.java
r1256 r1285 15 15 package de.ugoe.cs.autoquest.tasktrees.temporalrelation; 16 16 17 import java.util.HashMap; 18 import java.util.HashSet; 17 19 import java.util.Iterator; 18 20 import java.util.LinkedList; 19 21 import java.util.List; 22 import java.util.Map; 23 import java.util.Set; 24 import java.util.logging.Level; 20 25 21 26 import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; … … 32 37 import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor; 33 38 import de.ugoe.cs.util.StopWatch; 39 import de.ugoe.cs.util.console.Console; 34 40 35 41 /** … … 59 65 /** 60 66 * <p> 61 * the task comparator to be used for comparing tasks 67 * the task comparator to be used for comparing tasks for preparation 62 68 * </p> 63 69 */ 64 private TaskComparator taskComparator; 65 70 private TaskHandlingStrategy preparationTaskHandlingStrategy; 71 72 /** 73 * <p> 74 * the task comparator to be used for comparing tasks during iteration detection an trie 75 * generation 76 * </p> 77 */ 78 private TaskHandlingStrategy identityTaskHandlingStrategy;; 79 66 80 /** 67 81 * <p> … … 77 91 this.taskBuilder = taskBuilder; 78 92 79 this.taskComparator = new TaskComparator(minimalTaskEquality); 93 this.preparationTaskHandlingStrategy = new TaskHandlingStrategy(minimalTaskEquality); 94 this.identityTaskHandlingStrategy = new TaskHandlingStrategy(TaskEquality.IDENTICAL); 80 95 } 81 96 … … 139 154 */ 140 155 private void harmonizeEventTaskInstancesModel(RuleApplicationData appData) { 141 System.out.print("harmonizing task model of event task instances");156 Console.traceln(Level.INFO, "harmonizing task model of event task instances"); 142 157 appData.getStopWatch().start("harmonizing event tasks"); 143 144 SymbolMap<ITaskInstance, ITask> unifiedTaskMap = 145 new SymbolMap<ITaskInstance, ITask>(taskComparator); 158 159 SymbolMap<ITaskInstance, ITask> uniqueTasks = 160 preparationTaskHandlingStrategy.createSymbolMap(); 161 TaskComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 146 162 147 163 int unifiedTasks = 0; 148 164 ITask task; 149 165 List<IUserSession> sessions = appData.getSessions(); 150 166 for (IUserSession session : sessions) { 167 Console.traceln(Level.FINE, "handling session " + session); 151 168 for (ITaskInstance taskInstance : session) { 152 ITask task = unifiedTaskMap.getValue(taskInstance);169 task = uniqueTasks.getValue(taskInstance); 153 170 154 171 if (task == null) { 155 uni fiedTaskMap.addSymbol(taskInstance, taskInstance.getTask());172 uniqueTasks.addSymbol(taskInstance, taskInstance.getTask()); 156 173 } 157 174 else { … … 160 177 } 161 178 } 162 } 163 179 180 comparator.clearBuffers(); 181 } 164 182 165 183 appData.getStopWatch().stop("harmonizing event tasks"); 166 System.out.println(" -->harmonized " + unifiedTasks + " task occurrences (still " +167 unifiedTaskMap.size() + " different tasks)");168 184 Console.traceln(Level.INFO, "harmonized " + unifiedTasks + " task occurrences (still " + 185 uniqueTasks.size() + " different tasks)"); 186 169 187 appData.getStopWatch().dumpStatistics(System.out); 188 appData.getStopWatch().reset(); 170 189 } 171 190 … … 174 193 */ 175 194 private void detectAndReplaceIterations(RuleApplicationData appData) { 176 System.out.print("detecting iterations");195 Console.traceln(Level.FINE, "detecting iterations"); 177 196 appData.getStopWatch().start("detecting iterations"); 178 197 179 198 List<IUserSession> sessions = appData.getSessions(); 180 int iteratedTasks = 0; 181 182 ITask iteratedTask = null; 183 184 do { 185 iteratedTask = searchIteratedTask(sessions); 186 187 if (iteratedTask != null) { 188 replaceIterationsOf(iteratedTask, sessions, appData); 189 iteratedTasks++; 190 } 191 } 192 while (iteratedTask != null); 199 200 Set<ITask> iteratedTasks = searchIteratedTasks(sessions); 201 202 if (iteratedTasks.size() > 0) { 203 replaceIterationsOf(iteratedTasks, sessions, appData); 204 } 193 205 194 206 appData.getStopWatch().stop("detecting iterations"); 195 System.out.println(" --> found " + iteratedTasks+ " iterated tasks");207 Console.traceln(Level.INFO, "replaced " + iteratedTasks.size() + " iterated tasks"); 196 208 } 197 209 … … 199 211 * 200 212 */ 201 private ITask searchIteratedTask(List<IUserSession> sessions) { 213 private Set<ITask> searchIteratedTasks(List<IUserSession> sessions) { 214 Set<ITask> iteratedTasks = new HashSet<ITask>(); 202 215 for (IUserSession session : sessions) { 203 216 for (int i = 0; i < (session.size() - 1); i++) { 204 if (taskComparator.equals(session.get(i), session.get(i + 1))) { 205 return session.get(i).getTask(); 206 } 207 } 208 } 209 210 return null; 217 // we prepared the task instances to refer to unique tasks, if they are treated 218 // as equal. Therefore, we just compare the identity of the tasks of the task 219 // instances 220 if (session.get(i).getTask() == session.get(i + 1).getTask()) { 221 iteratedTasks.add(session.get(i).getTask()); 222 } 223 } 224 } 225 226 return iteratedTasks; 211 227 } 212 228 … … 214 230 * 215 231 */ 216 /* private ITask searchIteratedTask(List<IUserSession> sessions) { 217 int minNoOfRepetitions = 2; 218 int minNoOfIterationOccurrences = 1; 219 220 Map<ITask, Integer> iterationsCounter = new HashMap<ITask, Integer>(); 221 222 for (IUserSession session : sessions) { 223 for (int i = 0; i < (session.size() - minNoOfRepetitions + 1); i++) { 224 ITask task = session.get(i).getTask(); 225 226 // check if the task is iterated 227 boolean isIterated = true; 228 for (int j = i + 1; j < i + minNoOfRepetitions; j++) { 229 if (!taskComparator.equals(task, session.get(j).getTask())) { 230 isIterated = false; 231 break; 232 } 233 } 234 235 if (isIterated) { 236 Integer currentCounter = null; 237 238 for (Map.Entry<ITask, Integer> entry : iterationsCounter.entrySet()) { 239 if (taskComparator.equals(task, entry.getKey())) { 240 currentCounter = entry.getValue(); 241 break; 242 } 243 } 244 245 if (currentCounter == null) { 246 currentCounter = 1; 247 iterationsCounter.put(task, currentCounter); 248 } 249 else { 250 currentCounter++; 251 iterationsCounter.put(task, currentCounter); 252 } 253 254 if (currentCounter >= minNoOfIterationOccurrences) { 255 return task; 256 } 257 } 258 } 259 } 260 261 return null; 262 }*/ 263 264 /** 265 * 266 */ 267 private void replaceIterationsOf(ITask iteratedTask, 232 private void replaceIterationsOf(Set<ITask> iteratedTasks, 268 233 List<IUserSession> sessions, 269 234 RuleApplicationData appData) 270 235 { 271 IIteration iteration = taskFactory.createNewIteration(); 236 Map<ITask, IIteration> iterations = new HashMap<ITask, IIteration>(); 237 Map<IIteration, List<ITaskInstance>> iterationInstances = 238 new HashMap<IIteration, List<ITaskInstance>>(); 239 240 for (ITask iteratedTask : iteratedTasks) { 241 IIteration iteration = taskFactory.createNewIteration(); 242 iterations.put(iteratedTask, iteration); 243 iterationInstances.put(iteration, new LinkedList<ITaskInstance>()); 244 } 245 272 246 ITaskInstance iterationInstance = null; 273 274 List<ITaskInstance> iterationInstances = new LinkedList<ITaskInstance>();275 247 276 248 for (IUserSession session : sessions) { 277 249 int index = 0; 278 250 while (index < session.size()) { 279 if (taskComparator.equals(iteratedTask, session.get(index).getTask())) { 280 if (iterationInstance == null) { 251 // we prepared the task instances to refer to unique tasks, if they are treated 252 // as equal. Therefore, we just compare the identity of the tasks of the task 253 // instances 254 ITask currentTask = session.get(index).getTask(); 255 IIteration iteration = iterations.get(currentTask); 256 if (iteration != null) { 257 if ((iterationInstance == null) || (iterationInstance.getTask() != iteration)) 258 { 281 259 iterationInstance = taskFactory.createNewTaskInstance(iteration); 282 iterationInstances. add(iterationInstance);260 iterationInstances.get(iteration).add(iterationInstance); 283 261 taskBuilder.addTaskInstance(session, index, iterationInstance); 284 262 index++; … … 297 275 } 298 276 299 harmonizeIterationInstancesModel(iteration, iterationInstances); 277 for (Map.Entry<IIteration, List<ITaskInstance>> entry : iterationInstances.entrySet()) { 278 harmonizeIterationInstancesModel(entry.getKey(), entry.getValue()); 279 } 300 280 } 301 281 … … 307 287 { 308 288 List<ITask> iteratedTaskVariants = new LinkedList<ITask>(); 289 TaskComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 309 290 310 291 // merge the lexically different variants of iterated task to a unique list … … 315 296 boolean found = false; 316 297 for (ITask taskVariant : iteratedTaskVariants) { 317 if ( taskComparator.areLexicallyEqual(taskVariant, candidate)) {298 if (comparator.areLexicallyEqual(taskVariant, candidate)) { 318 299 taskBuilder.setTask(executionVariant, taskVariant); 319 300 found = true; … … 359 340 */ 360 341 private void detectAndReplaceTasks(RuleApplicationData appData) { 361 System.out.println("detecting and replacing tasks");342 Console.traceln(Level.FINE, "detecting and replacing tasks"); 362 343 appData.getStopWatch().start("detecting tasks"); 363 344 … … 370 351 371 352 appData.getStopWatch().stop("replacing tasks"); 372 System.out.println("detected and replaced " + appData.getLastFoundTasks().size() + " tasks"); 353 Console.traceln(Level.INFO, "detected and replaced " + appData.getLastFoundTasks().size() + 354 " tasks occuring " + appData.getLastFoundTasks().getOccurrenceCount() + 355 "times"); 373 356 } 374 357 … … 377 360 */ 378 361 private void getSequencesOccuringMostOften(RuleApplicationData appData) { 379 System.out.println("determining most prominent tasks");362 Console.traceln(Level.FINE, "determining most prominent tasks"); 380 363 381 364 Tasks tasks; … … 411 394 412 395 appData.getStopWatch().start("testtrie"); 413 Trie<ITaskInstance> trie = new Trie<ITaskInstance>( taskComparator);396 Trie<ITaskInstance> trie = new Trie<ITaskInstance>(identityTaskComparator); 414 397 415 398 for (IUserSession session : appData.getSessions()) { … … 433 416 tasksEqual = true; 434 417 for (int i = 0; i < task1.size(); i++) { 435 if (! taskComparator.equals(task1.get(i).getTask(), task2.get(i).getTask()))418 if (!identityTaskComparator.equals(task1.get(i).getTask(), task2.get(i).getTask())) 436 419 { 437 420 tasksEqual = false; … … 458 441 459 442 throw new IllegalArgumentException("both tries calculated different subsequences"); 460 } */443 } 461 444 462 445 /*TrieStatisticsDumper dumper = new TrieStatisticsDumper(); … … 466 449 appData.setLastFoundTasks(tasks); 467 450 468 System.out.println("found " + appData.getLastFoundTasks().size() + " tasks occurring" +469 451 Console.traceln(Level.FINE, "found " + appData.getLastFoundTasks().size() + " tasks " + 452 "occurring " + appData.getLastFoundTasks().getOccurrenceCount() + " times"); 470 453 } 471 454 … … 475 458 */ 476 459 private void createNewTrie(RuleApplicationData appData) { 477 System.out.println("training trie with a maximum sequence length of " +478 460 Console.traceln(Level.FINER, "training trie with a maximum sequence length of " + 461 appData.getTrainedSequenceLength()); 479 462 480 463 appData.getStopWatch().start("training trie"); 481 appData.setLastTrie(new TaskInstanceTrie(taskComparator)); 464 465 // we prepared the task instances to refer to unique tasks, if they are treated 466 // as equal. Therefore, we just compare the identity of the tasks of the task 467 // instances 468 appData.setLastTrie(new TaskInstanceTrie(identityTaskHandlingStrategy)); 482 469 483 470 appData.getLastTrie().trainSessions … … 496 483 (appData.getLastFoundTasks().getOccurrenceCount() > 1)) 497 484 { 498 System.out.println("replacing tasks occurrences");485 Console.traceln(Level.FINER, "replacing tasks occurrences"); 499 486 500 487 for (List<ITaskInstance> task : appData.getLastFoundTasks()) { 501 488 ISequence sequence = taskFactory.createNewSequence(); 502 489 503 System.out.println("replacing " + sequence.getId() + ": " + task);490 Console.traceln(Level.FINEST, "replacing " + sequence.getId() + ": " + task); 504 491 505 492 List<ITaskInstance> sequenceInstances = … … 507 494 508 495 harmonizeSequenceInstancesModel(sequence, sequenceInstances,task.size()); 509 appData.detectedAndReplacedTasks(sequenceInstances.size() > 0); 496 appData.detectedAndReplacedTasks 497 (appData.detectedAndReplacedTasks() || (sequenceInstances.size() > 0)); 510 498 511 499 if (sequenceInstances.size() < appData.getLastFoundTasks().getOccurrenceCount()) { 512 System.out.println(sequence.getId() + ": replaced task only " +513 514 500 Console.traceln(Level.FINE, sequence.getId() + ": replaced task only " + 501 sequenceInstances.size() + " times instead of expected " + 502 appData.getLastFoundTasks().getOccurrenceCount()); 515 503 } 516 504 } … … 526 514 int sequenceLength) 527 515 { 516 TaskComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); 528 517 529 518 // ensure for each subtask that lexically different variants are preserved … … 537 526 538 527 for (int i = 0; i < subTaskVariants.size(); i++) { 539 if ( taskComparator.areLexicallyEqual(subTaskVariants.get(i), candidate)) {528 if (comparator.areLexicallyEqual(subTaskVariants.get(i), candidate)) { 540 529 taskBuilder.setTask 541 530 (sequenceInstance.get(subTaskIndex), subTaskVariants.get(i)); … … 624 613 625 614 for (int j = 0; j < subList.size(); j++) { 626 if (!taskComparator.equals(list.get(i + j), subList.get(j))) { 615 // we prepared the task instances to refer to unique tasks, if they are treated 616 // as equal. Therefore, we just compare the identity of the tasks of the task 617 // instances 618 if (list.get(i + j).getTask() != subList.get(j).getTask()) { 627 619 matchFound = false; 628 620 break; … … 655 647 656 648 for (int j = 0; j < subList.size(); j++) { 657 if (!taskComparator.equals(list.get(i + j), subList.get(j))) { 649 // we prepared the task instances to refer to unique tasks, if they are treated 650 // as equal. Therefore, we just compare the identity of the tasks of the task 651 // instances 652 if (list.get(i + j) != subList.get(j)) { 658 653 matchFound = false; 659 654 break; -
trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/TaskComparator.java
r1256 r1285 19 19 import java.util.HashMap; 20 20 21 import de.ugoe.cs.autoquest.eventcore.IEventType;22 21 import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; 23 22 import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEqualityRuleManager; 24 import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;25 import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;26 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;27 import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;28 23 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; 29 24 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; … … 39 34 40 35 /** */ 41 private static final int MAX_BUFFER_SIZE = 10* 1024 * 1024;36 private static final int MAX_BUFFER_SIZE = 2 * 1024 * 1024; 42 37 43 38 /** */ … … 79 74 80 75 if (task1 != task2) { 81 if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) {76 //if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) { 82 77 long key = ((long) System.identityHashCode(task1)) << 32; 83 78 key += System.identityHashCode(task2); … … 92 87 } 93 88 } 94 }89 /*} 95 90 else { 96 91 result = false; 97 } 92 }*/ 98 93 } 99 94 else { … … 118 113 if (result == null) { 119 114 result = lexicalComparer.compare(task1, task2); 120 if (equalityBuffer.size() < 1024 * 1024 * 10) {115 if (equalityBuffer.size() < MAX_BUFFER_SIZE) { 121 116 lexicalEqualityBuffer.put(key, result); 122 117 } … … 130 125 } 131 126 132 /* (non-Javadoc) 133 * @see SymbolComparator#getBucketSearchOrder(Object) 134 */ 135 @Override 136 public int[] getBucketSearchOrder(ITaskInstance taskInstance) { 137 // 0 = sequence; 1 = selection; 2 = iteration; 3 = optional; 4 = event task in general; 138 // other = hashCode of name of event type 139 140 ITask task = taskInstance.getTask(); 141 142 if (task instanceof IEventTask) { 143 // event tasks are most likely equal to those of the event type with the same name, 144 // Afterwards, they may be equal to iterations, optionals, other event tasks, 145 // selections, and finally the rest. 146 IEventType eventType = ((IEventTask) task).getEventType(); 147 return new int[] { eventType.getName().hashCode(), 2, 3, 4, 1 }; 148 } 149 else if (task instanceof ISequence) { 150 return new int[] { 0, 2, 3, 1 }; 151 } 152 else if (task instanceof ISelection) { 153 return new int[] { 1, 4, 2, 3 }; 154 } 155 else if (task instanceof IIteration) { 156 return new int[] { 2, 1, 4 }; 157 } 158 159 return null; 160 } 161 127 /** 128 * <p> 129 * TODO: comment 130 * </p> 131 * 132 */ 133 void clearBuffers() { 134 equalityBuffer.clear(); 135 init(); 136 } 137 162 138 /** 163 139 * … … 275 251 } 276 252 } 253 277 254 } -
trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/TaskInstanceTrie.java
r1273 r1285 20 20 import java.util.Map; 21 21 22 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; 22 23 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; 23 24 import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; … … 33 34 * @author Patrick Harms 34 35 */ 35 publicclass TaskInstanceTrie extends Trie<ITaskInstance> {36 class TaskInstanceTrie extends Trie<ITaskInstance> { 36 37 37 38 /** */ … … 43 44 * </p> 44 45 */ 45 private Task Comparator comparator;46 46 private TaskHandlingStrategy taskStrategy; 47 47 48 /** 48 49 * <p> … … 52 53 * @param taskComparator2 53 54 */ 54 public TaskInstanceTrie(Task Comparator taskComparator) {55 super(task Comparator);56 this. comparator = taskComparator;55 public TaskInstanceTrie(TaskHandlingStrategy taskStrategy) { 56 super(taskStrategy); 57 this.taskStrategy = taskStrategy; 57 58 } 58 59 … … 65 66 } 66 67 67 SymbolMap<ITaskInstance, List<ITaskInstance>> equalTaskInstancesMap = 68 new SymbolMap<ITaskInstance, List<ITaskInstance>>(comparator); 69 70 Map<ITaskInstance, List<ITaskInstance>> taskInstancesMap = 71 new HashMap<ITaskInstance, List<ITaskInstance>>(); 68 SymbolMap<ITaskInstance, Counter> equalTaskInstancesMap = 69 taskStrategy.createSymbolMap(); 70 71 Map<ITask, Counter> instanceCountMap = new HashMap<ITask, Counter>(); 72 72 73 73 System.out.println("preparing training"); 74 int noOfTaskInstances = 0; 74 75 for (IUserSession session : userSessions) { 75 76 for (ITaskInstance taskInstance : session) { 76 List<ITaskInstance> equalTaskInstances = 77 equalTaskInstancesMap.getValue(taskInstance); 78 79 if (equalTaskInstances == null) { 80 equalTaskInstances = new LinkedList<ITaskInstance>(); 81 equalTaskInstancesMap.addSymbol(taskInstance, equalTaskInstances); 77 Counter counter = equalTaskInstancesMap.getValue(taskInstance); 78 79 if (counter == null) { 80 counter = new Counter(); 81 equalTaskInstancesMap.addSymbol(taskInstance, counter); 82 82 } 83 83 84 equalTaskInstances.add(taskInstance); 85 taskInstancesMap.put(taskInstance, equalTaskInstances); 86 } 87 } 88 89 System.out.println("performing training"); 84 counter.count++; 85 instanceCountMap.put(taskInstance.getTask(), counter); 86 87 noOfTaskInstances++; 88 } 89 } 90 91 System.out.println("performing training of " + noOfTaskInstances + " task instances"); 92 Counter processedTaskInstances = new Counter(); 93 int counterRecheckAt = noOfTaskInstances / 10; // recheck the maximum count after each 94 // 10% of processed task instances 90 95 for (IUserSession session : userSessions) { 91 train(session, maxOrder, taskInstancesMap);96 train(session, maxOrder, instanceCountMap, processedTaskInstances, counterRecheckAt); 92 97 } 93 98 … … 122 127 * 123 128 */ 124 private void train(IUserSession userSession, 125 int maxOrder, 126 Map<ITaskInstance, List<ITaskInstance>> taskInstancesMap) 129 private void train(IUserSession userSession, 130 int maxOrder, 131 Map<ITask, Counter> taskInstanceCountMap, 132 Counter processedTaskInstances, 133 int counterRecheckAt) 127 134 { 128 135 List<ITaskInstance> executedTasks = userSession.getExecutedTasks(); … … 130 137 List<ITaskInstance> subsequence = new LinkedList<ITaskInstance>(); 131 138 132 int numberOfTrainedSubsequences = 0;133 139 int sequenceMaxCount = 0; 134 140 135 141 for (ITaskInstance currentTaskInstance : executedTasks) { 136 int occurrenceCount = taskInstancesMap.get(currentTaskInstance).size(); 137 138 if ((numberOfTrainedSubsequences % 10) == 0) { 142 143 int occurrenceCount = taskInstanceCountMap.get(currentTaskInstance.getTask()).count; 144 145 if (processedTaskInstances.count >= counterRecheckAt) { 139 146 sequenceMaxCount = getCurrentSequenceMaxCount(); 147 processedTaskInstances.count = 0; 140 148 } 141 149 … … 158 166 add(subsequence); 159 167 subsequence.remove(0); 160 161 numberOfTrainedSubsequences++;162 168 } 163 169 } 170 171 processedTaskInstances.count++; 164 172 } 165 173 … … 219 227 } 220 228 229 /** 230 * @author Patrick Harms 231 */ 232 private static class Counter { 233 int count = 0; 234 } 235 221 236 }
Note: See TracChangeset
for help on using the changeset viewer.