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