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.taskequality; |
---|
16 | |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
23 | |
---|
24 | // TODO: Auto-generated Javadoc |
---|
25 | /** |
---|
26 | * <p> |
---|
27 | * This class is capable of comparing any task which is not a selection with a |
---|
28 | * selection. This is needed, because selections may contain exactly that task. |
---|
29 | * Therefore, if this task is selected out of a selection the selection is equal |
---|
30 | * to the task itself. The rule returns lexically equal, if the selection |
---|
31 | * contains a lexically equal task. The same applies for syntactical and |
---|
32 | * semantical equality. |
---|
33 | * </p> |
---|
34 | * |
---|
35 | * @author Patrick Harms |
---|
36 | */ |
---|
37 | public class TaskAndSelectionComparisonRule implements TaskComparisonRule { |
---|
38 | |
---|
39 | /* |
---|
40 | * (non-Javadoc) |
---|
41 | * |
---|
42 | * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
43 | */ |
---|
44 | @Override |
---|
45 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
46 | final TaskEquality equality = getEquality(task1, task2, |
---|
47 | TaskEquality.LEXICALLY_EQUAL); |
---|
48 | return (equality != null) |
---|
49 | && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
50 | } |
---|
51 | |
---|
52 | /* |
---|
53 | * (non-Javadoc) |
---|
54 | * |
---|
55 | * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) |
---|
56 | */ |
---|
57 | @Override |
---|
58 | public boolean areLexicallyEqual(ITaskInstance instance1, |
---|
59 | ITaskInstance instance2) { |
---|
60 | final TaskEquality equality = getEquality(instance1, instance2, |
---|
61 | TaskEquality.LEXICALLY_EQUAL); |
---|
62 | return (equality != null) |
---|
63 | && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
64 | } |
---|
65 | |
---|
66 | /* |
---|
67 | * (non-Javadoc) |
---|
68 | * |
---|
69 | * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
70 | */ |
---|
71 | @Override |
---|
72 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
73 | final TaskEquality equality = getEquality(task1, task2, |
---|
74 | TaskEquality.SEMANTICALLY_EQUAL); |
---|
75 | return (equality != null) |
---|
76 | && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
77 | } |
---|
78 | |
---|
79 | /* |
---|
80 | * (non-Javadoc) |
---|
81 | * |
---|
82 | * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, |
---|
83 | * ITaskInstance) |
---|
84 | */ |
---|
85 | @Override |
---|
86 | public boolean areSemanticallyEqual(ITaskInstance instance1, |
---|
87 | ITaskInstance instance2) { |
---|
88 | final TaskEquality equality = getEquality(instance1, instance2, |
---|
89 | TaskEquality.SEMANTICALLY_EQUAL); |
---|
90 | return (equality != null) |
---|
91 | && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * (non-Javadoc) |
---|
96 | * |
---|
97 | * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
98 | */ |
---|
99 | @Override |
---|
100 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
101 | final TaskEquality equality = getEquality(task1, task2, |
---|
102 | TaskEquality.SYNTACTICALLY_EQUAL); |
---|
103 | return (equality != null) |
---|
104 | && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
105 | } |
---|
106 | |
---|
107 | /* |
---|
108 | * (non-Javadoc) |
---|
109 | * |
---|
110 | * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, |
---|
111 | * ITaskInstance) |
---|
112 | */ |
---|
113 | @Override |
---|
114 | public boolean areSyntacticallyEqual(ITaskInstance instance1, |
---|
115 | ITaskInstance instance2) { |
---|
116 | final TaskEquality equality = getEquality(instance1, instance2, |
---|
117 | TaskEquality.SYNTACTICALLY_EQUAL); |
---|
118 | return (equality != null) |
---|
119 | && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * <p> |
---|
124 | * used to to call the task equality rule manager for the comparison of the |
---|
125 | * two provided children. If no required equality level is provided, than |
---|
126 | * the most concrete equality is returned. Otherwise, the required equality |
---|
127 | * is returned as long as the children are equal on that level. |
---|
128 | * </p> |
---|
129 | * |
---|
130 | * @param child1 |
---|
131 | * the first task to be compared |
---|
132 | * @param child2 |
---|
133 | * the second task to be compared |
---|
134 | * @param requiredEqualityLevel |
---|
135 | * the equality level to be checked for |
---|
136 | * |
---|
137 | * @return the determined equality |
---|
138 | */ |
---|
139 | private TaskEquality callRuleManager(ITask child1, ITask child2, |
---|
140 | TaskEquality requiredEqualityLevel) { |
---|
141 | if (requiredEqualityLevel == null) { |
---|
142 | return TaskEqualityRuleManager.getInstance() |
---|
143 | .compare(child1, child2); |
---|
144 | } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual( |
---|
145 | child1, child2, requiredEqualityLevel)) { |
---|
146 | return requiredEqualityLevel; |
---|
147 | } else { |
---|
148 | return TaskEquality.UNEQUAL; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * <p> |
---|
154 | * used to to call the task equality rule manager for the comparison of the |
---|
155 | * two provided children. If no required equality level is provided, than |
---|
156 | * the most concrete equality is returned. Otherwise, the required equality |
---|
157 | * is returned as long as the children are equal on that level. |
---|
158 | * </p> |
---|
159 | * |
---|
160 | * @param taskInstance1 |
---|
161 | * the first task instance to be compared |
---|
162 | * @param taskInstance2 |
---|
163 | * the second task instance to be compared |
---|
164 | * @param requiredEqualityLevel |
---|
165 | * the equality level to be checked for |
---|
166 | * |
---|
167 | * @return the determined equality |
---|
168 | */ |
---|
169 | private TaskEquality callRuleManager(ITaskInstance taskInstance1, |
---|
170 | ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) { |
---|
171 | if (requiredEqualityLevel == null) { |
---|
172 | return TaskEqualityRuleManager.getInstance().compare(taskInstance1, |
---|
173 | taskInstance2); |
---|
174 | } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual( |
---|
175 | taskInstance1, taskInstance2, requiredEqualityLevel)) { |
---|
176 | return requiredEqualityLevel; |
---|
177 | } else { |
---|
178 | return TaskEquality.UNEQUAL; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | /* |
---|
183 | * (non-Javadoc) |
---|
184 | * |
---|
185 | * @see TaskComparisonRule#compare(ITask, ITask) |
---|
186 | */ |
---|
187 | @Override |
---|
188 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
189 | return getEquality(task1, task2, null); |
---|
190 | } |
---|
191 | |
---|
192 | /* |
---|
193 | * (non-Javadoc) |
---|
194 | * |
---|
195 | * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) |
---|
196 | */ |
---|
197 | @Override |
---|
198 | public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { |
---|
199 | return getEquality(instance1, instance2, null); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * <p> |
---|
204 | * compares two tasks with each other checking for the provided required |
---|
205 | * level of equality. One of the tasks must be a selection, the other one |
---|
206 | * not. If this is not the case, the method returns null. The returned |
---|
207 | * equality level is at most lexical equality as the selection can not be |
---|
208 | * identical to something not being a selection. |
---|
209 | * </p> |
---|
210 | * |
---|
211 | * @param task1 |
---|
212 | * the first task to be compared |
---|
213 | * @param task2 |
---|
214 | * the second task to be compared |
---|
215 | * @param requiredEqualityLevel |
---|
216 | * the equality level to be checked for |
---|
217 | * |
---|
218 | * @return the determined equality. |
---|
219 | */ |
---|
220 | private TaskEquality getEquality(ITask task1, ITask task2, |
---|
221 | TaskEquality requiredEqualityLevel) { |
---|
222 | ISelection selection = null; |
---|
223 | ITask task = null; |
---|
224 | |
---|
225 | if (task1 instanceof ISelection) { |
---|
226 | if (task2 instanceof ISelection) { |
---|
227 | // the rule is not responsible for two selections |
---|
228 | return null; |
---|
229 | } |
---|
230 | |
---|
231 | selection = (ISelection) task1; |
---|
232 | task = task2; |
---|
233 | } else if (task2 instanceof ISelection) { |
---|
234 | if (task1 instanceof ISelection) { |
---|
235 | // the rule is not responsible for two selections |
---|
236 | return null; |
---|
237 | } |
---|
238 | |
---|
239 | selection = (ISelection) task2; |
---|
240 | task = task1; |
---|
241 | } else { |
---|
242 | return null; |
---|
243 | } |
---|
244 | |
---|
245 | // now, that we found the selection and the task, lets compare the |
---|
246 | // children of the selection |
---|
247 | // with the task. |
---|
248 | final List<ITask> children = selection.getChildren(); |
---|
249 | |
---|
250 | if (children.size() < 1) { |
---|
251 | return null; |
---|
252 | } |
---|
253 | |
---|
254 | TaskEquality mostConcreteNodeEquality = null; |
---|
255 | |
---|
256 | for (final ITask child : children) { |
---|
257 | final TaskEquality taskEquality = callRuleManager(child, task, |
---|
258 | requiredEqualityLevel); |
---|
259 | |
---|
260 | if (taskEquality != TaskEquality.UNEQUAL) { |
---|
261 | if (mostConcreteNodeEquality == null) { |
---|
262 | mostConcreteNodeEquality = taskEquality; |
---|
263 | } else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) { |
---|
264 | mostConcreteNodeEquality = taskEquality; |
---|
265 | |
---|
266 | } |
---|
267 | |
---|
268 | if ((requiredEqualityLevel != null) |
---|
269 | && (mostConcreteNodeEquality |
---|
270 | .isAtLeast(requiredEqualityLevel))) { |
---|
271 | // if we found one child of the selection that is as equal |
---|
272 | // as required, then |
---|
273 | // we can consider the selection to be sufficiently equal to |
---|
274 | // the other task. |
---|
275 | // So we break up checking further children. |
---|
276 | break; |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | // although the subtask may be identical to the task, we can not return |
---|
282 | // identical, as |
---|
283 | // the selection is not identical to the task, but at most lexically |
---|
284 | // equal |
---|
285 | if (mostConcreteNodeEquality == TaskEquality.IDENTICAL) { |
---|
286 | return TaskEquality.LEXICALLY_EQUAL; |
---|
287 | } else { |
---|
288 | return mostConcreteNodeEquality; |
---|
289 | } |
---|
290 | |
---|
291 | } |
---|
292 | |
---|
293 | /** |
---|
294 | * <p> |
---|
295 | * compares two task instances with each other checking for the provided |
---|
296 | * required level of equality. One of the task instances must be a |
---|
297 | * selection, the other one not. If this is not the case, the method returns |
---|
298 | * null. The returned equality level is at most lexical equality as the |
---|
299 | * selection can not be identical to something not being a selection. |
---|
300 | * </p> |
---|
301 | * |
---|
302 | * @param taskInstance1 |
---|
303 | * the first task instance to be compared |
---|
304 | * @param taskInstance2 |
---|
305 | * the second task instance to be compared |
---|
306 | * @param requiredEqualityLevel |
---|
307 | * the equality level to be checked for |
---|
308 | * |
---|
309 | * @return the determined equality. |
---|
310 | */ |
---|
311 | private TaskEquality getEquality(ITaskInstance taskInstance1, |
---|
312 | ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) { |
---|
313 | ISelectionInstance selection = null; |
---|
314 | ITaskInstance task = null; |
---|
315 | |
---|
316 | if (taskInstance1 instanceof ISelectionInstance) { |
---|
317 | if (taskInstance2 instanceof ISelectionInstance) { |
---|
318 | // the rule is not responsible for two selections |
---|
319 | return null; |
---|
320 | } |
---|
321 | |
---|
322 | selection = (ISelectionInstance) taskInstance1; |
---|
323 | task = taskInstance2; |
---|
324 | } else if (taskInstance2 instanceof ISelectionInstance) { |
---|
325 | if (taskInstance1 instanceof ISelectionInstance) { |
---|
326 | // the rule is not responsible for two selections |
---|
327 | return null; |
---|
328 | } |
---|
329 | |
---|
330 | selection = (ISelectionInstance) taskInstance2; |
---|
331 | task = taskInstance1; |
---|
332 | } else { |
---|
333 | return null; |
---|
334 | } |
---|
335 | |
---|
336 | // now, that we found the selection and the task, lets compare the child |
---|
337 | // of the selection |
---|
338 | // with the task. |
---|
339 | final ITaskInstance child = selection.getChild(); |
---|
340 | |
---|
341 | if (child == null) { |
---|
342 | return null; |
---|
343 | } |
---|
344 | |
---|
345 | final TaskEquality taskEquality = callRuleManager(child, task, |
---|
346 | requiredEqualityLevel); |
---|
347 | |
---|
348 | // although the subtask may be identical to the task, we can not return |
---|
349 | // identical, as |
---|
350 | // the selection is not identical to the task, but at most lexically |
---|
351 | // equal |
---|
352 | if (taskEquality == TaskEquality.IDENTICAL) { |
---|
353 | return TaskEquality.LEXICALLY_EQUAL; |
---|
354 | } else { |
---|
355 | return taskEquality; |
---|
356 | } |
---|
357 | |
---|
358 | } |
---|
359 | |
---|
360 | /* |
---|
361 | * (non-Javadoc) |
---|
362 | * |
---|
363 | * @see TaskComparisonRule#isApplicable(ITask, ITask) |
---|
364 | */ |
---|
365 | @Override |
---|
366 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
367 | return ((task1 instanceof ISelection) && (!(task2 instanceof ISelection))) |
---|
368 | || ((task2 instanceof ISelection) && (!(task1 instanceof ISelection))); |
---|
369 | } |
---|
370 | |
---|
371 | /* |
---|
372 | * (non-Javadoc) |
---|
373 | * |
---|
374 | * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) |
---|
375 | */ |
---|
376 | @Override |
---|
377 | public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { |
---|
378 | return isApplicable(instance1.getTask(), instance2.getTask()); |
---|
379 | } |
---|
380 | } |
---|