[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 java.util.List; |
---|
| 18 | |
---|
[922] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1146] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[439] | 21 | |
---|
| 22 | /** |
---|
[557] | 23 | * <p> |
---|
[1146] | 24 | * this task comparison rule is capable of comparing selections. If both selections do not have |
---|
| 25 | * children, they are treated as lexically equal. If they have children, each child of both |
---|
| 26 | * selections is compared to each child of the respective other selection. The resulting equality |
---|
| 27 | * is the most concrete one of all these comparisons. I.e. if all children are at least lexically |
---|
| 28 | * equal, then the selections are lexically equal. If all children are at least syntactically |
---|
| 29 | * equal, then the selections are syntactically equal. If all children are at least semantically |
---|
| 30 | * equal, then the selections are semantically equal. If only one of the selections has children, |
---|
[1154] | 31 | * then the selections are unequal. The comparison is broken up, if only a specific equality is |
---|
| 32 | * checked for and this equality is ensured. |
---|
[557] | 33 | * </p> |
---|
[439] | 34 | * |
---|
| 35 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 36 | * @author 2012, last modified by $Author: patrick$ |
---|
| 37 | */ |
---|
[1146] | 38 | public class SelectionComparisonRule implements TaskComparisonRule { |
---|
[439] | 39 | |
---|
[1125] | 40 | /* (non-Javadoc) |
---|
[1146] | 41 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[557] | 42 | */ |
---|
| 43 | @Override |
---|
[1146] | 44 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 45 | return (task1 instanceof ISelection) && (task2 instanceof ISelection); |
---|
[1125] | 46 | } |
---|
| 47 | |
---|
| 48 | /* (non-Javadoc) |
---|
[1146] | 49 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 50 | */ |
---|
| 51 | @Override |
---|
[1146] | 52 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 53 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 54 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
[1125] | 55 | } |
---|
| 56 | |
---|
| 57 | /* (non-Javadoc) |
---|
[1146] | 58 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 59 | */ |
---|
| 60 | @Override |
---|
[1146] | 61 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 62 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 63 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
[1125] | 64 | } |
---|
| 65 | |
---|
| 66 | /* (non-Javadoc) |
---|
[1146] | 67 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 68 | */ |
---|
| 69 | @Override |
---|
[1146] | 70 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 71 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 72 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
[1125] | 73 | } |
---|
| 74 | |
---|
| 75 | /* (non-Javadoc) |
---|
[1146] | 76 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 77 | */ |
---|
| 78 | @Override |
---|
[1146] | 79 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 80 | return getEquality(task1, task2, null); |
---|
[1125] | 81 | } |
---|
[557] | 82 | |
---|
[1125] | 83 | /** |
---|
[1154] | 84 | * <p> |
---|
| 85 | * compares two selections with each other checking for the provided required level of |
---|
| 86 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
| 87 | * the required equality level, the more checks this method performs. |
---|
| 88 | * </p> |
---|
[1125] | 89 | * |
---|
[1154] | 90 | * @param task1 the first task to be compared |
---|
| 91 | * @param task2 the second task to be compared |
---|
| 92 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 93 | * |
---|
| 94 | * @return the determined equality. |
---|
[1125] | 95 | */ |
---|
[1146] | 96 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
| 97 | List<ITask> children1 = ((ISelection) task1).getChildren(); |
---|
| 98 | List<ITask> children2 = ((ISelection) task2).getChildren(); |
---|
[807] | 99 | |
---|
[1125] | 100 | // if both selections do not have children, they are lexically equal. If only one of them |
---|
| 101 | // has children, they are unequal. |
---|
| 102 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
[1146] | 103 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[439] | 104 | } |
---|
[1125] | 105 | else if ((children1.size() == 0) || (children2.size() == 0)) { |
---|
[1146] | 106 | return TaskEquality.UNEQUAL; |
---|
[807] | 107 | } |
---|
[557] | 108 | |
---|
[1146] | 109 | TaskEquality selectionEquality; |
---|
[557] | 110 | |
---|
[1125] | 111 | if (requiredEqualityLevel == null) { |
---|
| 112 | // calculate the common equality level for all children of both selections. |
---|
| 113 | // do it in both directions to ensure commutative comparison |
---|
| 114 | selectionEquality = getCommonEqualityLevel(children1, children2); |
---|
[1146] | 115 | if (selectionEquality != TaskEquality.UNEQUAL) { |
---|
[1125] | 116 | return selectionEquality.getCommonDenominator |
---|
| 117 | (getCommonEqualityLevel(children2, children1)); |
---|
| 118 | } |
---|
| 119 | else { |
---|
[1146] | 120 | return TaskEquality.UNEQUAL; |
---|
[1125] | 121 | } |
---|
| 122 | } |
---|
| 123 | else { |
---|
| 124 | // we are searching for a specific equality |
---|
| 125 | if (checkEqualityLevel(children1, children2, requiredEqualityLevel) && |
---|
| 126 | checkEqualityLevel(children2, children1, requiredEqualityLevel)) |
---|
| 127 | { |
---|
| 128 | return requiredEqualityLevel; |
---|
| 129 | } |
---|
| 130 | else { |
---|
[1146] | 131 | return TaskEquality.UNEQUAL; |
---|
[1125] | 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * <p> |
---|
[1154] | 138 | * determines the common equality level for all tasks in the first list compared to all |
---|
| 139 | * tasks in the second list. If for one task in the first list, there is no equal task in the |
---|
| 140 | * second list, the method return unequality. |
---|
[1125] | 141 | * </p> |
---|
| 142 | * |
---|
[1154] | 143 | * @param children1 the first list to be compared |
---|
| 144 | * @param children2 the second list to be compared |
---|
| 145 | * |
---|
| 146 | * @return the common task equality identified for all tasks in the first list with respect to |
---|
| 147 | * the second list |
---|
[1125] | 148 | */ |
---|
[1146] | 149 | private TaskEquality getCommonEqualityLevel(List<ITask> children1, List<ITask> children2) { |
---|
| 150 | TaskEquality listEquality = TaskEquality.LEXICALLY_EQUAL; |
---|
[1125] | 151 | |
---|
[1146] | 152 | TaskEquality childEquality; |
---|
| 153 | TaskEquality currentEquality; |
---|
| 154 | for (ITask child1 : children1) { |
---|
[807] | 155 | childEquality = null; |
---|
[1146] | 156 | for (ITask child2 : children2) { |
---|
[1125] | 157 | currentEquality = callRuleManager(child1, child2, null); |
---|
[1146] | 158 | if ((currentEquality != null) && (currentEquality != TaskEquality.UNEQUAL)) { |
---|
[807] | 159 | if (childEquality == null) { |
---|
| 160 | childEquality = currentEquality; |
---|
| 161 | } |
---|
| 162 | else { |
---|
| 163 | childEquality = childEquality.getCommonDenominator(currentEquality); |
---|
| 164 | } |
---|
[1125] | 165 | |
---|
[1146] | 166 | if (childEquality == TaskEquality.SEMANTICALLY_EQUAL) { |
---|
[1125] | 167 | // as we calculate only the common denominator, we can break up here for |
---|
| 168 | // the current child. We will not improve the denominator anymore |
---|
| 169 | break; |
---|
| 170 | } |
---|
[807] | 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
[1125] | 174 | if (childEquality == null) { |
---|
| 175 | // we did not find any child in the second list, that is equal to the searched |
---|
| 176 | // child |
---|
[1146] | 177 | return TaskEquality.UNEQUAL; |
---|
[807] | 178 | } |
---|
| 179 | else { |
---|
[1125] | 180 | listEquality = listEquality.getCommonDenominator(childEquality); |
---|
[807] | 181 | } |
---|
| 182 | } |
---|
[557] | 183 | |
---|
[1125] | 184 | return listEquality; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * <p> |
---|
[1154] | 189 | * ensures for the two given lists, that for each task in the first list there is a task |
---|
| 190 | * in the second list being on the given level equal to the task in the first list. |
---|
| 191 | * </p> |
---|
| 192 | * |
---|
| 193 | * @param children1 the first list to be compared |
---|
| 194 | * @param children2 the second list to be compared |
---|
| 195 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 196 | * |
---|
| 197 | * @return true if each task in the first list has an equal task in the second list when |
---|
| 198 | * considering the given equality level, false else. |
---|
[1125] | 199 | */ |
---|
[1146] | 200 | private boolean checkEqualityLevel(List<ITask> children1, |
---|
| 201 | List<ITask> children2, |
---|
| 202 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 203 | { |
---|
[1146] | 204 | TaskEquality childEquality; |
---|
| 205 | TaskEquality currentEquality; |
---|
| 206 | for (ITask child1 : children1) { |
---|
[807] | 207 | childEquality = null; |
---|
[1146] | 208 | for (ITask child2 : children2) { |
---|
[1125] | 209 | currentEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
| 210 | if ((currentEquality != null) && (currentEquality.isAtLeast(requiredEqualityLevel))) |
---|
| 211 | { |
---|
| 212 | // we found at least one equal child with sufficient equality in the |
---|
| 213 | // second list. So be can break up for this child. |
---|
| 214 | childEquality = currentEquality; |
---|
| 215 | break; |
---|
[557] | 216 | } |
---|
| 217 | } |
---|
[807] | 218 | |
---|
[1125] | 219 | if (childEquality == null) { |
---|
| 220 | // we did not find any child in the second list, that is equal to the searched |
---|
| 221 | // child |
---|
| 222 | return false; |
---|
[807] | 223 | } |
---|
[557] | 224 | } |
---|
| 225 | |
---|
[1125] | 226 | // for all children, we found an equality |
---|
| 227 | return true; |
---|
[439] | 228 | } |
---|
[807] | 229 | |
---|
[1125] | 230 | /** |
---|
| 231 | * <p> |
---|
[1154] | 232 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 233 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 234 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 235 | * on that level. |
---|
| 236 | * </p> |
---|
| 237 | * |
---|
| 238 | * @param child1 the first task to be compared |
---|
| 239 | * @param child2 the second task to be compared |
---|
| 240 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 241 | * |
---|
| 242 | * @return the determined equality |
---|
[1125] | 243 | */ |
---|
[1146] | 244 | private TaskEquality callRuleManager(ITask child1, |
---|
| 245 | ITask child2, |
---|
| 246 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 247 | { |
---|
| 248 | if (requiredEqualityLevel == null) { |
---|
[1190] | 249 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
[1125] | 250 | } |
---|
[1190] | 251 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 252 | (child1, child2, requiredEqualityLevel)) |
---|
| 253 | { |
---|
[1125] | 254 | return requiredEqualityLevel; |
---|
| 255 | } |
---|
| 256 | else { |
---|
[1146] | 257 | return TaskEquality.UNEQUAL; |
---|
[1125] | 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
[439] | 261 | } |
---|