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