1 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality; |
---|
7 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
8 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
9 | import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; |
---|
10 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
11 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
12 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
13 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
14 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
15 | |
---|
16 | /** |
---|
17 | * <p> |
---|
18 | * iterations in a list of nodes are equal subsequences following each other directly. The |
---|
19 | * subsequences can be of any length depending on the type of equality they need to have. If the |
---|
20 | * subsequences have to be lexically equal, then they have to have the same length if they only |
---|
21 | * contain event tasks. As an example entering text can be done through appropriate keystrokes or |
---|
22 | * through pasting the text. As a result, two syntactically different sequences are semantically |
---|
23 | * equal. If both follow each other, then they are an iteration of semantically equal children. |
---|
24 | * But they are not lexically equal. |
---|
25 | * </p> |
---|
26 | * <p> |
---|
27 | * This class determines equal subsequences following each other. It is provided with a minimal node |
---|
28 | * equality the equal nodes should have. Through this, it is possible to find e.g. lexically |
---|
29 | * equal subsequence through a first application of this rule and semantically equal children to |
---|
30 | * a later application of this rule. This is used by the {@link TemporalRelationshipRuleManager} |
---|
31 | * which instantiates this rule three times, each with a different minimal equality. |
---|
32 | * </p> |
---|
33 | * <p> |
---|
34 | * The equal subsequences are determined through trial and error. This algorithm has a high effort |
---|
35 | * as it tries in the worst case all possible combinations of sub lists in all possible parts of |
---|
36 | * the list of children of a provided parent node. The steps for each trial are. |
---|
37 | * <ul> |
---|
38 | * <li>for all possible subparts of the children of the provided parent |
---|
39 | * <ul> |
---|
40 | * <li>for all possible first sublists in the subpart |
---|
41 | * <ul> |
---|
42 | * <li>for all succeeding next sublists in this part</li> |
---|
43 | * <ul> |
---|
44 | * <li>check if this sublist is equal to all previously identified sublist in this part</li> |
---|
45 | * </ul> |
---|
46 | * </ul> |
---|
47 | * <li> |
---|
48 | * if a combination of sublists is found in this subpart which are all equal to each other |
---|
49 | * at the provided minimal equality level, an iteration in this subpart was found. |
---|
50 | * </li> |
---|
51 | * <ul> |
---|
52 | * <li>merge the identified equal sublists to an iteration</li> |
---|
53 | * </ul> |
---|
54 | * </ul> |
---|
55 | * </ul> |
---|
56 | * The algorithm tries to optimize if all children are event tasks and if the sublists shall be |
---|
57 | * lexically equal. In this case, the sublist all have to have the same length. The trial and |
---|
58 | * error reduces to a minimum of possible sublists. |
---|
59 | * </p> |
---|
60 | * |
---|
61 | * @author Patrick Harms |
---|
62 | */ |
---|
63 | public class DefaultIterationDetectionRule implements TemporalRelationshipRule { |
---|
64 | |
---|
65 | /** |
---|
66 | * <p> |
---|
67 | * the node equality manager needed for comparing task tree nodes with each other |
---|
68 | * </p> |
---|
69 | */ |
---|
70 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
71 | |
---|
72 | /** |
---|
73 | * <p> |
---|
74 | * the minimal node equality two identified sublists need to have to consider them as equal |
---|
75 | * and to create an iteration for |
---|
76 | * </p> |
---|
77 | */ |
---|
78 | private NodeEquality minimalNodeEquality; |
---|
79 | |
---|
80 | /** |
---|
81 | * <p> |
---|
82 | * instantiates the rule and initializes it with a node equality rule manager and the minimal |
---|
83 | * node equality identified sublist must have to consider them as iterated. |
---|
84 | * </p> |
---|
85 | */ |
---|
86 | DefaultIterationDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager, |
---|
87 | NodeEquality minimalNodeEquality) |
---|
88 | { |
---|
89 | super(); |
---|
90 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
91 | this.minimalNodeEquality = minimalNodeEquality; |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * (non-Javadoc) |
---|
96 | * |
---|
97 | * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) |
---|
98 | */ |
---|
99 | @Override |
---|
100 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
101 | ITaskTreeBuilder treeBuilder, |
---|
102 | ITaskTreeNodeFactory nodeFactory, |
---|
103 | boolean finalize) |
---|
104 | { |
---|
105 | if (!(parent instanceof ISequence)) { |
---|
106 | return null; |
---|
107 | } |
---|
108 | |
---|
109 | if (!finalize) { |
---|
110 | // the rule is always feasible as iterations may occur at any time |
---|
111 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
112 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
113 | return result; |
---|
114 | } |
---|
115 | |
---|
116 | // parent must already have at least 2 children |
---|
117 | if ((parent.getChildren() == null) || (parent.getChildren().size() < 2)) { |
---|
118 | return null; |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | // to find longer iterations first, start with long sequences |
---|
123 | SubSequences subSequences = getEqualSubsequences(parent, treeBuilder, nodeFactory); |
---|
124 | |
---|
125 | if (subSequences != null) { |
---|
126 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
127 | |
---|
128 | mergeEqualNodes(subSequences.equalVariants, treeBuilder, nodeFactory); |
---|
129 | IIteration newIteration = createIterationBasedOnIdentifiedVariants |
---|
130 | (subSequences, treeBuilder, nodeFactory, result); |
---|
131 | |
---|
132 | determineNewlyCreatedParentTasks(parent, newIteration, result); |
---|
133 | |
---|
134 | // remove iterated children |
---|
135 | for (int j = subSequences.start; j < subSequences.end; j++) { |
---|
136 | treeBuilder.removeChild((ISequence) parent, subSequences.start); |
---|
137 | } |
---|
138 | |
---|
139 | // add the new iteration instead |
---|
140 | treeBuilder.addChild((ISequence) parent, subSequences.start, newIteration); |
---|
141 | |
---|
142 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
143 | return result; |
---|
144 | } |
---|
145 | |
---|
146 | return null; |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * <p> |
---|
151 | * this method initiates the trial and error algorithm denoted in the description of this class. |
---|
152 | * Its main purpose is the selection of a subpart of all children in the parent node in which |
---|
153 | * equal sublists shall be searched. It is important, to always find the last iterations in a |
---|
154 | * part first. The reason for this are iterations of iterations. If we always found the first |
---|
155 | * iteration in a subpart first, then this may be an iteration of iterations. However, there |
---|
156 | * may be subsequent iterations to be included in this iteration. But these iterations are not |
---|
157 | * found yet, as they occur later in the sequence. Therefore, if we always find the last |
---|
158 | * iteration in a sequence first, iterations of iterations are identified, last. |
---|
159 | * </p> |
---|
160 | * |
---|
161 | * @param parent the parent node in which iterations of children shall be found |
---|
162 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
163 | * @param nodeFactory the node factory that can be used for instantiating task tree nodes |
---|
164 | * |
---|
165 | * @return the iterated subsequences identified in a specific part (contains the equal |
---|
166 | * subsequences as well as the start (inclusive) and end (exclusive) index of the |
---|
167 | * subpart in which the sequences were found) |
---|
168 | */ |
---|
169 | private SubSequences getEqualSubsequences(ITaskTreeNode parent, |
---|
170 | ITaskTreeBuilder treeBuilder, |
---|
171 | ITaskTreeNodeFactory nodeFactory) |
---|
172 | { |
---|
173 | SubSequences subSequences = null; |
---|
174 | |
---|
175 | FIND_ITERATION: |
---|
176 | for (int end = parent.getChildren().size(); end > 0; end--) { |
---|
177 | for (int start = 0; start < end; start++) { |
---|
178 | boolean useEqualSublistLengths = equalSublistLengthsCanBeUsed(parent, start, end); |
---|
179 | |
---|
180 | subSequences = new SubSequences(); |
---|
181 | subSequences.start = start; |
---|
182 | |
---|
183 | boolean foundFurtherVariants = findFurtherVariants |
---|
184 | (subSequences, parent, start, end, treeBuilder, nodeFactory, |
---|
185 | useEqualSublistLengths); |
---|
186 | |
---|
187 | if (foundFurtherVariants) { |
---|
188 | break FIND_ITERATION; |
---|
189 | } |
---|
190 | else { |
---|
191 | subSequences = null; |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | return subSequences; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * <p> |
---|
201 | * for optimization purposes, we check if the length of the sublists to be identified as |
---|
202 | * iterations has to be the same for any sublist. This only applies, if the minimum node |
---|
203 | * equality to be checked for is lexical equality. If the children of the parent are all event |
---|
204 | * tasks, then sublists can only be lexically equal, if they all have the same length. |
---|
205 | * Therefore we check, if the minimal node equality is lexical equality. And if so, we also |
---|
206 | * check if all children of the parent in which an iteration shall be searched for are event |
---|
207 | * tasks. |
---|
208 | * </p> |
---|
209 | * |
---|
210 | * @param parent the parent node to search for iterations of its children |
---|
211 | * @param start the beginning of the subpart (inclusive) to be considered |
---|
212 | * @param end the end of the subpart (exclusive) to be considered |
---|
213 | * |
---|
214 | * @return true, if the sublists must have the same lengths, false else |
---|
215 | */ |
---|
216 | private boolean equalSublistLengthsCanBeUsed(ITaskTreeNode parent, int start, int end) { |
---|
217 | boolean equalLengthsCanBeUsed = minimalNodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL); |
---|
218 | |
---|
219 | if (equalLengthsCanBeUsed) { |
---|
220 | for (int i = start; i < end; i++) { |
---|
221 | if (!(parent.getChildren().get(i) instanceof IEventTask)) { |
---|
222 | equalLengthsCanBeUsed = false; |
---|
223 | break; |
---|
224 | } |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | return equalLengthsCanBeUsed; |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * <p> |
---|
233 | * this method starts at a specific position in the list of children of the provided parent |
---|
234 | * and checks, if it finds a further sublist, that matches the already found sublists. If |
---|
235 | * the sublist lengths must be equal, it only searches for a sublist of the same length of the |
---|
236 | * already found sublists. The method calls itself if it identifies a further equal sublist but |
---|
237 | * if the end of the subpart of children is not yet reached. |
---|
238 | * </p> |
---|
239 | * |
---|
240 | * @param subSequences the sublist found so far against which equality of the next |
---|
241 | * sublist must be checked |
---|
242 | * @param parent the parent node of which the children are analyzed |
---|
243 | * @param start the starting index from which to start the next sublist to be |
---|
244 | * identified |
---|
245 | * @param end the end index (exclusive) of the current subpart of children |
---|
246 | * in which iterations are searched for |
---|
247 | * @param treeBuilder the tree builder that can be used for connecting task tree |
---|
248 | * nodes |
---|
249 | * @param nodeFactory the node factory that can be used for instantiating task tree |
---|
250 | * nodes |
---|
251 | * @param useEqualSublistLengths true if the sublists to be searched for all need to have the |
---|
252 | * same length |
---|
253 | * |
---|
254 | * @return true if a further equal variant was found, false else |
---|
255 | */ |
---|
256 | private boolean findFurtherVariants(SubSequences subSequences, |
---|
257 | ITaskTreeNode parent, |
---|
258 | int start, |
---|
259 | int end, |
---|
260 | ITaskTreeBuilder treeBuilder, |
---|
261 | ITaskTreeNodeFactory nodeFactory, |
---|
262 | boolean useEqualSublistLengths) |
---|
263 | { |
---|
264 | boolean foundFurtherVariants = (start == end) && (subSequences.equalVariants.size() > 1); |
---|
265 | |
---|
266 | int minChildCount = 1; |
---|
267 | int maxChildCount = end - start; |
---|
268 | |
---|
269 | if (useEqualSublistLengths && (subSequences.equalVariants.size() > 0)) { |
---|
270 | minChildCount = subSequences.equalVariants.get(0).getChildren().size(); |
---|
271 | maxChildCount = Math.min(minChildCount, maxChildCount); |
---|
272 | } |
---|
273 | |
---|
274 | for (int childCount = minChildCount; childCount <= maxChildCount; childCount++) { |
---|
275 | if (useEqualSublistLengths && (((end - start) % childCount) != 0)) { |
---|
276 | continue; |
---|
277 | } |
---|
278 | |
---|
279 | ISequence furtherVariant = nodeFactory.createNewSequence(); |
---|
280 | |
---|
281 | for (int j = start; j < start + childCount; j++) { |
---|
282 | treeBuilder.addChild(furtherVariant, parent.getChildren().get(j)); |
---|
283 | } |
---|
284 | |
---|
285 | boolean allVariantsEqual = true; |
---|
286 | |
---|
287 | for (ITaskTreeNode equalVariant : subSequences.equalVariants) { |
---|
288 | NodeEquality nodeEquality = |
---|
289 | nodeEqualityRuleManager.applyRules(equalVariant, furtherVariant); |
---|
290 | |
---|
291 | if (!nodeEquality.isAtLeast(minimalNodeEquality)) { |
---|
292 | allVariantsEqual = false; |
---|
293 | break; |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | if (allVariantsEqual) { |
---|
298 | |
---|
299 | // we found a further variant. Add it to the list of variants and try to find |
---|
300 | // further variants. Ignore, if none is available |
---|
301 | int index = subSequences.equalVariants.size(); |
---|
302 | subSequences.equalVariants.add(index, furtherVariant); |
---|
303 | |
---|
304 | foundFurtherVariants = findFurtherVariants |
---|
305 | (subSequences, parent, start + childCount, end, treeBuilder, nodeFactory, |
---|
306 | useEqualSublistLengths); |
---|
307 | |
---|
308 | if (foundFurtherVariants) { |
---|
309 | subSequences.end = end; |
---|
310 | break; |
---|
311 | } |
---|
312 | else { |
---|
313 | subSequences.equalVariants.remove(index); |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | return foundFurtherVariants; |
---|
319 | } |
---|
320 | |
---|
321 | /** |
---|
322 | * <p> |
---|
323 | * this method merges task tree nodes in a list, if they can be merged. for this, it tries |
---|
324 | * to merge every node with every other node in the provided list using the |
---|
325 | * {@link #mergeEqualTasks(ITaskTreeNode, ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory)} |
---|
326 | * method. If a merge is possible, it removes the merged nodes from the list and adds the |
---|
327 | * merge result. |
---|
328 | * </p> |
---|
329 | * |
---|
330 | * @param nodes the list of nodes to be merged |
---|
331 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
332 | * @param nodeFactory the node factory that can be used for instantiating task tree nodes |
---|
333 | */ |
---|
334 | private void mergeEqualNodes(List<ITaskTreeNode> nodes, |
---|
335 | ITaskTreeBuilder treeBuilder, |
---|
336 | ITaskTreeNodeFactory nodeFactory) |
---|
337 | { |
---|
338 | int index1 = 0; |
---|
339 | int index2 = 0; |
---|
340 | ITaskTreeNode variant1; |
---|
341 | ITaskTreeNode variant2; |
---|
342 | |
---|
343 | while (index1 < nodes.size()) { |
---|
344 | variant1 = nodes.get(index1); |
---|
345 | index2 = index1 + 1; |
---|
346 | |
---|
347 | while (index2 < nodes.size()) { |
---|
348 | variant2 = nodes.get(index2); |
---|
349 | ITaskTreeNode mergedChild = |
---|
350 | mergeEqualTasks(variant1, variant2, treeBuilder, nodeFactory); |
---|
351 | |
---|
352 | if (mergedChild != null) { |
---|
353 | // if we merged something start from the beginning to perform the next merge |
---|
354 | nodes.remove(index2); |
---|
355 | nodes.remove(index1); |
---|
356 | nodes.add(index1, mergedChild); |
---|
357 | index1 = -1; |
---|
358 | break; |
---|
359 | } |
---|
360 | else { |
---|
361 | index2++; |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | index1++; |
---|
366 | } |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | * <p> |
---|
371 | * this method merges two equal tasks with each other if possible. If the tasks are lexically |
---|
372 | * equal, the first of them is returned as merge result. If both tasks are of the same |
---|
373 | * temporal relationship type, the appropriate merge method is called to merge them. If one |
---|
374 | * of the nodes is a selection, the other one is added as a variant of this selection. |
---|
375 | * (However, if both nodes are selections, they are merged using the appropriate merge method.) |
---|
376 | * If merging is not possible, then a selection of both provided nodes is created and |
---|
377 | * returned as merge result. |
---|
378 | * </p> |
---|
379 | * |
---|
380 | * @param node1 the first task to be merged |
---|
381 | * @param node2 the second task to be merged |
---|
382 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
383 | * @param nodeFactory the node factory that can be used for instantiating task tree nodes |
---|
384 | * |
---|
385 | * @return the result of the merge |
---|
386 | */ |
---|
387 | private ITaskTreeNode mergeEqualTasks(ITaskTreeNode node1, |
---|
388 | ITaskTreeNode node2, |
---|
389 | ITaskTreeBuilder treeBuilder, |
---|
390 | ITaskTreeNodeFactory nodeFactory) |
---|
391 | { |
---|
392 | ITaskTreeNode mergeResult = null; |
---|
393 | |
---|
394 | if ((node1 instanceof ISequence) && (node2 instanceof ISequence)) { |
---|
395 | mergeResult = mergeEqualSequences |
---|
396 | ((ISequence) node1, (ISequence) node2, treeBuilder, nodeFactory); |
---|
397 | } |
---|
398 | else if ((node1 instanceof ISelection) && (node2 instanceof ISelection)) { |
---|
399 | mergeResult = mergeEqualSelections |
---|
400 | ((ISelection) node1, (ISelection) node2, treeBuilder, nodeFactory); |
---|
401 | } |
---|
402 | else if ((node1 instanceof IIteration) && (node2 instanceof IIteration)) { |
---|
403 | mergeResult = mergeEqualIterations |
---|
404 | ((IIteration) node1, (IIteration) node2, treeBuilder, nodeFactory); |
---|
405 | } |
---|
406 | else if (node1 instanceof ISelection) { |
---|
407 | treeBuilder.addChild((ISelection) node1, node2); |
---|
408 | mergeResult = node1; |
---|
409 | } |
---|
410 | else if (node2 instanceof ISelection) { |
---|
411 | treeBuilder.addChild((ISelection) node2, node1); |
---|
412 | mergeResult = node2; |
---|
413 | } |
---|
414 | else if (node1 instanceof IIteration) { |
---|
415 | mergeResult = mergeEqualTasks |
---|
416 | (((IIteration) node1).getChildren().get(0), node2, treeBuilder, nodeFactory); |
---|
417 | |
---|
418 | if (mergeResult != null) { |
---|
419 | IIteration iteration = nodeFactory.createNewIteration(); |
---|
420 | treeBuilder.setChild(iteration, mergeResult); |
---|
421 | mergeResult = iteration; |
---|
422 | } |
---|
423 | } |
---|
424 | else if (node2 instanceof IIteration) { |
---|
425 | mergeResult = mergeEqualTasks |
---|
426 | (((IIteration) node2).getChildren().get(0), node1, treeBuilder, nodeFactory); |
---|
427 | |
---|
428 | if (mergeResult != null) { |
---|
429 | IIteration iteration = nodeFactory.createNewIteration(); |
---|
430 | treeBuilder.setChild(iteration, mergeResult); |
---|
431 | mergeResult = iteration; |
---|
432 | } |
---|
433 | } |
---|
434 | else { |
---|
435 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(node1, node2); |
---|
436 | |
---|
437 | if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
438 | mergeResult = node1; |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | if (mergeResult == null) { |
---|
443 | mergeResult = nodeFactory.createNewSelection(); |
---|
444 | treeBuilder.addChild((ISelection) mergeResult, node1); |
---|
445 | treeBuilder.addChild((ISelection) mergeResult, node2); |
---|
446 | } |
---|
447 | |
---|
448 | return mergeResult; |
---|
449 | } |
---|
450 | |
---|
451 | /** |
---|
452 | * <p> |
---|
453 | * merges equal sequences. This is done through trying to merge each node of sequence 1 with |
---|
454 | * the node in sequence 2 being located at the same position. If not all children can be merged |
---|
455 | * or if the sequences have different lengths, null is returned to indicate, that merging is |
---|
456 | * not possible. For merging children, the |
---|
457 | * {@link #mergeEqualTasks(ITaskTreeNode, ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory)} |
---|
458 | * method is called. |
---|
459 | * </p> |
---|
460 | * |
---|
461 | * @param sequence1 the first sequence to be merged |
---|
462 | * @param sequence2 the second sequence to be merged |
---|
463 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
464 | * @param nodeFactory the node factory that can be used for instantiating task tree nodes |
---|
465 | * |
---|
466 | * @return the result of the merge or null if merging was not possible |
---|
467 | */ |
---|
468 | private ISequence mergeEqualSequences(ISequence sequence1, |
---|
469 | ISequence sequence2, |
---|
470 | ITaskTreeBuilder treeBuilder, |
---|
471 | ITaskTreeNodeFactory nodeFactory) |
---|
472 | { |
---|
473 | ISequence mergeResult = null; |
---|
474 | |
---|
475 | if (sequence1.getChildren().size() == sequence2.getChildren().size()) { |
---|
476 | mergeResult = nodeFactory.createNewSequence(); |
---|
477 | |
---|
478 | for (int i = 0; i < sequence1.getChildren().size(); i++) { |
---|
479 | ITaskTreeNode mergedNode = mergeEqualTasks |
---|
480 | (sequence1.getChildren().get(i), sequence2.getChildren().get(i), |
---|
481 | treeBuilder, nodeFactory); |
---|
482 | |
---|
483 | if (mergedNode != null) { |
---|
484 | treeBuilder.addChild(mergeResult, mergedNode); |
---|
485 | } |
---|
486 | else { |
---|
487 | mergeResult = null; |
---|
488 | break; |
---|
489 | } |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | return mergeResult; |
---|
494 | } |
---|
495 | |
---|
496 | /** |
---|
497 | * <p> |
---|
498 | * merges equal selections. This is done through trying to merge each node of selections with |
---|
499 | * each other. For this, the method |
---|
500 | * {@link #mergeEqualNodes(List, ITaskTreeBuilder, ITaskTreeNodeFactory)} is called with a |
---|
501 | * join of the child list of both selections. |
---|
502 | * </p> |
---|
503 | * |
---|
504 | * @param selection1 the first selection to be merged |
---|
505 | * @param selection2 the second selection to be merged |
---|
506 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
507 | * @param nodeFactory the node factory that can be used for instantiating task tree nodes |
---|
508 | * |
---|
509 | * @return the result of the merge which is not null |
---|
510 | */ |
---|
511 | private ITaskTreeNode mergeEqualSelections(ISelection selection1, |
---|
512 | ISelection selection2, |
---|
513 | ITaskTreeBuilder treeBuilder, |
---|
514 | ITaskTreeNodeFactory nodeFactory) |
---|
515 | { |
---|
516 | ISelection mergeResult = nodeFactory.createNewSelection(); |
---|
517 | |
---|
518 | for (int i = 0; i < selection1.getChildren().size(); i++) { |
---|
519 | treeBuilder.addChild(mergeResult, selection1.getChildren().get(i)); |
---|
520 | } |
---|
521 | |
---|
522 | for (int i = 0; i < selection2.getChildren().size(); i++) { |
---|
523 | treeBuilder.addChild(mergeResult, selection2.getChildren().get(i)); |
---|
524 | } |
---|
525 | |
---|
526 | mergeEqualNodes(mergeResult.getChildren(), treeBuilder, nodeFactory); |
---|
527 | |
---|
528 | return mergeResult; |
---|
529 | } |
---|
530 | |
---|
531 | /** |
---|
532 | * <p> |
---|
533 | * merges equal iterations. This is done through merging the children of both iterations. If |
---|
534 | * this is possible, a resulting iteration with the merge result of the children as its own |
---|
535 | * child is returned. Otherwise null is returned to indicate that merging was not possible. |
---|
536 | * </p> |
---|
537 | * |
---|
538 | * @param selection1 the first iteration to be merged |
---|
539 | * @param selection2 the second iteration to be merged |
---|
540 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
541 | * @param nodeFactory the node factory that can be used for instantiating task tree nodes |
---|
542 | * |
---|
543 | * @return the result of the merge or null if merging is not possible |
---|
544 | */ |
---|
545 | private ITaskTreeNode mergeEqualIterations(IIteration iteration1, |
---|
546 | IIteration iteration2, |
---|
547 | ITaskTreeBuilder treeBuilder, |
---|
548 | ITaskTreeNodeFactory nodeFactory) |
---|
549 | { |
---|
550 | ITaskTreeNode mergedChild = mergeEqualTasks |
---|
551 | (iteration1.getChildren().get(0), iteration2.getChildren().get(0), |
---|
552 | treeBuilder, nodeFactory); |
---|
553 | |
---|
554 | IIteration mergeResult = null; |
---|
555 | |
---|
556 | if (mergedChild != null) { |
---|
557 | mergeResult = nodeFactory.createNewIteration(); |
---|
558 | treeBuilder.setChild(mergeResult, mergedChild); |
---|
559 | } |
---|
560 | |
---|
561 | return mergeResult; |
---|
562 | } |
---|
563 | |
---|
564 | /** |
---|
565 | * <p> |
---|
566 | * this is a convenience method to create an iteration based on the identified and already |
---|
567 | * merged iterated subsequences. This method creates the simplest iteration possible. As an |
---|
568 | * example, if always the same task tree node is iterated, it becomes the child of the |
---|
569 | * iteration. If a sequence of tasks is iterated, this sequence becomes the child of the |
---|
570 | * iteration. It several equal sublists or nodes which are not lexically equal are iterated |
---|
571 | * they become a selection which in turn become the child of the iteration. |
---|
572 | * </p> |
---|
573 | * |
---|
574 | * @param subsequences the identified and already merged equal subsequences |
---|
575 | * @param treeBuilder the tree builder that can be used for connecting task tree nodes |
---|
576 | * @param nodeFactory the node factory that can be used for instantiating the iteration |
---|
577 | * |
---|
578 | * @return the resulting iteration |
---|
579 | */ |
---|
580 | private IIteration createIterationBasedOnIdentifiedVariants(SubSequences subsequences, |
---|
581 | ITaskTreeBuilder treeBuilder, |
---|
582 | ITaskTreeNodeFactory nodeFactory, |
---|
583 | RuleApplicationResult result) |
---|
584 | { |
---|
585 | IIteration newIteration = nodeFactory.createNewIteration(); |
---|
586 | result.addNewlyCreatedParentNode(newIteration); |
---|
587 | |
---|
588 | if (subsequences.equalVariants.size() == 1) { |
---|
589 | // all children are the same. Create an iteration of this child |
---|
590 | if (subsequences.equalVariants.get(0).getChildren().size() == 1) { |
---|
591 | // there is only one equal variant and this has only one child. So create an |
---|
592 | // iteration of this child |
---|
593 | treeBuilder.setChild |
---|
594 | (newIteration, subsequences.equalVariants.get(0).getChildren().get(0)); |
---|
595 | } |
---|
596 | else { |
---|
597 | // there was an iteration of one equal sequence |
---|
598 | treeBuilder.setChild(newIteration, subsequences.equalVariants.get(0)); |
---|
599 | result.addNewlyCreatedParentNode(subsequences.equalVariants.get(0)); |
---|
600 | } |
---|
601 | } |
---|
602 | else { |
---|
603 | // there are distinct variants of equal subsequences or children --> create an |
---|
604 | // iterated selection |
---|
605 | ISelection selection = nodeFactory.createNewSelection(); |
---|
606 | result.addNewlyCreatedParentNode(selection); |
---|
607 | |
---|
608 | for (ITaskTreeNode variant : subsequences.equalVariants) { |
---|
609 | if (variant.getChildren().size() == 1) { |
---|
610 | treeBuilder.addChild(selection, variant.getChildren().get(0)); |
---|
611 | } |
---|
612 | else { |
---|
613 | treeBuilder.addChild(selection, variant); |
---|
614 | result.addNewlyCreatedParentNode(variant); |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | treeBuilder.setChild(newIteration, selection); |
---|
619 | } |
---|
620 | |
---|
621 | return newIteration; |
---|
622 | } |
---|
623 | |
---|
624 | /** |
---|
625 | * <p> |
---|
626 | * as the method has to denote all newly created parent nodes this method identifies them by |
---|
627 | * comparing the existing subtree with the newly created iteration. Only those parent nodes |
---|
628 | * in the new iteration, which are not already found in the existing sub tree are denoted as |
---|
629 | * newly created. We do this in this way, as during the iteration detection algorithm, many |
---|
630 | * parent nodes are created, which may be discarded later. It is easier to identify the |
---|
631 | * remaining newly created parent nodes through this way than to integrate it into the |
---|
632 | * algorithm. |
---|
633 | * </p> |
---|
634 | * |
---|
635 | * @param existingSubTree the existing subtree |
---|
636 | * @param newSubTree the identified iteration |
---|
637 | * @param result the rule application result into which the newly created parent nodes |
---|
638 | * shall be stored. |
---|
639 | */ |
---|
640 | private void determineNewlyCreatedParentTasks(ITaskTreeNode existingSubTree, |
---|
641 | ITaskTreeNode newSubTree, |
---|
642 | RuleApplicationResult result) |
---|
643 | { |
---|
644 | List<ITaskTreeNode> existingParentNodes = getParentNodes(existingSubTree); |
---|
645 | List<ITaskTreeNode> newParentNodes = getParentNodes(newSubTree); |
---|
646 | |
---|
647 | boolean foundNode; |
---|
648 | for (ITaskTreeNode newParentNode : newParentNodes) { |
---|
649 | foundNode = false; |
---|
650 | for (ITaskTreeNode existingParentNode : existingParentNodes) { |
---|
651 | // It is sufficient to compare the references. The algorithm reuses nodes as they |
---|
652 | // are. So any node existing in the new structure that is also in the old structure |
---|
653 | // was unchanged an therefore does not need to be handled as a newly created one. |
---|
654 | // but every node in the new structure that is not included in the old structure |
---|
655 | // must be treated as a newly created one. |
---|
656 | if (newParentNode == existingParentNode) { |
---|
657 | foundNode = true; |
---|
658 | break; |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | if (!foundNode) { |
---|
663 | result.addNewlyCreatedParentNode(newParentNode); |
---|
664 | } |
---|
665 | } |
---|
666 | |
---|
667 | } |
---|
668 | |
---|
669 | /** |
---|
670 | * <p> |
---|
671 | * convenience method to determine all parent nodes existing in a subtree |
---|
672 | * </p> |
---|
673 | * |
---|
674 | * @param subtree the subtree to search for parent nodes in |
---|
675 | * |
---|
676 | * @return a list of parent nodes existing in the subtree |
---|
677 | */ |
---|
678 | private List<ITaskTreeNode> getParentNodes(ITaskTreeNode subtree) { |
---|
679 | List<ITaskTreeNode> parentNodes = new ArrayList<ITaskTreeNode>(); |
---|
680 | |
---|
681 | if (subtree.getChildren().size() > 0) { |
---|
682 | parentNodes.add(subtree); |
---|
683 | |
---|
684 | for (ITaskTreeNode child : subtree.getChildren()) { |
---|
685 | parentNodes.addAll(getParentNodes(child)); |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | return parentNodes; |
---|
690 | } |
---|
691 | |
---|
692 | /** |
---|
693 | * <p> |
---|
694 | * used to have a container for equal sublists identified in a sub part of the children of |
---|
695 | * a parent node. |
---|
696 | * </p> |
---|
697 | * |
---|
698 | * @author Patrick Harms |
---|
699 | */ |
---|
700 | private static class SubSequences { |
---|
701 | |
---|
702 | /** |
---|
703 | * <p> |
---|
704 | * the beginning of the subpart of the children of the parent node in which the sublists |
---|
705 | * are found (inclusive) |
---|
706 | * </p> |
---|
707 | */ |
---|
708 | public int start; |
---|
709 | |
---|
710 | /** |
---|
711 | * <p> |
---|
712 | * the end of the subpart of the children of the parent node in which the sublists |
---|
713 | * are found (exclusive) |
---|
714 | * </p> |
---|
715 | */ |
---|
716 | public int end; |
---|
717 | |
---|
718 | /** |
---|
719 | * <p> |
---|
720 | * the equal sublists found in the subpart of the children of the parent node |
---|
721 | * </p> |
---|
722 | */ |
---|
723 | List<ITaskTreeNode> equalVariants = new ArrayList<ITaskTreeNode>(); |
---|
724 | |
---|
725 | } |
---|
726 | |
---|
727 | } |
---|