source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/IterationComparisonRule.java @ 655

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
  • Property svn:executable set to *
File size: 6.3 KB
Line 
1package de.ugoe.cs.quest.tasktrees.nodeequality;
2
3import de.ugoe.cs.quest.tasktrees.treeifc.IIteration;
4import de.ugoe.cs.quest.tasktrees.treeifc.ISelection;
5import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
6
7/**
8 * <p>
9 * This class is capable of comparing Iterations. Iterations equal at distinct levels
10 * in distinct situations. The following table shows the results of the comparison for the
11 * specific situations (the parameters are commutative). In any other situation, the comparison
12 * returns <code>NodeEquality.UNEQUAL</code>:
13 * </p>
14 *
15 * <table border="1">
16 *   <tr>
17 *     <th>iteration 1</th>
18 *     <th>iteration 2</th>
19 *     <th>comparison result</th>
20 *   </tr>
21 *   <tr>
22 *     <td>any iteration</td>
23 *     <td>any iteration with a child that is lexically equal to the child of iteration 1</td>
24 *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td>
25 *   </tr>
26 *   <tr>
27 *     <td>any iteration</td>
28 *     <td>any iteration with a child that is syntactically equal to the child of iteration 1</td>
29 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
30 *   </tr>
31 *   <tr>
32 *     <td>any iteration</td>
33 *     <td>any iteration with a child that is semantically equal to the child of iteration 1</td>
34 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
35 *   </tr>
36 *   <tr>
37 *     <td>an iteration with a selection of syntactically equal children</td>
38 *     <td>an iteration with a child that is syntactically equal to the children of the child
39 *     selection of iteration 1</td>
40 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
41 *   </tr>
42 *   <tr>
43 *     <td>an iteration with a selection of semantically equal children</td>
44 *     <td>an iteration with a child that is semantically equal to the children of the child
45 *     selection of iteration 1</td>
46 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
47 *   </tr>
48 * </table>
49 *
50 * @version $Revision: $ $Date: 19.02.2012$
51 * @author 2012, last modified by $Author: patrick$
52 */
53public class IterationComparisonRule implements NodeComparisonRule {
54   
55    /** the rule manager for internally comparing task tree nodes */
56    private NodeEqualityRuleManager mRuleManager;
57
58    /**
59     * <p>
60     * simple constructor to provide the rule with the node equality rule manager to be able
61     * to perform comparisons of the children of provided task tree nodes
62     * </p>
63     *
64     * @param ruleManager the rule manager for comparing task tree nodes
65     */
66    IterationComparisonRule(NodeEqualityRuleManager ruleManager) {
67        super();
68        mRuleManager = ruleManager;
69    }
70
71    /*
72     * (non-Javadoc)
73     *
74     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
75     */
76    @Override
77    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
78        if ((!(node1 instanceof IIteration)) || (!(node2 instanceof IIteration))) {
79            return null;
80        }
81
82        // if both iterations do not have children, they are equal although this doesn't make sense
83        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
84            return NodeEquality.IDENTICAL;
85        }
86
87        ITaskTreeNode child1 = node1.getChildren().get(0);
88        ITaskTreeNode child2 = node2.getChildren().get(0);
89
90        // iterations may have 3 different structures.
91        // 1. they have one child, which is the iterated one
92        // 2. they have a sequence of children, which is iterated
93        // 3. they have a selection of different iterated variants (usually the variants are
94        // semantically equal)
95        //
96        // the permutations of the three variants in combination must be checked
97
98        // check if both nodes are the same variants of iterations and if their children are equal.
99        // This condition matches, if both iterations are the same variants of iteration. I.e. three
100        // combinations of the permutation are handled herewith.
101        NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
102
103        if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
104            return nodeEquality;
105        }
106
107        // compare one iteration with a single node as a child and another one with a selection of
108        // semantically equal nodes
109        return selectionChildrenSemanticallyEqualNode(child1, child2);
110       
111        // all other combinations (i.e. sequence with single child and sequence with selection)
112        // can not match
113    }
114
115    /**
116     * <p>
117     * compares two task tree nodes. One of them must be a selection of at least semantically
118     * equal children. The other one can be any task tree node. The method returns a node equality
119     * that is not <code>NodeEquality.UNEQUAL</code> if the other node is at least semantically
120     * equal to the children of the selection. It returns more concrete equalities, if the
121     * equality between the other node and the children of the selection is more concrete.
122     * </p>
123     *
124     * @param taskTreeNode  the first task tree node to compare
125     * @param taskTreeNode2 the second task tree node to compare
126     *
127     * @return as described
128     */
129    private NodeEquality selectionChildrenSemanticallyEqualNode(ITaskTreeNode taskTreeNode,
130                                                                ITaskTreeNode taskTreeNode2)
131    {
132        ISelection selection = null;
133        ITaskTreeNode node = null;
134        if (taskTreeNode instanceof ISelection) {
135            selection = (ISelection) taskTreeNode;
136            node = taskTreeNode2;
137        }
138        else if (taskTreeNode2 instanceof ISelection) {
139            selection = (ISelection) taskTreeNode2;
140            node = taskTreeNode;
141        }
142        else {
143            return NodeEquality.UNEQUAL;
144        }
145
146        NodeEquality lessConcreteEqualityForAllComparisons = NodeEquality.IDENTICAL;
147
148        for (ITaskTreeNode child : selection.getChildren()) {
149            NodeEquality nodeEquality = mRuleManager.applyRules(node, child);
150
151            if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
152                return NodeEquality.UNEQUAL;
153            }
154            else if (!lessConcreteEqualityForAllComparisons.isAtLeast(nodeEquality)) {
155                lessConcreteEqualityForAllComparisons = nodeEquality;
156            }
157        }
158
159        return lessConcreteEqualityForAllComparisons;
160    }
161
162}
Note: See TracBrowser for help on using the repository browser.