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