[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; |
---|
[816] | 16 | |
---|
[922] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
[1146] | 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[816] | 19 | |
---|
| 20 | /** |
---|
| 21 | * <p> |
---|
[1146] | 22 | * This class is capable of comparing any task which is not an iteration with an |
---|
| 23 | * iteration. This is needed, because iterations may iterate exactly that task. In this |
---|
| 24 | * case, the iteration would be equal to that task if it was executed exactly once. The rule |
---|
| 25 | * returns lexically equal, it the child of the iteration is lexically equal to the task |
---|
[816] | 26 | * or if the child of the iteration is a selection and this selections contains a lexically equal |
---|
[1146] | 27 | * task. The same applies for syntactical and semantical equality. |
---|
[816] | 28 | * </p> |
---|
| 29 | |
---|
| 30 | * @author Patrick Harms |
---|
| 31 | */ |
---|
[1146] | 32 | public class TaskAndIterationComparisonRule implements TaskComparisonRule { |
---|
[816] | 33 | |
---|
[1125] | 34 | /* (non-Javadoc) |
---|
[1146] | 35 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[816] | 36 | */ |
---|
| 37 | @Override |
---|
[1146] | 38 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 39 | return ((task1 instanceof IIteration) && (!(task2 instanceof IIteration))) || |
---|
| 40 | ((task2 instanceof IIteration) && (!(task1 instanceof IIteration))); |
---|
[1125] | 41 | } |
---|
| 42 | |
---|
| 43 | /* (non-Javadoc) |
---|
[1146] | 44 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 45 | */ |
---|
| 46 | @Override |
---|
[1146] | 47 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 48 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 49 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
[1125] | 50 | } |
---|
| 51 | |
---|
| 52 | /* (non-Javadoc) |
---|
[1146] | 53 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 54 | */ |
---|
| 55 | @Override |
---|
[1146] | 56 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 57 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 58 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
[1125] | 59 | } |
---|
| 60 | |
---|
| 61 | /* (non-Javadoc) |
---|
[1146] | 62 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 63 | */ |
---|
| 64 | @Override |
---|
[1146] | 65 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 66 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 67 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
[1125] | 68 | } |
---|
| 69 | |
---|
| 70 | /* (non-Javadoc) |
---|
[1146] | 71 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 72 | */ |
---|
| 73 | @Override |
---|
[1146] | 74 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 75 | return getEquality(task1, task2, null); |
---|
[1125] | 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
[1154] | 79 | * <p> |
---|
| 80 | * compares two tasks with each other checking for the provided required level of |
---|
| 81 | * equality. One of the tasks must be an iteration, the other one not. If this is not the |
---|
| 82 | * case, the method returns null. The returned equality level is at most lexical equality |
---|
| 83 | * as the iteration can not be identical to something not being an iteration. |
---|
| 84 | * </p> |
---|
[1125] | 85 | * |
---|
[1154] | 86 | * @param task1 the first task to be compared |
---|
| 87 | * @param task2 the second task to be compared |
---|
| 88 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 89 | * |
---|
| 90 | * @return the determined equality. |
---|
[1125] | 91 | */ |
---|
[1146] | 92 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
[816] | 93 | IIteration iteration = null; |
---|
[1146] | 94 | ITask task = null; |
---|
[816] | 95 | |
---|
[1146] | 96 | if (task1 instanceof IIteration) { |
---|
| 97 | if (task2 instanceof IIteration) { |
---|
[816] | 98 | // the rule is not responsible for two iterations |
---|
| 99 | return null; |
---|
| 100 | } |
---|
| 101 | |
---|
[1146] | 102 | iteration = (IIteration) task1; |
---|
| 103 | task = task2; |
---|
[816] | 104 | } |
---|
[1146] | 105 | else if (task2 instanceof IIteration) { |
---|
| 106 | if (task1 instanceof IIteration) { |
---|
[816] | 107 | // the rule is not responsible for two iterations |
---|
| 108 | return null; |
---|
| 109 | } |
---|
| 110 | |
---|
[1146] | 111 | iteration = (IIteration) task2; |
---|
| 112 | task = task1; |
---|
[816] | 113 | } |
---|
| 114 | else { |
---|
| 115 | return null; |
---|
| 116 | } |
---|
| 117 | |
---|
[1146] | 118 | ITask child = iteration.getMarkedTask(); |
---|
[1125] | 119 | |
---|
[1146] | 120 | // now, that we found the iteration and the task, lets compare the child of the iteration |
---|
| 121 | // with the task. |
---|
| 122 | if (child == null) { |
---|
[816] | 123 | return null; |
---|
| 124 | } |
---|
| 125 | |
---|
[1146] | 126 | TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel); |
---|
[816] | 127 | |
---|
[1146] | 128 | // although the subtask may be identical to the task, we can not return identical, as |
---|
| 129 | // the iteration is not identical to the task, but at most lexically equal |
---|
| 130 | if (taskEquality == TaskEquality.IDENTICAL) { |
---|
| 131 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[816] | 132 | } |
---|
| 133 | else { |
---|
[1146] | 134 | return taskEquality; |
---|
[816] | 135 | } |
---|
| 136 | |
---|
| 137 | } |
---|
[1125] | 138 | |
---|
| 139 | /** |
---|
| 140 | * <p> |
---|
[1154] | 141 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 142 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 143 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 144 | * on that level. |
---|
| 145 | * </p> |
---|
| 146 | * |
---|
| 147 | * @param child1 the first task to be compared |
---|
| 148 | * @param child2 the second task to be compared |
---|
| 149 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 150 | * |
---|
| 151 | * @return the determined equality |
---|
[1125] | 152 | */ |
---|
[1146] | 153 | private TaskEquality callRuleManager(ITask child1, |
---|
| 154 | ITask child2, |
---|
| 155 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 156 | { |
---|
| 157 | if (requiredEqualityLevel == null) { |
---|
[1190] | 158 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
[1125] | 159 | } |
---|
[1190] | 160 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 161 | (child1, child2, requiredEqualityLevel)) |
---|
| 162 | { |
---|
[1125] | 163 | return requiredEqualityLevel; |
---|
| 164 | } |
---|
| 165 | else { |
---|
[1146] | 166 | return TaskEquality.UNEQUAL; |
---|
[1125] | 167 | } |
---|
| 168 | } |
---|
[816] | 169 | } |
---|