1 | // Module : $RCSfile: NodeIdentityRule.java,v $ |
---|
2 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 19.02.2012 $ |
---|
3 | // Project : TaskTreeCreator |
---|
4 | // Creation : 2012 by patrick |
---|
5 | // Copyright : Patrick Harms, 2012 |
---|
6 | |
---|
7 | package de.ugoe.cs.quest.tasktrees.nodeequality; |
---|
8 | |
---|
9 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
10 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
11 | |
---|
12 | /** |
---|
13 | * <p> |
---|
14 | * this node comparison rule is capable of comparing selections. If both selections do not have |
---|
15 | * children, they are treated as lexically equal. If they have the same number of children other |
---|
16 | * than 0 and all these children are lexically equal, then the selections are lexically equal. |
---|
17 | * They are syntactically equal, if each child of both selections is syntactically equal to any |
---|
18 | * other child. The rule can not compare semantical equality if the nodes are not at least |
---|
19 | * syntactically equal and returns null, if it can not decide this. |
---|
20 | * </p> |
---|
21 | * |
---|
22 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
23 | * @author 2012, last modified by $Author: patrick$ |
---|
24 | */ |
---|
25 | public class SelectionComparisonRule implements NodeComparisonRule { |
---|
26 | |
---|
27 | /** the rule manager for internally comparing task tree nodes */ |
---|
28 | private NodeEqualityRuleManager mRuleManager; |
---|
29 | |
---|
30 | /** |
---|
31 | * <p> |
---|
32 | * simple constructor to provide the rule with the node equality rule manager to be able |
---|
33 | * to perform comparisons of the children of provided task tree nodes |
---|
34 | * </p> |
---|
35 | * |
---|
36 | * @param ruleManager the rule manager for comparing task tree nodes |
---|
37 | */ |
---|
38 | SelectionComparisonRule(NodeEqualityRuleManager ruleManager) { |
---|
39 | super(); |
---|
40 | mRuleManager = ruleManager; |
---|
41 | } |
---|
42 | |
---|
43 | /* |
---|
44 | * (non-Javadoc) |
---|
45 | * |
---|
46 | * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode) |
---|
47 | */ |
---|
48 | @Override |
---|
49 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
50 | if ((!(node1 instanceof ISelection)) || (!(node2 instanceof ISelection))) { |
---|
51 | return null; |
---|
52 | } |
---|
53 | |
---|
54 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
55 | if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) { |
---|
56 | return NodeEquality.LEXICALLY_EQUAL; |
---|
57 | } |
---|
58 | |
---|
59 | // Selections are syntactically equal, if they have children, which are all syntactically |
---|
60 | // equal. |
---|
61 | // They are lexically equals, if they have the same number and order of lexically equal |
---|
62 | // children |
---|
63 | boolean lexicallyEqual = node1.getChildren().size() == node2.getChildren().size(); |
---|
64 | |
---|
65 | for (int i = 0; i < node1.getChildren().size(); i++) { |
---|
66 | ITaskTreeNode child1 = node1.getChildren().get(i); |
---|
67 | boolean foundLexicallyEqualChild = false; |
---|
68 | |
---|
69 | for (int j = 0; j < node2.getChildren().size(); j++) { |
---|
70 | ITaskTreeNode child2 = node2.getChildren().get(j); |
---|
71 | |
---|
72 | NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2); |
---|
73 | |
---|
74 | if (!nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) { |
---|
75 | return null; |
---|
76 | } |
---|
77 | else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
78 | foundLexicallyEqualChild = true; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | // if we compare two children at the same position and if they are lexically equal |
---|
83 | // then it can be further expected, that the selections are lexically equal |
---|
84 | lexicallyEqual &= foundLexicallyEqualChild; |
---|
85 | } |
---|
86 | |
---|
87 | if (lexicallyEqual) { |
---|
88 | return NodeEquality.LEXICALLY_EQUAL; |
---|
89 | } |
---|
90 | else { |
---|
91 | return NodeEquality.SYNTACTICALLY_EQUAL; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|