[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 | |
---|
[1125] | 83 | /* (non-Javadoc) |
---|
[1146] | 84 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[557] | 85 | */ |
---|
| 86 | @Override |
---|
[1146] | 87 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 88 | return (task1 instanceof IIteration) && (task2 instanceof IIteration); |
---|
[1125] | 89 | } |
---|
| 90 | |
---|
| 91 | /* (non-Javadoc) |
---|
[1146] | 92 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 93 | */ |
---|
| 94 | @Override |
---|
[1146] | 95 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 96 | ITask child1 = ((IIteration) task1).getMarkedTask(); |
---|
| 97 | ITask child2 = ((IIteration) task2).getMarkedTask(); |
---|
[1125] | 98 | |
---|
[1146] | 99 | if (child1 != null) { |
---|
| 100 | if (child2 == null) { |
---|
| 101 | return false; |
---|
[1125] | 102 | } |
---|
| 103 | else { |
---|
| 104 | // iterations may have 3 different structures. |
---|
| 105 | // 1. they have one child, which is the iterated one |
---|
| 106 | // 2. they have a sequence of children, which is iterated |
---|
| 107 | // 3. they have a selection of different iterated variants (usually the variants |
---|
| 108 | // are semantically equal) |
---|
| 109 | // check if the type of children match. If not, return false. If they match, |
---|
| 110 | // use the equality manager to perform further comparisons |
---|
| 111 | |
---|
| 112 | if (((child1 instanceof ISelection) && (child2 instanceof ISelection)) || |
---|
| 113 | ((child1 instanceof ISequence) && (child2 instanceof ISequence)) || |
---|
| 114 | ((child1 instanceof IEventTask) && (child2 instanceof IEventTask))) |
---|
| 115 | { |
---|
| 116 | return getNodeEquality |
---|
[1146] | 117 | (child1, child2).isAtLeast(TaskEquality.LEXICALLY_EQUAL); |
---|
[1125] | 118 | } |
---|
| 119 | } |
---|
[557] | 120 | } |
---|
[1146] | 121 | else if (child2 == null) { |
---|
| 122 | return true; |
---|
| 123 | } |
---|
[1125] | 124 | |
---|
| 125 | return false; |
---|
| 126 | } |
---|
[557] | 127 | |
---|
[1125] | 128 | /* (non-Javadoc) |
---|
[1146] | 129 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 130 | */ |
---|
| 131 | @Override |
---|
[1146] | 132 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 133 | ITask child1 = ((IIteration) task1).getMarkedTask(); |
---|
| 134 | ITask child2 = ((IIteration) task2).getMarkedTask(); |
---|
[1125] | 135 | |
---|
[1146] | 136 | if (child1 != null) { |
---|
| 137 | if (child2 == null) { |
---|
| 138 | return false; |
---|
[1125] | 139 | } |
---|
| 140 | else { |
---|
| 141 | // iterations may have 3 different structures. |
---|
| 142 | // 1. they have one child, which is the iterated one |
---|
| 143 | // 2. they have a sequence of children, which is iterated |
---|
| 144 | // 3. they have a selection of different iterated variants (usually the variants |
---|
| 145 | // are semantically equal) |
---|
| 146 | // ignore the type of the children but check them for equality. |
---|
| 147 | |
---|
[1146] | 148 | return getNodeEquality(child1, child2).isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL); |
---|
[1125] | 149 | } |
---|
[807] | 150 | } |
---|
[1146] | 151 | else if (child2 == null) { |
---|
| 152 | return true; |
---|
| 153 | } |
---|
[1125] | 154 | |
---|
| 155 | return false; |
---|
| 156 | } |
---|
[807] | 157 | |
---|
[1125] | 158 | /* (non-Javadoc) |
---|
[1146] | 159 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 160 | */ |
---|
| 161 | @Override |
---|
[1146] | 162 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 163 | return compare(task1, task2).isAtLeast(TaskEquality.SEMANTICALLY_EQUAL); |
---|
[1125] | 164 | } |
---|
| 165 | |
---|
| 166 | /* (non-Javadoc) |
---|
[1146] | 167 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 168 | */ |
---|
| 169 | @Override |
---|
[1146] | 170 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 171 | ITask child1 = ((IIteration) task1).getMarkedTask(); |
---|
| 172 | ITask child2 = ((IIteration) task2).getMarkedTask(); |
---|
[1125] | 173 | |
---|
[557] | 174 | // if both iterations do not have children, they are equal although this doesn't make sense |
---|
[1146] | 175 | if ((child1 == null) && (child2 == null)) { |
---|
| 176 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[557] | 177 | } |
---|
[1146] | 178 | else if ((child1 == null) || (child2 == null)) { |
---|
| 179 | return TaskEquality.UNEQUAL; |
---|
[807] | 180 | } |
---|
[557] | 181 | |
---|
| 182 | // iterations may have 3 different structures. |
---|
| 183 | // 1. they have one child, which is the iterated one |
---|
| 184 | // 2. they have a sequence of children, which is iterated |
---|
| 185 | // 3. they have a selection of different iterated variants (usually the variants are |
---|
| 186 | // semantically equal) |
---|
| 187 | // |
---|
| 188 | // the permutations of the three variants in combination must be checked |
---|
| 189 | |
---|
[1146] | 190 | // check if both tasks are the same variants of iterations and if their children are equal. |
---|
[557] | 191 | // This condition matches, if both iterations are the same variants of iteration. I.e. three |
---|
| 192 | // combinations of the permutation are handled herewith. |
---|
[1146] | 193 | TaskEquality taskEquality = getNodeEquality(child1, child2); |
---|
[1125] | 194 | |
---|
[1146] | 195 | if (taskEquality != null) { |
---|
| 196 | return taskEquality; |
---|
[1125] | 197 | } |
---|
[557] | 198 | |
---|
[1146] | 199 | // compare one iteration with a single task as a child and another one with a selection of |
---|
| 200 | // semantically equal tasks |
---|
[1125] | 201 | return selectionChildrenSemanticallyEqualNode(child1, child2); |
---|
| 202 | |
---|
| 203 | // all other combinations (i.e. sequence with single child and sequence with selection) |
---|
| 204 | // can not match |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
[1154] | 208 | * <p> |
---|
| 209 | * compares two tasks with each other by calling the rule manager. If the rule manager returns |
---|
| 210 | * identity, then the returned equality is set to lexically equal. The reason is, that |
---|
| 211 | * the children of the iterations are compared and that therefore the distinct iterations |
---|
| 212 | * can be at most lexically equal. |
---|
| 213 | * </p> |
---|
| 214 | * |
---|
| 215 | * @param child1 the first task to be compared |
---|
| 216 | * @param child2 the second task to be compared |
---|
| 217 | * |
---|
| 218 | * @return the determined equality being at most lexical equality. |
---|
[1125] | 219 | */ |
---|
[1146] | 220 | private TaskEquality getNodeEquality(ITask child1, ITask child2) { |
---|
| 221 | TaskEquality taskEquality = callRuleManager(child1, child2, null); |
---|
[1125] | 222 | |
---|
[1146] | 223 | if (taskEquality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)) { |
---|
[807] | 224 | // prevent, that identical is returned, because the iterations itself are not identical |
---|
| 225 | // although the iterated tasks are |
---|
[1146] | 226 | if (taskEquality == TaskEquality.IDENTICAL) { |
---|
| 227 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[807] | 228 | } |
---|
| 229 | else { |
---|
[1146] | 230 | return taskEquality; |
---|
[807] | 231 | } |
---|
[557] | 232 | } |
---|
| 233 | |
---|
[1146] | 234 | return TaskEquality.UNEQUAL; |
---|
[439] | 235 | } |
---|
[557] | 236 | |
---|
| 237 | /** |
---|
| 238 | * <p> |
---|
[1154] | 239 | * compares two tasks. One of them must be a selection, the other one can be any task. |
---|
| 240 | * The method returns a task equality that is not <code>NodeEquality.UNEQUAL</code> |
---|
[1146] | 241 | * if the other task is at least semantically equal to the children of the selection. It |
---|
| 242 | * returns more concrete equalities, if the equality between the other task and the children |
---|
[807] | 243 | * of the selection is more concrete. |
---|
[557] | 244 | * </p> |
---|
| 245 | * |
---|
[1154] | 246 | * @param task1 the first task to compare |
---|
| 247 | * @param task2 the second task to compare |
---|
[557] | 248 | * |
---|
| 249 | * @return as described |
---|
| 250 | */ |
---|
[1146] | 251 | private TaskEquality selectionChildrenSemanticallyEqualNode(ITask task1, ITask task2) { |
---|
[557] | 252 | ISelection selection = null; |
---|
[1146] | 253 | ITask task = null; |
---|
| 254 | if (task1 instanceof ISelection) { |
---|
| 255 | selection = (ISelection) task1; |
---|
| 256 | task = task2; |
---|
[557] | 257 | } |
---|
[1146] | 258 | else if (task2 instanceof ISelection) { |
---|
| 259 | selection = (ISelection) task2; |
---|
| 260 | task = task1; |
---|
[557] | 261 | } |
---|
| 262 | else { |
---|
[1146] | 263 | return TaskEquality.UNEQUAL; |
---|
[557] | 264 | } |
---|
[439] | 265 | |
---|
[807] | 266 | // Iterations, where one has a selection and the other one not can at most be syntactically |
---|
| 267 | // equal but not identical |
---|
[1146] | 268 | TaskEquality commonDenominatorForAllComparisons = TaskEquality.SYNTACTICALLY_EQUAL; |
---|
[439] | 269 | |
---|
[1146] | 270 | for (ITask child : selection.getChildren()) { |
---|
| 271 | TaskEquality taskEquality = |
---|
| 272 | callRuleManager(task, child, commonDenominatorForAllComparisons); |
---|
[557] | 273 | |
---|
[1146] | 274 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) |
---|
[807] | 275 | { |
---|
[1146] | 276 | return TaskEquality.UNEQUAL; |
---|
[557] | 277 | } |
---|
[807] | 278 | |
---|
| 279 | commonDenominatorForAllComparisons = |
---|
[1146] | 280 | commonDenominatorForAllComparisons.getCommonDenominator(taskEquality); |
---|
[557] | 281 | } |
---|
| 282 | |
---|
[807] | 283 | return commonDenominatorForAllComparisons; |
---|
[439] | 284 | } |
---|
| 285 | |
---|
[1125] | 286 | /** |
---|
| 287 | * <p> |
---|
[1154] | 288 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 289 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 290 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 291 | * on that level. |
---|
| 292 | * </p> |
---|
| 293 | * |
---|
| 294 | * @param child1 the first task to be compared |
---|
| 295 | * @param child2 the second task to be compared |
---|
| 296 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 297 | * |
---|
| 298 | * @return the determined equality |
---|
[1125] | 299 | */ |
---|
[1146] | 300 | private TaskEquality callRuleManager(ITask child1, |
---|
| 301 | ITask child2, |
---|
| 302 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 303 | { |
---|
| 304 | if (requiredEqualityLevel == null) { |
---|
[1190] | 305 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
[1125] | 306 | } |
---|
[1190] | 307 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 308 | (child1, child2, requiredEqualityLevel)) |
---|
| 309 | { |
---|
[1125] | 310 | return requiredEqualityLevel; |
---|
| 311 | } |
---|
| 312 | else { |
---|
[1146] | 313 | return TaskEquality.UNEQUAL; |
---|
[1125] | 314 | } |
---|
| 315 | } |
---|
[439] | 316 | } |
---|