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