source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/IterationComparisonRule.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: 8.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.nodeequality;
16
17import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
20
21/**
22 * <p>
23 * This class is capable of comparing Iterations. Iterations equal at distinct levels
24 * in distinct situations. The following table shows the results of the comparison for the
25 * specific situations (the parameters are commutative). In any other situation, the comparison
26 * returns <code>NodeEquality.UNEQUAL</code>:
27 * </p>
28 *
29 * <table border="1">
30 *   <tr>
31 *     <th>iteration 1</th>
32 *     <th>iteration 2</th>
33 *     <th>comparison result</th>
34 *   </tr>
35 *   <tr>
36 *     <td>any iteration</td>
37 *     <td>any iteration with a child that is lexically equal to the child of iteration 1</td>
38 *     <td><code>NodeEquality.LEXICALLY_EQUAL</code></td>
39 *   </tr>
40 *   <tr>
41 *     <td>any iteration</td>
42 *     <td>any iteration with a child that is syntactically equal to the child of iteration 1</td>
43 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
44 *   </tr>
45 *   <tr>
46 *     <td>any iteration</td>
47 *     <td>any iteration with a child that is semantically equal to the child of iteration 1</td>
48 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
49 *   </tr>
50 *   <tr>
51 *     <td>an iteration with a selection of syntactically equal children</td>
52 *     <td>an iteration with a child that is syntactically equal to the children of the child
53 *     selection of iteration 1</td>
54 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
55 *   </tr>
56 *   <tr>
57 *     <td>an iteration with a selection of syntactically equal children</td>
58 *     <td>an iteration with a selection of syntactically equal children that are all syntactically
59 *     equal to the selection of children of iteration 1</td>
60 *     <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td>
61 *   </tr>
62 *   <tr>
63 *     <td>an iteration with a selection of semantically equal children</td>
64 *     <td>an iteration with a child that is semantically equal to the children of the child
65 *     selection of iteration 1</td>
66 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
67 *   </tr>
68 *   <tr>
69 *     <td>an iteration with a selection of semantically equal children</td>
70 *     <td>an iteration with a selection of semantically equal children that are all semantically
71 *     equal to the selection of children of iteration 1</td>
72 *     <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td>
73 *   </tr>
74 * </table>
75 *
76 * @version $Revision: $ $Date: 19.02.2012$
77 * @author 2012, last modified by $Author: patrick$
78 */
79public class IterationComparisonRule implements NodeComparisonRule {
80   
81    /** the rule manager for internally comparing task tree nodes */
82    private NodeEqualityRuleManager mRuleManager;
83
84    /**
85     * <p>
86     * simple constructor to provide the rule with the node equality rule manager to be able
87     * to perform comparisons of the children of provided task tree nodes
88     * </p>
89     *
90     * @param ruleManager the rule manager for comparing task tree nodes
91     */
92    IterationComparisonRule(NodeEqualityRuleManager ruleManager) {
93        super();
94        mRuleManager = ruleManager;
95    }
96
97    /*
98     * (non-Javadoc)
99     *
100     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
101     */
102    @Override
103    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
104        if ((!(node1 instanceof IIteration)) || (!(node2 instanceof IIteration))) {
105            return null;
106        }
107
108        if (node1 == node2) {
109            return NodeEquality.IDENTICAL;
110        }
111
112        // if both iterations do not have children, they are equal although this doesn't make sense
113        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
114            return NodeEquality.LEXICALLY_EQUAL;
115        }
116        else if ((node1.getChildren().size() == 0) || (node2.getChildren().size() == 0)) {
117            return NodeEquality.UNEQUAL;
118        }
119
120        ITaskTreeNode child1 = node1.getChildren().get(0);
121        ITaskTreeNode child2 = node2.getChildren().get(0);
122
123        // iterations may have 3 different structures.
124        // 1. they have one child, which is the iterated one
125        // 2. they have a sequence of children, which is iterated
126        // 3. they have a selection of different iterated variants (usually the variants are
127        // semantically equal)
128        //
129        // the permutations of the three variants in combination must be checked
130
131        // check if both nodes are the same variants of iterations and if their children are equal.
132        // This condition matches, if both iterations are the same variants of iteration. I.e. three
133        // combinations of the permutation are handled herewith.
134        NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
135
136        if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
137            // prevent, that identical is returned, because the iterations itself are not identical
138            // although the iterated tasks are
139            if (nodeEquality == NodeEquality.IDENTICAL) {
140                return NodeEquality.LEXICALLY_EQUAL;
141            }
142            else {
143                return nodeEquality;
144            }
145        }
146
147        // compare one iteration with a single node as a child and another one with a selection of
148        // semantically equal nodes
149        return selectionChildrenSemanticallyEqualNode(child1, child2);
150       
151        // all other combinations (i.e. sequence with single child and sequence with selection)
152        // can not match
153    }
154
155    /**
156     * <p>
157     * compares two task tree nodes. One of them must be a selection, the other one can be any task
158     * tree node. The method returns a node equality that is not <code>NodeEquality.UNEQUAL</code>
159     * if the other node is at least semantically equal to the children of the selection. It
160     * returns more concrete equalities, if the equality between the other node and the children
161     * of the selection is more concrete.
162     * </p>
163     *
164     * @param taskTreeNode  the first task tree node to compare
165     * @param taskTreeNode2 the second task tree node to compare
166     *
167     * @return as described
168     */
169    private NodeEquality selectionChildrenSemanticallyEqualNode(ITaskTreeNode taskTreeNode,
170                                                                ITaskTreeNode taskTreeNode2)
171    {
172        ISelection selection = null;
173        ITaskTreeNode node = null;
174        if (taskTreeNode instanceof ISelection) {
175            selection = (ISelection) taskTreeNode;
176            node = taskTreeNode2;
177        }
178        else if (taskTreeNode2 instanceof ISelection) {
179            selection = (ISelection) taskTreeNode2;
180            node = taskTreeNode;
181        }
182        else {
183            return NodeEquality.UNEQUAL;
184        }
185
186        // Iterations, where one has a selection and the other one not can at most be syntactically
187        // equal but not identical
188        NodeEquality commonDenominatorForAllComparisons = NodeEquality.SYNTACTICALLY_EQUAL;
189
190        for (ITaskTreeNode child : selection.getChildren()) {
191            NodeEquality nodeEquality = mRuleManager.applyRules(node, child);
192
193            if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL))
194            {
195                return NodeEquality.UNEQUAL;
196            }
197           
198            commonDenominatorForAllComparisons =
199                commonDenominatorForAllComparisons.getCommonDenominator(nodeEquality);
200        }
201
202        return commonDenominatorForAllComparisons;
203    }
204
205}
Note: See TracBrowser for help on using the repository browser.