[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 | |
---|
| 17 | import java.util.ArrayList; |
---|
| 18 | import java.util.List; |
---|
| 19 | |
---|
[1146] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[439] | 21 | |
---|
| 22 | /** |
---|
[557] | 23 | * <p> |
---|
[1146] | 24 | * The task equality rule manager is capable of comparing tasks based on its internal list |
---|
[1154] | 25 | * of comparison rules. These rules are asked for comparing the two provided tasks. If a rule |
---|
| 26 | * returns a task equality other than null, this equality is returned. Otherwise the next rule |
---|
| 27 | * is asked. |
---|
[557] | 28 | * </p> |
---|
[439] | 29 | * |
---|
| 30 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 31 | * @author 2012, last modified by $Author: patrick$ |
---|
| 32 | */ |
---|
[1146] | 33 | public class TaskEqualityRuleManager { |
---|
[1189] | 34 | |
---|
| 35 | /** |
---|
| 36 | * <p> |
---|
| 37 | * the singleton instance of this class |
---|
| 38 | * </p> |
---|
| 39 | */ |
---|
| 40 | private static final TaskEqualityRuleManager instance = new TaskEqualityRuleManager(); |
---|
[439] | 41 | |
---|
[1154] | 42 | /** |
---|
| 43 | * <p> |
---|
| 44 | * the rules that can be used for comparing tasks |
---|
| 45 | * </p> |
---|
| 46 | */ |
---|
[1146] | 47 | private List<TaskComparisonRule> mRuleIndex = null; |
---|
[439] | 48 | |
---|
[557] | 49 | /** |
---|
| 50 | * <p> |
---|
[1146] | 51 | * initializes the task equality rule manager by filling the internal list of comparison rules. |
---|
[557] | 52 | * </p> |
---|
| 53 | */ |
---|
[1189] | 54 | private TaskEqualityRuleManager() { |
---|
[1146] | 55 | mRuleIndex = new ArrayList<TaskComparisonRule>(); |
---|
| 56 | mRuleIndex.add(new TaskIdentityRule()); |
---|
[807] | 57 | mRuleIndex.add(new GUIEventTaskComparisonRule()); |
---|
| 58 | mRuleIndex.add(new EventTaskComparisonRule()); |
---|
[557] | 59 | mRuleIndex.add(new IterationComparisonRule(this)); |
---|
| 60 | mRuleIndex.add(new SequenceComparisonRule(this)); |
---|
| 61 | mRuleIndex.add(new SelectionComparisonRule(this)); |
---|
[1146] | 62 | mRuleIndex.add(new TaskAndIterationComparisonRule(this)); |
---|
| 63 | mRuleIndex.add(new TaskAndSelectionComparisonRule(this)); |
---|
[557] | 64 | } |
---|
[439] | 65 | |
---|
[1189] | 66 | |
---|
[557] | 67 | /** |
---|
| 68 | * <p> |
---|
[1189] | 69 | * returns the singleton instance of this class |
---|
| 70 | * </p> |
---|
| 71 | * |
---|
| 72 | * @return as described |
---|
| 73 | */ |
---|
| 74 | public static TaskEqualityRuleManager getInstance() { |
---|
| 75 | return instance; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * <p> |
---|
[1146] | 80 | * this method performs a comparison of the two provided tasks. It iterates its internal |
---|
| 81 | * comparison rules. If the first rule returns a task equality other than null, |
---|
[557] | 82 | * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality |
---|
| 83 | * <code>NodeEquality.UNEQUAL</code> is returned. |
---|
| 84 | * </p> |
---|
| 85 | * |
---|
[1146] | 86 | * @param task1 the first task to be compared |
---|
| 87 | * @param task2 the second task to be compared |
---|
[557] | 88 | * |
---|
| 89 | * @return as described |
---|
| 90 | * |
---|
| 91 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 92 | * manager before a call to this method. |
---|
| 93 | */ |
---|
[1146] | 94 | public TaskEquality compare(ITask task1, ITask task2) |
---|
[557] | 95 | throws IllegalStateException |
---|
[439] | 96 | { |
---|
[557] | 97 | if (mRuleIndex == null) { |
---|
| 98 | throw new IllegalStateException("not initialized"); |
---|
| 99 | } |
---|
| 100 | |
---|
[1146] | 101 | // LOG.info("checking for equality of " + task1 + " and " + task2); |
---|
| 102 | TaskEquality taskEquality = null; |
---|
[557] | 103 | |
---|
[1146] | 104 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
| 105 | if (rule.isApplicable(task1, task2)) { |
---|
| 106 | taskEquality = rule.compare(task1, task2); |
---|
| 107 | if (taskEquality != null) { |
---|
[1125] | 108 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
[1146] | 109 | return taskEquality; |
---|
[1125] | 110 | } |
---|
[557] | 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
[1146] | 114 | // LOG.warning("no rule could be applied --> handling tasks as unequal"); |
---|
[557] | 115 | |
---|
[1146] | 116 | return TaskEquality.UNEQUAL; |
---|
[439] | 117 | } |
---|
| 118 | |
---|
[1125] | 119 | /** |
---|
| 120 | * <p> |
---|
[1154] | 121 | * this method two tasks with respect to the fiven equality level and returns true, if this |
---|
| 122 | * level is given. |
---|
[1125] | 123 | * </p> |
---|
[1154] | 124 | * |
---|
| 125 | * @param task1 the first task to be compared |
---|
| 126 | * @param task2 the second task to be compared |
---|
| 127 | * @param equalityLevel the level of equality to be checked for |
---|
| 128 | * |
---|
| 129 | * @return as described |
---|
| 130 | * |
---|
| 131 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 132 | * manager before a call to this method. |
---|
[1125] | 133 | */ |
---|
[1146] | 134 | public boolean areAtLeastEqual(ITask task1, ITask task2, TaskEquality equalityLevel) { |
---|
[1125] | 135 | if (equalityLevel == null) { |
---|
| 136 | throw new IllegalArgumentException("required equality level must not be null"); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | switch (equalityLevel) { |
---|
| 140 | case IDENTICAL: |
---|
[1146] | 141 | return areIdentical(task1, task2); |
---|
[1125] | 142 | case LEXICALLY_EQUAL: |
---|
[1146] | 143 | return areLexicallyEqual(task1, task2); |
---|
[1125] | 144 | case SYNTACTICALLY_EQUAL: |
---|
[1146] | 145 | return areSyntacticallyEqual(task1, task2); |
---|
[1125] | 146 | case SEMANTICALLY_EQUAL: |
---|
[1146] | 147 | return areSemanticallyEqual(task1, task2); |
---|
[1125] | 148 | case UNEQUAL: |
---|
[1146] | 149 | return !areSemanticallyEqual(task1, task2); |
---|
[1125] | 150 | default: |
---|
| 151 | throw new IllegalArgumentException("unknown required equality: " + equalityLevel); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * <p> |
---|
[1154] | 157 | * this method checks if the two given tasks are identical. For this, it iterates its internal |
---|
| 158 | * comparison rules. If the first rule returns true, than this method returns true as well. |
---|
| 159 | * If no rule returns true, this method returns false. |
---|
[1125] | 160 | * </p> |
---|
[1154] | 161 | * |
---|
| 162 | * @param task1 the first task to be compared |
---|
| 163 | * @param task2 the second task to be compared |
---|
| 164 | * |
---|
| 165 | * @return as described |
---|
| 166 | * |
---|
| 167 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 168 | * manager before a call to this method. |
---|
[1125] | 169 | */ |
---|
[1146] | 170 | public boolean areIdentical(ITask task1, ITask task2) { |
---|
[1125] | 171 | if (mRuleIndex == null) { |
---|
| 172 | throw new IllegalStateException("not initialized"); |
---|
| 173 | } |
---|
| 174 | |
---|
[1146] | 175 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
| 176 | if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) { |
---|
[1125] | 177 | return true; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | return false; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * <p> |
---|
[1154] | 186 | * this method checks if the two given tasks are lexically equal. For this, it iterates its |
---|
| 187 | * internal comparison rules. If the first rule returns true, than this method returns true |
---|
| 188 | * as well. If no rule returns true, this method returns false. |
---|
[1125] | 189 | * </p> |
---|
[1154] | 190 | * |
---|
| 191 | * @param task1 the first task to be compared |
---|
| 192 | * @param task2 the second task to be compared |
---|
| 193 | * |
---|
| 194 | * @return as described |
---|
| 195 | * |
---|
| 196 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 197 | * manager before a call to this method. |
---|
[1125] | 198 | */ |
---|
[1146] | 199 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
[1125] | 200 | if (mRuleIndex == null) { |
---|
| 201 | throw new IllegalStateException("not initialized"); |
---|
| 202 | } |
---|
| 203 | |
---|
[1146] | 204 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
| 205 | if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) { |
---|
[1125] | 206 | return true; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | return false; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * <p> |
---|
[1154] | 215 | * this method checks if the two given tasks are syntactically equal. For this, it iterates its |
---|
| 216 | * internal comparison rules. If the first rule returns true, than this method returns true |
---|
| 217 | * as well. If no rule returns true, this method returns false. |
---|
[1125] | 218 | * </p> |
---|
[1154] | 219 | * |
---|
| 220 | * @param task1 the first task to be compared |
---|
| 221 | * @param task2 the second task to be compared |
---|
| 222 | * |
---|
| 223 | * @return as described |
---|
| 224 | * |
---|
| 225 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 226 | * manager before a call to this method. |
---|
[1125] | 227 | */ |
---|
[1146] | 228 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
[1125] | 229 | if (mRuleIndex == null) { |
---|
| 230 | throw new IllegalStateException("not initialized"); |
---|
| 231 | } |
---|
| 232 | |
---|
[1146] | 233 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
| 234 | if (rule.isApplicable(task1, task2) && rule.areSyntacticallyEqual(task1, task2)) { |
---|
[1125] | 235 | return true; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | return false; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
| 243 | * <p> |
---|
[1154] | 244 | * this method checks if the two given tasks are semantically equal. For this, it iterates its |
---|
| 245 | * internal comparison rules. If the first rule returns true, than this method returns true |
---|
| 246 | * as well. If no rule returns true, this method returns false. |
---|
[1125] | 247 | * </p> |
---|
[1154] | 248 | * |
---|
| 249 | * @param task1 the first task to be compared |
---|
| 250 | * @param task2 the second task to be compared |
---|
| 251 | * |
---|
| 252 | * @return as described |
---|
| 253 | * |
---|
| 254 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
| 255 | * manager before a call to this method. |
---|
[1125] | 256 | */ |
---|
[1146] | 257 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
[1125] | 258 | if (mRuleIndex == null) { |
---|
| 259 | throw new IllegalStateException("not initialized"); |
---|
| 260 | } |
---|
| 261 | |
---|
[1146] | 262 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
| 263 | if (rule.isApplicable(task1, task2) && rule.areSemanticallyEqual(task1, task2)) { |
---|
[1125] | 264 | return true; |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | return false; |
---|
| 269 | } |
---|
| 270 | |
---|
[439] | 271 | } |
---|