[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; |
---|
[439] | 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; |
---|
[439] | 23 | |
---|
| 24 | /** |
---|
[557] | 25 | * <p> |
---|
[1146] | 26 | * this task comparison rule is capable of comparing selections. If both selections do not have |
---|
| 27 | * children, they are treated as lexically equal. If they have children, each child of both |
---|
| 28 | * selections is compared to each child of the respective other selection. The resulting equality |
---|
| 29 | * is the most concrete one of all these comparisons. I.e. if all children are at least lexically |
---|
| 30 | * equal, then the selections are lexically equal. If all children are at least syntactically |
---|
| 31 | * equal, then the selections are syntactically equal. If all children are at least semantically |
---|
| 32 | * equal, then the selections are semantically equal. If only one of the selections has children, |
---|
[1154] | 33 | * then the selections are unequal. The comparison is broken up, if only a specific equality is |
---|
| 34 | * checked for and this equality is ensured. |
---|
[557] | 35 | * </p> |
---|
[439] | 36 | * |
---|
| 37 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 38 | * @author 2012, last modified by $Author: patrick$ |
---|
| 39 | */ |
---|
[1146] | 40 | public class SelectionComparisonRule implements TaskComparisonRule { |
---|
[439] | 41 | |
---|
[1125] | 42 | /* (non-Javadoc) |
---|
[1294] | 43 | * @see TaskComparisonRule#isApplicable(ITask, ITask) |
---|
[557] | 44 | */ |
---|
| 45 | @Override |
---|
[1146] | 46 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 47 | return (task1 instanceof ISelection) && (task2 instanceof ISelection); |
---|
[1125] | 48 | } |
---|
| 49 | |
---|
| 50 | /* (non-Javadoc) |
---|
[1294] | 51 | * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 52 | */ |
---|
| 53 | @Override |
---|
[1146] | 54 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 55 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 56 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
[1125] | 57 | } |
---|
| 58 | |
---|
| 59 | /* (non-Javadoc) |
---|
[1294] | 60 | * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 61 | */ |
---|
| 62 | @Override |
---|
[1146] | 63 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 64 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 65 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
[1125] | 66 | } |
---|
| 67 | |
---|
| 68 | /* (non-Javadoc) |
---|
[1294] | 69 | * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 70 | */ |
---|
| 71 | @Override |
---|
[1146] | 72 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 73 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 74 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
[1125] | 75 | } |
---|
| 76 | |
---|
| 77 | /* (non-Javadoc) |
---|
[1294] | 78 | * @see TaskComparisonRule#compare(ITask, ITask) |
---|
[1125] | 79 | */ |
---|
| 80 | @Override |
---|
[1146] | 81 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 82 | return getEquality(task1, task2, null); |
---|
[1125] | 83 | } |
---|
[557] | 84 | |
---|
[1294] | 85 | /* (non-Javadoc) |
---|
| 86 | * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance) |
---|
| 87 | */ |
---|
| 88 | @Override |
---|
| 89 | public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 90 | return isApplicable(instance1.getTask(), instance2.getTask()); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /* (non-Javadoc) |
---|
| 94 | * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance) |
---|
| 95 | */ |
---|
| 96 | @Override |
---|
| 97 | public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 98 | TaskEquality equality = getEquality(instance1, instance2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 99 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /* (non-Javadoc) |
---|
| 103 | * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance) |
---|
| 104 | */ |
---|
| 105 | @Override |
---|
| 106 | public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 107 | TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 108 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /* (non-Javadoc) |
---|
| 112 | * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance) |
---|
| 113 | */ |
---|
| 114 | @Override |
---|
| 115 | public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 116 | TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 117 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /* (non-Javadoc) |
---|
| 121 | * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance) |
---|
| 122 | */ |
---|
| 123 | @Override |
---|
| 124 | public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 125 | return getEquality(instance1, instance2, null); |
---|
| 126 | } |
---|
| 127 | |
---|
[1125] | 128 | /** |
---|
[1154] | 129 | * <p> |
---|
| 130 | * compares two selections with each other checking for the provided required level of |
---|
| 131 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
| 132 | * the required equality level, the more checks this method performs. |
---|
| 133 | * </p> |
---|
[1125] | 134 | * |
---|
[1154] | 135 | * @param task1 the first task to be compared |
---|
| 136 | * @param task2 the second task to be compared |
---|
| 137 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 138 | * |
---|
| 139 | * @return the determined equality. |
---|
[1125] | 140 | */ |
---|
[1146] | 141 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
| 142 | List<ITask> children1 = ((ISelection) task1).getChildren(); |
---|
| 143 | List<ITask> children2 = ((ISelection) task2).getChildren(); |
---|
[807] | 144 | |
---|
[1125] | 145 | // if both selections do not have children, they are lexically equal. If only one of them |
---|
| 146 | // has children, they are unequal. |
---|
| 147 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
[1146] | 148 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[439] | 149 | } |
---|
[1125] | 150 | else if ((children1.size() == 0) || (children2.size() == 0)) { |
---|
[1146] | 151 | return TaskEquality.UNEQUAL; |
---|
[807] | 152 | } |
---|
[557] | 153 | |
---|
[1125] | 154 | if (requiredEqualityLevel == null) { |
---|
| 155 | // calculate the common equality level for all children of both selections. |
---|
| 156 | // do it in both directions to ensure commutative comparison |
---|
[1294] | 157 | return getMostConcreteEqualityLevel(children1, children2); |
---|
[1125] | 158 | } |
---|
| 159 | else { |
---|
| 160 | // we are searching for a specific equality |
---|
[1294] | 161 | if (checkEqualityLevel(children1, children2, requiredEqualityLevel)) { |
---|
[1125] | 162 | return requiredEqualityLevel; |
---|
| 163 | } |
---|
| 164 | else { |
---|
[1146] | 165 | return TaskEquality.UNEQUAL; |
---|
[1125] | 166 | } |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
| 171 | * <p> |
---|
[1294] | 172 | * determines the most concrete equality level for all tasks in the first list compared to all |
---|
| 173 | * tasks in the second list. It is sufficient, if there is one task in one list for which there |
---|
| 174 | * exist an equal task in the other list. |
---|
[1125] | 175 | * </p> |
---|
| 176 | * |
---|
[1154] | 177 | * @param children1 the first list to be compared |
---|
| 178 | * @param children2 the second list to be compared |
---|
| 179 | * |
---|
[1294] | 180 | * @return the most concrete task equality identified for all tasks in the first list with |
---|
| 181 | * respect to the second list |
---|
[1125] | 182 | */ |
---|
[1294] | 183 | private TaskEquality getMostConcreteEqualityLevel(List<ITask> children1, List<ITask> children2) |
---|
| 184 | { |
---|
[1146] | 185 | TaskEquality childEquality; |
---|
| 186 | TaskEquality currentEquality; |
---|
| 187 | for (ITask child1 : children1) { |
---|
[807] | 188 | childEquality = null; |
---|
[1146] | 189 | for (ITask child2 : children2) { |
---|
[1125] | 190 | currentEquality = callRuleManager(child1, child2, null); |
---|
[1146] | 191 | if ((currentEquality != null) && (currentEquality != TaskEquality.UNEQUAL)) { |
---|
[807] | 192 | if (childEquality == null) { |
---|
| 193 | childEquality = currentEquality; |
---|
| 194 | } |
---|
| 195 | else { |
---|
| 196 | childEquality = childEquality.getCommonDenominator(currentEquality); |
---|
| 197 | } |
---|
[1125] | 198 | |
---|
[1294] | 199 | if (childEquality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)) { |
---|
| 200 | // as we calculate the most concrete equality, we can break up here |
---|
| 201 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[1125] | 202 | } |
---|
[807] | 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
[557] | 206 | |
---|
[1294] | 207 | // as the comparison should be commutative, we do not need to check, if in list 2 there is |
---|
| 208 | // a child equal to one in list 1 |
---|
| 209 | return TaskEquality.UNEQUAL; |
---|
[1125] | 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * <p> |
---|
[1294] | 214 | * ensures for the two given lists, that for at least one task in the first list there is a task |
---|
[1154] | 215 | * in the second list being on the given level equal to the task in the first list. |
---|
| 216 | * </p> |
---|
| 217 | * |
---|
| 218 | * @param children1 the first list to be compared |
---|
| 219 | * @param children2 the second list to be compared |
---|
| 220 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 221 | * |
---|
[1294] | 222 | * @return true if there is a task in the first list that has an equal task in the second list |
---|
| 223 | * when considering the given equality level, false else. |
---|
[1125] | 224 | */ |
---|
[1146] | 225 | private boolean checkEqualityLevel(List<ITask> children1, |
---|
| 226 | List<ITask> children2, |
---|
| 227 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 228 | { |
---|
[1146] | 229 | TaskEquality currentEquality; |
---|
| 230 | for (ITask child1 : children1) { |
---|
| 231 | for (ITask child2 : children2) { |
---|
[1125] | 232 | currentEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
| 233 | if ((currentEquality != null) && (currentEquality.isAtLeast(requiredEqualityLevel))) |
---|
| 234 | { |
---|
| 235 | // we found at least one equal child with sufficient equality in the |
---|
| 236 | // second list. So be can break up for this child. |
---|
[1294] | 237 | return true; |
---|
[557] | 238 | } |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
[1294] | 242 | return false; |
---|
[439] | 243 | } |
---|
[807] | 244 | |
---|
[1125] | 245 | /** |
---|
| 246 | * <p> |
---|
[1154] | 247 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 248 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 249 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 250 | * on that level. |
---|
| 251 | * </p> |
---|
| 252 | * |
---|
| 253 | * @param child1 the first task to be compared |
---|
| 254 | * @param child2 the second task to be compared |
---|
| 255 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 256 | * |
---|
| 257 | * @return the determined equality |
---|
[1125] | 258 | */ |
---|
[1146] | 259 | private TaskEquality callRuleManager(ITask child1, |
---|
| 260 | ITask child2, |
---|
| 261 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 262 | { |
---|
| 263 | if (requiredEqualityLevel == null) { |
---|
[1190] | 264 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
[1125] | 265 | } |
---|
[1190] | 266 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 267 | (child1, child2, requiredEqualityLevel)) |
---|
| 268 | { |
---|
[1125] | 269 | return requiredEqualityLevel; |
---|
| 270 | } |
---|
| 271 | else { |
---|
[1146] | 272 | return TaskEquality.UNEQUAL; |
---|
[1125] | 273 | } |
---|
| 274 | } |
---|
| 275 | |
---|
[1294] | 276 | /** |
---|
| 277 | * <p> |
---|
| 278 | * compares two selection instances with each other checking for the provided required level of |
---|
| 279 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
| 280 | * the required equality level, the more checks this method performs. |
---|
| 281 | * </p> |
---|
| 282 | * |
---|
| 283 | * @param taskInstance1 the first task instance to be compared |
---|
| 284 | * @param taskInstance2 the second task instance to be compared |
---|
| 285 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 286 | * |
---|
| 287 | * @return the determined equality. |
---|
| 288 | */ |
---|
| 289 | private TaskEquality getEquality(ITaskInstance taskInstance1, |
---|
| 290 | ITaskInstance taskInstance2, |
---|
| 291 | TaskEquality requiredEqualityLevel) |
---|
| 292 | { |
---|
| 293 | ITaskInstance child1 = ((ISelectionInstance) taskInstance1).getChild(); |
---|
| 294 | ITaskInstance child2 = ((ISelectionInstance) taskInstance2).getChild(); |
---|
| 295 | |
---|
| 296 | // if both selections do not have children, they are lexically equal. If only one of them |
---|
| 297 | // has children, they are unequal. |
---|
| 298 | if ((child1 == null) && (child2 == null)) { |
---|
| 299 | return TaskEquality.LEXICALLY_EQUAL; |
---|
| 300 | } |
---|
| 301 | else if ((child1 == null) || (child2 == null)) { |
---|
| 302 | return TaskEquality.UNEQUAL; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | TaskEquality equality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
| 306 | |
---|
| 307 | if (equality == TaskEquality.IDENTICAL) { |
---|
| 308 | // two different selection instances can be at most lexically equal even if their |
---|
| 309 | // children are identical |
---|
| 310 | return TaskEquality.LEXICALLY_EQUAL; |
---|
| 311 | } |
---|
| 312 | else { |
---|
| 313 | return equality; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | /** |
---|
| 318 | * <p> |
---|
| 319 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 320 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 321 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 322 | * on that level. |
---|
| 323 | * </p> |
---|
| 324 | * |
---|
| 325 | * @param taskInstance1 the first task instance to be compared |
---|
| 326 | * @param taskInstance2 the second task instance to be compared |
---|
| 327 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 328 | * |
---|
| 329 | * @return the determined equality |
---|
| 330 | */ |
---|
| 331 | private TaskEquality callRuleManager(ITaskInstance taskInstance1, |
---|
| 332 | ITaskInstance taskInstance2, |
---|
| 333 | TaskEquality requiredEqualityLevel) |
---|
| 334 | { |
---|
| 335 | if (requiredEqualityLevel == null) { |
---|
| 336 | return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2); |
---|
| 337 | } |
---|
| 338 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 339 | (taskInstance1, taskInstance2, requiredEqualityLevel)) |
---|
| 340 | { |
---|
| 341 | return requiredEqualityLevel; |
---|
| 342 | } |
---|
| 343 | else { |
---|
| 344 | return TaskEquality.UNEQUAL; |
---|
| 345 | } |
---|
| 346 | } |
---|
[439] | 347 | } |
---|