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.ITaskInstanceList; |
---|
48 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
49 | import de.ugoe.cs.autoquest.usageprofiles.SymbolMap; |
---|
50 | import de.ugoe.cs.util.StopWatch; |
---|
51 | import de.ugoe.cs.util.console.Console; |
---|
52 | |
---|
53 | /** |
---|
54 | * <p> |
---|
55 | * This class implements the major rule for creating task trees based on a set |
---|
56 | * of recorded user sessions. For this, it first harmonizes all tasks. This |
---|
57 | * eases later comparison. Then it searches the sessions for iterations and |
---|
58 | * replaces them accordingly. Then it searches for sub sequences being the |
---|
59 | * longest and occurring most often. For each found sub sequence, it replaces |
---|
60 | * the occurrences by creating appropriate {@link ISequence}s. Afterwards, again |
---|
61 | * searches for iterations and then again for sub sequences until no more |
---|
62 | * replacements are done. |
---|
63 | * </p> |
---|
64 | * <p> |
---|
65 | * |
---|
66 | * |
---|
67 | * @author Patrick Harms |
---|
68 | */ |
---|
69 | class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule { |
---|
70 | |
---|
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; |
---|
85 | |
---|
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; |
---|
93 | |
---|
94 | /** |
---|
95 | * <p> |
---|
96 | * the task handling strategy to be used for comparing tasks during |
---|
97 | * iteration detection i.e., after the tasks are |
---|
98 | * harmonized |
---|
99 | * </p> |
---|
100 | */ |
---|
101 | private TaskHandlingStrategy identityTaskHandlingStrategy; |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | /** |
---|
106 | * <p> |
---|
107 | * instantiates the rule and initializes it with a task equality to be |
---|
108 | * considered when comparing tasks as well as a task factory and builder to |
---|
109 | * be used for creating task structures. |
---|
110 | * </p> |
---|
111 | * |
---|
112 | * @param minimalTaskEquality |
---|
113 | * the task equality to be considered when comparing tasks |
---|
114 | * @param taskFactory |
---|
115 | * the task factory to be used for creating substructures |
---|
116 | * @param taskBuilder |
---|
117 | * the task builder to be used for creating substructures |
---|
118 | */ |
---|
119 | |
---|
120 | SequenceForTaskDetectionRuleAlignment(TaskEquality minimalTaskEquality, |
---|
121 | ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
122 | this.taskFactory = taskFactory; |
---|
123 | this.taskBuilder = taskBuilder; |
---|
124 | |
---|
125 | this.preparationTaskHandlingStrategy = new TaskHandlingStrategy( |
---|
126 | minimalTaskEquality); |
---|
127 | this.identityTaskHandlingStrategy = new TaskHandlingStrategy( |
---|
128 | TaskEquality.IDENTICAL); |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * (non-Javadoc) |
---|
134 | * |
---|
135 | * @see java.lang.Object#toString() |
---|
136 | */ |
---|
137 | @Override |
---|
138 | public String toString() { |
---|
139 | return "SequenceForTaskDetectionRuleAlignment"; |
---|
140 | } |
---|
141 | |
---|
142 | /* |
---|
143 | * (non-Javadoc) |
---|
144 | * |
---|
145 | * @see |
---|
146 | * de.ugoe.cs.autoquest.tasktrees.temporalrelation.ISessionScopeRule#apply |
---|
147 | * (java.util.List) |
---|
148 | */ |
---|
149 | @Override |
---|
150 | public RuleApplicationResult apply(List<IUserSession> sessions) { |
---|
151 | RuleApplicationData appData = new RuleApplicationData(sessions); |
---|
152 | |
---|
153 | // this is the real rule application. Loop while something is replaced. |
---|
154 | SymbolMap<ITaskInstance, ITask> uniqueTasks = harmonizeEventTaskInstancesModel(appData); |
---|
155 | |
---|
156 | // Generate a substitution matrix between all occurring events. |
---|
157 | Console.traceln(Level.INFO, "generating substitution matrix"); |
---|
158 | ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix( |
---|
159 | uniqueTasks, 6, -3); |
---|
160 | submat.generate(); |
---|
161 | |
---|
162 | // Generate pairwise alignments |
---|
163 | Console.traceln(Level.INFO, "generating pairwise alignments"); |
---|
164 | LinkedList<Match> matchseqs = new LinkedList<Match>(); |
---|
165 | PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator |
---|
166 | .generate(appData.getNumberSequences(), submat, 9); |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | // Retrieve all matches reached a specific threshold |
---|
171 | Console.traceln(Level.INFO, "retrieving significant sequence pieces"); |
---|
172 | for (int i = 0; i < appData.getNumberSequences().size(); i++) { |
---|
173 | Console.traceln( |
---|
174 | Level.FINEST, |
---|
175 | "retrieving significant sequence pieces: " |
---|
176 | + Math.round((float) i / (float) appData.getNumberSequences().size() |
---|
177 | * 100) + "%"); |
---|
178 | for (int j = 0; j < appData.getNumberSequences().size(); j++) { |
---|
179 | if (i != j) { |
---|
180 | matchseqs.addAll(alignments.get(i, j).getMatches()); |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | Console.traceln(Level.FINEST, |
---|
185 | "retrieving significant sequence pieces: 100%"); |
---|
186 | Console.traceln(Level.INFO, "searching for patterns occuring most"); |
---|
187 | |
---|
188 | // search each match in every other sequence |
---|
189 | for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) { |
---|
190 | Match pattern = it.next(); |
---|
191 | |
---|
192 | // Skip sequences with more 0 events (scrolls) than other events. |
---|
193 | // Both of the pattern sequences are equally long, so the zero |
---|
194 | // counts just need to be smaller than the length of one sequence |
---|
195 | if (pattern.getFirstSequence().eventCount(0) |
---|
196 | + pattern.getSecondSequence().eventCount(0) + 1 > pattern |
---|
197 | .getFirstSequence().size()) |
---|
198 | continue; |
---|
199 | |
---|
200 | for (int j = 0; j < appData.getNumberSequences().size(); j++) { |
---|
201 | LinkedList<Integer> startpositions = appData.getNumberSequences().get(j) |
---|
202 | .containsPattern(pattern); |
---|
203 | if (startpositions.size() > 0) { |
---|
204 | NumberSequence tempns = appData.getNumberSequences().get(j); |
---|
205 | |
---|
206 | for (Iterator<Integer> jt = startpositions.iterator(); jt |
---|
207 | .hasNext();) { |
---|
208 | int start = jt.next(); |
---|
209 | //TODO: Debug Output |
---|
210 | System.out.println("Found match "); |
---|
211 | pattern.getFirstSequence().printSequence(); |
---|
212 | pattern.getSecondSequence().printSequence(); |
---|
213 | System.out.println("in sequence " + (j+1) + " at position " + start); |
---|
214 | for(int k=0;k<tempns.getSequence().length;k++) { |
---|
215 | System.out.print(k + ": " + tempns.getSequence()[k] + " "); |
---|
216 | System.out.println(appData.getNumber2Task().get(tempns.getSequence()[k])); |
---|
217 | } |
---|
218 | pattern.addOccurence( |
---|
219 | new MatchOccurence(start, j)); |
---|
220 | } |
---|
221 | |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | Console.traceln(Level.INFO, "sorting results"); |
---|
227 | // Sort results to get the most occurring results |
---|
228 | Comparator<Match> comparator = new Comparator<Match>() { |
---|
229 | public int compare(Match m1, Match m2) { |
---|
230 | return m2.occurenceCount() - m1.occurenceCount(); |
---|
231 | |
---|
232 | } |
---|
233 | }; |
---|
234 | //Collections.sort(matchseqs, comparator); |
---|
235 | |
---|
236 | |
---|
237 | // TODO: Harmonize matches: finding double matches and merge them |
---|
238 | /* |
---|
239 | Console.traceln(Level.INFO, "harmonizing matches"); |
---|
240 | int i=0; |
---|
241 | |
---|
242 | while(i<matchseqs.size()) { |
---|
243 | int j=i; |
---|
244 | while(j<matchseqs.size()) { |
---|
245 | if(i!=j) { |
---|
246 | if(matchseqs.get(i).equals(matchseqs.get(j))) { |
---|
247 | //matchseqs.get(i).addOccurencesOf(matchseqs.get(j)); |
---|
248 | matchseqs.remove(j); |
---|
249 | |
---|
250 | //System.out.println("Sequence " + i); |
---|
251 | //matchseqs.get(i).getFirstSequence().printSequence(); |
---|
252 | //matchseqs.get(i).getSecondSequence().printSequence(); |
---|
253 | //System.out.println("is equal to sequence " + j); |
---|
254 | //matchseqs.get(j).getFirstSequence().printSequence(); |
---|
255 | //matchseqs.get(j).getSecondSequence().printSequence(); |
---|
256 | continue; |
---|
257 | } |
---|
258 | } |
---|
259 | j++; |
---|
260 | } |
---|
261 | i++; |
---|
262 | } |
---|
263 | Collections.sort(matchseqs, comparator); |
---|
264 | */ |
---|
265 | |
---|
266 | |
---|
267 | // Just printing the results out |
---|
268 | for (int i = 0; i < matchseqs.size(); i++) { |
---|
269 | // Every pattern consists of 2 sequences, therefore the minimum |
---|
270 | // occurrences here is 2. |
---|
271 | // We just need the sequences also occurring in other sequences as |
---|
272 | // well |
---|
273 | if (matchseqs.get(i).occurenceCount() > 2) { |
---|
274 | |
---|
275 | ISequence task = matchAsSequence(appData,matchseqs.get(i)); |
---|
276 | for(Iterator<MatchOccurence> it = matchseqs.get(i).getOccurences().iterator();it.hasNext();) { |
---|
277 | MatchOccurence oc = it.next(); |
---|
278 | System.out.println("Trying to replace sequence: "); |
---|
279 | matchseqs.get(i).getFirstSequence().printSequence(); |
---|
280 | matchseqs.get(i).getSecondSequence().printSequence(); |
---|
281 | System.out.println(" in session number: " + (oc.getSequenceId()+1) + " at position " + (oc.getStartindex()) + "-" + (oc.getStartindex()+matchseqs.get(i).getFirstSequence().size())); |
---|
282 | System.out.println("Printing session: "); |
---|
283 | for(int j=0;j<sessions.get(oc.getSequenceId()).size();j++) { |
---|
284 | System.out.println(j+ ": " + sessions.get(oc.getSequenceId()).get(j)); |
---|
285 | } |
---|
286 | List<ISequenceInstance> sequenceInstances = new LinkedList<ISequenceInstance>(); |
---|
287 | RuleUtils.createNewSubSequenceInRange(sessions.get(oc.getSequenceId()), oc.getStartindex(), oc.getStartindex()+matchseqs.get(i).getFirstSequence().size()-1, task, taskFactory, taskBuilder); |
---|
288 | } |
---|
289 | System.out.println(task); |
---|
290 | //matchseqs.get(i).getFirstSequence().printSequence(); |
---|
291 | //matchseqs.get(i).getSecondSequence().printSequence(); |
---|
292 | //System.out.println("Found pattern " |
---|
293 | // + matchseqs.get(i).occurenceCount() + " times"); |
---|
294 | System.out.println(); |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | alignments = null; |
---|
300 | |
---|
301 | do { |
---|
302 | |
---|
303 | appData.getStopWatch().start("whole loop"); // |
---|
304 | detectAndReplaceIterations(appData); |
---|
305 | |
---|
306 | appData.getStopWatch().start("task replacement"); // |
---|
307 | //detectAndReplaceTasks(appData); // |
---|
308 | appData.getStopWatch().stop("task replacement"); // |
---|
309 | appData.getStopWatch().stop("whole loop"); |
---|
310 | |
---|
311 | appData.getStopWatch().dumpStatistics(System.out); // |
---|
312 | appData.getStopWatch().reset(); |
---|
313 | |
---|
314 | } while (appData.detectedAndReplacedTasks()); |
---|
315 | |
---|
316 | |
---|
317 | Console.println("created " |
---|
318 | + appData.getResult().getNewlyCreatedTasks().size() |
---|
319 | + " new tasks and " |
---|
320 | + appData.getResult().getNewlyCreatedTaskInstances().size() |
---|
321 | + " appropriate instances\n"); |
---|
322 | |
---|
323 | if ((appData.getResult().getNewlyCreatedTasks().size() > 0) |
---|
324 | || (appData.getResult().getNewlyCreatedTaskInstances().size() > 0)) { |
---|
325 | appData.getResult().setRuleApplicationStatus( |
---|
326 | RuleApplicationStatus.FINISHED); |
---|
327 | } |
---|
328 | |
---|
329 | return appData.getResult(); |
---|
330 | } |
---|
331 | |
---|
332 | |
---|
333 | |
---|
334 | /** |
---|
335 | * <p> |
---|
336 | * harmonizes the event task instances by unifying tasks. This is done, as |
---|
337 | * initially the event tasks being equal with respect to the considered task |
---|
338 | * equality are distinct objects. The comparison of these distinct objects |
---|
339 | * is more time consuming than comparing the object references. |
---|
340 | * </p> |
---|
341 | * |
---|
342 | * @param appData |
---|
343 | * the rule application data combining all data used for applying |
---|
344 | * this rule |
---|
345 | * @return Returns the unique tasks symbol map |
---|
346 | */ |
---|
347 | private SymbolMap<ITaskInstance, ITask> harmonizeEventTaskInstancesModel( |
---|
348 | RuleApplicationData appData) { |
---|
349 | Console.traceln(Level.INFO, |
---|
350 | "harmonizing task model of event task instances"); |
---|
351 | appData.getStopWatch().start("harmonizing event tasks"); |
---|
352 | |
---|
353 | SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy |
---|
354 | .createSymbolMap(); |
---|
355 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy |
---|
356 | .getTaskComparator(); |
---|
357 | |
---|
358 | int unifiedTasks = 0; |
---|
359 | ITask task; |
---|
360 | List<IUserSession> sessions = appData.getSessions(); |
---|
361 | for (int j = 0; j< sessions.size();j++) { |
---|
362 | IUserSession session = sessions.get(j); |
---|
363 | |
---|
364 | NumberSequence templist = new NumberSequence(session.size()); |
---|
365 | |
---|
366 | for (int i = 0; i < session.size(); i++) { |
---|
367 | ITaskInstance taskInstance = session.get(i); |
---|
368 | task = uniqueTasks.getValue(taskInstance); |
---|
369 | |
---|
370 | if (task == null) { |
---|
371 | uniqueTasks.addSymbol(taskInstance, taskInstance.getTask()); |
---|
372 | templist.getSequence()[i] = taskInstance.getTask().getId(); |
---|
373 | |
---|
374 | } else { |
---|
375 | taskBuilder.setTask(taskInstance, task); |
---|
376 | templist.getSequence()[i] = task.getId(); |
---|
377 | unifiedTasks++; |
---|
378 | } |
---|
379 | appData.getNumber2Task().put(templist.getSequence()[i], taskInstance.getTask()); |
---|
380 | |
---|
381 | //System.out.println("TaskID: " + taskInstance.getTask().getId()+ " Numbersequence: " + templist.getSequence()[i]); |
---|
382 | } |
---|
383 | //Each NumberSequence is identified by its id, beginning to count at zero |
---|
384 | templist.setId(j); |
---|
385 | appData.getNumberSequences().add(templist); |
---|
386 | comparator.clearBuffers(); |
---|
387 | } |
---|
388 | |
---|
389 | appData.getStopWatch().stop("harmonizing event tasks"); |
---|
390 | Console.traceln(Level.INFO, "harmonized " + unifiedTasks |
---|
391 | + " task occurrences (still " + uniqueTasks.size() |
---|
392 | + " different tasks)"); |
---|
393 | |
---|
394 | appData.getStopWatch().dumpStatistics(System.out); |
---|
395 | appData.getStopWatch().reset(); |
---|
396 | return uniqueTasks; |
---|
397 | } |
---|
398 | |
---|
399 | /** |
---|
400 | * <p> |
---|
401 | * searches for direct iterations of single tasks in all sequences and |
---|
402 | * replaces them with {@link IIteration}s, respectively appropriate |
---|
403 | * instances. Also all single occurrences of a task that is iterated |
---|
404 | * somewhen are replaced with iterations to have again an efficient way for |
---|
405 | * task comparisons. |
---|
406 | * </p> |
---|
407 | * |
---|
408 | * @param appData |
---|
409 | * the rule application data combining all data used for applying |
---|
410 | * this rule |
---|
411 | */ |
---|
412 | private void detectAndReplaceIterations(RuleApplicationData appData) { |
---|
413 | Console.traceln(Level.FINE, "detecting iterations"); |
---|
414 | appData.getStopWatch().start("detecting iterations"); |
---|
415 | |
---|
416 | List<IUserSession> sessions = appData.getSessions(); |
---|
417 | |
---|
418 | Set<ITask> iteratedTasks = searchIteratedTasks(sessions); |
---|
419 | |
---|
420 | if (iteratedTasks.size() > 0) { |
---|
421 | replaceIterationsOf(iteratedTasks, sessions, appData); |
---|
422 | } |
---|
423 | |
---|
424 | appData.getStopWatch().stop("detecting iterations"); |
---|
425 | Console.traceln(Level.INFO, "replaced " + iteratedTasks.size() |
---|
426 | + " iterated tasks"); |
---|
427 | } |
---|
428 | |
---|
429 | /** |
---|
430 | * <p> |
---|
431 | * searches the provided sessions for task iterations. If a task is |
---|
432 | * iterated, it is added to the returned set. |
---|
433 | * </p> |
---|
434 | * |
---|
435 | * @param the |
---|
436 | * session to search for iterations in |
---|
437 | * |
---|
438 | * @return a set of tasks being iterated somewhere |
---|
439 | */ |
---|
440 | private Set<ITask> searchIteratedTasks(List<IUserSession> sessions) { |
---|
441 | Set<ITask> iteratedTasks = new HashSet<ITask>(); |
---|
442 | for (IUserSession session : sessions) { |
---|
443 | for (int i = 0; i < (session.size() - 1); i++) { |
---|
444 | // we prepared the task instances to refer to unique tasks, if |
---|
445 | // they are treated |
---|
446 | // as equal. Therefore, we just compare the identity of the |
---|
447 | // tasks of the task |
---|
448 | // instances |
---|
449 | if (session.get(i).getTask() == session.get(i + 1).getTask()) { |
---|
450 | iteratedTasks.add(session.get(i).getTask()); |
---|
451 | } |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | return iteratedTasks; |
---|
456 | } |
---|
457 | |
---|
458 | /** |
---|
459 | * <p> |
---|
460 | * replaces all occurrences of all tasks provided in the set with iterations |
---|
461 | * </p> |
---|
462 | * |
---|
463 | * @param iteratedTasks |
---|
464 | * the tasks to be replaced with iterations |
---|
465 | * @param sessions |
---|
466 | * the sessions in which the tasks are to be replaced |
---|
467 | * @param appData |
---|
468 | * the rule application data combining all data used for applying |
---|
469 | * this rule |
---|
470 | */ |
---|
471 | private void replaceIterationsOf(Set<ITask> iteratedTasks, |
---|
472 | List<IUserSession> sessions, RuleApplicationData appData) { |
---|
473 | Map<ITask, IIteration> iterations = new HashMap<ITask, IIteration>(); |
---|
474 | Map<IIteration, List<IIterationInstance>> iterationInstances = new HashMap<IIteration, List<IIterationInstance>>(); |
---|
475 | |
---|
476 | for (ITask iteratedTask : iteratedTasks) { |
---|
477 | IIteration iteration = taskFactory.createNewIteration(); |
---|
478 | iterations.put(iteratedTask, iteration); |
---|
479 | iterationInstances.put(iteration, |
---|
480 | new LinkedList<IIterationInstance>()); |
---|
481 | } |
---|
482 | |
---|
483 | IIterationInstance iterationInstance; |
---|
484 | |
---|
485 | for (IUserSession session : sessions) { |
---|
486 | int index = 0; |
---|
487 | iterationInstance = null; |
---|
488 | |
---|
489 | while (index < session.size()) { |
---|
490 | // we prepared the task instances to refer to unique tasks, if |
---|
491 | // they are treated |
---|
492 | // as equal. Therefore, we just compare the identity of the |
---|
493 | // tasks of the task |
---|
494 | // instances |
---|
495 | ITask currentTask = session.get(index).getTask(); |
---|
496 | IIteration iteration = iterations.get(currentTask); |
---|
497 | if (iteration != null) { |
---|
498 | if ((iterationInstance == null) |
---|
499 | || (iterationInstance.getTask() != iteration)) { |
---|
500 | iterationInstance = taskFactory |
---|
501 | .createNewTaskInstance(iteration); |
---|
502 | iterationInstances.get(iteration) |
---|
503 | .add(iterationInstance); |
---|
504 | taskBuilder.addTaskInstance(session, index, |
---|
505 | iterationInstance); |
---|
506 | index++; |
---|
507 | } |
---|
508 | |
---|
509 | taskBuilder.addChild(iterationInstance, session.get(index)); |
---|
510 | taskBuilder.removeTaskInstance(session, index); |
---|
511 | } else { |
---|
512 | if (iterationInstance != null) { |
---|
513 | iterationInstance = null; |
---|
514 | } |
---|
515 | index++; |
---|
516 | } |
---|
517 | } |
---|
518 | } |
---|
519 | |
---|
520 | for (Map.Entry<IIteration, List<IIterationInstance>> entry : iterationInstances |
---|
521 | .entrySet()) { |
---|
522 | harmonizeIterationInstancesModel(entry.getKey(), entry.getValue()); |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | |
---|
527 | ISequence matchAsSequence(RuleApplicationData appData,Match m) { |
---|
528 | |
---|
529 | ISequence sequence = taskFactory.createNewSequence(); |
---|
530 | |
---|
531 | int[] first = m.getFirstSequence().getSequence(); |
---|
532 | int[] second = m.getSecondSequence().getSequence(); |
---|
533 | |
---|
534 | |
---|
535 | //Both sequences of a match are equally long |
---|
536 | for(int i=0;i<m.getFirstSequence().size();i++) { |
---|
537 | |
---|
538 | //Two gaps aligned to each other: Have not seen it happening so far, just to handle it |
---|
539 | if(first[i]==-1 && second[i]==-1) { |
---|
540 | //TODO: Do nothing? |
---|
541 | } |
---|
542 | //Both events are equal, we can simply add the task referring to the number |
---|
543 | else if(first[i]==second[i]) { |
---|
544 | taskBuilder.addChild(sequence, appData.getNumber2Task().get(first[i])); |
---|
545 | } |
---|
546 | //We have a gap in the first sequence, we need to add the task of the second sequence as optional |
---|
547 | else if(first[i]==-1 && second[i]!=-1) { |
---|
548 | IOptional optional = taskFactory.createNewOptional(); |
---|
549 | taskBuilder.setMarkedTask(optional, appData.getNumber2Task().get(second[i])); |
---|
550 | taskBuilder.addChild(sequence,optional); |
---|
551 | } |
---|
552 | //We have a gap in the second sequence, we need to add the task of the first sequence as optional |
---|
553 | else if(first[i]!=-1 && second[i]==-1) { |
---|
554 | IOptional optional = taskFactory.createNewOptional(); |
---|
555 | taskBuilder.setMarkedTask(optional, appData.getNumber2Task().get(first[i])); |
---|
556 | taskBuilder.addChild(sequence,optional); |
---|
557 | } |
---|
558 | //Both tasks are unequal, we need to insert a selection here |
---|
559 | else { |
---|
560 | ISelection selection = taskFactory.createNewSelection(); |
---|
561 | taskBuilder.addChild(selection, appData.getNumber2Task().get(first[i])); |
---|
562 | taskBuilder.addChild(selection, appData.getNumber2Task().get(second[i])); |
---|
563 | taskBuilder.addChild(sequence,selection); |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | //TODO: Debug output |
---|
568 | for (int i =0;i<sequence.getChildren().size();i++) { |
---|
569 | System.out.println(sequence.getChildren().get(i)); |
---|
570 | |
---|
571 | if(sequence.getChildren().get(i).getType() == "selection") { |
---|
572 | for(int j=0; j< ((ISelection) sequence.getChildren().get(i)).getChildren().size();j++) { |
---|
573 | System.out.println("\t" +((ISelection) sequence.getChildren().get(i)).getChildren().get(j)); |
---|
574 | } |
---|
575 | } |
---|
576 | } |
---|
577 | return sequence; |
---|
578 | } |
---|
579 | |
---|
580 | |
---|
581 | /** |
---|
582 | * <p> |
---|
583 | * TODO clarify why this is done |
---|
584 | * </p> |
---|
585 | */ |
---|
586 | private void harmonizeIterationInstancesModel(IIteration iteration, |
---|
587 | List<IIterationInstance> iterationInstances) { |
---|
588 | List<ITask> iteratedTaskVariants = new LinkedList<ITask>(); |
---|
589 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy |
---|
590 | .getTaskComparator(); |
---|
591 | |
---|
592 | // merge the lexically different variants of iterated task to a unique |
---|
593 | // list |
---|
594 | for (IIterationInstance iterationInstance : iterationInstances) { |
---|
595 | for (ITaskInstance executionVariant : iterationInstance) { |
---|
596 | ITask candidate = executionVariant.getTask(); |
---|
597 | |
---|
598 | boolean found = false; |
---|
599 | for (ITask taskVariant : iteratedTaskVariants) { |
---|
600 | if (comparator.areLexicallyEqual(taskVariant, candidate)) { |
---|
601 | taskBuilder.setTask(executionVariant, taskVariant); |
---|
602 | found = true; |
---|
603 | break; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | if (!found) { |
---|
608 | iteratedTaskVariants.add(candidate); |
---|
609 | } |
---|
610 | } |
---|
611 | } |
---|
612 | |
---|
613 | // if there are more than one lexically different variant of iterated |
---|
614 | // tasks, adapt the |
---|
615 | // iteration model to be a selection of different variants. In this case |
---|
616 | // also adapt |
---|
617 | // the generated iteration instances to correctly contain selection |
---|
618 | // instances. If there |
---|
619 | // is only one variant of an iterated task, simply set this as the |
---|
620 | // marked task of the |
---|
621 | // iteration. In this case, the instances can be preserved as is |
---|
622 | if (iteratedTaskVariants.size() > 1) { |
---|
623 | ISelection selection = taskFactory.createNewSelection(); |
---|
624 | |
---|
625 | for (ITask variant : iteratedTaskVariants) { |
---|
626 | taskBuilder.addChild(selection, variant); |
---|
627 | } |
---|
628 | |
---|
629 | taskBuilder.setMarkedTask(iteration, selection); |
---|
630 | |
---|
631 | for (IIterationInstance instance : iterationInstances) { |
---|
632 | for (int i = 0; i < instance.size(); i++) { |
---|
633 | ISelectionInstance selectionInstance = taskFactory |
---|
634 | .createNewTaskInstance(selection); |
---|
635 | taskBuilder.setChild(selectionInstance, instance.get(i)); |
---|
636 | taskBuilder.setTaskInstance(instance, i, selectionInstance); |
---|
637 | } |
---|
638 | } |
---|
639 | } else { |
---|
640 | taskBuilder.setMarkedTask(iteration, iteratedTaskVariants.get(0)); |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | * TODO go on commenting |
---|
646 | * |
---|
647 | * @param appData |
---|
648 | * the rule application data combining all data used for applying |
---|
649 | * this rule |
---|
650 | */ |
---|
651 | private void detectAndReplaceTasks(RuleApplicationData appData) { |
---|
652 | Console.traceln(Level.FINE, "detecting and replacing tasks"); |
---|
653 | appData.getStopWatch().start("detecting tasks"); |
---|
654 | |
---|
655 | //getSequencesOccuringMostOften(appData); |
---|
656 | |
---|
657 | appData.getStopWatch().stop("detecting tasks"); |
---|
658 | appData.getStopWatch().start("replacing tasks"); |
---|
659 | |
---|
660 | replaceSequencesOccurringMostOften(appData); |
---|
661 | |
---|
662 | appData.getStopWatch().stop("replacing tasks"); |
---|
663 | |
---|
664 | //Console.traceln(Level.INFO, "detected and replaced " |
---|
665 | // + appData.getLastFoundTasks().size() + " tasks occuring " |
---|
666 | // + appData.getLastFoundTasks().getOccurrenceCount() + " times"); |
---|
667 | } |
---|
668 | |
---|
669 | |
---|
670 | |
---|
671 | |
---|
672 | /** |
---|
673 | * @param appData |
---|
674 | * the rule application data combining all data used for applying |
---|
675 | * this rule |
---|
676 | */ |
---|
677 | private void replaceSequencesOccurringMostOften(RuleApplicationData appData) { |
---|
678 | appData.detectedAndReplacedTasks(false); |
---|
679 | |
---|
680 | /* |
---|
681 | Console.traceln(Level.FINER, "replacing tasks occurrences"); |
---|
682 | |
---|
683 | for (List<ITaskInstance> task : appData.getLastFoundTasks()) { |
---|
684 | ISequence sequence = taskFactory.createNewSequence(); |
---|
685 | |
---|
686 | Console.traceln(Level.FINEST, "replacing " + sequence.getId() |
---|
687 | + ": " + task); |
---|
688 | |
---|
689 | List<ISequenceInstance> sequenceInstances = replaceTaskOccurrences( |
---|
690 | task, appData.getSessions(), sequence); |
---|
691 | |
---|
692 | harmonizeSequenceInstancesModel(sequence, sequenceInstances, |
---|
693 | task.size()); |
---|
694 | appData.detectedAndReplacedTasks(appData |
---|
695 | .detectedAndReplacedTasks() |
---|
696 | || (sequenceInstances.size() > 0)); |
---|
697 | |
---|
698 | if (sequenceInstances.size() < appData.getLastFoundTasks() |
---|
699 | .getOccurrenceCount()) { |
---|
700 | Console.traceln(Level.FINE, |
---|
701 | sequence.getId() |
---|
702 | + ": replaced task only " |
---|
703 | + sequenceInstances.size() |
---|
704 | + " times instead of expected " |
---|
705 | + appData.getLastFoundTasks() |
---|
706 | .getOccurrenceCount()); |
---|
707 | } |
---|
708 | } |
---|
709 | */ |
---|
710 | } |
---|
711 | |
---|
712 | |
---|
713 | /** |
---|
714 | * |
---|
715 | */ |
---|
716 | private void harmonizeSequenceInstancesModel(ISequence sequence, |
---|
717 | List<ISequenceInstance> sequenceInstances, int sequenceLength) { |
---|
718 | TaskInstanceComparator comparator = preparationTaskHandlingStrategy |
---|
719 | .getTaskComparator(); |
---|
720 | |
---|
721 | // ensure for each subtask that lexically different variants are |
---|
722 | // preserved |
---|
723 | for (int subTaskIndex = 0; subTaskIndex < sequenceLength; subTaskIndex++) { |
---|
724 | List<ITask> subTaskVariants = new LinkedList<ITask>(); |
---|
725 | |
---|
726 | for (ISequenceInstance sequenceInstance : sequenceInstances) { |
---|
727 | ITask candidate = sequenceInstance.get(subTaskIndex).getTask(); |
---|
728 | |
---|
729 | boolean found = false; |
---|
730 | |
---|
731 | for (int i = 0; i < subTaskVariants.size(); i++) { |
---|
732 | if (comparator.areLexicallyEqual(subTaskVariants.get(i), |
---|
733 | candidate)) { |
---|
734 | taskBuilder.setTask(sequenceInstance.get(subTaskIndex), |
---|
735 | subTaskVariants.get(i)); |
---|
736 | |
---|
737 | found = true; |
---|
738 | break; |
---|
739 | } |
---|
740 | } |
---|
741 | |
---|
742 | if (!found) { |
---|
743 | subTaskVariants.add(candidate); |
---|
744 | } |
---|
745 | } |
---|
746 | |
---|
747 | // if there are more than one lexically different variant of the sub |
---|
748 | // task at |
---|
749 | // the considered position, adapt the sequence model at that |
---|
750 | // position to have |
---|
751 | // a selection of the different variants. In this case also adapt |
---|
752 | // the |
---|
753 | // generated sequence instances to correctly contain selection |
---|
754 | // instances. If |
---|
755 | // there is only one variant of sub tasks at the given position, |
---|
756 | // simply set |
---|
757 | // this variant as the sub task of the selection. In this case, the |
---|
758 | // instances |
---|
759 | // can be preserved as is |
---|
760 | if (subTaskVariants.size() > 1) { |
---|
761 | ISelection selection = taskFactory.createNewSelection(); |
---|
762 | |
---|
763 | for (ITask variant : subTaskVariants) { |
---|
764 | taskBuilder.addChild(selection, variant); |
---|
765 | } |
---|
766 | |
---|
767 | taskBuilder.addChild(sequence, selection); |
---|
768 | |
---|
769 | for (ISequenceInstance instance : sequenceInstances) { |
---|
770 | ISelectionInstance selectionInstance = taskFactory |
---|
771 | .createNewTaskInstance(selection); |
---|
772 | taskBuilder.setChild(selectionInstance, |
---|
773 | instance.get(subTaskIndex)); |
---|
774 | taskBuilder.setTaskInstance(instance, subTaskIndex, |
---|
775 | selectionInstance); |
---|
776 | } |
---|
777 | } else if (subTaskVariants.size() == 1) { |
---|
778 | taskBuilder.addChild(sequence, subTaskVariants.get(0)); |
---|
779 | } |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | /** |
---|
784 | * @param tree |
---|
785 | */ |
---|
786 | private List<ISequenceInstance> replaceTaskOccurrences( |
---|
787 | List<ITaskInstance> task, List<IUserSession> sessions, |
---|
788 | ISequence temporalTaskModel) { |
---|
789 | List<ISequenceInstance> sequenceInstances = new LinkedList<ISequenceInstance>(); |
---|
790 | |
---|
791 | for (IUserSession session : sessions) { |
---|
792 | int index = -1; |
---|
793 | |
---|
794 | do { |
---|
795 | index = getSubListIndex(session, task, ++index); |
---|
796 | |
---|
797 | if (index > -1) { |
---|
798 | sequenceInstances.add(RuleUtils |
---|
799 | .createNewSubSequenceInRange(session, index, index |
---|
800 | + task.size() - 1, temporalTaskModel, |
---|
801 | taskFactory, taskBuilder)); |
---|
802 | } |
---|
803 | } while (index > -1); |
---|
804 | } |
---|
805 | |
---|
806 | return sequenceInstances; |
---|
807 | } |
---|
808 | |
---|
809 | /** |
---|
810 | * @param trie |
---|
811 | * @param object |
---|
812 | * @return |
---|
813 | */ |
---|
814 | private int getSubListIndex(ITaskInstanceList list, |
---|
815 | List<ITaskInstance> subList, int startIndex) { |
---|
816 | boolean matchFound; |
---|
817 | int result = -1; |
---|
818 | |
---|
819 | for (int i = startIndex; i <= list.size() - subList.size(); i++) { |
---|
820 | matchFound = true; |
---|
821 | |
---|
822 | for (int j = 0; j < subList.size(); j++) { |
---|
823 | // we prepared the task instances to refer to unique tasks, if |
---|
824 | // they are treated |
---|
825 | // as equal. Therefore, we just compare the identity of the |
---|
826 | // tasks of the task |
---|
827 | // instances |
---|
828 | if (list.get(i + j).getTask() != subList.get(j).getTask()) { |
---|
829 | matchFound = false; |
---|
830 | break; |
---|
831 | } |
---|
832 | } |
---|
833 | |
---|
834 | if (matchFound) { |
---|
835 | result = i; |
---|
836 | break; |
---|
837 | } |
---|
838 | } |
---|
839 | |
---|
840 | return result; |
---|
841 | } |
---|
842 | |
---|
843 | |
---|
844 | /** |
---|
845 | * |
---|
846 | */ |
---|
847 | private static class RuleApplicationData { |
---|
848 | |
---|
849 | |
---|
850 | private HashMap<Integer,ITask> number2task; |
---|
851 | |
---|
852 | |
---|
853 | private ArrayList<NumberSequence> numberseqs; |
---|
854 | |
---|
855 | /** |
---|
856 | * |
---|
857 | */ |
---|
858 | private List<IUserSession> sessions; |
---|
859 | |
---|
860 | |
---|
861 | |
---|
862 | /** |
---|
863 | * |
---|
864 | */ |
---|
865 | private boolean detectedAndReplacedTasks; |
---|
866 | |
---|
867 | /** |
---|
868 | * |
---|
869 | */ |
---|
870 | private RuleApplicationResult result; |
---|
871 | |
---|
872 | /** |
---|
873 | * |
---|
874 | */ |
---|
875 | private StopWatch stopWatch; |
---|
876 | |
---|
877 | /** |
---|
878 | * |
---|
879 | */ |
---|
880 | private RuleApplicationData(List<IUserSession> sessions) { |
---|
881 | this.sessions = sessions; |
---|
882 | numberseqs = new ArrayList<NumberSequence>(); |
---|
883 | number2task = new HashMap<Integer,ITask>(); |
---|
884 | stopWatch= new StopWatch(); |
---|
885 | result = new RuleApplicationResult(); |
---|
886 | } |
---|
887 | |
---|
888 | /** |
---|
889 | * @return the tree |
---|
890 | */ |
---|
891 | private List<IUserSession> getSessions() { |
---|
892 | return sessions; |
---|
893 | } |
---|
894 | |
---|
895 | |
---|
896 | private ArrayList<NumberSequence> getNumberSequences() { |
---|
897 | return numberseqs; |
---|
898 | } |
---|
899 | |
---|
900 | |
---|
901 | |
---|
902 | /** |
---|
903 | * |
---|
904 | */ |
---|
905 | private void detectedAndReplacedTasks(boolean detectedAndReplacedTasks) { |
---|
906 | this.detectedAndReplacedTasks = detectedAndReplacedTasks; |
---|
907 | } |
---|
908 | |
---|
909 | /** |
---|
910 | * |
---|
911 | */ |
---|
912 | private boolean detectedAndReplacedTasks() { |
---|
913 | return detectedAndReplacedTasks; |
---|
914 | } |
---|
915 | |
---|
916 | /** |
---|
917 | * @return the result |
---|
918 | */ |
---|
919 | private RuleApplicationResult getResult() { |
---|
920 | return result; |
---|
921 | } |
---|
922 | |
---|
923 | /** |
---|
924 | * @return the stopWatch |
---|
925 | */ |
---|
926 | private StopWatch getStopWatch() { |
---|
927 | return stopWatch; |
---|
928 | } |
---|
929 | |
---|
930 | private HashMap<Integer,ITask> getNumber2Task() { |
---|
931 | return number2task; |
---|
932 | } |
---|
933 | |
---|
934 | } |
---|
935 | |
---|
936 | |
---|
937 | |
---|
938 | |
---|
939 | } |
---|