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