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

Last change on this file since 557 was 557, checked in by pharms, 12 years ago
  • adapted task tree creation stuff to more general event handling
  • Property svn:executable set to *
File size: 2.5 KB
RevLine 
[439]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
[557]6
[439]7package de.ugoe.cs.quest.tasktrees.nodeequality;
8
[557]9import de.ugoe.cs.quest.tasktrees.treeifc.ISequence;
10import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
[439]11
12/**
[557]13 * <p>
14 * This rule is capable of comparing sequences. If both sequences do not have children, they are
15 * treated as lexically equal. Sequences are lexically equal, if they have the same number and
16 * order of lexically equal children. The rule can not decide, if two sequences are syntactically
17 * or semantically equal.
18 * </p>
[439]19 *
20 * @version $Revision: $ $Date: 19.02.2012$
21 * @author 2012, last modified by $Author: patrick$
22 */
[557]23public class SequenceComparisonRule implements NodeComparisonRule {
[439]24
[557]25    /** the rule manager for internally comparing task tree nodes */
26    private NodeEqualityRuleManager mRuleManager;
[439]27
[557]28    /**
29     * <p>
30     * simple constructor to provide the rule with the node equality rule manager to be able
31     * to perform comparisons of the children of provided task tree nodes
32     * </p>
33     *
34     * @param ruleManager the rule manager for comparing task tree nodes
35     */
36    SequenceComparisonRule(NodeEqualityRuleManager ruleManager) {
37        super();
38        mRuleManager = ruleManager;
39    }
[439]40
[557]41    /*
42     * (non-Javadoc)
43     *
44     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
45     */
46    @Override
47    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
48        if ((!(node1 instanceof ISequence)) || (!(node2 instanceof ISequence))) {
49            return null;
50        }
[439]51
[557]52        // if both sequences do not have children, they are equal although this doesn't make sense
53        if ((node1.getChildren().size() == 0) && (node2.getChildren().size() == 0)) {
54            return NodeEquality.LEXICALLY_EQUAL;
55        }
56
57        //
58        if (node1.getChildren().size() != node2.getChildren().size()) {
59            return null;
60        }
61
62        for (int i = 0; i < node1.getChildren().size(); i++) {
63            ITaskTreeNode child1 = node1.getChildren().get(i);
64            ITaskTreeNode child2 = node2.getChildren().get(i);
65
66            NodeEquality nodeEquality = mRuleManager.applyRules(child1, child2);
67
68            if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) {
69                return null;
70            }
71        }
72
73        return NodeEquality.LEXICALLY_EQUAL;
[439]74    }
75
76}
Note: See TracBrowser for help on using the repository browser.