[1552] | 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 | |
---|
| 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
| 16 | |
---|
| 17 | import java.util.ArrayList; |
---|
[1621] | 18 | import java.util.Collections; |
---|
| 19 | import java.util.Comparator; |
---|
[1552] | 20 | import java.util.HashMap; |
---|
| 21 | import java.util.HashSet; |
---|
| 22 | import java.util.Iterator; |
---|
| 23 | import java.util.LinkedList; |
---|
| 24 | import java.util.List; |
---|
| 25 | import java.util.Map; |
---|
| 26 | import java.util.Set; |
---|
| 27 | import java.util.logging.Level; |
---|
| 28 | |
---|
[1620] | 29 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Match; |
---|
[1621] | 30 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.MatchOccurence; |
---|
[1553] | 31 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence; |
---|
[1589] | 32 | import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.PairwiseAlignmentGenerator; |
---|
| 33 | import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.PairwiseAlignmentStorage; |
---|
[1572] | 34 | import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.ObjectDistanceSubstitionMatrix; |
---|
[1552] | 35 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; |
---|
| 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
[1630] | 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
[1552] | 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 44 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
| 45 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 46 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
| 47 | import de.ugoe.cs.autoquest.usageprofiles.SymbolMap; |
---|
| 48 | import de.ugoe.cs.util.StopWatch; |
---|
| 49 | import de.ugoe.cs.util.console.Console; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * <p> |
---|
[1559] | 53 | * This class implements the major rule for creating task trees based on a set |
---|
| 54 | * of recorded user sessions. For this, it first harmonizes all tasks. This |
---|
| 55 | * eases later comparison. Then it searches the sessions for iterations and |
---|
| 56 | * replaces them accordingly. Then it searches for sub sequences being the |
---|
| 57 | * longest and occurring most often. For each found sub sequence, it replaces |
---|
| 58 | * the occurrences by creating appropriate {@link ISequence}s. Afterwards, again |
---|
| 59 | * searches for iterations and then again for sub sequences until no more |
---|
| 60 | * replacements are done. |
---|
[1552] | 61 | * </p> |
---|
| 62 | * <p> |
---|
| 63 | * |
---|
[1612] | 64 | * |
---|
[1552] | 65 | * @author Patrick Harms |
---|
| 66 | */ |
---|
| 67 | class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule { |
---|
| 68 | |
---|
[1680] | 69 | |
---|
| 70 | private int iteration = 0; |
---|
[1559] | 71 | /** |
---|
| 72 | * <p> |
---|
| 73 | * the task factory to be used for creating substructures for the temporal |
---|
| 74 | * relationships identified during rul application |
---|
| 75 | * </p> |
---|
| 76 | */ |
---|
| 77 | private ITaskFactory taskFactory; |
---|
| 78 | /** |
---|
| 79 | * <p> |
---|
| 80 | * the task builder to be used for creating substructures for the temporal |
---|
| 81 | * relationships identified during rule application |
---|
| 82 | * </p> |
---|
| 83 | */ |
---|
| 84 | private ITaskBuilder taskBuilder; |
---|
[1552] | 85 | |
---|
[1559] | 86 | /** |
---|
| 87 | * <p> |
---|
| 88 | * the task handling strategy to be used for comparing tasks for |
---|
| 89 | * preparation, i.e., before the tasks are harmonized |
---|
| 90 | * </p> |
---|
| 91 | */ |
---|
| 92 | private TaskHandlingStrategy preparationTaskHandlingStrategy; |
---|
[1552] | 93 | |
---|
[1558] | 94 | |
---|
[1559] | 95 | /** |
---|
| 96 | * <p> |
---|
| 97 | * instantiates the rule and initializes it with a task equality to be |
---|
| 98 | * considered when comparing tasks as well as a task factory and builder to |
---|
| 99 | * be used for creating task structures. |
---|
| 100 | * </p> |
---|
| 101 | * |
---|
| 102 | * @param minimalTaskEquality |
---|
| 103 | * the task equality to be considered when comparing tasks |
---|
| 104 | * @param taskFactory |
---|
| 105 | * the task factory to be used for creating substructures |
---|
| 106 | * @param taskBuilder |
---|
| 107 | * the task builder to be used for creating substructures |
---|
| 108 | */ |
---|
[1552] | 109 | |
---|
[1559] | 110 | SequenceForTaskDetectionRuleAlignment(TaskEquality minimalTaskEquality, |
---|
| 111 | ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
| 112 | this.taskFactory = taskFactory; |
---|
| 113 | this.taskBuilder = taskBuilder; |
---|
[1552] | 114 | |
---|
[1559] | 115 | this.preparationTaskHandlingStrategy = new TaskHandlingStrategy( |
---|
| 116 | minimalTaskEquality); |
---|
[1552] | 117 | |
---|
[1559] | 118 | } |
---|
| 119 | |
---|
| 120 | /* |
---|
| 121 | * (non-Javadoc) |
---|
| 122 | * |
---|
| 123 | * @see java.lang.Object#toString() |
---|
| 124 | */ |
---|
| 125 | @Override |
---|
| 126 | public String toString() { |
---|
| 127 | return "SequenceForTaskDetectionRuleAlignment"; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* |
---|
| 131 | * (non-Javadoc) |
---|
| 132 | * |
---|
| 133 | * @see |
---|
| 134 | * de.ugoe.cs.autoquest.tasktrees.temporalrelation.ISessionScopeRule#apply |
---|
| 135 | * (java.util.List) |
---|
| 136 | */ |
---|
| 137 | @Override |
---|
| 138 | public RuleApplicationResult apply(List<IUserSession> sessions) { |
---|
| 139 | RuleApplicationData appData = new RuleApplicationData(sessions); |
---|
[1687] | 140 | harmonizeEventTaskInstancesModel(appData); |
---|
[1679] | 141 | |
---|
[1630] | 142 | do { |
---|
[1680] | 143 | iteration++; |
---|
| 144 | appData.detectedAndReplacedTasks = false; |
---|
| 145 | appData.getStopWatch().start("whole loop"); |
---|
| 146 | detectAndReplaceIterations(appData); |
---|
| 147 | appData.getStopWatch().start("task replacement"); |
---|
[1668] | 148 | detectAndReplaceTasks(appData); // |
---|
[1680] | 149 | appData.getStopWatch().stop("task replacement"); |
---|
[1657] | 150 | appData.getStopWatch().stop("whole loop"); |
---|
[1680] | 151 | appData.getStopWatch().dumpStatistics(System.out); |
---|
[1657] | 152 | appData.getStopWatch().reset(); |
---|
| 153 | |
---|
| 154 | } while (appData.detectedAndReplacedTasks()); |
---|
| 155 | |
---|
[1559] | 156 | Console.println("created " |
---|
| 157 | + appData.getResult().getNewlyCreatedTasks().size() |
---|
| 158 | + " new tasks and " |
---|
| 159 | + appData.getResult().getNewlyCreatedTaskInstances().size() |
---|
| 160 | + " appropriate instances\n"); |
---|
| 161 | |
---|
| 162 | if ((appData.getResult().getNewlyCreatedTasks().size() > 0) |
---|
| 163 | || (appData.getResult().getNewlyCreatedTaskInstances().size() > 0)) { |
---|
| 164 | appData.getResult().setRuleApplicationStatus( |
---|
| 165 | RuleApplicationStatus.FINISHED); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | return appData.getResult(); |
---|
| 169 | } |
---|
| 170 | |
---|
[1675] | 171 | private ArrayList<NumberSequence> createNumberSequences( |
---|
| 172 | RuleApplicationData appData) { |
---|
| 173 | ArrayList<NumberSequence> result = new ArrayList<NumberSequence>(); |
---|
| 174 | for (int i = 0; i < appData.getSessions().size(); i++) { |
---|
| 175 | IUserSession session = appData.getSessions().get(i); |
---|
| 176 | NumberSequence templist = new NumberSequence(session.size()); |
---|
| 177 | for (int j = 0; j < session.size(); j++) { |
---|
| 178 | ITaskInstance taskInstance = session.get(j); |
---|
| 179 | templist.getSequence()[j] = taskInstance.getTask().getId(); |
---|
| 180 | } |
---|
[1680] | 181 | //System.out.println(); |
---|
[1675] | 182 | // Each NumberSequence is identified by its id, beginning to count |
---|
| 183 | // at zero |
---|
| 184 | templist.setId(i); |
---|
| 185 | result.add(templist); |
---|
| 186 | } |
---|
| 187 | return result; |
---|
| 188 | } |
---|
| 189 | |
---|
[1559] | 190 | /** |
---|
| 191 | * <p> |
---|
| 192 | * harmonizes the event task instances by unifying tasks. This is done, as |
---|
| 193 | * initially the event tasks being equal with respect to the considered task |
---|
| 194 | * equality are distinct objects. The comparison of these distinct objects |
---|
| 195 | * is more time consuming than comparing the object references. |
---|
| 196 | * </p> |
---|
| 197 | * |
---|
| 198 | * @param appData |
---|
| 199 | * the rule application data combining all data used for applying |
---|
| 200 | * this rule |
---|
| 201 | * @return Returns the unique tasks symbol map |
---|
| 202 | */ |
---|
[1675] | 203 | private void harmonizeEventTaskInstancesModel(RuleApplicationData appData) { |
---|
[1559] | 204 | Console.traceln(Level.INFO, |
---|
| 205 | "harmonizing task model of event task instances"); |
---|
| 206 | appData.getStopWatch().start("harmonizing event tasks"); |
---|
[1690] | 207 | SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy.createSymbolMap(); |
---|
[1675] | 208 | |
---|
[1559] | 209 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy |
---|
| 210 | .getTaskComparator(); |
---|
| 211 | |
---|
| 212 | int unifiedTasks = 0; |
---|
| 213 | ITask task; |
---|
| 214 | List<IUserSession> sessions = appData.getSessions(); |
---|
[1657] | 215 | for (int j = 0; j < sessions.size(); j++) { |
---|
[1654] | 216 | IUserSession session = sessions.get(j); |
---|
[1657] | 217 | |
---|
[1559] | 218 | for (int i = 0; i < session.size(); i++) { |
---|
| 219 | ITaskInstance taskInstance = session.get(i); |
---|
[1690] | 220 | task = uniqueTasks.getValue(taskInstance); |
---|
[1559] | 221 | |
---|
| 222 | if (task == null) { |
---|
[1690] | 223 | uniqueTasks.addSymbol(taskInstance, |
---|
[1675] | 224 | taskInstance.getTask()); |
---|
[1690] | 225 | appData.getUniqueTasks().add(taskInstance.getTask()); |
---|
| 226 | appData.getNumber2Task().put(taskInstance.getTask().getId(),taskInstance.getTask()); |
---|
[1559] | 227 | } else { |
---|
| 228 | taskBuilder.setTask(taskInstance, task); |
---|
| 229 | unifiedTasks++; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | comparator.clearBuffers(); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | appData.getStopWatch().stop("harmonizing event tasks"); |
---|
| 236 | Console.traceln(Level.INFO, "harmonized " + unifiedTasks |
---|
[1668] | 237 | + " task occurrences (still " + appData.getUniqueTasks().size() |
---|
[1559] | 238 | + " different tasks)"); |
---|
| 239 | |
---|
| 240 | appData.getStopWatch().dumpStatistics(System.out); |
---|
| 241 | appData.getStopWatch().reset(); |
---|
| 242 | } |
---|
[1552] | 243 | |
---|
[1559] | 244 | /** |
---|
| 245 | * <p> |
---|
| 246 | * searches for direct iterations of single tasks in all sequences and |
---|
| 247 | * replaces them with {@link IIteration}s, respectively appropriate |
---|
| 248 | * instances. Also all single occurrences of a task that is iterated |
---|
| 249 | * somewhen are replaced with iterations to have again an efficient way for |
---|
| 250 | * task comparisons. |
---|
| 251 | * </p> |
---|
| 252 | * |
---|
| 253 | * @param appData |
---|
| 254 | * the rule application data combining all data used for applying |
---|
| 255 | * this rule |
---|
| 256 | */ |
---|
| 257 | private void detectAndReplaceIterations(RuleApplicationData appData) { |
---|
| 258 | Console.traceln(Level.FINE, "detecting iterations"); |
---|
| 259 | appData.getStopWatch().start("detecting iterations"); |
---|
[1552] | 260 | |
---|
[1559] | 261 | List<IUserSession> sessions = appData.getSessions(); |
---|
[1552] | 262 | |
---|
[1559] | 263 | Set<ITask> iteratedTasks = searchIteratedTasks(sessions); |
---|
[1552] | 264 | |
---|
[1559] | 265 | if (iteratedTasks.size() > 0) { |
---|
| 266 | replaceIterationsOf(iteratedTasks, sessions, appData); |
---|
| 267 | } |
---|
[1552] | 268 | |
---|
[1559] | 269 | appData.getStopWatch().stop("detecting iterations"); |
---|
| 270 | Console.traceln(Level.INFO, "replaced " + iteratedTasks.size() |
---|
| 271 | + " iterated tasks"); |
---|
| 272 | } |
---|
[1552] | 273 | |
---|
[1559] | 274 | /** |
---|
| 275 | * <p> |
---|
| 276 | * searches the provided sessions for task iterations. If a task is |
---|
| 277 | * iterated, it is added to the returned set. |
---|
| 278 | * </p> |
---|
| 279 | * |
---|
| 280 | * @param the |
---|
| 281 | * session to search for iterations in |
---|
| 282 | * |
---|
| 283 | * @return a set of tasks being iterated somewhere |
---|
| 284 | */ |
---|
| 285 | private Set<ITask> searchIteratedTasks(List<IUserSession> sessions) { |
---|
| 286 | Set<ITask> iteratedTasks = new HashSet<ITask>(); |
---|
| 287 | for (IUserSession session : sessions) { |
---|
| 288 | for (int i = 0; i < (session.size() - 1); i++) { |
---|
| 289 | // we prepared the task instances to refer to unique tasks, if |
---|
| 290 | // they are treated |
---|
| 291 | // as equal. Therefore, we just compare the identity of the |
---|
| 292 | // tasks of the task |
---|
| 293 | // instances |
---|
| 294 | if (session.get(i).getTask() == session.get(i + 1).getTask()) { |
---|
| 295 | iteratedTasks.add(session.get(i).getTask()); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | return iteratedTasks; |
---|
| 300 | } |
---|
[1552] | 301 | |
---|
[1559] | 302 | /** |
---|
| 303 | * <p> |
---|
| 304 | * replaces all occurrences of all tasks provided in the set with iterations |
---|
| 305 | * </p> |
---|
| 306 | * |
---|
| 307 | * @param iteratedTasks |
---|
| 308 | * the tasks to be replaced with iterations |
---|
| 309 | * @param sessions |
---|
| 310 | * the sessions in which the tasks are to be replaced |
---|
| 311 | * @param appData |
---|
| 312 | * the rule application data combining all data used for applying |
---|
| 313 | * this rule |
---|
| 314 | */ |
---|
| 315 | private void replaceIterationsOf(Set<ITask> iteratedTasks, |
---|
| 316 | List<IUserSession> sessions, RuleApplicationData appData) { |
---|
| 317 | Map<ITask, IIteration> iterations = new HashMap<ITask, IIteration>(); |
---|
| 318 | Map<IIteration, List<IIterationInstance>> iterationInstances = new HashMap<IIteration, List<IIterationInstance>>(); |
---|
[1552] | 319 | |
---|
[1559] | 320 | for (ITask iteratedTask : iteratedTasks) { |
---|
[1675] | 321 | |
---|
[1559] | 322 | IIteration iteration = taskFactory.createNewIteration(); |
---|
[1690] | 323 | appData.getUniqueTasks().add( iteration); |
---|
[1675] | 324 | appData.getNumber2Task().put(iteration.getId(), iteration); |
---|
[1559] | 325 | iterations.put(iteratedTask, iteration); |
---|
| 326 | iterationInstances.put(iteration, |
---|
| 327 | new LinkedList<IIterationInstance>()); |
---|
| 328 | } |
---|
[1552] | 329 | |
---|
[1559] | 330 | IIterationInstance iterationInstance; |
---|
[1552] | 331 | |
---|
[1559] | 332 | for (IUserSession session : sessions) { |
---|
| 333 | int index = 0; |
---|
| 334 | iterationInstance = null; |
---|
[1552] | 335 | |
---|
[1559] | 336 | while (index < session.size()) { |
---|
| 337 | // we prepared the task instances to refer to unique tasks, if |
---|
| 338 | // they are treated |
---|
| 339 | // as equal. Therefore, we just compare the identity of the |
---|
| 340 | // tasks of the task |
---|
| 341 | // instances |
---|
| 342 | ITask currentTask = session.get(index).getTask(); |
---|
| 343 | IIteration iteration = iterations.get(currentTask); |
---|
| 344 | if (iteration != null) { |
---|
| 345 | if ((iterationInstance == null) |
---|
| 346 | || (iterationInstance.getTask() != iteration)) { |
---|
| 347 | iterationInstance = taskFactory |
---|
| 348 | .createNewTaskInstance(iteration); |
---|
| 349 | iterationInstances.get(iteration) |
---|
[1687] | 350 | .add(iterationInstance);//TODO:: Don't create TaskInstances here, use a set of tasks instead |
---|
[1559] | 351 | taskBuilder.addTaskInstance(session, index, |
---|
| 352 | iterationInstance); |
---|
| 353 | index++; |
---|
| 354 | } |
---|
[1552] | 355 | |
---|
[1559] | 356 | taskBuilder.addChild(iterationInstance, session.get(index)); |
---|
| 357 | taskBuilder.removeTaskInstance(session, index); |
---|
| 358 | } else { |
---|
| 359 | if (iterationInstance != null) { |
---|
| 360 | iterationInstance = null; |
---|
| 361 | } |
---|
| 362 | index++; |
---|
| 363 | } |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | } |
---|
[1552] | 367 | |
---|
[1657] | 368 | ISequence matchAsSequence(RuleApplicationData appData, Match m) { |
---|
[1645] | 369 | |
---|
[1657] | 370 | ISequence sequence = taskFactory.createNewSequence(); |
---|
[1690] | 371 | appData.uniqueTasks.add(sequence); |
---|
| 372 | appData.number2task.put(sequence.getId(), sequence); |
---|
| 373 | |
---|
[1657] | 374 | |
---|
| 375 | int[] first = m.getFirstSequence().getSequence(); |
---|
| 376 | int[] second = m.getSecondSequence().getSequence(); |
---|
| 377 | |
---|
| 378 | // Both sequences of a match are equally long |
---|
| 379 | for (int i = 0; i < m.getFirstSequence().size(); i++) { |
---|
| 380 | |
---|
| 381 | // Two gaps aligned to each other: Have not seen it happening so |
---|
| 382 | // far, just to handle it |
---|
| 383 | if (first[i] == -1 && second[i] == -1) { |
---|
| 384 | // TODO: Do nothing? |
---|
| 385 | } |
---|
| 386 | // Both events are equal, we can simply add the task referring to |
---|
| 387 | // the number |
---|
| 388 | else if (first[i] == second[i]) { |
---|
| 389 | taskBuilder.addChild(sequence, |
---|
| 390 | appData.getNumber2Task().get(first[i])); |
---|
| 391 | } |
---|
| 392 | // We have a gap in the first sequence, we need to add the task of |
---|
| 393 | // the second sequence as optional |
---|
| 394 | else if (first[i] == -1 && second[i] != -1) { |
---|
| 395 | IOptional optional = taskFactory.createNewOptional(); |
---|
[1690] | 396 | appData.uniqueTasks.add(optional); |
---|
[1675] | 397 | appData.number2task.put(optional.getId(), optional); |
---|
[1657] | 398 | taskBuilder.setMarkedTask(optional, appData.getNumber2Task() |
---|
| 399 | .get(second[i])); |
---|
| 400 | taskBuilder.addChild(sequence, optional); |
---|
| 401 | } |
---|
| 402 | // We have a gap in the second sequence, we need to add the task of |
---|
| 403 | // the first sequence as optional |
---|
| 404 | else if (first[i] != -1 && second[i] == -1) { |
---|
| 405 | IOptional optional = taskFactory.createNewOptional(); |
---|
[1690] | 406 | appData.uniqueTasks.add(optional); |
---|
[1675] | 407 | appData.number2task.put(optional.getId(), optional); |
---|
[1657] | 408 | taskBuilder.setMarkedTask(optional, appData.getNumber2Task() |
---|
| 409 | .get(first[i])); |
---|
| 410 | taskBuilder.addChild(sequence, optional); |
---|
| 411 | } |
---|
[1675] | 412 | // Both tasks are not equal, we need to insert a selection here |
---|
[1657] | 413 | else { |
---|
| 414 | ISelection selection = taskFactory.createNewSelection(); |
---|
[1690] | 415 | appData.getUniqueTasks().add(selection); |
---|
[1675] | 416 | appData.number2task.put(selection.getId(), selection); |
---|
[1657] | 417 | taskBuilder.addChild(selection, |
---|
| 418 | appData.getNumber2Task().get(first[i])); |
---|
| 419 | taskBuilder.addChild(selection, |
---|
| 420 | appData.getNumber2Task().get(second[i])); |
---|
| 421 | taskBuilder.addChild(sequence, selection); |
---|
| 422 | } |
---|
[1630] | 423 | } |
---|
[1657] | 424 | |
---|
| 425 | // TODO: Debug output |
---|
| 426 | /* |
---|
| 427 | * for (int i =0;i<sequence.getChildren().size();i++) { |
---|
| 428 | * System.out.println(sequence.getChildren().get(i)); |
---|
| 429 | * |
---|
| 430 | * if(sequence.getChildren().get(i).getType() == "selection") { for(int |
---|
| 431 | * j=0; j< ((ISelection) |
---|
| 432 | * sequence.getChildren().get(i)).getChildren().size();j++) { |
---|
| 433 | * System.out.println("\t" +((ISelection) |
---|
| 434 | * sequence.getChildren().get(i)).getChildren().get(j)); } } } |
---|
| 435 | */ |
---|
| 436 | return sequence; |
---|
| 437 | } |
---|
| 438 | |
---|
[1552] | 439 | |
---|
[1559] | 440 | /** |
---|
| 441 | * |
---|
| 442 | * @param appData |
---|
| 443 | * the rule application data combining all data used for applying |
---|
| 444 | * this rule |
---|
| 445 | */ |
---|
| 446 | private void detectAndReplaceTasks(RuleApplicationData appData) { |
---|
| 447 | Console.traceln(Level.FINE, "detecting and replacing tasks"); |
---|
| 448 | appData.getStopWatch().start("detecting tasks"); |
---|
| 449 | |
---|
[1675] | 450 | // Create NumberSequences |
---|
| 451 | appData.setNumberSequences(this.createNumberSequences(appData)); |
---|
| 452 | |
---|
[1668] | 453 | // Generate a substitution matrix between all occurring events. |
---|
[1675] | 454 | Console.traceln(Level.INFO, "generating substitution matrix"); |
---|
| 455 | ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix( |
---|
| 456 | appData.getUniqueTasks(), 6, -3); |
---|
| 457 | submat.generate(); |
---|
[1559] | 458 | |
---|
[1675] | 459 | // Generate pairwise alignments |
---|
| 460 | Console.traceln(Level.INFO, "generating pairwise alignments"); |
---|
| 461 | LinkedList<Match> matchseqs = new LinkedList<Match>(); |
---|
| 462 | PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator |
---|
| 463 | .generate(appData.getNumberSequences(), submat, 9); |
---|
[1559] | 464 | |
---|
[1675] | 465 | // Retrieve all matches reached a specific threshold |
---|
| 466 | Console.traceln(Level.INFO, "retrieving significant sequence pieces"); |
---|
| 467 | for (int i = 0; i < appData.getNumberSequences().size(); i++) { |
---|
| 468 | Console.traceln( |
---|
| 469 | Level.FINEST, |
---|
| 470 | "retrieving significant sequence pieces: " |
---|
| 471 | + Math.round((float) i |
---|
| 472 | / (float) appData.getNumberSequences() |
---|
| 473 | .size() * 100) + "%"); |
---|
| 474 | for (int j = 0; j < appData.getNumberSequences().size(); j++) { |
---|
| 475 | if (i != j) { |
---|
| 476 | matchseqs.addAll(alignments.get(i, j).getMatches()); |
---|
[1668] | 477 | } |
---|
[1675] | 478 | } |
---|
| 479 | } |
---|
[1679] | 480 | |
---|
[1675] | 481 | Console.traceln(Level.FINEST, |
---|
| 482 | "retrieving significant sequence pieces: 100%"); |
---|
| 483 | Console.traceln(Level.INFO, "searching for patterns occuring most"); |
---|
[1559] | 484 | |
---|
[1675] | 485 | // search each match in every other sequence |
---|
| 486 | for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) { |
---|
| 487 | Match pattern = it.next(); |
---|
[1657] | 488 | |
---|
[1675] | 489 | // Skip sequences with more 0 events (scrolls) than other events. |
---|
| 490 | // Both of the pattern sequences are equally long, so the zero |
---|
| 491 | // counts just need to be smaller than the length of one sequence |
---|
| 492 | if (pattern.getFirstSequence().eventCount(0) |
---|
| 493 | + pattern.getSecondSequence().eventCount(0) + 1 > pattern |
---|
| 494 | .getFirstSequence().size()) |
---|
| 495 | continue; |
---|
[1559] | 496 | |
---|
[1675] | 497 | for (int j = 0; j < appData.getNumberSequences().size(); j++) { |
---|
| 498 | LinkedList<Integer> startpositions = appData |
---|
| 499 | .getNumberSequences().get(j).containsPattern(pattern); |
---|
| 500 | if (startpositions.size() > 0) { |
---|
| 501 | for (Iterator<Integer> jt = startpositions.iterator(); jt |
---|
| 502 | .hasNext();) { |
---|
| 503 | int start = jt.next(); |
---|
| 504 | pattern.addOccurence(new MatchOccurence(start, start |
---|
| 505 | + pattern.size(), j)); |
---|
| 506 | } |
---|
[1559] | 507 | |
---|
[1668] | 508 | } |
---|
[1675] | 509 | } |
---|
| 510 | } |
---|
[1668] | 511 | |
---|
[1675] | 512 | Console.traceln(Level.INFO, "sorting results"); |
---|
| 513 | // Sort results to get the most occurring results |
---|
| 514 | Comparator<Match> comparator = new Comparator<Match>() { |
---|
| 515 | public int compare(Match m1, Match m2) { |
---|
| 516 | return m2.occurenceCount() - m1.occurenceCount(); |
---|
[1668] | 517 | |
---|
[1675] | 518 | } |
---|
| 519 | }; |
---|
| 520 | Collections.sort(matchseqs, comparator); |
---|
| 521 | appData.getStopWatch().stop("detecting tasks"); |
---|
[1668] | 522 | |
---|
[1675] | 523 | appData.getStopWatch().start("replacing tasks"); |
---|
| 524 | HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>(); |
---|
| 525 | // Replace matches in the sessions |
---|
| 526 | for (int i = 0; i < matchseqs.size(); i++) { |
---|
| 527 | // Every pattern consists of 2 sequences, therefore the minimum |
---|
| 528 | // occurrences here is 2. |
---|
| 529 | // We just need the sequences also occurring in other sequences as |
---|
| 530 | // well |
---|
| 531 | if (matchseqs.get(i).occurenceCount() > 2) { |
---|
[1679] | 532 | |
---|
| 533 | appData.detectedAndReplacedTasks = true; |
---|
[1675] | 534 | ISequence task = matchAsSequence(appData, matchseqs.get(i)); |
---|
| 535 | invalidOccurence: for (Iterator<MatchOccurence> it = matchseqs |
---|
| 536 | .get(i).getOccurences().iterator(); it.hasNext();) { |
---|
| 537 | MatchOccurence oc = it.next(); |
---|
| 538 | |
---|
[1680] | 539 | if(iteration==-1) { |
---|
| 540 | System.out.println("Trying to replace sequence: "); |
---|
| 541 | matchseqs.get(i).getFirstSequence().printSequence(); |
---|
| 542 | matchseqs.get(i).getSecondSequence().printSequence(); |
---|
| 543 | System.out.println(" in session number: " + |
---|
| 544 | (oc.getSequenceId() + 1) + " at position " + |
---|
| 545 | (oc.getStartindex()) + "-" + oc.getEndindex()); |
---|
| 546 | System.out.println(); |
---|
| 547 | |
---|
[1675] | 548 | |
---|
[1680] | 549 | System.out.println("Printing session: "); |
---|
| 550 | for (int j = 0; j < |
---|
| 551 | appData.getSessions().get(oc.getSequenceId()).size(); |
---|
| 552 | j++) { |
---|
| 553 | System.out.println(j + ": " |
---|
| 554 | + appData.getSessions().get(oc.getSequenceId()).get(j)); |
---|
| 555 | } |
---|
| 556 | } |
---|
[1675] | 557 | |
---|
| 558 | // Check if nothing has been replaced in the sequence we |
---|
| 559 | // want to replace |
---|
| 560 | if (replacedOccurences.get(oc.getSequenceId()) == null) { |
---|
| 561 | replacedOccurences.put(oc.getSequenceId(), |
---|
| 562 | new LinkedList<MatchOccurence>()); |
---|
| 563 | } else { |
---|
| 564 | // check if we have any replaced occurence with indexes |
---|
| 565 | // smaller than ours. If so, we need to adjust our start |
---|
| 566 | // and endpoints |
---|
| 567 | // of the replacement. |
---|
| 568 | // Also do a check if we have replaced this specific |
---|
| 569 | // MatchOccurence in this sequence already. Jump to the |
---|
| 570 | // next occurence if this is the case. |
---|
| 571 | // This is no more neccessary once the matches are |
---|
| 572 | // harmonized. |
---|
| 573 | for (Iterator<MatchOccurence> jt = replacedOccurences |
---|
| 574 | .get(oc.getSequenceId()).iterator(); jt |
---|
| 575 | .hasNext();) { |
---|
| 576 | MatchOccurence tmpOC = jt.next(); |
---|
| 577 | |
---|
| 578 | if (oc.getStartindex() >= tmpOC.getStartindex() |
---|
| 579 | && oc.getStartindex() <= tmpOC |
---|
| 580 | .getEndindex()) { |
---|
| 581 | continue invalidOccurence; |
---|
| 582 | } |
---|
| 583 | if (oc.getEndindex() >= tmpOC.getStartindex()) { |
---|
| 584 | continue invalidOccurence; |
---|
| 585 | |
---|
| 586 | } else if (oc.getStartindex() > tmpOC.getEndindex()) { |
---|
| 587 | int diff = tmpOC.getEndindex() |
---|
| 588 | - tmpOC.getStartindex(); |
---|
| 589 | // Just to be sure. |
---|
| 590 | if (diff > 0) { |
---|
| 591 | oc.setStartindex(oc.getStartindex() - diff |
---|
| 592 | + 1); |
---|
| 593 | oc.setEndindex(oc.getEndindex() - diff + 1); |
---|
| 594 | } else { |
---|
| 595 | Console.traceln(Level.WARNING, |
---|
| 596 | "End index of a Match before start. This should never happen"); |
---|
[1668] | 597 | } |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | } |
---|
[1675] | 601 | ISequenceInstance sequenceInstances = RuleUtils |
---|
| 602 | .createNewSubSequenceInRange(appData.getSessions() |
---|
| 603 | .get(oc.getSequenceId()), oc |
---|
| 604 | .getStartindex(), oc.getEndindex(), task, |
---|
| 605 | taskFactory, taskBuilder); |
---|
| 606 | // Adjust the length of the match regarding to the length of |
---|
| 607 | // instance. (OptionalInstances may be shorter) |
---|
| 608 | oc.setEndindex(oc.getStartindex() |
---|
| 609 | + sequenceInstances.size() |
---|
| 610 | - RuleUtils.missedOptionals); |
---|
| 611 | replacedOccurences.get(oc.getSequenceId()).add(oc); |
---|
[1668] | 612 | } |
---|
[1675] | 613 | } |
---|
| 614 | } |
---|
[1668] | 615 | |
---|
[1675] | 616 | alignments = null; |
---|
| 617 | matchseqs = null; |
---|
[1668] | 618 | appData.getStopWatch().stop("replacing tasks"); |
---|
[1559] | 619 | } |
---|
| 620 | |
---|
[1687] | 621 | |
---|
[1552] | 622 | |
---|
[1559] | 623 | /** |
---|
[1552] | 624 | * |
---|
| 625 | */ |
---|
[1559] | 626 | private static class RuleApplicationData { |
---|
| 627 | |
---|
[1657] | 628 | private HashMap<Integer, ITask> number2task; |
---|
| 629 | |
---|
[1690] | 630 | //TODO: We Actually just need number2task here |
---|
| 631 | private HashSet<ITask> uniqueTasks; |
---|
[1675] | 632 | |
---|
[1630] | 633 | private ArrayList<NumberSequence> numberseqs; |
---|
[1657] | 634 | |
---|
[1559] | 635 | /** |
---|
[1552] | 636 | * |
---|
| 637 | */ |
---|
[1559] | 638 | private List<IUserSession> sessions; |
---|
| 639 | |
---|
| 640 | /** |
---|
[1552] | 641 | * |
---|
| 642 | */ |
---|
[1559] | 643 | private boolean detectedAndReplacedTasks; |
---|
| 644 | |
---|
| 645 | /** |
---|
[1552] | 646 | * |
---|
| 647 | */ |
---|
[1630] | 648 | private RuleApplicationResult result; |
---|
[1559] | 649 | |
---|
| 650 | /** |
---|
[1552] | 651 | * |
---|
| 652 | */ |
---|
[1630] | 653 | private StopWatch stopWatch; |
---|
[1559] | 654 | |
---|
| 655 | /** |
---|
[1552] | 656 | * |
---|
| 657 | */ |
---|
[1559] | 658 | private RuleApplicationData(List<IUserSession> sessions) { |
---|
| 659 | this.sessions = sessions; |
---|
[1630] | 660 | numberseqs = new ArrayList<NumberSequence>(); |
---|
[1690] | 661 | uniqueTasks = new HashSet<ITask>(); |
---|
[1657] | 662 | number2task = new HashMap<Integer, ITask>(); |
---|
| 663 | stopWatch = new StopWatch(); |
---|
| 664 | result = new RuleApplicationResult(); |
---|
[1559] | 665 | } |
---|
[1552] | 666 | |
---|
[1559] | 667 | /** |
---|
| 668 | * @return the tree |
---|
| 669 | */ |
---|
| 670 | private List<IUserSession> getSessions() { |
---|
| 671 | return sessions; |
---|
| 672 | } |
---|
[1552] | 673 | |
---|
[1690] | 674 | private HashSet<ITask> getUniqueTasks() { |
---|
[1668] | 675 | return uniqueTasks; |
---|
| 676 | } |
---|
[1675] | 677 | |
---|
| 678 | // private void setUniqueTasks(SymbolMap<ITaskInstance, ITask> ut) { |
---|
| 679 | // this.uniqueTasks = ut; |
---|
| 680 | // } |
---|
| 681 | |
---|
| 682 | private void setNumberSequences(ArrayList<NumberSequence> numberseqs) { |
---|
| 683 | this.numberseqs = numberseqs; |
---|
[1668] | 684 | } |
---|
[1675] | 685 | |
---|
[1630] | 686 | private ArrayList<NumberSequence> getNumberSequences() { |
---|
| 687 | return numberseqs; |
---|
[1559] | 688 | } |
---|
[1552] | 689 | |
---|
[1559] | 690 | /** |
---|
[1552] | 691 | * |
---|
| 692 | */ |
---|
[1559] | 693 | private boolean detectedAndReplacedTasks() { |
---|
| 694 | return detectedAndReplacedTasks; |
---|
| 695 | } |
---|
[1552] | 696 | |
---|
[1559] | 697 | /** |
---|
| 698 | * @return the result |
---|
| 699 | */ |
---|
| 700 | private RuleApplicationResult getResult() { |
---|
| 701 | return result; |
---|
| 702 | } |
---|
[1552] | 703 | |
---|
[1559] | 704 | /** |
---|
| 705 | * @return the stopWatch |
---|
| 706 | */ |
---|
| 707 | private StopWatch getStopWatch() { |
---|
| 708 | return stopWatch; |
---|
| 709 | } |
---|
[1552] | 710 | |
---|
[1657] | 711 | private HashMap<Integer, ITask> getNumber2Task() { |
---|
[1630] | 712 | return number2task; |
---|
[1559] | 713 | } |
---|
[1552] | 714 | |
---|
[1630] | 715 | } |
---|
[1552] | 716 | |
---|
| 717 | } |
---|