Changeset 1285 for trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRule.java
- Timestamp:
- 07/30/13 09:44:39 (11 years ago)
- File:
-
- 1 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;
Note: See TracChangeset
for help on using the changeset viewer.