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