[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 de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
[922] | 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1125] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
[1146] | 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[439] | 22 | |
---|
| 23 | /** |
---|
[557] | 24 | * <p> |
---|
| 25 | * This class is capable of comparing Iterations. Iterations equal at distinct levels |
---|
| 26 | * in distinct situations. The following table shows the results of the comparison for the |
---|
| 27 | * specific situations (the parameters are commutative). In any other situation, the comparison |
---|
| 28 | * returns <code>NodeEquality.UNEQUAL</code>: |
---|
| 29 | * </p> |
---|
[439] | 30 | * |
---|
[557] | 31 | * <table border="1"> |
---|
| 32 | * <tr> |
---|
| 33 | * <th>iteration 1</th> |
---|
| 34 | * <th>iteration 2</th> |
---|
| 35 | * <th>comparison result</th> |
---|
| 36 | * </tr> |
---|
| 37 | * <tr> |
---|
| 38 | * <td>any iteration</td> |
---|
| 39 | * <td>any iteration with a child that is lexically equal to the child of iteration 1</td> |
---|
| 40 | * <td><code>NodeEquality.LEXICALLY_EQUAL</code></td> |
---|
| 41 | * </tr> |
---|
| 42 | * <tr> |
---|
| 43 | * <td>any iteration</td> |
---|
| 44 | * <td>any iteration with a child that is syntactically equal to the child of iteration 1</td> |
---|
| 45 | * <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> |
---|
| 46 | * </tr> |
---|
| 47 | * <tr> |
---|
| 48 | * <td>any iteration</td> |
---|
| 49 | * <td>any iteration with a child that is semantically equal to the child of iteration 1</td> |
---|
| 50 | * <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> |
---|
| 51 | * </tr> |
---|
| 52 | * <tr> |
---|
| 53 | * <td>an iteration with a selection of syntactically equal children</td> |
---|
| 54 | * <td>an iteration with a child that is syntactically equal to the children of the child |
---|
| 55 | * selection of iteration 1</td> |
---|
| 56 | * <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> |
---|
| 57 | * </tr> |
---|
| 58 | * <tr> |
---|
[807] | 59 | * <td>an iteration with a selection of syntactically equal children</td> |
---|
| 60 | * <td>an iteration with a selection of syntactically equal children that are all syntactically |
---|
| 61 | * equal to the selection of children of iteration 1</td> |
---|
| 62 | * <td><code>NodeEquality.SYNTACTICALLY_EQUAL</code></td> |
---|
| 63 | * </tr> |
---|
| 64 | * <tr> |
---|
[557] | 65 | * <td>an iteration with a selection of semantically equal children</td> |
---|
| 66 | * <td>an iteration with a child that is semantically equal to the children of the child |
---|
| 67 | * selection of iteration 1</td> |
---|
| 68 | * <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> |
---|
| 69 | * </tr> |
---|
[807] | 70 | * <tr> |
---|
| 71 | * <td>an iteration with a selection of semantically equal children</td> |
---|
| 72 | * <td>an iteration with a selection of semantically equal children that are all semantically |
---|
| 73 | * equal to the selection of children of iteration 1</td> |
---|
| 74 | * <td><code>NodeEquality.SEMANTICALLY_EQUAL</code></td> |
---|
| 75 | * </tr> |
---|
[557] | 76 | * </table> |
---|
| 77 | * |
---|
[439] | 78 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 79 | * @author 2012, last modified by $Author: patrick$ |
---|
| 80 | */ |
---|
[1146] | 81 | public class IterationComparisonRule implements TaskComparisonRule { |
---|
[557] | 82 | |
---|
[1146] | 83 | /** the rule manager for internally comparing tasks */ |
---|
| 84 | private TaskEqualityRuleManager mRuleManager; |
---|
[439] | 85 | |
---|
[557] | 86 | /** |
---|
| 87 | * <p> |
---|
[1146] | 88 | * simple constructor to provide the rule with the task equality rule manager to be able |
---|
| 89 | * to perform comparisons of the children of provided tasks |
---|
[557] | 90 | * </p> |
---|
| 91 | * |
---|
[1146] | 92 | * @param ruleManager the rule manager for comparing tasks |
---|
[557] | 93 | */ |
---|
[1146] | 94 | IterationComparisonRule(TaskEqualityRuleManager ruleManager) { |
---|
[557] | 95 | super(); |
---|
| 96 | mRuleManager = ruleManager; |
---|
| 97 | } |
---|
[439] | 98 | |
---|
[1125] | 99 | /* (non-Javadoc) |
---|
[1146] | 100 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[557] | 101 | */ |
---|
| 102 | @Override |
---|
[1146] | 103 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 104 | return (task1 instanceof IIteration) && (task2 instanceof IIteration); |
---|
[1125] | 105 | } |
---|
| 106 | |
---|
| 107 | /* (non-Javadoc) |
---|
[1146] | 108 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 109 | */ |
---|
| 110 | @Override |
---|
[1146] | 111 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 112 | ITask child1 = ((IIteration) task1).getMarkedTask(); |
---|
| 113 | ITask child2 = ((IIteration) task2).getMarkedTask(); |
---|
[1125] | 114 | |
---|
[1146] | 115 | if (child1 != null) { |
---|
| 116 | if (child2 == null) { |
---|
| 117 | return false; |
---|
[1125] | 118 | } |
---|
| 119 | else { |
---|
| 120 | // iterations may have 3 different structures. |
---|
| 121 | // 1. they have one child, which is the iterated one |
---|
| 122 | // 2. they have a sequence of children, which is iterated |
---|
| 123 | // 3. they have a selection of different iterated variants (usually the variants |
---|
| 124 | // are semantically equal) |
---|
| 125 | // check if the type of children match. If not, return false. If they match, |
---|
| 126 | // use the equality manager to perform further comparisons |
---|
| 127 | |
---|
| 128 | if (((child1 instanceof ISelection) && (child2 instanceof ISelection)) || |
---|
| 129 | ((child1 instanceof ISequence) && (child2 instanceof ISequence)) || |
---|
| 130 | ((child1 instanceof IEventTask) && (child2 instanceof IEventTask))) |
---|
| 131 | { |
---|
| 132 | return getNodeEquality |
---|
[1146] | 133 | (child1, child2).isAtLeast(TaskEquality.LEXICALLY_EQUAL); |
---|
[1125] | 134 | } |
---|
| 135 | } |
---|
[557] | 136 | } |
---|
[1146] | 137 | else if (child2 == null) { |
---|
| 138 | return true; |
---|
| 139 | } |
---|
[1125] | 140 | |
---|
| 141 | return false; |
---|
| 142 | } |
---|
[557] | 143 | |
---|
[1125] | 144 | /* (non-Javadoc) |
---|
[1146] | 145 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 146 | */ |
---|
| 147 | @Override |
---|
[1146] | 148 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 149 | ITask child1 = ((IIteration) task1).getMarkedTask(); |
---|
| 150 | ITask child2 = ((IIteration) task2).getMarkedTask(); |
---|
[1125] | 151 | |
---|
[1146] | 152 | if (child1 != null) { |
---|
| 153 | if (child2 == null) { |
---|
| 154 | return false; |
---|
[1125] | 155 | } |
---|
| 156 | else { |
---|
| 157 | // iterations may have 3 different structures. |
---|
| 158 | // 1. they have one child, which is the iterated one |
---|
| 159 | // 2. they have a sequence of children, which is iterated |
---|
| 160 | // 3. they have a selection of different iterated variants (usually the variants |
---|
| 161 | // are semantically equal) |
---|
| 162 | // ignore the type of the children but check them for equality. |
---|
| 163 | |
---|
[1146] | 164 | return getNodeEquality(child1, child2).isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL); |
---|
[1125] | 165 | } |
---|
[807] | 166 | } |
---|
[1146] | 167 | else if (child2 == null) { |
---|
| 168 | return true; |
---|
| 169 | } |
---|
[1125] | 170 | |
---|
| 171 | return false; |
---|
| 172 | } |
---|
[807] | 173 | |
---|
[1125] | 174 | /* (non-Javadoc) |
---|
[1146] | 175 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 176 | */ |
---|
| 177 | @Override |
---|
[1146] | 178 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 179 | return compare(task1, task2).isAtLeast(TaskEquality.SEMANTICALLY_EQUAL); |
---|
[1125] | 180 | } |
---|
| 181 | |
---|
| 182 | /* (non-Javadoc) |
---|
[1146] | 183 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 184 | */ |
---|
| 185 | @Override |
---|
[1146] | 186 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 187 | ITask child1 = ((IIteration) task1).getMarkedTask(); |
---|
| 188 | ITask child2 = ((IIteration) task2).getMarkedTask(); |
---|
[1125] | 189 | |
---|
[557] | 190 | // if both iterations do not have children, they are equal although this doesn't make sense |
---|
[1146] | 191 | if ((child1 == null) && (child2 == null)) { |
---|
| 192 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[557] | 193 | } |
---|
[1146] | 194 | else if ((child1 == null) || (child2 == null)) { |
---|
| 195 | return TaskEquality.UNEQUAL; |
---|
[807] | 196 | } |
---|
[557] | 197 | |
---|
| 198 | // iterations may have 3 different structures. |
---|
| 199 | // 1. they have one child, which is the iterated one |
---|
| 200 | // 2. they have a sequence of children, which is iterated |
---|
| 201 | // 3. they have a selection of different iterated variants (usually the variants are |
---|
| 202 | // semantically equal) |
---|
| 203 | // |
---|
| 204 | // the permutations of the three variants in combination must be checked |
---|
| 205 | |
---|
[1146] | 206 | // check if both tasks are the same variants of iterations and if their children are equal. |
---|
[557] | 207 | // This condition matches, if both iterations are the same variants of iteration. I.e. three |
---|
| 208 | // combinations of the permutation are handled herewith. |
---|
[1146] | 209 | TaskEquality taskEquality = getNodeEquality(child1, child2); |
---|
[1125] | 210 | |
---|
[1146] | 211 | if (taskEquality != null) { |
---|
| 212 | return taskEquality; |
---|
[1125] | 213 | } |
---|
[557] | 214 | |
---|
[1146] | 215 | // compare one iteration with a single task as a child and another one with a selection of |
---|
| 216 | // semantically equal tasks |
---|
[1125] | 217 | return selectionChildrenSemanticallyEqualNode(child1, child2); |
---|
| 218 | |
---|
| 219 | // all other combinations (i.e. sequence with single child and sequence with selection) |
---|
| 220 | // can not match |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | /** |
---|
[1154] | 224 | * <p> |
---|
| 225 | * compares two tasks with each other by calling the rule manager. If the rule manager returns |
---|
| 226 | * identity, then the returned equality is set to lexically equal. The reason is, that |
---|
| 227 | * the children of the iterations are compared and that therefore the distinct iterations |
---|
| 228 | * can be at most lexically equal. |
---|
| 229 | * </p> |
---|
| 230 | * |
---|
| 231 | * @param child1 the first task to be compared |
---|
| 232 | * @param child2 the second task to be compared |
---|
| 233 | * |
---|
| 234 | * @return the determined equality being at most lexical equality. |
---|
[1125] | 235 | */ |
---|
[1146] | 236 | private TaskEquality getNodeEquality(ITask child1, ITask child2) { |
---|
| 237 | TaskEquality taskEquality = callRuleManager(child1, child2, null); |
---|
[1125] | 238 | |
---|
[1146] | 239 | if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) { |
---|
[807] | 240 | // prevent, that identical is returned, because the iterations itself are not identical |
---|
| 241 | // although the iterated tasks are |
---|
[1146] | 242 | if (taskEquality == TaskEquality.IDENTICAL) { |
---|
| 243 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[807] | 244 | } |
---|
| 245 | else { |
---|
[1146] | 246 | return taskEquality; |
---|
[807] | 247 | } |
---|
[557] | 248 | } |
---|
| 249 | |
---|
[1146] | 250 | return TaskEquality.UNEQUAL; |
---|
[439] | 251 | } |
---|
[557] | 252 | |
---|
| 253 | /** |
---|
| 254 | * <p> |
---|
[1154] | 255 | * compares two tasks. One of them must be a selection, the other one can be any task. |
---|
| 256 | * The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code> |
---|
[1146] | 257 | * if the other task is at least semantically equal to the children of the selection. It |
---|
| 258 | * returns more concrete equalities, if the equality between the other task and the children |
---|
[807] | 259 | * of the selection is more concrete. |
---|
[557] | 260 | * </p> |
---|
| 261 | * |
---|
[1154] | 262 | * @param task1 the first task to compare |
---|
| 263 | * @param task2 the second task to compare |
---|
[557] | 264 | * |
---|
| 265 | * @return as described |
---|
| 266 | */ |
---|
[1146] | 267 | private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) { |
---|
[557] | 268 | ISelection selection = null; |
---|
[1146] | 269 | ITask task = null; |
---|
| 270 | if (task1 instanceof ISelection) { |
---|
| 271 | selection = (ISelection) task1; |
---|
| 272 | task = task2; |
---|
[557] | 273 | } |
---|
[1146] | 274 | else if (task2 instanceof ISelection) { |
---|
| 275 | selection = (ISelection) task2; |
---|
| 276 | task = task1; |
---|
[557] | 277 | } |
---|
| 278 | else { |
---|
[1146] | 279 | return TaskEquality.UNEQUAL; |
---|
[557] | 280 | } |
---|
[439] | 281 | |
---|
[807] | 282 | // Iterations, where one has a selection and the other one not can at most be syntactically |
---|
| 283 | // equal but not identical |
---|
[1146] | 284 | TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL; |
---|
[439] | 285 | |
---|
[1146] | 286 | for (ITask child : selection.getChildren()) { |
---|
| 287 | TaskEquality taskEquality = |
---|
| 288 | callRuleManager(task, child, commonDenominatorForAllComparisons); |
---|
[557] | 289 | |
---|
[1146] | 290 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) |
---|
[807] | 291 | { |
---|
[1146] | 292 | return TaskEquality.UNEQUAL; |
---|
[557] | 293 | } |
---|
[807] | 294 | |
---|
| 295 | commonDenominatorForAllComparisons = |
---|
[1146] | 296 | commonDenominatorForAllComparisons.getCommonDenominator(taskEquality); |
---|
[557] | 297 | } |
---|
| 298 | |
---|
[807] | 299 | return commonDenominatorForAllComparisons; |
---|
[439] | 300 | } |
---|
| 301 | |
---|
[1125] | 302 | /** |
---|
| 303 | * <p> |
---|
[1154] | 304 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 305 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 306 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 307 | * on that level. |
---|
| 308 | * </p> |
---|
| 309 | * |
---|
| 310 | * @param child1 the first task to be compared |
---|
| 311 | * @param child2 the second task to be compared |
---|
| 312 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 313 | * |
---|
| 314 | * @return the determined equality |
---|
[1125] | 315 | */ |
---|
[1146] | 316 | private TaskEquality callRuleManager(ITask child1, |
---|
| 317 | ITask child2, |
---|
| 318 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 319 | { |
---|
| 320 | if (requiredEqualityLevel == null) { |
---|
| 321 | return mRuleManager.compare(child1, child2); |
---|
| 322 | } |
---|
| 323 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
| 324 | return requiredEqualityLevel; |
---|
| 325 | } |
---|
| 326 | else { |
---|
[1146] | 327 | return TaskEquality.UNEQUAL; |
---|
[1125] | 328 | } |
---|
| 329 | } |
---|
[439] | 330 | } |
---|