source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/nodeequality/SequenceComparisonRule.java @ 1061

Last change on this file since 1061 was 922, checked in by sherbold, 12 years ago
  • renaming of packages from de.ugoe.cs.quest to de.ugoe.cs.autoquest
  • Property svn:executable set to *
File size: 2.6 KB
RevLine 
[922]1package de.ugoe.cs.autoquest.tasktrees.nodeequality;
[439]2
[922]3import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
4import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
[439]5
6/**
[557]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>
[439]13 *
14 * @version $Revision: $ $Date: 19.02.2012$
15 * @author 2012, last modified by $Author: patrick$
16 */
[557]17public class SequenceComparisonRule implements NodeComparisonRule {
[439]18
[557]19    /** the rule manager for internally comparing task tree nodes */
20    private NodeEqualityRuleManager mRuleManager;
[439]21
[557]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    }
[439]34
[557]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        }
[439]45
[807]46        if (node1 == node2) {
47            return NodeEquality.IDENTICAL;
48        }
49
[557]50        // if both sequences do not have children, they are equal although this doesn't make sense
51        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
52            return NodeEquality.LEXICALLY_EQUAL;
53        }
54
55        if (node1.getChildren().size() != node2.getChildren().size()) {
[807]56            return NodeEquality.UNEQUAL;
[557]57        }
58
[807]59        NodeEquality resultingEquality = NodeEquality.LEXICALLY_EQUAL;
[557]60        for (int i = 0; i < node1.getChildren().size(); i++) {
61            ITaskTreeNode child1 = node1.getChildren().get(i);
62            ITaskTreeNode child2 = node2.getChildren().get(i);
63
64            NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
65
[807]66            if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL)) {
67                return NodeEquality.UNEQUAL;
[557]68            }
[807]69           
70            resultingEquality = resultingEquality.getCommonDenominator(nodeEquality);
[557]71        }
72
[807]73        return resultingEquality;
[439]74    }
75
76}
Note: See TracBrowser for help on using the repository browser.