[1113] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[1107] | 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
| 16 | |
---|
[1285] | 17 | import java.util.HashMap; |
---|
| 18 | import java.util.HashSet; |
---|
[1107] | 19 | import java.util.Iterator; |
---|
| 20 | import java.util.LinkedList; |
---|
| 21 | import java.util.List; |
---|
[1285] | 22 | import java.util.Map; |
---|
| 23 | import java.util.Set; |
---|
| 24 | import java.util.logging.Level; |
---|
[1107] | 25 | |
---|
[1146] | 26 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; |
---|
[1127] | 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
[1294] | 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
[1127] | 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1294] | 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1107] | 31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
[1294] | 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1146] | 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
| 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[1256] | 39 | import de.ugoe.cs.autoquest.usageprofiles.SymbolMap; |
---|
[1401] | 40 | import de.ugoe.cs.autoquest.usageprofiles.Trie; |
---|
[1119] | 41 | import de.ugoe.cs.autoquest.usageprofiles.TrieProcessor; |
---|
[1129] | 42 | import de.ugoe.cs.util.StopWatch; |
---|
[1285] | 43 | import de.ugoe.cs.util.console.Console; |
---|
[1107] | 44 | |
---|
| 45 | /** |
---|
| 46 | * <p> |
---|
[1401] | 47 | * This class implements the major rule for creating task trees based on a set of recorded |
---|
| 48 | * user sessions. For this, it first harmonizes all tasks. This eases later comparison. Then it |
---|
| 49 | * searches the sessions for iterations and replaces them accordingly. Then it searches for sub |
---|
| 50 | * sequences being the longest and occurring most often. For each found sub sequence, it replaces |
---|
| 51 | * the occurrences by creating appropriate {@link ISequence}s. Afterwards, again searches for |
---|
| 52 | * iterations and then again for sub sequences until no more replacements are done. |
---|
[1107] | 53 | * </p> |
---|
[1401] | 54 | * <p> |
---|
| 55 | * For determining the longest sequence occurring most often, the implementation uses a |
---|
| 56 | * {@link Trie}. The depth of the tree is initially 3. If the algorithm has a longest sequence |
---|
| 57 | * occurring most often whose length is equal to the depth of the trie, it recalculates the trie |
---|
| 58 | * with an increased depth. |
---|
| 59 | * </p> |
---|
[1107] | 60 | * |
---|
| 61 | * @author Patrick Harms |
---|
| 62 | */ |
---|
[1146] | 63 | class SequenceForTaskDetectionRule implements ISessionScopeRule { |
---|
[1107] | 64 | |
---|
| 65 | /** |
---|
| 66 | * <p> |
---|
[1146] | 67 | * the task factory to be used for creating substructures for the temporal |
---|
[1401] | 68 | * relationships identified during rul application |
---|
[1107] | 69 | * </p> |
---|
| 70 | */ |
---|
[1146] | 71 | private ITaskFactory taskFactory; |
---|
[1107] | 72 | /** |
---|
| 73 | * <p> |
---|
[1146] | 74 | * the task builder to be used for creating substructures for the temporal relationships |
---|
[1107] | 75 | * identified during rule application |
---|
| 76 | * </p> |
---|
| 77 | */ |
---|
[1146] | 78 | private ITaskBuilder taskBuilder; |
---|
[1107] | 79 | |
---|
| 80 | /** |
---|
| 81 | * <p> |
---|
[1401] | 82 | * the task handling strategy to be used for comparing tasks for preparation, i.e., before |
---|
| 83 | * the tasks are harmonized |
---|
[1107] | 84 | * </p> |
---|
| 85 | */ |
---|
[1285] | 86 | private TaskHandlingStrategy preparationTaskHandlingStrategy; |
---|
| 87 | |
---|
[1107] | 88 | /** |
---|
| 89 | * <p> |
---|
[1401] | 90 | * the task handling strategy to be used for comparing tasks during iteration detection an trie |
---|
| 91 | * generation, i.e., after the tasks are harmonized |
---|
[1285] | 92 | * </p> |
---|
| 93 | */ |
---|
| 94 | private TaskHandlingStrategy identityTaskHandlingStrategy;; |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * <p> |
---|
[1401] | 98 | * instantiates the rule and initializes it with a task equality to be considered when |
---|
| 99 | * comparing tasks as well as a task factory and builder to be used for creating task |
---|
| 100 | * structures. |
---|
[1107] | 101 | * </p> |
---|
[1401] | 102 | * |
---|
| 103 | * @param minimalTaskEquality the task equality to be considered when comparing tasks |
---|
| 104 | * @param taskFactory the task factory to be used for creating substructures |
---|
| 105 | * @param taskBuilder the task builder to be used for creating substructures |
---|
[1107] | 106 | */ |
---|
[1189] | 107 | SequenceForTaskDetectionRule(TaskEquality minimalTaskEquality, |
---|
| 108 | ITaskFactory taskFactory, |
---|
| 109 | ITaskBuilder taskBuilder) |
---|
[1107] | 110 | { |
---|
[1146] | 111 | this.taskFactory = taskFactory; |
---|
| 112 | this.taskBuilder = taskBuilder; |
---|
[1107] | 113 | |
---|
[1285] | 114 | this.preparationTaskHandlingStrategy = new TaskHandlingStrategy(minimalTaskEquality); |
---|
| 115 | this.identityTaskHandlingStrategy = new TaskHandlingStrategy(TaskEquality.IDENTICAL); |
---|
[1107] | 116 | } |
---|
| 117 | |
---|
[1119] | 118 | /* (non-Javadoc) |
---|
| 119 | * @see java.lang.Object#toString() |
---|
| 120 | */ |
---|
| 121 | @Override |
---|
| 122 | public String toString() { |
---|
[1127] | 123 | return "SequenceForTaskDetectionRule"; |
---|
[1119] | 124 | } |
---|
| 125 | |
---|
[1146] | 126 | /* (non-Javadoc) |
---|
| 127 | * @see de.ugoe.cs.autoquest.tasktrees.temporalrelation.ISessionScopeRule#apply(java.util.List) |
---|
[1107] | 128 | */ |
---|
| 129 | @Override |
---|
[1146] | 130 | public RuleApplicationResult apply(List<IUserSession> sessions) { |
---|
[1127] | 131 | RuleApplicationData appData = new RuleApplicationData(sessions); |
---|
[1337] | 132 | |
---|
[1127] | 133 | // this is the real rule application. Loop while something is replaced. |
---|
[1256] | 134 | harmonizeEventTaskInstancesModel(appData); |
---|
[1107] | 135 | do { |
---|
[1127] | 136 | System.out.println(); |
---|
[1107] | 137 | |
---|
[1127] | 138 | appData.getStopWatch().start("whole loop"); |
---|
| 139 | detectAndReplaceIterations(appData); |
---|
[1133] | 140 | |
---|
[1127] | 141 | appData.getStopWatch().start("task replacement"); |
---|
| 142 | detectAndReplaceTasks(appData); |
---|
| 143 | appData.getStopWatch().stop("task replacement"); |
---|
| 144 | appData.getStopWatch().stop("whole loop"); |
---|
[1107] | 145 | |
---|
[1146] | 146 | //((TaskTreeNodeComparator) taskComparator).getStopWatch().dumpStatistics(System.out); |
---|
| 147 | //((TaskTreeNodeComparator) taskComparator).getStopWatch().reset(); |
---|
[1127] | 148 | |
---|
| 149 | appData.getStopWatch().dumpStatistics(System.out); |
---|
| 150 | appData.getStopWatch().reset(); |
---|
| 151 | |
---|
[1107] | 152 | } |
---|
[1146] | 153 | while (appData.detectedAndReplacedTasks()); |
---|
[1107] | 154 | |
---|
[1356] | 155 | Console.println |
---|
[1146] | 156 | ("created " + appData.getResult().getNewlyCreatedTasks().size() + |
---|
| 157 | " new tasks and " + appData.getResult().getNewlyCreatedTaskInstances().size() + |
---|
| 158 | " appropriate instances\n"); |
---|
[1107] | 159 | |
---|
[1146] | 160 | if ((appData.getResult().getNewlyCreatedTasks().size() > 0) || |
---|
| 161 | (appData.getResult().getNewlyCreatedTaskInstances().size() > 0)) |
---|
| 162 | { |
---|
[1127] | 163 | appData.getResult().setRuleApplicationStatus(RuleApplicationStatus.FINISHED); |
---|
[1119] | 164 | } |
---|
| 165 | |
---|
[1127] | 166 | return appData.getResult(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
[1256] | 170 | * <p> |
---|
[1401] | 171 | * harmonizes the event task instances by unifying tasks. This is done, as initially the |
---|
| 172 | * event tasks being equal with respect to the considered task equality are distinct objects. |
---|
| 173 | * The comparison of these distinct objects is more time consuming than comparing the object |
---|
| 174 | * references. |
---|
[1256] | 175 | * </p> |
---|
| 176 | * |
---|
[1401] | 177 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1127] | 178 | */ |
---|
[1256] | 179 | private void harmonizeEventTaskInstancesModel(RuleApplicationData appData) { |
---|
[1285] | 180 | Console.traceln(Level.INFO, "harmonizing task model of event task instances"); |
---|
[1256] | 181 | appData.getStopWatch().start("harmonizing event tasks"); |
---|
[1285] | 182 | |
---|
| 183 | SymbolMap<ITaskInstance, ITask> uniqueTasks = |
---|
| 184 | preparationTaskHandlingStrategy.createSymbolMap(); |
---|
[1294] | 185 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); |
---|
[1256] | 186 | |
---|
| 187 | int unifiedTasks = 0; |
---|
[1285] | 188 | ITask task; |
---|
[1256] | 189 | List<IUserSession> sessions = appData.getSessions(); |
---|
[1356] | 190 | int sessionNo = 0; |
---|
[1256] | 191 | for (IUserSession session : sessions) { |
---|
[1356] | 192 | Console.traceln(Level.FINE, "handling " + (++sessionNo) + ". " + session); |
---|
[1256] | 193 | for (ITaskInstance taskInstance : session) { |
---|
[1285] | 194 | task = uniqueTasks.getValue(taskInstance); |
---|
[1256] | 195 | |
---|
| 196 | if (task == null) { |
---|
[1285] | 197 | uniqueTasks.addSymbol(taskInstance, taskInstance.getTask()); |
---|
[1256] | 198 | } |
---|
| 199 | else { |
---|
| 200 | taskBuilder.setTask(taskInstance, task); |
---|
| 201 | unifiedTasks++; |
---|
| 202 | } |
---|
| 203 | } |
---|
[1285] | 204 | |
---|
| 205 | comparator.clearBuffers(); |
---|
[1256] | 206 | } |
---|
| 207 | |
---|
| 208 | appData.getStopWatch().stop("harmonizing event tasks"); |
---|
[1285] | 209 | Console.traceln(Level.INFO, "harmonized " + unifiedTasks + " task occurrences (still " + |
---|
| 210 | uniqueTasks.size() + " different tasks)"); |
---|
| 211 | |
---|
[1256] | 212 | appData.getStopWatch().dumpStatistics(System.out); |
---|
[1285] | 213 | appData.getStopWatch().reset(); |
---|
[1256] | 214 | } |
---|
| 215 | |
---|
| 216 | /** |
---|
[1401] | 217 | * <p> |
---|
| 218 | * searches for direct iterations of single tasks in all sequences and replaces them with |
---|
| 219 | * {@link IIteration}s, respectively appropriate instances. Also all single occurrences of |
---|
| 220 | * a task that is iterated somewhen are replaced with iterations to have again an efficient |
---|
| 221 | * way for task comparisons. |
---|
| 222 | * </p> |
---|
| 223 | * |
---|
| 224 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1256] | 225 | */ |
---|
[1127] | 226 | private void detectAndReplaceIterations(RuleApplicationData appData) { |
---|
[1285] | 227 | Console.traceln(Level.FINE, "detecting iterations"); |
---|
[1127] | 228 | appData.getStopWatch().start("detecting iterations"); |
---|
[1119] | 229 | |
---|
[1146] | 230 | List<IUserSession> sessions = appData.getSessions(); |
---|
[1127] | 231 | |
---|
[1285] | 232 | Set<ITask> iteratedTasks = searchIteratedTasks(sessions); |
---|
[1146] | 233 | |
---|
[1285] | 234 | if (iteratedTasks.size() > 0) { |
---|
| 235 | replaceIterationsOf(iteratedTasks, sessions, appData); |
---|
[1127] | 236 | } |
---|
| 237 | |
---|
| 238 | appData.getStopWatch().stop("detecting iterations"); |
---|
[1285] | 239 | Console.traceln(Level.INFO, "replaced " + iteratedTasks.size() + " iterated tasks"); |
---|
[1127] | 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
[1401] | 243 | * <p> |
---|
| 244 | * searches the provided sessions for task iterations. If a task is iterated, it is added |
---|
| 245 | * to the returned set. |
---|
| 246 | * </p> |
---|
| 247 | * |
---|
| 248 | * @param the session to search for iterations in |
---|
| 249 | * |
---|
| 250 | * @return a set of tasks being iterated somewhere |
---|
[1127] | 251 | */ |
---|
[1285] | 252 | private Set<ITask> searchIteratedTasks(List<IUserSession> sessions) { |
---|
| 253 | Set<ITask> iteratedTasks = new HashSet<ITask>(); |
---|
[1146] | 254 | for (IUserSession session : sessions) { |
---|
| 255 | for (int i = 0; i < (session.size() - 1); i++) { |
---|
[1285] | 256 | // we prepared the task instances to refer to unique tasks, if they are treated |
---|
| 257 | // as equal. Therefore, we just compare the identity of the tasks of the task |
---|
| 258 | // instances |
---|
| 259 | if (session.get(i).getTask() == session.get(i + 1).getTask()) { |
---|
| 260 | iteratedTasks.add(session.get(i).getTask()); |
---|
[1146] | 261 | } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[1285] | 265 | return iteratedTasks; |
---|
[1146] | 266 | } |
---|
| 267 | |
---|
| 268 | /** |
---|
[1401] | 269 | * <p> |
---|
| 270 | * replaces all occurrences of all tasks provided in the set with iterations |
---|
| 271 | * </p> |
---|
[1146] | 272 | * |
---|
[1401] | 273 | * @param iteratedTasks the tasks to be replaced with iterations |
---|
| 274 | * @param sessions the sessions in which the tasks are to be replaced |
---|
| 275 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1146] | 276 | */ |
---|
[1285] | 277 | private void replaceIterationsOf(Set<ITask> iteratedTasks, |
---|
[1146] | 278 | List<IUserSession> sessions, |
---|
| 279 | RuleApplicationData appData) |
---|
[1127] | 280 | { |
---|
[1285] | 281 | Map<ITask, IIteration> iterations = new HashMap<ITask, IIteration>(); |
---|
[1294] | 282 | Map<IIteration, List<IIterationInstance>> iterationInstances = |
---|
| 283 | new HashMap<IIteration, List<IIterationInstance>>(); |
---|
[1285] | 284 | |
---|
| 285 | for (ITask iteratedTask : iteratedTasks) { |
---|
| 286 | IIteration iteration = taskFactory.createNewIteration(); |
---|
| 287 | iterations.put(iteratedTask, iteration); |
---|
[1294] | 288 | iterationInstances.put(iteration, new LinkedList<IIterationInstance>()); |
---|
[1285] | 289 | } |
---|
| 290 | |
---|
[1337] | 291 | IIterationInstance iterationInstance; |
---|
[1127] | 292 | |
---|
[1146] | 293 | for (IUserSession session : sessions) { |
---|
| 294 | int index = 0; |
---|
[1337] | 295 | iterationInstance = null; |
---|
| 296 | |
---|
[1146] | 297 | while (index < session.size()) { |
---|
[1285] | 298 | // we prepared the task instances to refer to unique tasks, if they are treated |
---|
| 299 | // as equal. Therefore, we just compare the identity of the tasks of the task |
---|
| 300 | // instances |
---|
| 301 | ITask currentTask = session.get(index).getTask(); |
---|
| 302 | IIteration iteration = iterations.get(currentTask); |
---|
| 303 | if (iteration != null) { |
---|
| 304 | if ((iterationInstance == null) || (iterationInstance.getTask() != iteration)) |
---|
| 305 | { |
---|
[1146] | 306 | iterationInstance = taskFactory.createNewTaskInstance(iteration); |
---|
[1285] | 307 | iterationInstances.get(iteration).add(iterationInstance); |
---|
[1146] | 308 | taskBuilder.addTaskInstance(session, index, iterationInstance); |
---|
| 309 | index++; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | taskBuilder.addChild(iterationInstance, session.get(index)); |
---|
| 313 | taskBuilder.removeTaskInstance(session, index); |
---|
| 314 | } |
---|
| 315 | else { |
---|
| 316 | if (iterationInstance != null) { |
---|
| 317 | iterationInstance = null; |
---|
| 318 | } |
---|
| 319 | index++; |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
[1294] | 324 | for (Map.Entry<IIteration, List<IIterationInstance>> entry : iterationInstances.entrySet()) |
---|
| 325 | { |
---|
[1285] | 326 | harmonizeIterationInstancesModel(entry.getKey(), entry.getValue()); |
---|
| 327 | } |
---|
[1146] | 328 | } |
---|
[1127] | 329 | |
---|
[1146] | 330 | /** |
---|
[1401] | 331 | * <p> |
---|
| 332 | * TODO clarify why this is done |
---|
| 333 | * </p> |
---|
[1146] | 334 | */ |
---|
[1294] | 335 | private void harmonizeIterationInstancesModel(IIteration iteration, |
---|
| 336 | List<IIterationInstance> iterationInstances) |
---|
[1146] | 337 | { |
---|
| 338 | List<ITask> iteratedTaskVariants = new LinkedList<ITask>(); |
---|
[1294] | 339 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); |
---|
[1146] | 340 | |
---|
| 341 | // merge the lexically different variants of iterated task to a unique list |
---|
[1294] | 342 | for (IIterationInstance iterationInstance : iterationInstances) { |
---|
[1146] | 343 | for (ITaskInstance executionVariant : iterationInstance) { |
---|
| 344 | ITask candidate = executionVariant.getTask(); |
---|
[1127] | 345 | |
---|
[1146] | 346 | boolean found = false; |
---|
| 347 | for (ITask taskVariant : iteratedTaskVariants) { |
---|
[1285] | 348 | if (comparator.areLexicallyEqual(taskVariant, candidate)) { |
---|
[1146] | 349 | taskBuilder.setTask(executionVariant, taskVariant); |
---|
| 350 | found = true; |
---|
| 351 | break; |
---|
| 352 | } |
---|
[1127] | 353 | } |
---|
[1146] | 354 | |
---|
| 355 | if (!found) { |
---|
| 356 | iteratedTaskVariants.add(candidate); |
---|
| 357 | } |
---|
[1107] | 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
[1146] | 361 | // if there are more than one lexically different variant of iterated tasks, adapt the |
---|
| 362 | // iteration model to be a selection of different variants. In this case also adapt |
---|
| 363 | // the generated iteration instances to correctly contain selection instances. If there |
---|
| 364 | // is only one variant of an iterated task, simply set this as the marked task of the |
---|
| 365 | // iteration. In this case, the instances can be preserved as is |
---|
| 366 | if (iteratedTaskVariants.size() > 1) { |
---|
| 367 | ISelection selection = taskFactory.createNewSelection(); |
---|
| 368 | |
---|
| 369 | for (ITask variant : iteratedTaskVariants) { |
---|
| 370 | taskBuilder.addChild(selection, variant); |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | taskBuilder.setMarkedTask(iteration, selection); |
---|
| 374 | |
---|
[1294] | 375 | for (IIterationInstance instance : iterationInstances) { |
---|
[1146] | 376 | for (int i = 0; i < instance.size(); i++) { |
---|
[1294] | 377 | ISelectionInstance selectionInstance = |
---|
| 378 | taskFactory.createNewTaskInstance(selection); |
---|
| 379 | taskBuilder.setChild(selectionInstance, instance.get(i)); |
---|
[1146] | 380 | taskBuilder.setTaskInstance(instance, i, selectionInstance); |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | } |
---|
| 384 | else { |
---|
| 385 | taskBuilder.setMarkedTask(iteration, iteratedTaskVariants.get(0)); |
---|
| 386 | } |
---|
[1107] | 387 | } |
---|
| 388 | |
---|
| 389 | /** |
---|
[1401] | 390 | * TODO go on commenting |
---|
| 391 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1127] | 392 | */ |
---|
| 393 | private void detectAndReplaceTasks(RuleApplicationData appData) { |
---|
[1285] | 394 | Console.traceln(Level.FINE, "detecting and replacing tasks"); |
---|
[1127] | 395 | appData.getStopWatch().start("detecting tasks"); |
---|
| 396 | |
---|
| 397 | getSequencesOccuringMostOften(appData); |
---|
| 398 | |
---|
| 399 | appData.getStopWatch().stop("detecting tasks"); |
---|
| 400 | appData.getStopWatch().start("replacing tasks"); |
---|
| 401 | |
---|
| 402 | replaceSequencesOccurringMostOften(appData); |
---|
| 403 | |
---|
| 404 | appData.getStopWatch().stop("replacing tasks"); |
---|
[1285] | 405 | Console.traceln(Level.INFO, "detected and replaced " + appData.getLastFoundTasks().size() + |
---|
| 406 | " tasks occuring " + appData.getLastFoundTasks().getOccurrenceCount() + |
---|
[1287] | 407 | " times"); |
---|
[1127] | 408 | } |
---|
| 409 | |
---|
| 410 | /** |
---|
[1401] | 411 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1107] | 412 | */ |
---|
[1127] | 413 | private void getSequencesOccuringMostOften(RuleApplicationData appData) { |
---|
[1285] | 414 | Console.traceln(Level.FINE, "determining most prominent tasks"); |
---|
[1119] | 415 | |
---|
[1127] | 416 | Tasks tasks; |
---|
[1119] | 417 | boolean createNewTrie = (appData.getLastTrie() == null) || |
---|
[1146] | 418 | appData.detectedAndReplacedTasks(); // tree has changed |
---|
[1107] | 419 | |
---|
[1119] | 420 | do { |
---|
| 421 | if (createNewTrie) { |
---|
| 422 | createNewTrie(appData); |
---|
| 423 | } |
---|
[1107] | 424 | |
---|
[1127] | 425 | MaxCountAndLongestTasksFinder finder = new MaxCountAndLongestTasksFinder(); |
---|
[1119] | 426 | appData.getLastTrie().process(finder); |
---|
| 427 | |
---|
[1127] | 428 | tasks = finder.getFoundTasks(); |
---|
[1119] | 429 | |
---|
| 430 | createNewTrie = false; |
---|
| 431 | |
---|
[1146] | 432 | for (List<ITaskInstance> task : tasks) { |
---|
[1119] | 433 | if (task.size() >= appData.getTrainedSequenceLength()) { |
---|
| 434 | // Trie must be recreated with a longer sequence length to be sure that |
---|
| 435 | // the found sequences occurring most often are found in their whole length |
---|
| 436 | appData.setTrainedSequenceLength(appData.getTrainedSequenceLength() + 1); |
---|
| 437 | createNewTrie = true; |
---|
| 438 | break; |
---|
| 439 | } |
---|
[1107] | 440 | } |
---|
| 441 | } |
---|
[1119] | 442 | while (createNewTrie); |
---|
| 443 | |
---|
[1256] | 444 | // create a normal trie for comparison |
---|
| 445 | /*System.out.println("creating test trie for comparison"); |
---|
| 446 | |
---|
| 447 | appData.getStopWatch().start("testtrie"); |
---|
[1285] | 448 | Trie<ITaskInstance> trie = new Trie<ITaskInstance>(identityTaskComparator); |
---|
[1256] | 449 | |
---|
| 450 | for (IUserSession session : appData.getSessions()) { |
---|
| 451 | trie.train(session.getExecutedTasks(), appData.getTrainedSequenceLength()); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | appData.getStopWatch().stop("testtrie"); |
---|
| 455 | |
---|
| 456 | MaxCountAndLongestTasksFinder finder = new MaxCountAndLongestTasksFinder(); |
---|
| 457 | trie.process(finder); |
---|
| 458 | |
---|
| 459 | boolean allTasksEqual = finder.getFoundTasks().size() == tasks.size(); |
---|
| 460 | |
---|
| 461 | allTasksEqual &= finder.getFoundTasks().occurrenceCount == tasks.occurrenceCount; |
---|
| 462 | |
---|
| 463 | for (List<ITaskInstance> task1 : finder.getFoundTasks()) { |
---|
| 464 | boolean foundTask = false; |
---|
| 465 | for (List<ITaskInstance> task2 : tasks) { |
---|
| 466 | boolean tasksEqual = false; |
---|
| 467 | if (task1.size() == task2.size()) { |
---|
| 468 | tasksEqual = true; |
---|
| 469 | for (int i = 0; i < task1.size(); i++) { |
---|
[1285] | 470 | if (!identityTaskComparator.equals(task1.get(i).getTask(), task2.get(i).getTask())) |
---|
[1256] | 471 | { |
---|
| 472 | tasksEqual = false; |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | if (tasksEqual) { |
---|
| 478 | foundTask = true; |
---|
| 479 | break; |
---|
| 480 | } |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | if (!foundTask) { |
---|
| 484 | System.out.println("different is " + task1); |
---|
| 485 | allTasksEqual = false; |
---|
| 486 | break; |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | if (!allTasksEqual) { |
---|
| 491 | System.out.println(finder.getFoundTasks()); |
---|
| 492 | System.out.println(tasks); |
---|
| 493 | |
---|
| 494 | throw new IllegalArgumentException("both tries calculated different subsequences"); |
---|
[1285] | 495 | } |
---|
[1256] | 496 | |
---|
| 497 | /*TrieStatisticsDumper dumper = new TrieStatisticsDumper(); |
---|
| 498 | appData.getLastTrie().process(dumper); |
---|
| 499 | dumper.dumpCounters();*/ |
---|
| 500 | |
---|
[1127] | 501 | appData.setLastFoundTasks(tasks); |
---|
| 502 | |
---|
[1285] | 503 | Console.traceln(Level.FINE, "found " + appData.getLastFoundTasks().size() + " tasks " + |
---|
| 504 | "occurring " + appData.getLastFoundTasks().getOccurrenceCount() + " times"); |
---|
[1107] | 505 | } |
---|
| 506 | |
---|
| 507 | /** |
---|
[1401] | 508 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1107] | 509 | */ |
---|
[1127] | 510 | private void createNewTrie(RuleApplicationData appData) { |
---|
[1285] | 511 | Console.traceln(Level.FINER, "training trie with a maximum sequence length of " + |
---|
| 512 | appData.getTrainedSequenceLength()); |
---|
[1119] | 513 | |
---|
| 514 | appData.getStopWatch().start("training trie"); |
---|
[1285] | 515 | |
---|
| 516 | // we prepared the task instances to refer to unique tasks, if they are treated |
---|
| 517 | // as equal. Therefore, we just compare the identity of the tasks of the task |
---|
| 518 | // instances |
---|
| 519 | appData.setLastTrie(new TaskInstanceTrie(identityTaskHandlingStrategy)); |
---|
[1119] | 520 | |
---|
[1256] | 521 | appData.getLastTrie().trainSessions |
---|
| 522 | (appData.getSessions(), appData.getTrainedSequenceLength()); |
---|
[1127] | 523 | |
---|
[1119] | 524 | appData.getStopWatch().stop("training trie"); |
---|
| 525 | } |
---|
| 526 | |
---|
| 527 | /** |
---|
[1401] | 528 | * @param appData the rule application data combining all data used for applying this rule |
---|
[1127] | 529 | */ |
---|
| 530 | private void replaceSequencesOccurringMostOften(RuleApplicationData appData) { |
---|
[1146] | 531 | appData.detectedAndReplacedTasks(false); |
---|
[1127] | 532 | |
---|
| 533 | if ((appData.getLastFoundTasks().size() > 0) && |
---|
| 534 | (appData.getLastFoundTasks().getOccurrenceCount() > 1)) |
---|
| 535 | { |
---|
[1285] | 536 | Console.traceln(Level.FINER, "replacing tasks occurrences"); |
---|
[1127] | 537 | |
---|
[1146] | 538 | for (List<ITaskInstance> task : appData.getLastFoundTasks()) { |
---|
| 539 | ISequence sequence = taskFactory.createNewSequence(); |
---|
| 540 | |
---|
[1285] | 541 | Console.traceln(Level.FINEST, "replacing " + sequence.getId() + ": " + task); |
---|
[1127] | 542 | |
---|
[1294] | 543 | List<ISequenceInstance> sequenceInstances = |
---|
[1146] | 544 | replaceTaskOccurrences(task, appData.getSessions(), sequence); |
---|
[1119] | 545 | |
---|
[1146] | 546 | harmonizeSequenceInstancesModel(sequence, sequenceInstances,task.size()); |
---|
[1285] | 547 | appData.detectedAndReplacedTasks |
---|
| 548 | (appData.detectedAndReplacedTasks() || (sequenceInstances.size() > 0)); |
---|
[1127] | 549 | |
---|
[1146] | 550 | if (sequenceInstances.size() < appData.getLastFoundTasks().getOccurrenceCount()) { |
---|
[1285] | 551 | Console.traceln(Level.FINE, sequence.getId() + ": replaced task only " + |
---|
| 552 | sequenceInstances.size() + " times instead of expected " + |
---|
| 553 | appData.getLastFoundTasks().getOccurrenceCount()); |
---|
[1127] | 554 | } |
---|
| 555 | } |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | /** |
---|
[1146] | 561 | * |
---|
| 562 | */ |
---|
[1294] | 563 | private void harmonizeSequenceInstancesModel(ISequence sequence, |
---|
| 564 | List<ISequenceInstance> sequenceInstances, |
---|
| 565 | int sequenceLength) |
---|
[1146] | 566 | { |
---|
[1294] | 567 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy.getTaskComparator(); |
---|
[1146] | 568 | |
---|
| 569 | // ensure for each subtask that lexically different variants are preserved |
---|
| 570 | for (int subTaskIndex = 0; subTaskIndex < sequenceLength; subTaskIndex++) { |
---|
| 571 | List<ITask> subTaskVariants = new LinkedList<ITask>(); |
---|
| 572 | |
---|
[1294] | 573 | for (ISequenceInstance sequenceInstance : sequenceInstances) { |
---|
[1146] | 574 | ITask candidate = sequenceInstance.get(subTaskIndex).getTask(); |
---|
| 575 | |
---|
| 576 | boolean found = false; |
---|
| 577 | |
---|
| 578 | for (int i = 0; i < subTaskVariants.size(); i++) { |
---|
[1285] | 579 | if (comparator.areLexicallyEqual(subTaskVariants.get(i), candidate)) { |
---|
[1146] | 580 | taskBuilder.setTask |
---|
| 581 | (sequenceInstance.get(subTaskIndex), subTaskVariants.get(i)); |
---|
| 582 | |
---|
| 583 | found = true; |
---|
| 584 | break; |
---|
| 585 | } |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | if (!found) { |
---|
| 589 | subTaskVariants.add(candidate); |
---|
| 590 | } |
---|
| 591 | } |
---|
| 592 | |
---|
| 593 | // if there are more than one lexically different variant of the sub task at |
---|
| 594 | // the considered position, adapt the sequence model at that position to have |
---|
| 595 | // a selection of the different variants. In this case also adapt the |
---|
| 596 | // generated sequence instances to correctly contain selection instances. If |
---|
| 597 | // there is only one variant of sub tasks at the given position, simply set |
---|
| 598 | // this variant as the sub task of the selection. In this case, the instances |
---|
| 599 | // can be preserved as is |
---|
| 600 | if (subTaskVariants.size() > 1) { |
---|
| 601 | ISelection selection = taskFactory.createNewSelection(); |
---|
| 602 | |
---|
| 603 | for (ITask variant : subTaskVariants) { |
---|
| 604 | taskBuilder.addChild(selection, variant); |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | taskBuilder.addChild(sequence, selection); |
---|
| 608 | |
---|
[1294] | 609 | for (ISequenceInstance instance : sequenceInstances) { |
---|
| 610 | ISelectionInstance selectionInstance = |
---|
[1146] | 611 | taskFactory.createNewTaskInstance(selection); |
---|
[1294] | 612 | taskBuilder.setChild(selectionInstance, instance.get(subTaskIndex)); |
---|
[1146] | 613 | taskBuilder.setTaskInstance(instance, subTaskIndex, selectionInstance); |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | else if (subTaskVariants.size() == 1) { |
---|
| 617 | taskBuilder.addChild(sequence, subTaskVariants.get(0)); |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | /** |
---|
[1127] | 623 | * @param tree |
---|
| 624 | */ |
---|
[1294] | 625 | private List<ISequenceInstance> replaceTaskOccurrences(List<ITaskInstance> task, |
---|
| 626 | List<IUserSession> sessions, |
---|
| 627 | ISequence temporalTaskModel) |
---|
[1127] | 628 | { |
---|
[1294] | 629 | List<ISequenceInstance> sequenceInstances = new LinkedList<ISequenceInstance>(); |
---|
[1146] | 630 | |
---|
| 631 | for (IUserSession session : sessions) { |
---|
[1127] | 632 | int index = -1; |
---|
[1119] | 633 | |
---|
[1127] | 634 | do { |
---|
[1146] | 635 | index = getSubListIndex(session, task, ++index); |
---|
[1127] | 636 | |
---|
| 637 | if (index > -1) { |
---|
[1146] | 638 | sequenceInstances.add |
---|
| 639 | (RuleUtils.createNewSubSequenceInRange |
---|
| 640 | (session, index, index + task.size() - 1, temporalTaskModel, |
---|
| 641 | taskFactory, taskBuilder)); |
---|
[1107] | 642 | } |
---|
| 643 | } |
---|
[1127] | 644 | while (index > -1); |
---|
[1107] | 645 | } |
---|
[1146] | 646 | |
---|
| 647 | return sequenceInstances; |
---|
[1107] | 648 | } |
---|
| 649 | |
---|
| 650 | /** |
---|
[1146] | 651 | * @param trie |
---|
| 652 | * @param object |
---|
[1127] | 653 | * @return |
---|
| 654 | */ |
---|
[1146] | 655 | private int getSubListIndex(ITaskInstanceList list, |
---|
| 656 | List<ITaskInstance> subList, |
---|
| 657 | int startIndex) |
---|
[1127] | 658 | { |
---|
[1146] | 659 | boolean matchFound; |
---|
| 660 | int result = -1; |
---|
[1127] | 661 | |
---|
[1146] | 662 | for (int i = startIndex; i <= list.size() - subList.size(); i++) { |
---|
| 663 | matchFound = true; |
---|
[1127] | 664 | |
---|
[1146] | 665 | for (int j = 0; j < subList.size(); j++) { |
---|
[1285] | 666 | // we prepared the task instances to refer to unique tasks, if they are treated |
---|
| 667 | // as equal. Therefore, we just compare the identity of the tasks of the task |
---|
| 668 | // instances |
---|
| 669 | if (list.get(i + j).getTask() != subList.get(j).getTask()) { |
---|
[1146] | 670 | matchFound = false; |
---|
| 671 | break; |
---|
| 672 | } |
---|
[1127] | 673 | } |
---|
| 674 | |
---|
[1146] | 675 | if (matchFound) { |
---|
| 676 | result = i; |
---|
| 677 | break; |
---|
| 678 | } |
---|
[1127] | 679 | } |
---|
[1107] | 680 | |
---|
[1146] | 681 | return result; |
---|
[1107] | 682 | } |
---|
| 683 | |
---|
| 684 | /** |
---|
| 685 | * @param trie |
---|
| 686 | * @param object |
---|
| 687 | * @return |
---|
| 688 | */ |
---|
[1146] | 689 | private int getSubListIndex(List<ITaskInstance> list, |
---|
| 690 | List<ITaskInstance> subList, |
---|
[1107] | 691 | int startIndex) |
---|
| 692 | { |
---|
| 693 | boolean matchFound; |
---|
| 694 | int result = -1; |
---|
| 695 | |
---|
| 696 | for (int i = startIndex; i <= list.size() - subList.size(); i++) { |
---|
| 697 | matchFound = true; |
---|
| 698 | |
---|
| 699 | for (int j = 0; j < subList.size(); j++) { |
---|
[1285] | 700 | // we prepared the task instances to refer to unique tasks, if they are treated |
---|
| 701 | // as equal. Therefore, we just compare the identity of the tasks of the task |
---|
| 702 | // instances |
---|
| 703 | if (list.get(i + j) != subList.get(j)) { |
---|
[1107] | 704 | matchFound = false; |
---|
| 705 | break; |
---|
| 706 | } |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | if (matchFound) { |
---|
| 710 | result = i; |
---|
| 711 | break; |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | return result; |
---|
| 716 | } |
---|
| 717 | |
---|
[1119] | 718 | /** |
---|
| 719 | * @author Patrick Harms |
---|
| 720 | */ |
---|
[1146] | 721 | private class MaxCountAndLongestTasksFinder implements TrieProcessor<ITaskInstance> { |
---|
[1119] | 722 | |
---|
| 723 | /** |
---|
| 724 | * |
---|
| 725 | */ |
---|
| 726 | private int currentCount; |
---|
| 727 | |
---|
| 728 | /** |
---|
| 729 | * |
---|
| 730 | */ |
---|
[1146] | 731 | private List<List<ITaskInstance>> foundTasks = new LinkedList<List<ITaskInstance>>(); |
---|
[1119] | 732 | |
---|
| 733 | /** |
---|
| 734 | * |
---|
| 735 | */ |
---|
[1127] | 736 | public MaxCountAndLongestTasksFinder() { |
---|
[1119] | 737 | super(); |
---|
| 738 | this.currentCount = 0; |
---|
| 739 | } |
---|
| 740 | |
---|
| 741 | /* (non-Javadoc) |
---|
| 742 | * @see de.ugoe.cs.autoquest.usageprofiles.TrieProcessor#process(java.util.List, int) |
---|
| 743 | */ |
---|
| 744 | @Override |
---|
[1146] | 745 | public TrieProcessor.Result process(List<ITaskInstance> foundTask, int count) { |
---|
| 746 | if (foundTask.size() < 2) { |
---|
| 747 | // ignore single tasks |
---|
[1119] | 748 | return TrieProcessor.Result.CONTINUE; |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | if (count < 2) { |
---|
| 752 | // ignore singular occurrences |
---|
| 753 | return TrieProcessor.Result.SKIP_NODE; |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | if (this.currentCount > count) { |
---|
[1146] | 757 | // ignore this children of this task, as they may have only smaller counts than |
---|
[1127] | 758 | // the already found tasks |
---|
[1119] | 759 | return TrieProcessor.Result.SKIP_NODE; |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | if (this.currentCount < count) { |
---|
[1127] | 763 | // the provided task occurs more often that all tasks found so far. |
---|
| 764 | // clear all found tasks and use the new count as the one searched for |
---|
| 765 | foundTasks.clear(); |
---|
[1119] | 766 | this.currentCount = count; |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | if (this.currentCount == count) { |
---|
[1127] | 770 | // the task is of interest. Sort it into the other found tasks so that |
---|
[1119] | 771 | // the longest come first |
---|
| 772 | boolean added = false; |
---|
[1127] | 773 | for (int i = 0; i < foundTasks.size(); i++) { |
---|
[1146] | 774 | if (foundTasks.get(i).size() < foundTask.size()) { |
---|
[1119] | 775 | // defensive copy |
---|
[1146] | 776 | foundTasks.add(i, new LinkedList<ITaskInstance>(foundTask)); // defensive copy |
---|
[1119] | 777 | added = true; |
---|
| 778 | break; |
---|
| 779 | } |
---|
| 780 | } |
---|
| 781 | |
---|
| 782 | if (!added) { |
---|
[1146] | 783 | foundTasks.add(new LinkedList<ITaskInstance>(foundTask)); // defensive copy |
---|
[1119] | 784 | } |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | return TrieProcessor.Result.CONTINUE; |
---|
| 788 | } |
---|
| 789 | |
---|
| 790 | /** |
---|
[1133] | 791 | * @return |
---|
[1119] | 792 | */ |
---|
[1127] | 793 | public Tasks getFoundTasks() { |
---|
[1119] | 794 | removePermutationsOfShorterTasks(); |
---|
[1127] | 795 | return new Tasks(currentCount, foundTasks); |
---|
[1119] | 796 | } |
---|
| 797 | |
---|
| 798 | /** |
---|
| 799 | * |
---|
| 800 | */ |
---|
| 801 | private void removePermutationsOfShorterTasks() { |
---|
| 802 | // now iterate the sorted list and for each task remove all other tasks that are shorter |
---|
| 803 | // (i.e. come later in the sorted list) and that represent a subpart of the task |
---|
[1127] | 804 | for (int i = 0; i < foundTasks.size(); i++) { |
---|
| 805 | for (int j = i + 1; j < foundTasks.size();) { |
---|
| 806 | if (foundTasks.get(j).size() < foundTasks.get(i).size()) { |
---|
[1119] | 807 | // found a task that is a potential subtask. Check for this and remove the |
---|
| 808 | // subtask if needed |
---|
[1146] | 809 | List<ITaskInstance> longTask = foundTasks.get(i); |
---|
| 810 | List<ITaskInstance> shortTask = foundTasks.get(j); |
---|
[1119] | 811 | |
---|
| 812 | if (getSubListIndex(longTask, shortTask, 0) > -1) { |
---|
[1127] | 813 | foundTasks.remove(j); |
---|
[1119] | 814 | } |
---|
| 815 | else { |
---|
| 816 | j++; |
---|
| 817 | } |
---|
| 818 | } |
---|
| 819 | else { |
---|
| 820 | j++; |
---|
| 821 | } |
---|
| 822 | } |
---|
| 823 | } |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | } |
---|
| 827 | |
---|
[1256] | 828 | // /** |
---|
| 829 | // * @author Patrick Harms |
---|
| 830 | // */ |
---|
| 831 | // private class TrieStatisticsDumper implements TrieProcessor<ITaskInstance> { |
---|
| 832 | // |
---|
| 833 | // /** |
---|
| 834 | // * |
---|
| 835 | // */ |
---|
| 836 | // private Map<Integer, Integer> counters = new HashMap<Integer, Integer>(); |
---|
| 837 | // |
---|
| 838 | // /* (non-Javadoc) |
---|
| 839 | // * @see de.ugoe.cs.autoquest.usageprofiles.TrieProcessor#process(java.util.List, int) |
---|
| 840 | // */ |
---|
| 841 | // @Override |
---|
| 842 | // public TrieProcessor.Result process(List<ITaskInstance> subsequence, int count) { |
---|
| 843 | // if (subsequence.size() == 1) { |
---|
| 844 | // Integer value = counters.get(count); |
---|
| 845 | // |
---|
| 846 | // if (value == null) { |
---|
| 847 | // value = 0; |
---|
| 848 | // } |
---|
| 849 | // |
---|
| 850 | // counters.put(count, value + 1); |
---|
| 851 | // |
---|
| 852 | // return TrieProcessor.Result.CONTINUE; |
---|
| 853 | // } |
---|
| 854 | // else { |
---|
| 855 | // // ignore singular occurrences |
---|
| 856 | // return TrieProcessor.Result.SKIP_NODE; |
---|
| 857 | // } |
---|
| 858 | // } |
---|
| 859 | // |
---|
| 860 | // /** |
---|
| 861 | // * @return |
---|
| 862 | // */ |
---|
| 863 | // public void dumpCounters() { |
---|
| 864 | // int dumpedCounters = 0; |
---|
| 865 | // |
---|
| 866 | // int count = 1; |
---|
| 867 | // while (dumpedCounters < counters.size()) { |
---|
| 868 | // Integer value = counters.get(count++); |
---|
| 869 | // if (value != null) { |
---|
| 870 | // System.out.println(value + " symbols occurred " + count + " times"); |
---|
| 871 | // dumpedCounters++; |
---|
| 872 | // } |
---|
| 873 | // } |
---|
| 874 | // } |
---|
| 875 | // |
---|
| 876 | // } |
---|
| 877 | |
---|
[1119] | 878 | /** |
---|
| 879 | * |
---|
| 880 | */ |
---|
[1133] | 881 | private static class RuleApplicationData { |
---|
[1119] | 882 | |
---|
| 883 | /** |
---|
| 884 | * |
---|
| 885 | */ |
---|
[1146] | 886 | private List<IUserSession> sessions; |
---|
[1119] | 887 | |
---|
| 888 | /** |
---|
| 889 | * |
---|
| 890 | */ |
---|
[1256] | 891 | private TaskInstanceTrie lastTrie; |
---|
[1119] | 892 | |
---|
| 893 | /** |
---|
[1127] | 894 | * default and minimum trained sequence length is 3 |
---|
[1119] | 895 | */ |
---|
[1127] | 896 | private int trainedSequenceLength = 3; |
---|
[1119] | 897 | |
---|
| 898 | /** |
---|
| 899 | * |
---|
| 900 | */ |
---|
[1127] | 901 | private Tasks lastFoundTasks = new Tasks(Integer.MAX_VALUE, null); |
---|
[1119] | 902 | |
---|
| 903 | /** |
---|
[1127] | 904 | * |
---|
[1119] | 905 | */ |
---|
[1146] | 906 | private boolean detectedAndReplacedTasks; |
---|
[1119] | 907 | |
---|
| 908 | /** |
---|
| 909 | * |
---|
| 910 | */ |
---|
[1127] | 911 | private RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 912 | |
---|
| 913 | /** |
---|
| 914 | * |
---|
| 915 | */ |
---|
[1119] | 916 | private StopWatch stopWatch = new StopWatch(); |
---|
| 917 | |
---|
| 918 | /** |
---|
| 919 | * |
---|
| 920 | */ |
---|
[1146] | 921 | private RuleApplicationData(List<IUserSession> sessions) { |
---|
[1127] | 922 | this.sessions = sessions; |
---|
[1119] | 923 | } |
---|
| 924 | |
---|
| 925 | /** |
---|
[1127] | 926 | * @return the tree |
---|
[1119] | 927 | */ |
---|
[1146] | 928 | private List<IUserSession> getSessions() { |
---|
[1127] | 929 | return sessions; |
---|
[1119] | 930 | } |
---|
| 931 | |
---|
| 932 | /** |
---|
[1127] | 933 | * @param lastTrie the lastTrie to set |
---|
[1119] | 934 | */ |
---|
[1256] | 935 | private void setLastTrie(TaskInstanceTrie lastTrie) { |
---|
[1127] | 936 | this.lastTrie = lastTrie; |
---|
[1119] | 937 | } |
---|
| 938 | |
---|
| 939 | /** |
---|
| 940 | * @return the lastTrie |
---|
| 941 | */ |
---|
[1256] | 942 | private TaskInstanceTrie getLastTrie() { |
---|
[1119] | 943 | return lastTrie; |
---|
| 944 | } |
---|
| 945 | |
---|
| 946 | /** |
---|
[1127] | 947 | * @param trainedSequenceLength the trainedSequenceLength to set |
---|
| 948 | */ |
---|
| 949 | private void setTrainedSequenceLength(int trainedSequenceLength) { |
---|
| 950 | this.trainedSequenceLength = trainedSequenceLength; |
---|
| 951 | } |
---|
| 952 | |
---|
| 953 | /** |
---|
[1119] | 954 | * @return the trainedSequenceLength |
---|
| 955 | */ |
---|
| 956 | private int getTrainedSequenceLength() { |
---|
| 957 | return trainedSequenceLength; |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | /** |
---|
[1127] | 961 | * @param lastFoundSequences the lastFoundSequences to set |
---|
[1119] | 962 | */ |
---|
[1127] | 963 | private void setLastFoundTasks(Tasks lastFoundSequences) { |
---|
| 964 | this.lastFoundTasks = lastFoundSequences; |
---|
[1119] | 965 | } |
---|
| 966 | |
---|
| 967 | /** |
---|
[1127] | 968 | * @return the lastFoundSequences |
---|
[1119] | 969 | */ |
---|
[1127] | 970 | private Tasks getLastFoundTasks() { |
---|
| 971 | return lastFoundTasks; |
---|
[1119] | 972 | } |
---|
| 973 | |
---|
| 974 | /** |
---|
| 975 | * |
---|
| 976 | */ |
---|
[1146] | 977 | private void detectedAndReplacedTasks(boolean detectedAndReplacedTasks) { |
---|
| 978 | this.detectedAndReplacedTasks = detectedAndReplacedTasks; |
---|
[1119] | 979 | } |
---|
| 980 | |
---|
| 981 | /** |
---|
| 982 | * |
---|
| 983 | */ |
---|
[1146] | 984 | private boolean detectedAndReplacedTasks() { |
---|
| 985 | return detectedAndReplacedTasks; |
---|
[1119] | 986 | } |
---|
[1127] | 987 | |
---|
| 988 | /** |
---|
| 989 | * @return the result |
---|
| 990 | */ |
---|
| 991 | private RuleApplicationResult getResult() { |
---|
| 992 | return result; |
---|
| 993 | } |
---|
| 994 | |
---|
| 995 | /** |
---|
| 996 | * @return the stopWatch |
---|
| 997 | */ |
---|
| 998 | private StopWatch getStopWatch() { |
---|
| 999 | return stopWatch; |
---|
| 1000 | } |
---|
| 1001 | |
---|
[1119] | 1002 | } |
---|
| 1003 | |
---|
| 1004 | |
---|
| 1005 | /** |
---|
| 1006 | * @author Patrick Harms |
---|
| 1007 | */ |
---|
[1146] | 1008 | private static class Tasks implements Iterable<List<ITaskInstance>> { |
---|
[1119] | 1009 | |
---|
| 1010 | /** |
---|
| 1011 | * |
---|
| 1012 | */ |
---|
| 1013 | private int occurrenceCount; |
---|
| 1014 | |
---|
| 1015 | /** |
---|
| 1016 | * |
---|
| 1017 | */ |
---|
[1146] | 1018 | private List<List<ITaskInstance>> sequences; |
---|
[1119] | 1019 | |
---|
| 1020 | /** |
---|
| 1021 | * @param occurrenceCount |
---|
| 1022 | * @param sequences |
---|
| 1023 | */ |
---|
[1146] | 1024 | private Tasks(int occurrenceCount, List<List<ITaskInstance>> sequences) { |
---|
[1119] | 1025 | super(); |
---|
| 1026 | this.occurrenceCount = occurrenceCount; |
---|
| 1027 | this.sequences = sequences; |
---|
| 1028 | } |
---|
| 1029 | |
---|
| 1030 | /** |
---|
| 1031 | * @return |
---|
| 1032 | */ |
---|
| 1033 | private int getOccurrenceCount() { |
---|
| 1034 | return occurrenceCount; |
---|
| 1035 | } |
---|
| 1036 | |
---|
| 1037 | /** |
---|
| 1038 | * @return |
---|
| 1039 | */ |
---|
| 1040 | private int size() { |
---|
| 1041 | return this.sequences.size(); |
---|
| 1042 | } |
---|
| 1043 | |
---|
| 1044 | /** |
---|
| 1045 | * |
---|
| 1046 | */ |
---|
| 1047 | |
---|
| 1048 | /* (non-Javadoc) |
---|
| 1049 | * @see java.lang.Iterable#iterator() |
---|
| 1050 | */ |
---|
| 1051 | @Override |
---|
[1146] | 1052 | public Iterator<List<ITaskInstance>> iterator() { |
---|
[1119] | 1053 | return this.sequences.iterator(); |
---|
| 1054 | } |
---|
| 1055 | |
---|
[1146] | 1056 | /* (non-Javadoc) |
---|
[1256] | 1057 | * @see java.lang.Object#toString() |
---|
[1146] | 1058 | */ |
---|
| 1059 | @Override |
---|
[1256] | 1060 | public String toString() { |
---|
| 1061 | StringBuffer result = new StringBuffer(); |
---|
| 1062 | result.append(this.occurrenceCount); |
---|
| 1063 | result.append(" occurrences:\n"); |
---|
[1146] | 1064 | |
---|
[1256] | 1065 | for (List<ITaskInstance> task : sequences) { |
---|
| 1066 | result.append(task); |
---|
| 1067 | result.append("\n"); |
---|
[1146] | 1068 | } |
---|
| 1069 | |
---|
[1256] | 1070 | return result.toString(); |
---|
[1146] | 1071 | } |
---|
| 1072 | |
---|
| 1073 | } |
---|
[1337] | 1074 | |
---|
| 1075 | // methods for internal testing |
---|
| 1076 | // private void checkMatchingOfSessions(List<List<Event>> flattenedSessions, |
---|
| 1077 | // List<IUserSession> sessions, |
---|
| 1078 | // String when) |
---|
| 1079 | // { |
---|
| 1080 | // List<List<Event>> currentFlattenedSessions = flattenSessions(sessions); |
---|
| 1081 | // if (flattenedSessions.size() != currentFlattenedSessions.size()) { |
---|
| 1082 | // System.out.println("################## number of sessions changed after " + when); |
---|
| 1083 | // } |
---|
| 1084 | // else { |
---|
| 1085 | // for (int i = 0; i < flattenedSessions.size(); i++) { |
---|
| 1086 | // List<Event> expected = flattenedSessions.get(i); |
---|
| 1087 | // List<Event> current = currentFlattenedSessions.get(i); |
---|
| 1088 | // |
---|
| 1089 | // if (expected.size() != current.size()) { |
---|
| 1090 | // System.out.println |
---|
| 1091 | // ("################## length of session " + i + " changed after " + when); |
---|
| 1092 | // } |
---|
| 1093 | // else { |
---|
| 1094 | // for (int j = 0; j < expected.size(); j++) { |
---|
| 1095 | // if (!expected.get(j).equals(current.get(j))) { |
---|
| 1096 | // System.out.println("################## event " + j + " of session " + |
---|
| 1097 | // i + " changed after " + when); |
---|
| 1098 | // } |
---|
| 1099 | // } |
---|
| 1100 | // } |
---|
| 1101 | // } |
---|
| 1102 | // } |
---|
| 1103 | // } |
---|
| 1104 | // |
---|
| 1105 | // private List<List<Event>> flattenSessions(List<IUserSession> sessions) { |
---|
| 1106 | // List<List<Event>> flattenedSessions = new ArrayList<List<Event>>(); |
---|
| 1107 | // for (IUserSession session : sessions) { |
---|
| 1108 | // List<Event> flattenedUserSession = new ArrayList<Event>(); |
---|
| 1109 | // flatten(session, flattenedUserSession); |
---|
| 1110 | // flattenedSessions.add(flattenedUserSession); |
---|
| 1111 | // } |
---|
| 1112 | // |
---|
| 1113 | // return flattenedSessions; |
---|
| 1114 | // } |
---|
| 1115 | // |
---|
| 1116 | // private void flatten(IUserSession iUserSession, List<Event> flattenedUserSession) { |
---|
| 1117 | // for (ITaskInstance instance : iUserSession) { |
---|
| 1118 | // flatten(instance, flattenedUserSession); |
---|
| 1119 | // } |
---|
| 1120 | // } |
---|
| 1121 | // |
---|
| 1122 | // private void flatten(ITaskInstance instance, List<Event> flattenedUserSession) { |
---|
| 1123 | // if (instance instanceof ITaskInstanceList) { |
---|
| 1124 | // for (ITaskInstance child : (ITaskInstanceList) instance) { |
---|
| 1125 | // flatten(child, flattenedUserSession); |
---|
| 1126 | // } |
---|
| 1127 | // } |
---|
| 1128 | // else if (instance instanceof ISelectionInstance) { |
---|
| 1129 | // flatten(((ISelectionInstance) instance).getChild(), flattenedUserSession); |
---|
| 1130 | // } |
---|
| 1131 | // else if (instance instanceof IOptionalInstance) { |
---|
| 1132 | // flatten(((IOptionalInstance) instance).getChild(), flattenedUserSession); |
---|
| 1133 | // } |
---|
| 1134 | // else if (instance instanceof IEventTaskInstance) { |
---|
| 1135 | // flattenedUserSession.add(((IEventTaskInstance) instance).getEvent()); |
---|
| 1136 | // } |
---|
| 1137 | // } |
---|
| 1138 | |
---|
[1107] | 1139 | } |
---|