[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 | |
---|
[1146] | 34 | /** the rule manager for internally comparing tasks */ |
---|
| 35 | private TaskEqualityRuleManager mRuleManager; |
---|
[816] | 36 | |
---|
| 37 | /** |
---|
| 38 | * <p> |
---|
[1146] | 39 | * simple constructor to provide the rule with the task equality rule manager to be able |
---|
| 40 | * to perform comparisons of the children of provided tasks |
---|
[816] | 41 | * </p> |
---|
| 42 | * |
---|
[1146] | 43 | * @param ruleManager the rule manager for comparing tasks |
---|
[816] | 44 | */ |
---|
[1146] | 45 | TaskAndIterationComparisonRule(TaskEqualityRuleManager ruleManager) { |
---|
[816] | 46 | super(); |
---|
| 47 | mRuleManager = ruleManager; |
---|
| 48 | } |
---|
| 49 | |
---|
[1125] | 50 | /* (non-Javadoc) |
---|
[1146] | 51 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[816] | 52 | */ |
---|
| 53 | @Override |
---|
[1146] | 54 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 55 | return ((task1 instanceof IIteration) && (!(task2 instanceof IIteration))) || |
---|
| 56 | ((task2 instanceof IIteration) && (!(task1 instanceof IIteration))); |
---|
[1125] | 57 | } |
---|
| 58 | |
---|
| 59 | /* (non-Javadoc) |
---|
[1146] | 60 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 61 | */ |
---|
| 62 | @Override |
---|
[1146] | 63 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 64 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 65 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
[1125] | 66 | } |
---|
| 67 | |
---|
| 68 | /* (non-Javadoc) |
---|
[1146] | 69 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 70 | */ |
---|
| 71 | @Override |
---|
[1146] | 72 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 73 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 74 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
[1125] | 75 | } |
---|
| 76 | |
---|
| 77 | /* (non-Javadoc) |
---|
[1146] | 78 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 79 | */ |
---|
| 80 | @Override |
---|
[1146] | 81 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 82 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 83 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
[1125] | 84 | } |
---|
| 85 | |
---|
| 86 | /* (non-Javadoc) |
---|
[1146] | 87 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 88 | */ |
---|
| 89 | @Override |
---|
[1146] | 90 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 91 | return getEquality(task1, task2, null); |
---|
[1125] | 92 | } |
---|
| 93 | |
---|
| 94 | /** |
---|
[1154] | 95 | * <p> |
---|
| 96 | * compares two tasks with each other checking for the provided required level of |
---|
| 97 | * equality. One of the tasks must be an iteration, the other one not. If this is not the |
---|
| 98 | * case, the method returns null. The returned equality level is at most lexical equality |
---|
| 99 | * as the iteration can not be identical to something not being an iteration. |
---|
| 100 | * </p> |
---|
[1125] | 101 | * |
---|
[1154] | 102 | * @param task1 the first task to be compared |
---|
| 103 | * @param task2 the second task to be compared |
---|
| 104 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 105 | * |
---|
| 106 | * @return the determined equality. |
---|
[1125] | 107 | */ |
---|
[1146] | 108 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
[816] | 109 | IIteration iteration = null; |
---|
[1146] | 110 | ITask task = null; |
---|
[816] | 111 | |
---|
[1146] | 112 | if (task1 instanceof IIteration) { |
---|
| 113 | if (task2 instanceof IIteration) { |
---|
[816] | 114 | // the rule is not responsible for two iterations |
---|
| 115 | return null; |
---|
| 116 | } |
---|
| 117 | |
---|
[1146] | 118 | iteration = (IIteration) task1; |
---|
| 119 | task = task2; |
---|
[816] | 120 | } |
---|
[1146] | 121 | else if (task2 instanceof IIteration) { |
---|
| 122 | if (task1 instanceof IIteration) { |
---|
[816] | 123 | // the rule is not responsible for two iterations |
---|
| 124 | return null; |
---|
| 125 | } |
---|
| 126 | |
---|
[1146] | 127 | iteration = (IIteration) task2; |
---|
| 128 | task = task1; |
---|
[816] | 129 | } |
---|
| 130 | else { |
---|
| 131 | return null; |
---|
| 132 | } |
---|
| 133 | |
---|
[1146] | 134 | ITask child = iteration.getMarkedTask(); |
---|
[1125] | 135 | |
---|
[1146] | 136 | // now, that we found the iteration and the task, lets compare the child of the iteration |
---|
| 137 | // with the task. |
---|
| 138 | if (child == null) { |
---|
[816] | 139 | return null; |
---|
| 140 | } |
---|
| 141 | |
---|
[1146] | 142 | TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel); |
---|
[816] | 143 | |
---|
[1146] | 144 | // although the subtask may be identical to the task, we can not return identical, as |
---|
| 145 | // the iteration is not identical to the task, but at most lexically equal |
---|
| 146 | if (taskEquality == TaskEquality.IDENTICAL) { |
---|
| 147 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[816] | 148 | } |
---|
| 149 | else { |
---|
[1146] | 150 | return taskEquality; |
---|
[816] | 151 | } |
---|
| 152 | |
---|
| 153 | } |
---|
[1125] | 154 | |
---|
| 155 | /** |
---|
| 156 | * <p> |
---|
[1154] | 157 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 158 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 159 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 160 | * on that level. |
---|
| 161 | * </p> |
---|
| 162 | * |
---|
| 163 | * @param child1 the first task to be compared |
---|
| 164 | * @param child2 the second task to be compared |
---|
| 165 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 166 | * |
---|
| 167 | * @return the determined equality |
---|
[1125] | 168 | */ |
---|
[1146] | 169 | private TaskEquality callRuleManager(ITask child1, |
---|
| 170 | ITask child2, |
---|
| 171 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 172 | { |
---|
| 173 | if (requiredEqualityLevel == null) { |
---|
| 174 | return mRuleManager.compare(child1, child2); |
---|
| 175 | } |
---|
| 176 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
| 177 | return requiredEqualityLevel; |
---|
| 178 | } |
---|
| 179 | else { |
---|
[1146] | 180 | return TaskEquality.UNEQUAL; |
---|
[1125] | 181 | } |
---|
| 182 | } |
---|
[816] | 183 | } |
---|