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

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
  • Property svn:executable set to *
File size: 3.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 lexically equal. If they have the same number of children other
10 * than 0 and all these children are lexically equal, then the selections are lexically equal.
11 * They are syntactically equal, if each child of both selections is syntactically equal to any
12 * other child. The rule can not compare semantical equality if the nodes are not at least
13 * syntactically equal and returns null, if it can not decide this.
14 * </p>
15 *
16 * @version $Revision: $ $Date: 19.02.2012$
17 * @author 2012, last modified by $Author: patrick$
18 */
19public class SelectionComparisonRule implements NodeComparisonRule {
20
21    /** the rule manager for internally comparing task tree nodes */
22    private NodeEqualityRuleManager mRuleManager;
23
24    /**
25     * <p>
26     * simple constructor to provide the rule with the node equality rule manager to be able
27     * to perform comparisons of the children of provided task tree nodes
28     * </p>
29     *
30     * @param ruleManager the rule manager for comparing task tree nodes
31     */
32    SelectionComparisonRule(NodeEqualityRuleManager ruleManager) {
33        super();
34        mRuleManager = ruleManager;
35    }
36
37    /*
38     * (non-Javadoc)
39     *
40     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
41     */
42    @Override
43    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
44        if ((!(node1 instanceof ISelection)) || (!(node2 instanceof ISelection))) {
45            return null;
46        }
47
48        // if both sequences do not have children, they are equal although this doesn't make sense
49        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
50            return NodeEquality.LEXICALLY_EQUAL;
51        }
52
53        // Selections are syntactically equal, if they have children, which are all syntactically
54        // equal.
55        // They are lexically equals, if they have the same number and order of lexically equal
56        // children
57        boolean lexicallyEqual = node1.getChildren().size() == node2.getChildren().size();
58
59        for (int i = 0; i < node1.getChildren().size(); i++) {
60            ITaskTreeNode child1 = node1.getChildren().get(i);
61            boolean foundLexicallyEqualChild = false;
62
63            for (int j = 0; j < node2.getChildren().size(); j++) {
64                ITaskTreeNode child2 = node2.getChildren().get(j);
65
66                NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
67
68                if (!nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) {
69                    return null;
70                }
71                else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) {
72                    foundLexicallyEqualChild = true;
73                }
74            }
75
76            // if we compare two children at the same position and if they are lexically equal
77            // then it can be further expected, that the selections are lexically equal
78            lexicallyEqual &= foundLexicallyEqualChild;
79        }
80
81        if (lexicallyEqual) {
82            return NodeEquality.LEXICALLY_EQUAL;
83        }
84        else {
85            return NodeEquality.SYNTACTICALLY_EQUAL;
86        }
87    }
88}
Note: See TracBrowser for help on using the repository browser.