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.ISelection; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * this node comparison rule is capable of comparing selections. If both selections do not have |
---|
25 | * children, they are treated as identical. If they have children, each child of both selections |
---|
26 | * is compared to each child of the respective other selection. The resulting equality is the most |
---|
27 | * concrete one of all these comparisons. I.e. if all children are at least lexically equal, then |
---|
28 | * the selections are lexically equal. If all children are at least syntactically equal, then the |
---|
29 | * selections are syntactically equal. If all children are at least semantically equal, then the |
---|
30 | * selections are semantically equal. If only one of the selections has children, then the |
---|
31 | * selections are unequal. |
---|
32 | * </p> |
---|
33 | * |
---|
34 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
35 | * @author 2012, last modified by $Author: patrick$ |
---|
36 | */ |
---|
37 | public class SelectionComparisonRule implements NodeComparisonRule { |
---|
38 | |
---|
39 | /** the rule manager for internally comparing task tree nodes */ |
---|
40 | private NodeEqualityRuleManager mRuleManager; |
---|
41 | |
---|
42 | /** |
---|
43 | * <p> |
---|
44 | * simple constructor to provide the rule with the node equality rule manager to be able |
---|
45 | * to perform comparisons of the children of provided task tree nodes |
---|
46 | * </p> |
---|
47 | * |
---|
48 | * @param ruleManager the rule manager for comparing task tree nodes |
---|
49 | */ |
---|
50 | SelectionComparisonRule(NodeEqualityRuleManager ruleManager) { |
---|
51 | super(); |
---|
52 | mRuleManager = ruleManager; |
---|
53 | } |
---|
54 | |
---|
55 | /* (non-Javadoc) |
---|
56 | * @see NodeComparisonRule#isApplicable(ITaskTreeNode, ITaskTreeNode) |
---|
57 | */ |
---|
58 | @Override |
---|
59 | public boolean isApplicable(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
60 | return (node1 instanceof ISelection) && (node2 instanceof ISelection); |
---|
61 | } |
---|
62 | |
---|
63 | /* (non-Javadoc) |
---|
64 | * @see NodeComparisonRule#areLexicallyEqual(ITaskTreeNode, ITaskTreeNode) |
---|
65 | */ |
---|
66 | @Override |
---|
67 | public boolean areLexicallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
68 | NodeEquality equality = getEquality(node1, node2, NodeEquality.LEXICALLY_EQUAL); |
---|
69 | return (equality != null) && (equality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)); |
---|
70 | } |
---|
71 | |
---|
72 | /* (non-Javadoc) |
---|
73 | * @see NodeComparisonRule#areSyntacticallyEqual(ITaskTreeNode, ITaskTreeNode) |
---|
74 | */ |
---|
75 | @Override |
---|
76 | public boolean areSyntacticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
77 | NodeEquality equality = getEquality(node1, node2, NodeEquality.SYNTACTICALLY_EQUAL); |
---|
78 | return (equality != null) && (equality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)); |
---|
79 | } |
---|
80 | |
---|
81 | /* (non-Javadoc) |
---|
82 | * @see NodeComparisonRule#areSemanticallyEqual(ITaskTreeNode, ITaskTreeNode) |
---|
83 | */ |
---|
84 | @Override |
---|
85 | public boolean areSemanticallyEqual(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
86 | NodeEquality equality = getEquality(node1, node2, NodeEquality.SEMANTICALLY_EQUAL); |
---|
87 | return (equality != null) && (equality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)); |
---|
88 | } |
---|
89 | |
---|
90 | /* (non-Javadoc) |
---|
91 | * @see NodeComparisonRule#compare(ITaskTreeNode, ITaskTreeNode) |
---|
92 | */ |
---|
93 | @Override |
---|
94 | public NodeEquality compare(ITaskTreeNode node1, ITaskTreeNode node2) { |
---|
95 | return getEquality(node1, node2, null); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * |
---|
100 | */ |
---|
101 | private NodeEquality getEquality(ITaskTreeNode node1, |
---|
102 | ITaskTreeNode node2, |
---|
103 | NodeEquality requiredEqualityLevel) |
---|
104 | { |
---|
105 | List<ITaskTreeNode> children1 = node1.getChildren(); |
---|
106 | List<ITaskTreeNode> children2 = node2.getChildren(); |
---|
107 | |
---|
108 | // if both selections do not have children, they are lexically equal. If only one of them |
---|
109 | // has children, they are unequal. |
---|
110 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
111 | return NodeEquality.LEXICALLY_EQUAL; |
---|
112 | } |
---|
113 | else if ((children1.size() == 0) || (children2.size() == 0)) { |
---|
114 | return NodeEquality.UNEQUAL; |
---|
115 | } |
---|
116 | |
---|
117 | NodeEquality selectionEquality; |
---|
118 | |
---|
119 | if (requiredEqualityLevel == null) { |
---|
120 | // calculate the common equality level for all children of both selections. |
---|
121 | // do it in both directions to ensure commutative comparison |
---|
122 | selectionEquality = getCommonEqualityLevel(children1, children2); |
---|
123 | if (selectionEquality != NodeEquality.UNEQUAL) { |
---|
124 | return selectionEquality.getCommonDenominator |
---|
125 | (getCommonEqualityLevel(children2, children1)); |
---|
126 | } |
---|
127 | else { |
---|
128 | return NodeEquality.UNEQUAL; |
---|
129 | } |
---|
130 | } |
---|
131 | else { |
---|
132 | // we are searching for a specific equality |
---|
133 | if (checkEqualityLevel(children1, children2, requiredEqualityLevel) && |
---|
134 | checkEqualityLevel(children2, children1, requiredEqualityLevel)) |
---|
135 | { |
---|
136 | return requiredEqualityLevel; |
---|
137 | } |
---|
138 | else { |
---|
139 | return NodeEquality.UNEQUAL; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * <p> |
---|
146 | * TODO: comment |
---|
147 | * </p> |
---|
148 | * |
---|
149 | * @param children1 |
---|
150 | * @param children2 |
---|
151 | * @param requiredEqualityLevel |
---|
152 | */ |
---|
153 | private NodeEquality getCommonEqualityLevel(List<ITaskTreeNode> children1, |
---|
154 | List<ITaskTreeNode> children2) |
---|
155 | { |
---|
156 | NodeEquality listEquality = NodeEquality.LEXICALLY_EQUAL; |
---|
157 | |
---|
158 | NodeEquality childEquality; |
---|
159 | NodeEquality currentEquality; |
---|
160 | for (ITaskTreeNode child1 : children1) { |
---|
161 | childEquality = null; |
---|
162 | for (ITaskTreeNode child2 : children2) { |
---|
163 | currentEquality = callRuleManager(child1, child2, null); |
---|
164 | if ((currentEquality != null) && (currentEquality != NodeEquality.UNEQUAL)) { |
---|
165 | if (childEquality == null) { |
---|
166 | childEquality = currentEquality; |
---|
167 | } |
---|
168 | else { |
---|
169 | childEquality = childEquality.getCommonDenominator(currentEquality); |
---|
170 | } |
---|
171 | |
---|
172 | if (childEquality == NodeEquality.SEMANTICALLY_EQUAL) { |
---|
173 | // as we calculate only the common denominator, we can break up here for |
---|
174 | // the current child. We will not improve the denominator anymore |
---|
175 | break; |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | if (childEquality == null) { |
---|
181 | // we did not find any child in the second list, that is equal to the searched |
---|
182 | // child |
---|
183 | return NodeEquality.UNEQUAL; |
---|
184 | } |
---|
185 | else { |
---|
186 | listEquality = listEquality.getCommonDenominator(childEquality); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | return listEquality; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * <p> |
---|
195 | * TODO: comment |
---|
196 | * </p> |
---|
197 | * |
---|
198 | * @param children1 |
---|
199 | * @param children2 |
---|
200 | * @param requiredEqualityLevel |
---|
201 | */ |
---|
202 | private boolean checkEqualityLevel(List<ITaskTreeNode> children1, |
---|
203 | List<ITaskTreeNode> children2, |
---|
204 | NodeEquality requiredEqualityLevel) |
---|
205 | { |
---|
206 | NodeEquality childEquality; |
---|
207 | NodeEquality currentEquality; |
---|
208 | for (ITaskTreeNode child1 : children1) { |
---|
209 | childEquality = null; |
---|
210 | for (ITaskTreeNode child2 : children2) { |
---|
211 | currentEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
212 | if ((currentEquality != null) && (currentEquality.isAtLeast(requiredEqualityLevel))) |
---|
213 | { |
---|
214 | // we found at least one equal child with sufficient equality in the |
---|
215 | // second list. So be can break up for this child. |
---|
216 | childEquality = currentEquality; |
---|
217 | break; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | if (childEquality == null) { |
---|
222 | // we did not find any child in the second list, that is equal to the searched |
---|
223 | // child |
---|
224 | return false; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | // for all children, we found an equality |
---|
229 | return true; |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * <p> |
---|
234 | * TODO: comment |
---|
235 | * </p> |
---|
236 | * |
---|
237 | * @param child1 |
---|
238 | * @param child2 |
---|
239 | * @param requiredEqualityLevel |
---|
240 | * @return |
---|
241 | */ |
---|
242 | private NodeEquality callRuleManager(ITaskTreeNode child1, |
---|
243 | ITaskTreeNode child2, |
---|
244 | NodeEquality requiredEqualityLevel) |
---|
245 | { |
---|
246 | if (requiredEqualityLevel == null) { |
---|
247 | return mRuleManager.compare(child1, child2); |
---|
248 | } |
---|
249 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
250 | return requiredEqualityLevel; |
---|
251 | } |
---|
252 | else { |
---|
253 | return NodeEquality.UNEQUAL; |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | } |
---|