1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.nodeequality; |
---|
16 | |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * This rule is capable of comparing sequences. If both sequences do not have children, they are |
---|
25 | * treated as lexically equal. Sequences are lexically equal, if they have the same number and |
---|
26 | * order of lexically equal children. The rule can not decide, if two sequences are syntactically |
---|
27 | * or semantically equal. |
---|
28 | * </p> |
---|
29 | * |
---|
30 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
31 | * @author 2012, last modified by $Author: patrick$ |
---|
32 | */ |
---|
33 | public class SequenceComparisonRule implements NodeComparisonRule { |
---|
34 | |
---|
35 | /** the rule manager for internally comparing task tree nodes */ |
---|
36 | private NodeEqualityRuleManager mRuleManager; |
---|
37 | |
---|
38 | /** |
---|
39 | * <p> |
---|
40 | * simple constructor to provide the rule with the node equality rule manager to be able |
---|
41 | * to perform comparisons of the children of provided task tree nodes |
---|
42 | * </p> |
---|
43 | * |
---|
44 | * @param ruleManager the rule manager for comparing task tree nodes |
---|
45 | */ |
---|
46 | SequenceComparisonRule(NodeEqualityRuleManager ruleManager) { |
---|
47 | super(); |
---|
48 | mRuleManager = ruleManager; |
---|
49 | } |
---|
50 | |
---|
51 | /* (non-Javadoc) |
---|
52 | * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode) |
---|
53 | */ |
---|
54 | @Override |
---|
55 | public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
56 | return (node1 instanceof ISequence) && (node2 instanceof ISequence); |
---|
57 | } |
---|
58 | |
---|
59 | /* (non-Javadoc) |
---|
60 | * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode) |
---|
61 | */ |
---|
62 | @Override |
---|
63 | public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
64 | NodeEquality equality = getEquality(node1, node2, NodeEquality.LEXICALLY_EQUAL); |
---|
65 | return (equality != null) && (equality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)); |
---|
66 | } |
---|
67 | |
---|
68 | /* (non-Javadoc) |
---|
69 | * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode) |
---|
70 | */ |
---|
71 | @Override |
---|
72 | public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
73 | NodeEquality equality = getEquality(node1, node2, NodeEquality.SYNTACTICALLY_EQUAL); |
---|
74 | return (equality != null) && (equality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)); |
---|
75 | } |
---|
76 | |
---|
77 | /* (non-Javadoc) |
---|
78 | * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode) |
---|
79 | */ |
---|
80 | @Override |
---|
81 | public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
82 | NodeEquality equality = getEquality(node1, node2, NodeEquality.SEMANTICALLY_EQUAL); |
---|
83 | return (equality != null) && (equality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)); |
---|
84 | } |
---|
85 | |
---|
86 | /* (non-Javadoc) |
---|
87 | * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode) |
---|
88 | */ |
---|
89 | @Override |
---|
90 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
91 | return getEquality(node1, node2, null); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * |
---|
96 | */ |
---|
97 | private NodeEquality getEquality(ITaskTreeNode node1, |
---|
98 | ITaskTreeNode node2, |
---|
99 | NodeEquality requiredEqualityLevel) |
---|
100 | { |
---|
101 | List<ITaskTreeNode> children1 = node1.getChildren(); |
---|
102 | List<ITaskTreeNode> children2 = node2.getChildren(); |
---|
103 | |
---|
104 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
105 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
106 | return NodeEquality.LEXICALLY_EQUAL; |
---|
107 | } |
---|
108 | |
---|
109 | if (children1.size() != children2.size()) { |
---|
110 | return NodeEquality.UNEQUAL; |
---|
111 | } |
---|
112 | |
---|
113 | NodeEquality resultingEquality = NodeEquality.LEXICALLY_EQUAL; |
---|
114 | for (int i = 0; i < children1.size(); i++) { |
---|
115 | ITaskTreeNode child1 = children1.get(i); |
---|
116 | ITaskTreeNode child2 = children2.get(i); |
---|
117 | |
---|
118 | NodeEquality nodeEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
119 | |
---|
120 | if ((nodeEquality == null) || (nodeEquality == NodeEquality.UNEQUAL)) { |
---|
121 | return NodeEquality.UNEQUAL; |
---|
122 | } |
---|
123 | |
---|
124 | resultingEquality = resultingEquality.getCommonDenominator(nodeEquality); |
---|
125 | } |
---|
126 | |
---|
127 | return resultingEquality; |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * <p> |
---|
132 | * TODO: comment |
---|
133 | * </p> |
---|
134 | * |
---|
135 | * @param child1 |
---|
136 | * @param child2 |
---|
137 | * @param requiredEqualityLevel |
---|
138 | * @return |
---|
139 | */ |
---|
140 | private NodeEquality callRuleManager(ITaskTreeNode child1, |
---|
141 | ITaskTreeNode child2, |
---|
142 | NodeEquality requiredEqualityLevel) |
---|
143 | { |
---|
144 | if (requiredEqualityLevel == null) { |
---|
145 | return mRuleManager.compare(child1, child2); |
---|
146 | } |
---|
147 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
148 | return requiredEqualityLevel; |
---|
149 | } |
---|
150 | else { |
---|
151 | return NodeEquality.UNEQUAL; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|