source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/DefaultIterationDetectionRule.java @ 1113

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