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.ArrayList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
22 | |
---|
23 | /** |
---|
24 | * <p> |
---|
25 | * The task equality rule manager is capable of comparing tasks and task |
---|
26 | * instances based on its internal list of comparison rules. These rules are |
---|
27 | * asked for comparing the two provided tasks or task instance. If a rule |
---|
28 | * returns a task equality other than null, this equality is returned. Otherwise |
---|
29 | * the next rule is asked. |
---|
30 | * </p> |
---|
31 | * |
---|
32 | * @author Patrick Harms |
---|
33 | */ |
---|
34 | public class TaskEqualityRuleManager { |
---|
35 | |
---|
36 | /** |
---|
37 | * <p> |
---|
38 | * returns the singleton instance of this class |
---|
39 | * </p> |
---|
40 | * |
---|
41 | * @return as described |
---|
42 | */ |
---|
43 | public static TaskEqualityRuleManager getInstance() { |
---|
44 | return instance; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * <p> |
---|
49 | * the singleton instance of this class |
---|
50 | * </p> |
---|
51 | */ |
---|
52 | private static final TaskEqualityRuleManager instance = new TaskEqualityRuleManager(); |
---|
53 | |
---|
54 | /** |
---|
55 | * <p> |
---|
56 | * the rules that can be used for comparing tasks |
---|
57 | * </p> |
---|
58 | */ |
---|
59 | private List<TaskComparisonRule> mRuleIndex = null; |
---|
60 | |
---|
61 | /** |
---|
62 | * <p> |
---|
63 | * initializes the task equality rule manager by filling the internal list |
---|
64 | * of comparison rules. |
---|
65 | * </p> |
---|
66 | */ |
---|
67 | private TaskEqualityRuleManager() { |
---|
68 | mRuleIndex = new ArrayList<TaskComparisonRule>(); |
---|
69 | mRuleIndex.add(new TaskIdentityRule()); |
---|
70 | mRuleIndex.add(new GUIEventTaskComparisonRule()); |
---|
71 | mRuleIndex.add(new EventTaskComparisonRule()); |
---|
72 | mRuleIndex.add(new IterationComparisonRule()); |
---|
73 | mRuleIndex.add(new SequenceComparisonRule()); |
---|
74 | mRuleIndex.add(new SelectionComparisonRule()); |
---|
75 | mRuleIndex.add(new TaskAndIterationComparisonRule()); |
---|
76 | mRuleIndex.add(new TaskAndSelectionComparisonRule()); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * <p> |
---|
81 | * this method two tasks with respect to the fiven equality level and |
---|
82 | * returns true, if this level is given. |
---|
83 | * </p> |
---|
84 | * |
---|
85 | * @param task1 |
---|
86 | * the first task to be compared |
---|
87 | * @param task2 |
---|
88 | * the second task to be compared |
---|
89 | * @param equalityLevel |
---|
90 | * the level of equality to be checked for |
---|
91 | * |
---|
92 | * @return as described |
---|
93 | * |
---|
94 | * @throws IllegalStateException |
---|
95 | * in the case, the {@link #init()} method was not called on the |
---|
96 | * manager before a call to this method. |
---|
97 | */ |
---|
98 | public boolean areAtLeastEqual(ITask task1, ITask task2, |
---|
99 | TaskEquality equalityLevel) { |
---|
100 | if (equalityLevel == null) { |
---|
101 | throw new IllegalArgumentException( |
---|
102 | "required equality level must not be null"); |
---|
103 | } |
---|
104 | |
---|
105 | switch (equalityLevel) { |
---|
106 | case IDENTICAL: |
---|
107 | return areIdentical(task1, task2); |
---|
108 | case LEXICALLY_EQUAL: |
---|
109 | return areLexicallyEqual(task1, task2); |
---|
110 | case SYNTACTICALLY_EQUAL: |
---|
111 | return areSyntacticallyEqual(task1, task2); |
---|
112 | case SEMANTICALLY_EQUAL: |
---|
113 | return areSemanticallyEqual(task1, task2); |
---|
114 | case UNEQUAL: |
---|
115 | return !areSemanticallyEqual(task1, task2); |
---|
116 | default: |
---|
117 | throw new IllegalArgumentException("unknown required equality: " |
---|
118 | + equalityLevel); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * <p> |
---|
124 | * this method compares two task instances with respect to the given |
---|
125 | * equality level and returns true, if this level is given. |
---|
126 | * </p> |
---|
127 | * |
---|
128 | * @param instance1 |
---|
129 | * the first task instance to be compared |
---|
130 | * @param instance2 |
---|
131 | * the second task instance to be compared |
---|
132 | * @param equalityLevel |
---|
133 | * the level of equality to be checked for |
---|
134 | * |
---|
135 | * @return as described |
---|
136 | * |
---|
137 | * @throws IllegalStateException |
---|
138 | * in the case, the {@link #init()} method was not called on the |
---|
139 | * manager before a call to this method. |
---|
140 | */ |
---|
141 | public boolean areAtLeastEqual(ITaskInstance instance1, |
---|
142 | ITaskInstance instance2, TaskEquality equalityLevel) { |
---|
143 | if (equalityLevel == null) { |
---|
144 | throw new IllegalArgumentException( |
---|
145 | "required equality level must not be null"); |
---|
146 | } |
---|
147 | |
---|
148 | switch (equalityLevel) { |
---|
149 | case IDENTICAL: |
---|
150 | return areIdentical(instance1, instance2); |
---|
151 | case LEXICALLY_EQUAL: |
---|
152 | return areLexicallyEqual(instance1, instance2); |
---|
153 | case SYNTACTICALLY_EQUAL: |
---|
154 | return areSyntacticallyEqual(instance1, instance2); |
---|
155 | case SEMANTICALLY_EQUAL: |
---|
156 | return areSemanticallyEqual(instance1, instance2); |
---|
157 | case UNEQUAL: |
---|
158 | return !areSemanticallyEqual(instance1, instance2); |
---|
159 | default: |
---|
160 | throw new IllegalArgumentException("unknown required equality: " |
---|
161 | + equalityLevel); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * <p> |
---|
167 | * this method checks if the two given tasks are identical. For this, it |
---|
168 | * iterates its internal comparison rules. If the first rule returns true, |
---|
169 | * than this method returns true as well. If no rule returns true, this |
---|
170 | * method returns false. |
---|
171 | * </p> |
---|
172 | * |
---|
173 | * @param task1 |
---|
174 | * the first task to be compared |
---|
175 | * @param task2 |
---|
176 | * the second task to be compared |
---|
177 | * |
---|
178 | * @return as described |
---|
179 | * |
---|
180 | * @throws IllegalStateException |
---|
181 | * in the case, the {@link #init()} method was not called on the |
---|
182 | * manager before a call to this method. |
---|
183 | */ |
---|
184 | public boolean areIdentical(ITask task1, ITask task2) { |
---|
185 | if (mRuleIndex == null) { |
---|
186 | throw new IllegalStateException("not initialized"); |
---|
187 | } |
---|
188 | |
---|
189 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
190 | if (rule.isApplicable(task1, task2) |
---|
191 | && rule.areLexicallyEqual(task1, task2)) { |
---|
192 | return true; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | return false; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * <p> |
---|
201 | * this method checks if the two given task instances are identical. For |
---|
202 | * this, it iterates its internal comparison rules. If the first rule |
---|
203 | * returns true, than this method returns true as well. If no rule returns |
---|
204 | * true, this method returns false. |
---|
205 | * </p> |
---|
206 | * |
---|
207 | * @param instance1 |
---|
208 | * the first task instance to be compared |
---|
209 | * @param instance2 |
---|
210 | * the second task instance to be compared |
---|
211 | * |
---|
212 | * @return as described |
---|
213 | * |
---|
214 | * @throws IllegalStateException |
---|
215 | * in the case, the {@link #init()} method was not called on the |
---|
216 | * manager before a call to this method. |
---|
217 | */ |
---|
218 | public boolean areIdentical(ITaskInstance instance1, ITaskInstance instance2) { |
---|
219 | if (mRuleIndex == null) { |
---|
220 | throw new IllegalStateException("not initialized"); |
---|
221 | } |
---|
222 | |
---|
223 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
224 | if (rule.isApplicable(instance1, instance2) |
---|
225 | && rule.areLexicallyEqual(instance1, instance2)) { |
---|
226 | return true; |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * <p> |
---|
235 | * this method checks if the two given tasks are lexically equal. For this, |
---|
236 | * it iterates its internal comparison rules. If the first rule returns |
---|
237 | * true, than this method returns true as well. If no rule returns true, |
---|
238 | * this method returns false. |
---|
239 | * </p> |
---|
240 | * |
---|
241 | * @param task1 |
---|
242 | * the first task to be compared |
---|
243 | * @param task2 |
---|
244 | * the second task to be compared |
---|
245 | * |
---|
246 | * @return as described |
---|
247 | * |
---|
248 | * @throws IllegalStateException |
---|
249 | * in the case, the {@link #init()} method was not called on the |
---|
250 | * manager before a call to this method. |
---|
251 | */ |
---|
252 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
253 | if (mRuleIndex == null) { |
---|
254 | throw new IllegalStateException("not initialized"); |
---|
255 | } |
---|
256 | |
---|
257 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
258 | if (rule.isApplicable(task1, task2) |
---|
259 | && rule.areLexicallyEqual(task1, task2)) { |
---|
260 | return true; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | return false; |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * <p> |
---|
269 | * this method checks if the two given task instances are lexically equal. |
---|
270 | * For this, it iterates its internal comparison rules. If the first rule |
---|
271 | * returns true, than this method returns true, as well. If no rule returns |
---|
272 | * true, this method returns false. |
---|
273 | * </p> |
---|
274 | * |
---|
275 | * @param instance1 |
---|
276 | * the first task instance to be compared |
---|
277 | * @param instance2 |
---|
278 | * the second task instance to be compared |
---|
279 | * |
---|
280 | * @return as described |
---|
281 | * |
---|
282 | * @throws IllegalStateException |
---|
283 | * in the case, the {@link #init()} method was not called on the |
---|
284 | * manager before a call to this method. |
---|
285 | */ |
---|
286 | public boolean areLexicallyEqual(ITaskInstance instance1, |
---|
287 | ITaskInstance instance2) { |
---|
288 | if (mRuleIndex == null) { |
---|
289 | throw new IllegalStateException("not initialized"); |
---|
290 | } |
---|
291 | |
---|
292 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
293 | if (rule.isApplicable(instance1, instance2) |
---|
294 | && rule.areLexicallyEqual(instance1, instance2)) { |
---|
295 | return true; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | return false; |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * <p> |
---|
304 | * this method checks if the two given tasks are semantically equal. For |
---|
305 | * this, it iterates its internal comparison rules. If the first rule |
---|
306 | * returns true, than this method returns true as well. If no rule returns |
---|
307 | * true, this method returns false. |
---|
308 | * </p> |
---|
309 | * |
---|
310 | * @param task1 |
---|
311 | * the first task to be compared |
---|
312 | * @param task2 |
---|
313 | * the second task to be compared |
---|
314 | * |
---|
315 | * @return as described |
---|
316 | * |
---|
317 | * @throws IllegalStateException |
---|
318 | * in the case, the {@link #init()} method was not called on the |
---|
319 | * manager before a call to this method. |
---|
320 | */ |
---|
321 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
322 | if (mRuleIndex == null) { |
---|
323 | throw new IllegalStateException("not initialized"); |
---|
324 | } |
---|
325 | |
---|
326 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
327 | if (rule.isApplicable(task1, task2) |
---|
328 | && rule.areSemanticallyEqual(task1, task2)) { |
---|
329 | return true; |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | return false; |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | * <p> |
---|
338 | * this method checks if the two given task instances are semantically |
---|
339 | * equal. For this, it iterates its internal comparison rules. If the first |
---|
340 | * rule returns true, than this method returns true, as well. If no rule |
---|
341 | * returns true, this method returns false. |
---|
342 | * </p> |
---|
343 | * |
---|
344 | * @param instance1 |
---|
345 | * the first task instance to be compared |
---|
346 | * @param instance2 |
---|
347 | * the second task instance to be compared |
---|
348 | * |
---|
349 | * @return as described |
---|
350 | * |
---|
351 | * @throws IllegalStateException |
---|
352 | * in the case, the {@link #init()} method was not called on the |
---|
353 | * manager before a call to this method. |
---|
354 | */ |
---|
355 | public boolean areSemanticallyEqual(ITaskInstance instance1, |
---|
356 | ITaskInstance instance2) { |
---|
357 | if (mRuleIndex == null) { |
---|
358 | throw new IllegalStateException("not initialized"); |
---|
359 | } |
---|
360 | |
---|
361 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
362 | if (rule.isApplicable(instance1, instance2) |
---|
363 | && rule.areSemanticallyEqual(instance1, instance2)) { |
---|
364 | return true; |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | return false; |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * <p> |
---|
373 | * this method checks if the two given tasks are syntactically equal. For |
---|
374 | * this, it iterates its internal comparison rules. If the first rule |
---|
375 | * returns true, than this method returns true as well. If no rule returns |
---|
376 | * true, this method returns false. |
---|
377 | * </p> |
---|
378 | * |
---|
379 | * @param task1 |
---|
380 | * the first task to be compared |
---|
381 | * @param task2 |
---|
382 | * the second task to be compared |
---|
383 | * |
---|
384 | * @return as described |
---|
385 | * |
---|
386 | * @throws IllegalStateException |
---|
387 | * in the case, the {@link #init()} method was not called on the |
---|
388 | * manager before a call to this method. |
---|
389 | */ |
---|
390 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
391 | if (mRuleIndex == null) { |
---|
392 | throw new IllegalStateException("not initialized"); |
---|
393 | } |
---|
394 | |
---|
395 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
396 | if (rule.isApplicable(task1, task2) |
---|
397 | && rule.areSyntacticallyEqual(task1, task2)) { |
---|
398 | return true; |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | return false; |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | * <p> |
---|
407 | * this method checks if the two given task instances are syntactically |
---|
408 | * equal. For this, it iterates its internal comparison rules. If the first |
---|
409 | * rule returns true, than this method returns true, as well. If no rule |
---|
410 | * returns true, this method returns false. |
---|
411 | * </p> |
---|
412 | * |
---|
413 | * @param instance1 |
---|
414 | * the first task instance to be compared |
---|
415 | * @param instance2 |
---|
416 | * the second task instance to be compared |
---|
417 | * |
---|
418 | * @return as described |
---|
419 | * |
---|
420 | * @throws IllegalStateException |
---|
421 | * in the case, the {@link #init()} method was not called on the |
---|
422 | * manager before a call to this method. |
---|
423 | */ |
---|
424 | public boolean areSyntacticallyEqual(ITaskInstance instance1, |
---|
425 | ITaskInstance instance2) { |
---|
426 | if (mRuleIndex == null) { |
---|
427 | throw new IllegalStateException("not initialized"); |
---|
428 | } |
---|
429 | |
---|
430 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
431 | if (rule.isApplicable(instance1, instance2) |
---|
432 | && rule.areSyntacticallyEqual(instance1, instance2)) { |
---|
433 | return true; |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | return false; |
---|
438 | } |
---|
439 | |
---|
440 | /** |
---|
441 | * <p> |
---|
442 | * this method performs a comparison of the two provided tasks. It iterates |
---|
443 | * its internal comparison rules. If the first rule returns a task equality |
---|
444 | * other than null, this equality is returned. Otherwise the next rule is |
---|
445 | * tried. If no rule returns an equality <code>NodeEquality.UNEQUAL</code> |
---|
446 | * is returned. |
---|
447 | * </p> |
---|
448 | * |
---|
449 | * @param task1 |
---|
450 | * the first task to be compared |
---|
451 | * @param task2 |
---|
452 | * the second task to be compared |
---|
453 | * |
---|
454 | * @return as described |
---|
455 | * |
---|
456 | * @throws IllegalStateException |
---|
457 | * in the case, the {@link #init()} method was not called on the |
---|
458 | * manager before a call to this method. |
---|
459 | */ |
---|
460 | public TaskEquality compare(ITask task1, ITask task2) |
---|
461 | throws IllegalStateException { |
---|
462 | if (mRuleIndex == null) { |
---|
463 | throw new IllegalStateException("not initialized"); |
---|
464 | } |
---|
465 | |
---|
466 | // LOG.info("checking for equality of " + task1 + " and " + task2); |
---|
467 | TaskEquality taskEquality = null; |
---|
468 | |
---|
469 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
470 | if (rule.isApplicable(task1, task2)) { |
---|
471 | taskEquality = rule.compare(task1, task2); |
---|
472 | if (taskEquality != null) { |
---|
473 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
474 | return taskEquality; |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | // LOG.warning("no rule could be applied --> handling tasks as unequal"); |
---|
480 | |
---|
481 | return TaskEquality.UNEQUAL; |
---|
482 | } |
---|
483 | |
---|
484 | /** |
---|
485 | * <p> |
---|
486 | * this method performs a comparison of the two provided task instances. It |
---|
487 | * iterates its internal comparison rules. If the first rule returns a task |
---|
488 | * instance equality other than null, this equality is returned. Otherwise |
---|
489 | * the next rule is tried. If no rule returns an equality |
---|
490 | * <code>TaskEquality.UNEQUAL</code> is returned. |
---|
491 | * </p> |
---|
492 | * |
---|
493 | * @param instance1 |
---|
494 | * the first task instance to be compared |
---|
495 | * @param instance2 |
---|
496 | * the second task instance to be compared |
---|
497 | * |
---|
498 | * @return as described |
---|
499 | * |
---|
500 | * @throws IllegalStateException |
---|
501 | * in the case, the {@link #init()} method was not called on the |
---|
502 | * manager before a call to this method. |
---|
503 | */ |
---|
504 | public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) |
---|
505 | throws IllegalStateException { |
---|
506 | if (mRuleIndex == null) { |
---|
507 | throw new IllegalStateException("not initialized"); |
---|
508 | } |
---|
509 | |
---|
510 | // LOG.info("checking for equality of " + instance1 + " and " + |
---|
511 | // instance2); |
---|
512 | TaskEquality instanceEquality = null; |
---|
513 | |
---|
514 | for (final TaskComparisonRule rule : mRuleIndex) { |
---|
515 | if (rule.isApplicable(instance1, instance2)) { |
---|
516 | instanceEquality = rule.compare(instance1, instance2); |
---|
517 | if (instanceEquality != null) { |
---|
518 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
519 | return instanceEquality; |
---|
520 | } |
---|
521 | } |
---|
522 | } |
---|
523 | |
---|
524 | // LOG.warning("no rule could be applied --> handling tasks as unequal"); |
---|
525 | |
---|
526 | return TaskEquality.UNEQUAL; |
---|
527 | } |
---|
528 | |
---|
529 | } |
---|