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

Last change on this file since 816 was 816, checked in by pharms, 12 years ago
  • improved node comparison between iterations and other nodes as well as selections and other nodes
File size: 2.9 KB
Line 
1package de.ugoe.cs.quest.tasktrees.nodeequality;
2
3import de.ugoe.cs.quest.tasktrees.treeifc.IIteration;
4import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
5
6/**
7 * <p>
8 * This class is capable of comparing any task tree node which is not an iteration with an
9 * iteration. This is needed, because iterations may iterate exactly that node. In this
10 * case, the iteration would be equal to that node if it was executed exactly once. The rule
11 * returns lexically equal, it the child of the iteration is lexically equal to the node
12 * or if the child of the iteration is a selection and this selections contains a lexically equal
13 * node. The same applies for syntactical and semantical equality.
14 * </p>
15
16 * @author Patrick Harms
17 */
18public class NodeAndIterationComparisonRule implements NodeComparisonRule {
19   
20    /** the rule manager for internally comparing task tree nodes */
21    private NodeEqualityRuleManager mRuleManager;
22
23    /**
24     * <p>
25     * simple constructor to provide the rule with the node equality rule manager to be able
26     * to perform comparisons of the children of provided task tree nodes
27     * </p>
28     *
29     * @param ruleManager the rule manager for comparing task tree nodes
30     */
31    NodeAndIterationComparisonRule(NodeEqualityRuleManager ruleManager) {
32        super();
33        mRuleManager = ruleManager;
34    }
35
36    /*
37     * (non-Javadoc)
38     *
39     * @see de.ugoe.cs.tasktree.nodeequality.NodeEqualityRule#apply(TaskTreeNode, TaskTreeNode)
40     */
41    @Override
42    public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) {
43        IIteration iteration = null;
44        ITaskTreeNode node = null;
45       
46        if (node1 instanceof IIteration) {
47            if (node2 instanceof IIteration) {
48                // the rule is not responsible for two iterations
49                return null;
50            }
51           
52            iteration = (IIteration) node1;
53            node = node2;
54        }
55        else if (node2 instanceof IIteration) {
56            if (node1 instanceof IIteration) {
57                // the rule is not responsible for two iterations
58                return null;
59            }
60           
61            iteration = (IIteration) node2;
62            node = node1;
63        }
64        else {
65            return null;
66        }
67
68        // now, that we found the iteration and the node, lets compare the child of the iteration
69        // with the node.
70        if (iteration.getChildren().size() < 1) {
71            return null;
72        }
73
74        NodeEquality nodeEquality = mRuleManager.applyRules(iteration.getChildren().get(0), node);
75
76        // although the subtask may be identical to the node, we can not return identical, as
77        // the iteration is not identical to the node, but at most lexically equal
78        if (nodeEquality == NodeEquality.IDENTICAL) {
79            return NodeEquality.LEXICALLY_EQUAL;
80        }
81        else {
82            return nodeEquality;
83        }
84
85    }
86}
Note: See TracBrowser for help on using the repository browser.