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