source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/SelectionComparisonRule.java @ 807

Last change on this file since 807 was 807, checked in by pharms, 12 years ago
  • improved node equality comparison to match the principle of lexical, syntactical and semantical node equality. As a result, more condensed task trees are created.
  • Property svn:executable set to *
File size: 4.4 KB
Line 
1package de.ugoe.cs.quest.tasktrees.nodeequality;
2
3import de.ugoe.cs.quest.tasktrees.treeifc.ISelection;
4import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
5
6/**
7 * <p>
8 * this node comparison rule is capable of comparing selections. If both selections do not have
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.
16 * </p>
17 *
18 * @version $Revision: $ $Date: 19.02.2012$
19 * @author 2012, last modified by $Author: patrick$
20 */
21public class SelectionComparisonRule implements NodeComparisonRule {
22
23    /** the rule manager for internally comparing task tree nodes */
24    private NodeEqualityRuleManager mRuleManager;
25
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    }
38
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;
48        }
49
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.
56        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
57            return NodeEquality.LEXICALLY_EQUAL;
58        }
59        else if ((node1.getChildren().size() == 0) || (node2.getChildren().size() == 0)) {
60            return NodeEquality.UNEQUAL;
61        }
62
63        NodeEquality selectionEquality = NodeEquality.LEXICALLY_EQUAL;
64
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        }
89
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                    }
102                }
103            }
104           
105            if (childEquality != null) {
106                selectionEquality = selectionEquality.getCommonDenominator(childEquality);
107            }
108            else {
109                return NodeEquality.UNEQUAL;
110            }
111        }
112
113        return selectionEquality;
114    }
115
116}
Note: See TracBrowser for help on using the repository browser.