1 | package de.ugoe.cs.quest.tasktrees.nodeequality; |
---|
2 | |
---|
3 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
4 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
5 | |
---|
6 | /** |
---|
7 | * <p> |
---|
8 | * This class is capable of comparing any task tree node which is not a selection with a |
---|
9 | * selection. This is needed, because selections may contain exactly that node. Therefore, if |
---|
10 | * this node is selected out of a selection the selection is equal to the node itself. |
---|
11 | * The rule returns lexically equal, it the selection contains a lexically equal node. The same |
---|
12 | * applies for syntactical and semantical equality. |
---|
13 | * </p> |
---|
14 | |
---|
15 | * @author Patrick Harms |
---|
16 | */ |
---|
17 | public class NodeAndSelectionComparisonRule implements NodeComparisonRule { |
---|
18 | |
---|
19 | /** the rule manager for internally comparing task tree nodes */ |
---|
20 | private NodeEqualityRuleManager mRuleManager; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * simple constructor to provide the rule with the node equality rule manager to be able |
---|
25 | * to perform comparisons of the children of provided task tree nodes |
---|
26 | * </p> |
---|
27 | * |
---|
28 | * @param ruleManager the rule manager for comparing task tree nodes |
---|
29 | */ |
---|
30 | NodeAndSelectionComparisonRule(NodeEqualityRuleManager ruleManager) { |
---|
31 | super(); |
---|
32 | mRuleManager = ruleManager; |
---|
33 | } |
---|
34 | |
---|
35 | /* |
---|
36 | * (non-Javadoc) |
---|
37 | * |
---|
38 | * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) |
---|
39 | */ |
---|
40 | @Override |
---|
41 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
42 | ISelection selection = null; |
---|
43 | ITaskTreeNode node = null; |
---|
44 | |
---|
45 | if (node1 instanceof ISelection) { |
---|
46 | if (node2 instanceof ISelection) { |
---|
47 | // the rule is not responsible for two iterations |
---|
48 | return null; |
---|
49 | } |
---|
50 | |
---|
51 | selection = (ISelection) node1; |
---|
52 | node = node2; |
---|
53 | } |
---|
54 | else if (node2 instanceof ISelection) { |
---|
55 | if (node1 instanceof ISelection) { |
---|
56 | // the rule is not responsible for two iterations |
---|
57 | return null; |
---|
58 | } |
---|
59 | |
---|
60 | selection = (ISelection) node2; |
---|
61 | node = node1; |
---|
62 | } |
---|
63 | else { |
---|
64 | return null; |
---|
65 | } |
---|
66 | |
---|
67 | // now, that we found the iteration and the node, lets compare the child of the iteration |
---|
68 | // with the node. |
---|
69 | if (selection.getChildren().size() < 1) { |
---|
70 | return null; |
---|
71 | } |
---|
72 | |
---|
73 | NodeEquality mostConcreteNodeEquality = null; |
---|
74 | |
---|
75 | for (ITaskTreeNode child : selection.getChildren()) { |
---|
76 | NodeEquality nodeEquality = mRuleManager.applyRules(child, node); |
---|
77 | |
---|
78 | if (nodeEquality != NodeEquality.UNEQUAL) { |
---|
79 | if (mostConcreteNodeEquality == null) { |
---|
80 | mostConcreteNodeEquality = nodeEquality; |
---|
81 | } |
---|
82 | else { |
---|
83 | mostConcreteNodeEquality = |
---|
84 | mostConcreteNodeEquality.getCommonDenominator(nodeEquality); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | // although the subtask may be identical to the node, we can not return identical, as |
---|
90 | // the selection is not identical to the node, but at most lexically equal |
---|
91 | if (mostConcreteNodeEquality == NodeEquality.IDENTICAL) { |
---|
92 | return NodeEquality.LEXICALLY_EQUAL; |
---|
93 | } |
---|
94 | else { |
---|
95 | return mostConcreteNodeEquality; |
---|
96 | } |
---|
97 | |
---|
98 | } |
---|
99 | } |
---|