[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 | |
---|
[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; |
---|
[816] | 21 | |
---|
| 22 | /** |
---|
| 23 | * <p> |
---|
[1146] | 24 | * This class is capable of comparing any task which is not a selection with a |
---|
| 25 | * selection. This is needed, because selections may contain exactly that task. Therefore, if |
---|
| 26 | * this task is selected out of a selection the selection is equal to the task itself. |
---|
| 27 | * The rule returns lexically equal, if the selection contains a lexically equal task. The same |
---|
[816] | 28 | * applies for syntactical and semantical equality. |
---|
| 29 | * </p> |
---|
| 30 | |
---|
| 31 | * @author Patrick Harms |
---|
| 32 | */ |
---|
[1146] | 33 | public class TaskAndSelectionComparisonRule implements TaskComparisonRule { |
---|
[816] | 34 | |
---|
[1146] | 35 | /** the rule manager for internally comparing tasks */ |
---|
| 36 | private TaskEqualityRuleManager mRuleManager; |
---|
[1125] | 37 | |
---|
[816] | 38 | /** |
---|
| 39 | * <p> |
---|
[1146] | 40 | * simple constructor to provide the rule with the task equality rule manager to be able |
---|
| 41 | * to perform comparisons of the children of provided tasks |
---|
[816] | 42 | * </p> |
---|
| 43 | * |
---|
[1146] | 44 | * @param ruleManager the rule manager for comparing tasks |
---|
[816] | 45 | */ |
---|
[1146] | 46 | TaskAndSelectionComparisonRule(TaskEqualityRuleManager ruleManager) { |
---|
[816] | 47 | super(); |
---|
| 48 | mRuleManager = ruleManager; |
---|
| 49 | } |
---|
| 50 | |
---|
[1125] | 51 | /* (non-Javadoc) |
---|
[1146] | 52 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[816] | 53 | */ |
---|
| 54 | @Override |
---|
[1146] | 55 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 56 | return ((task1 instanceof ISelection) && (!(task2 instanceof ISelection))) || |
---|
| 57 | ((task2 instanceof ISelection) && (!(task1 instanceof ISelection))); |
---|
[1125] | 58 | } |
---|
| 59 | |
---|
| 60 | /* (non-Javadoc) |
---|
[1146] | 61 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 62 | */ |
---|
| 63 | @Override |
---|
[1146] | 64 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 65 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 66 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
[1125] | 67 | } |
---|
| 68 | |
---|
| 69 | /* (non-Javadoc) |
---|
[1146] | 70 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 71 | */ |
---|
| 72 | @Override |
---|
[1146] | 73 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 74 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 75 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
[1125] | 76 | } |
---|
| 77 | |
---|
| 78 | /* (non-Javadoc) |
---|
[1146] | 79 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 80 | */ |
---|
| 81 | @Override |
---|
[1146] | 82 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 83 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 84 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
[1125] | 85 | } |
---|
| 86 | |
---|
| 87 | /* (non-Javadoc) |
---|
[1146] | 88 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 89 | */ |
---|
| 90 | @Override |
---|
[1146] | 91 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 92 | return getEquality(task1, task2, null); |
---|
[1125] | 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
[1154] | 96 | * <p> |
---|
| 97 | * compares two tasks with each other checking for the provided required level of |
---|
| 98 | * equality. One of the tasks must be a selection, the other one not. If this is not the |
---|
| 99 | * case, the method returns null. The returned equality level is at most lexical equality |
---|
| 100 | * as the selection can not be identical to something not being a selection. |
---|
| 101 | * </p> |
---|
[1125] | 102 | * |
---|
[1154] | 103 | * @param task1 the first task to be compared |
---|
| 104 | * @param task2 the second task to be compared |
---|
| 105 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 106 | * |
---|
| 107 | * @return the determined equality. |
---|
[1125] | 108 | */ |
---|
[1146] | 109 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
[816] | 110 | ISelection selection = null; |
---|
[1146] | 111 | ITask task = null; |
---|
[816] | 112 | |
---|
[1146] | 113 | if (task1 instanceof ISelection) { |
---|
| 114 | if (task2 instanceof ISelection) { |
---|
[1125] | 115 | // the rule is not responsible for two selections |
---|
[816] | 116 | return null; |
---|
| 117 | } |
---|
| 118 | |
---|
[1146] | 119 | selection = (ISelection) task1; |
---|
| 120 | task = task2; |
---|
[816] | 121 | } |
---|
[1146] | 122 | else if (task2 instanceof ISelection) { |
---|
| 123 | if (task1 instanceof ISelection) { |
---|
[1125] | 124 | // the rule is not responsible for two selections |
---|
[816] | 125 | return null; |
---|
| 126 | } |
---|
| 127 | |
---|
[1146] | 128 | selection = (ISelection) task2; |
---|
| 129 | task = task1; |
---|
[816] | 130 | } |
---|
| 131 | else { |
---|
| 132 | return null; |
---|
| 133 | } |
---|
| 134 | |
---|
[1146] | 135 | // now, that we found the selection and the task, lets compare the children of the selection |
---|
| 136 | // with the task. |
---|
| 137 | List<ITask> children = selection.getChildren(); |
---|
[1125] | 138 | |
---|
| 139 | if (children.size() < 1) { |
---|
[816] | 140 | return null; |
---|
| 141 | } |
---|
| 142 | |
---|
[1146] | 143 | TaskEquality mostConcreteNodeEquality = null; |
---|
[816] | 144 | |
---|
[1146] | 145 | for (ITask child : children) { |
---|
| 146 | TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel); |
---|
[816] | 147 | |
---|
[1146] | 148 | if (taskEquality != TaskEquality.UNEQUAL) { |
---|
[816] | 149 | if (mostConcreteNodeEquality == null) { |
---|
[1146] | 150 | mostConcreteNodeEquality = taskEquality; |
---|
[816] | 151 | } |
---|
[1146] | 152 | else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) { |
---|
| 153 | mostConcreteNodeEquality = taskEquality; |
---|
[1125] | 154 | |
---|
[816] | 155 | } |
---|
[1125] | 156 | |
---|
| 157 | if ((requiredEqualityLevel != null) && |
---|
| 158 | (mostConcreteNodeEquality.isAtLeast(requiredEqualityLevel))) |
---|
| 159 | { |
---|
| 160 | // if we found one child of the selection that is as equal as required, then |
---|
[1146] | 161 | // we can consider the selection to be sufficiently equal to the other task. |
---|
[1125] | 162 | // So we break up checking further children. |
---|
| 163 | break; |
---|
| 164 | } |
---|
[816] | 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
[1146] | 168 | // although the subtask may be identical to the task, we can not return identical, as |
---|
| 169 | // the selection is not identical to the task, but at most lexically equal |
---|
| 170 | if (mostConcreteNodeEquality == TaskEquality.IDENTICAL) { |
---|
| 171 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[816] | 172 | } |
---|
| 173 | else { |
---|
| 174 | return mostConcreteNodeEquality; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | } |
---|
[1125] | 178 | |
---|
| 179 | /** |
---|
| 180 | * <p> |
---|
[1154] | 181 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 182 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 183 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 184 | * on that level. |
---|
| 185 | * </p> |
---|
| 186 | * |
---|
| 187 | * @param child1 the first task to be compared |
---|
| 188 | * @param child2 the second task to be compared |
---|
| 189 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 190 | * |
---|
| 191 | * @return the determined equality |
---|
[1125] | 192 | */ |
---|
[1146] | 193 | private TaskEquality callRuleManager(ITask child1, |
---|
| 194 | ITask child2, |
---|
| 195 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 196 | { |
---|
| 197 | if (requiredEqualityLevel == null) { |
---|
| 198 | return mRuleManager.compare(child1, child2); |
---|
| 199 | } |
---|
| 200 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
| 201 | return requiredEqualityLevel; |
---|
| 202 | } |
---|
| 203 | else { |
---|
[1146] | 204 | return TaskEquality.UNEQUAL; |
---|
[1125] | 205 | } |
---|
| 206 | } |
---|
[816] | 207 | } |
---|