[922] | 1 | package de.ugoe.cs.autoquest.tasktrees.nodeequality; |
---|
[439] | 2 | |
---|
[922] | 3 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 4 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
[439] | 5 | |
---|
| 6 | /** |
---|
[557] | 7 | * <p> |
---|
| 8 | * this node comparison rule is capable of comparing selections. If both selections do not have |
---|
[807] | 9 | * children, they are treated as identical. If they have children, each child of both selections |
---|
| 10 | * is compared to each child of the respective other selection. The resulting equality is the most |
---|
| 11 | * concrete one of all these comparisons. I.e. if all children are at least lexically equal, then |
---|
| 12 | * the selections are lexically equal. If all children are at least syntactically equal, then the |
---|
| 13 | * selections are syntactically equal. If all children are at least semantically equal, then the |
---|
| 14 | * selections are semantically equal. If only one of the selections has children, then the |
---|
| 15 | * selections are unequal. |
---|
[557] | 16 | * </p> |
---|
[439] | 17 | * |
---|
| 18 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 19 | * @author 2012, last modified by $Author: patrick$ |
---|
| 20 | */ |
---|
[557] | 21 | public class SelectionComparisonRule implements NodeComparisonRule { |
---|
[439] | 22 | |
---|
[557] | 23 | /** the rule manager for internally comparing task tree nodes */ |
---|
| 24 | private NodeEqualityRuleManager mRuleManager; |
---|
[439] | 25 | |
---|
[557] | 26 | /** |
---|
| 27 | * <p> |
---|
| 28 | * simple constructor to provide the rule with the node equality rule manager to be able |
---|
| 29 | * to perform comparisons of the children of provided task tree nodes |
---|
| 30 | * </p> |
---|
| 31 | * |
---|
| 32 | * @param ruleManager the rule manager for comparing task tree nodes |
---|
| 33 | */ |
---|
| 34 | SelectionComparisonRule(NodeEqualityRuleManager ruleManager) { |
---|
| 35 | super(); |
---|
| 36 | mRuleManager = ruleManager; |
---|
| 37 | } |
---|
[439] | 38 | |
---|
[557] | 39 | /* |
---|
| 40 | * (non-Javadoc) |
---|
| 41 | * |
---|
| 42 | * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) |
---|
| 43 | */ |
---|
| 44 | @Override |
---|
| 45 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
| 46 | if ((!(node1 instanceof ISelection)) || (!(node2 instanceof ISelection))) { |
---|
| 47 | return null; |
---|
[439] | 48 | } |
---|
[557] | 49 | |
---|
[807] | 50 | if (node1 == node2) { |
---|
| 51 | return NodeEquality.IDENTICAL; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // if both sequences do not have children, they are identical. If only one of them has |
---|
| 55 | // children, they are unequal. |
---|
[557] | 56 | if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { |
---|
| 57 | return NodeEquality.LEXICALLY_EQUAL; |
---|
[439] | 58 | } |
---|
[807] | 59 | else if ((node1.getChildren().size() == 0) || (node2.getChildren().size() == 0)) { |
---|
| 60 | return NodeEquality.UNEQUAL; |
---|
| 61 | } |
---|
[557] | 62 | |
---|
[807] | 63 | NodeEquality selectionEquality = NodeEquality.LEXICALLY_EQUAL; |
---|
[557] | 64 | |
---|
[807] | 65 | // compare each child of selection one with each child of selection two |
---|
| 66 | NodeEquality childEquality; |
---|
| 67 | NodeEquality currentEquality; |
---|
| 68 | for (ITaskTreeNode child1 : node1.getChildren()) { |
---|
| 69 | childEquality = null; |
---|
| 70 | for (ITaskTreeNode child2 : node2.getChildren()) { |
---|
| 71 | currentEquality = mRuleManager.applyRules(child1, child2); |
---|
| 72 | if ((currentEquality != null) && (currentEquality != NodeEquality.UNEQUAL)) { |
---|
| 73 | if (childEquality == null) { |
---|
| 74 | childEquality = currentEquality; |
---|
| 75 | } |
---|
| 76 | else { |
---|
| 77 | childEquality = childEquality.getCommonDenominator(currentEquality); |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | if (childEquality != null) { |
---|
| 83 | selectionEquality = selectionEquality.getCommonDenominator(childEquality); |
---|
| 84 | } |
---|
| 85 | else { |
---|
| 86 | return NodeEquality.UNEQUAL; |
---|
| 87 | } |
---|
| 88 | } |
---|
[557] | 89 | |
---|
[807] | 90 | // compare each child of selection two with each child of selection one |
---|
| 91 | for (ITaskTreeNode child2 : node2.getChildren()) { |
---|
| 92 | childEquality = null; |
---|
| 93 | for (ITaskTreeNode child1 : node1.getChildren()) { |
---|
| 94 | currentEquality = mRuleManager.applyRules(child1, child2); |
---|
| 95 | if ((currentEquality != null) && (currentEquality != NodeEquality.UNEQUAL)) { |
---|
| 96 | if (childEquality == null) { |
---|
| 97 | childEquality = currentEquality; |
---|
| 98 | } |
---|
| 99 | else { |
---|
| 100 | childEquality = childEquality.getCommonDenominator(currentEquality); |
---|
| 101 | } |
---|
[557] | 102 | } |
---|
| 103 | } |
---|
[807] | 104 | |
---|
| 105 | if (childEquality != null) { |
---|
| 106 | selectionEquality = selectionEquality.getCommonDenominator(childEquality); |
---|
| 107 | } |
---|
| 108 | else { |
---|
| 109 | return NodeEquality.UNEQUAL; |
---|
| 110 | } |
---|
[557] | 111 | } |
---|
| 112 | |
---|
[807] | 113 | return selectionEquality; |
---|
[439] | 114 | } |
---|
[807] | 115 | |
---|
[439] | 116 | } |
---|