1 | package de.ugoe.cs.quest.tasktrees.nodeequality; |
---|
2 | |
---|
3 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
4 | import 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 | */ |
---|
17 | public 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 (node1 == node2) { |
---|
47 | return NodeEquality.IDENTICAL; |
---|
48 | } |
---|
49 | |
---|
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()) { |
---|
56 | return NodeEquality.UNEQUAL; |
---|
57 | } |
---|
58 | |
---|
59 | NodeEquality resultingEquality = NodeEquality.LEXICALLY_EQUAL; |
---|
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 | |
---|
66 | if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL)) { |
---|
67 | return NodeEquality.UNEQUAL; |
---|
68 | } |
---|
69 | |
---|
70 | resultingEquality = resultingEquality.getCommonDenominator(nodeEquality); |
---|
71 | } |
---|
72 | |
---|
73 | return resultingEquality; |
---|
74 | } |
---|
75 | |
---|
76 | } |
---|