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