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