source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/nodeequality/SequenceComparisonRule.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: 2.3 KB
Line 
1package de.ugoe.cs.quest.tasktrees.nodeequality;
2
3import de.ugoe.cs.quest.tasktrees.treeifc.ISequence;
4import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
5
6/**
7 * <p>
8 * This rule is capable of comparing sequences. If both sequences do not have children, they are
9 * treated as lexically equal. Sequences are lexically equal, if they have the same number and
10 * order of lexically equal children. The rule can not decide, if two sequences are syntactically
11 * or semantically equal.
12 * </p>
13 *
14 * @version $Revision: $ $Date: 19.02.2012$
15 * @author 2012, last modified by $Author: patrick$
16 */
17public class SequenceComparisonRule 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    SequenceComparisonRule(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        if ((!(node1 instanceof ISequence)) || (!(node2 instanceof ISequence))) {
43            return null;
44        }
45
46        // if both sequences do not have children, they are equal although this doesn't make sense
47        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
48            return NodeEquality.LEXICALLY_EQUAL;
49        }
50
51        //
52        if (node1.getChildren().size() != node2.getChildren().size()) {
53            return null;
54        }
55
56        for (int i = 0; i < node1.getChildren().size(); i++) {
57            ITaskTreeNode child1 = node1.getChildren().get(i);
58            ITaskTreeNode child2 = node2.getChildren().get(i);
59
60            NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
61
62            if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) {
63                return null;
64            }
65        }
66
67        return NodeEquality.LEXICALLY_EQUAL;
68    }
69
70}
Note: See TracBrowser for help on using the repository browser.