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