[1113] | 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 | |
---|
[1146] | 15 | package de.ugoe.cs.autoquest.tasktrees.taskequality; |
---|
[816] | 16 | |
---|
[1125] | 17 | import java.util.List; |
---|
| 18 | |
---|
[922] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1294] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1146] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1294] | 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[816] | 23 | |
---|
| 24 | /** |
---|
| 25 | * <p> |
---|
[1146] | 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 |
---|
[816] | 30 | * applies for syntactical and semantical equality. |
---|
| 31 | * </p> |
---|
| 32 | |
---|
| 33 | * @author Patrick Harms |
---|
| 34 | */ |
---|
[1146] | 35 | public class TaskAndSelectionComparisonRule implements TaskComparisonRule { |
---|
[816] | 36 | |
---|
[1125] | 37 | /* (non-Javadoc) |
---|
[1294] | 38 | * @see TaskComparisonRule#isApplicable(ITask, ITask) |
---|
[816] | 39 | */ |
---|
| 40 | @Override |
---|
[1146] | 41 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 42 | return ((task1 instanceof ISelection) && (!(task2 instanceof ISelection))) || |
---|
| 43 | ((task2 instanceof ISelection) && (!(task1 instanceof ISelection))); |
---|
[1125] | 44 | } |
---|
| 45 | |
---|
| 46 | /* (non-Javadoc) |
---|
[1294] | 47 | * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 48 | */ |
---|
| 49 | @Override |
---|
[1146] | 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)); |
---|
[1125] | 53 | } |
---|
| 54 | |
---|
| 55 | /* (non-Javadoc) |
---|
[1294] | 56 | * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 57 | */ |
---|
| 58 | @Override |
---|
[1146] | 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)); |
---|
[1125] | 62 | } |
---|
| 63 | |
---|
| 64 | /* (non-Javadoc) |
---|
[1294] | 65 | * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 66 | */ |
---|
| 67 | @Override |
---|
[1146] | 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)); |
---|
[1125] | 71 | } |
---|
| 72 | |
---|
| 73 | /* (non-Javadoc) |
---|
[1294] | 74 | * @see TaskComparisonRule#compare(ITask, ITask) |
---|
[1125] | 75 | */ |
---|
| 76 | @Override |
---|
[1146] | 77 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 78 | return getEquality(task1, task2, null); |
---|
[1125] | 79 | } |
---|
| 80 | |
---|
[1294] | 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 | |
---|
[1125] | 124 | /** |
---|
[1154] | 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> |
---|
[1125] | 131 | * |
---|
[1154] | 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. |
---|
[1125] | 137 | */ |
---|
[1146] | 138 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
[816] | 139 | ISelection selection = null; |
---|
[1146] | 140 | ITask task = null; |
---|
[816] | 141 | |
---|
[1146] | 142 | if (task1 instanceof ISelection) { |
---|
| 143 | if (task2 instanceof ISelection) { |
---|
[1125] | 144 | // the rule is not responsible for two selections |
---|
[816] | 145 | return null; |
---|
| 146 | } |
---|
| 147 | |
---|
[1146] | 148 | selection = (ISelection) task1; |
---|
| 149 | task = task2; |
---|
[816] | 150 | } |
---|
[1146] | 151 | else if (task2 instanceof ISelection) { |
---|
| 152 | if (task1 instanceof ISelection) { |
---|
[1125] | 153 | // the rule is not responsible for two selections |
---|
[816] | 154 | return null; |
---|
| 155 | } |
---|
| 156 | |
---|
[1146] | 157 | selection = (ISelection) task2; |
---|
| 158 | task = task1; |
---|
[816] | 159 | } |
---|
| 160 | else { |
---|
| 161 | return null; |
---|
| 162 | } |
---|
| 163 | |
---|
[1146] | 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(); |
---|
[1125] | 167 | |
---|
| 168 | if (children.size() < 1) { |
---|
[816] | 169 | return null; |
---|
| 170 | } |
---|
| 171 | |
---|
[1146] | 172 | TaskEquality mostConcreteNodeEquality = null; |
---|
[816] | 173 | |
---|
[1146] | 174 | for (ITask child : children) { |
---|
| 175 | TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel); |
---|
[816] | 176 | |
---|
[1146] | 177 | if (taskEquality != TaskEquality.UNEQUAL) { |
---|
[816] | 178 | if (mostConcreteNodeEquality == null) { |
---|
[1146] | 179 | mostConcreteNodeEquality = taskEquality; |
---|
[816] | 180 | } |
---|
[1146] | 181 | else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) { |
---|
| 182 | mostConcreteNodeEquality = taskEquality; |
---|
[1125] | 183 | |
---|
[816] | 184 | } |
---|
[1125] | 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 |
---|
[1146] | 190 | // we can consider the selection to be sufficiently equal to the other task. |
---|
[1125] | 191 | // So we break up checking further children. |
---|
| 192 | break; |
---|
| 193 | } |
---|
[816] | 194 | } |
---|
| 195 | } |
---|
| 196 | |
---|
[1146] | 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; |
---|
[816] | 201 | } |
---|
| 202 | else { |
---|
| 203 | return mostConcreteNodeEquality; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | } |
---|
[1125] | 207 | |
---|
| 208 | /** |
---|
| 209 | * <p> |
---|
[1154] | 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 |
---|
[1125] | 221 | */ |
---|
[1146] | 222 | private TaskEquality callRuleManager(ITask child1, |
---|
| 223 | ITask child2, |
---|
| 224 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 225 | { |
---|
| 226 | if (requiredEqualityLevel == null) { |
---|
[1190] | 227 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
[1125] | 228 | } |
---|
[1190] | 229 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 230 | (child1, child2, requiredEqualityLevel)) |
---|
| 231 | { |
---|
[1125] | 232 | return requiredEqualityLevel; |
---|
| 233 | } |
---|
| 234 | else { |
---|
[1146] | 235 | return TaskEquality.UNEQUAL; |
---|
[1125] | 236 | } |
---|
| 237 | } |
---|
[1294] | 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 | } |
---|
[816] | 333 | } |
---|