[1256] | 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 | |
---|
| 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
| 16 | |
---|
| 17 | import java.io.IOException; |
---|
| 18 | import java.io.ObjectInputStream; |
---|
| 19 | import java.util.HashMap; |
---|
| 20 | |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEqualityRuleManager; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 25 | import de.ugoe.cs.autoquest.usageprofiles.SymbolComparator; |
---|
| 26 | |
---|
| 27 | /** |
---|
[1401] | 28 | * <p> |
---|
| 29 | * implementation of a symbol comparator for task instances. Internally, it uses comparison buffers |
---|
| 30 | * to prevent comparing two tasks or task instances several times. It internally instantiates |
---|
| 31 | * comparers being the implementation strategy of the comparisons required for a specific level |
---|
| 32 | * of task equality. The comparers internally use the {@link TaskEqualityRuleManager} for |
---|
| 33 | * performing comparisons. |
---|
| 34 | * </p> |
---|
[1256] | 35 | */ |
---|
[1393] | 36 | public class TaskInstanceComparator implements SymbolComparator<ITaskInstance> { |
---|
[1256] | 37 | |
---|
| 38 | /** */ |
---|
| 39 | private static final long serialVersionUID = 1L; |
---|
| 40 | |
---|
[1401] | 41 | /** |
---|
| 42 | * the maximum size of the internal buffer used for storing comparison results |
---|
| 43 | */ |
---|
[1285] | 44 | private static final int MAX_BUFFER_SIZE = 2 * 1024 * 1024; |
---|
[1256] | 45 | |
---|
[1401] | 46 | /** |
---|
| 47 | * the considered level of task equality |
---|
| 48 | */ |
---|
| 49 | private TaskEquality minimalTaskEquality; |
---|
[1256] | 50 | |
---|
[1401] | 51 | /** |
---|
| 52 | * the comparer used internally for comparing two tasks |
---|
| 53 | */ |
---|
[1256] | 54 | private transient Comparer comparer; |
---|
| 55 | |
---|
[1401] | 56 | /** |
---|
| 57 | * the comparer used for comparing two tasks on the lexical level |
---|
| 58 | */ |
---|
[1256] | 59 | private transient Comparer lexicalComparer; |
---|
| 60 | |
---|
[1401] | 61 | /** |
---|
| 62 | * internal buffer used for storing comparison results |
---|
| 63 | */ |
---|
[1256] | 64 | private transient HashMap<Long, Boolean> equalityBuffer = new HashMap<Long, Boolean>(); |
---|
| 65 | |
---|
[1401] | 66 | /** |
---|
| 67 | * internal buffer used for storing comparison results only for lexical comparisons |
---|
| 68 | */ |
---|
[1256] | 69 | private transient HashMap<Long, Boolean> lexicalEqualityBuffer; |
---|
| 70 | |
---|
| 71 | /** |
---|
[1401] | 72 | * <p> |
---|
| 73 | * initializes the comparator with a considered task equality level |
---|
| 74 | * </p> |
---|
| 75 | * |
---|
| 76 | * @param minimalTaskEquality the considered task equality level |
---|
[1256] | 77 | */ |
---|
[1401] | 78 | public TaskInstanceComparator(TaskEquality minimalTaskEquality) { |
---|
| 79 | this.minimalTaskEquality = minimalTaskEquality; |
---|
[1256] | 80 | init(); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /* (non-Javadoc) |
---|
| 84 | * @see SymbolComparator#equals(Object, Object) |
---|
| 85 | */ |
---|
| 86 | @Override |
---|
| 87 | public boolean equals(ITaskInstance taskInstance1, ITaskInstance taskInstance2) { |
---|
| 88 | return equals(taskInstance1.getTask(), taskInstance2.getTask()); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
[1401] | 92 | * <p> |
---|
| 93 | * returns true, if this comparator considers the provided tasks as equal, false else |
---|
| 94 | * </p> |
---|
[1256] | 95 | * |
---|
[1401] | 96 | * @param task1 the first task to compare |
---|
| 97 | * @param task2 the second task to compare |
---|
| 98 | * |
---|
| 99 | * @return as described |
---|
[1256] | 100 | */ |
---|
| 101 | public boolean equals(ITask task1, ITask task2) { |
---|
| 102 | Boolean result; |
---|
| 103 | |
---|
| 104 | if (task1 != task2) { |
---|
[1285] | 105 | //if ((task1 instanceof IEventTask) && (task2 instanceof IEventTask)) { |
---|
[1256] | 106 | long key = ((long) System.identityHashCode(task1)) << 32; |
---|
| 107 | key += System.identityHashCode(task2); |
---|
| 108 | |
---|
| 109 | result = equalityBuffer.get(key); |
---|
| 110 | |
---|
| 111 | if (result == null) { |
---|
| 112 | result = comparer.compare(task1, task2); |
---|
| 113 | |
---|
| 114 | if (equalityBuffer.size() < MAX_BUFFER_SIZE) { |
---|
| 115 | equalityBuffer.put(key, result); |
---|
| 116 | } |
---|
| 117 | } |
---|
[1285] | 118 | /*} |
---|
[1256] | 119 | else { |
---|
| 120 | result = false; |
---|
[1285] | 121 | }*/ |
---|
[1256] | 122 | } |
---|
| 123 | else { |
---|
| 124 | result = true; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | return result; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
[1401] | 131 | * <p> |
---|
| 132 | * returns true, if this comparator considers the provided tasks as lexically equal, false else |
---|
| 133 | * </p> |
---|
| 134 | * |
---|
| 135 | * @param task1 the first task to compare |
---|
| 136 | * @param task2 the second task to compare |
---|
| 137 | * |
---|
| 138 | * @return as described |
---|
[1256] | 139 | */ |
---|
| 140 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 141 | Boolean result; |
---|
| 142 | |
---|
| 143 | if (task1 != task2) { |
---|
| 144 | long key = ((long) System.identityHashCode(task1)) << 32; |
---|
| 145 | key += System.identityHashCode(task2); |
---|
| 146 | |
---|
| 147 | result = lexicalEqualityBuffer.get(key); |
---|
| 148 | |
---|
| 149 | if (result == null) { |
---|
| 150 | result = lexicalComparer.compare(task1, task2); |
---|
[1285] | 151 | if (equalityBuffer.size() < MAX_BUFFER_SIZE) { |
---|
[1256] | 152 | lexicalEqualityBuffer.put(key, result); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | else { |
---|
| 157 | result = true; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | return result; |
---|
| 161 | } |
---|
| 162 | |
---|
[1285] | 163 | /** |
---|
| 164 | * <p> |
---|
[1401] | 165 | * can be called externally to clear the internal comparison buffers |
---|
[1285] | 166 | * </p> |
---|
[1256] | 167 | */ |
---|
[1394] | 168 | public void clearBuffers() { |
---|
[1285] | 169 | equalityBuffer.clear(); |
---|
| 170 | init(); |
---|
[1256] | 171 | } |
---|
[1285] | 172 | |
---|
[1256] | 173 | /** |
---|
[1401] | 174 | * <p> |
---|
| 175 | * initializes the comparator with comparers depending on the different comparison levels as |
---|
| 176 | * well as with the required comparison buffers. Comparers and buffers for lexical comparison |
---|
| 177 | * may be reused if the considered equality level is also lexical. |
---|
| 178 | * </p> |
---|
[1256] | 179 | */ |
---|
| 180 | private void init() { |
---|
[1401] | 181 | if (minimalTaskEquality == TaskEquality.LEXICALLY_EQUAL) { |
---|
[1256] | 182 | comparer = new LexicalComparer(); |
---|
| 183 | } |
---|
[1401] | 184 | else if (minimalTaskEquality == TaskEquality.SYNTACTICALLY_EQUAL) { |
---|
[1256] | 185 | comparer = new SyntacticalComparer(); |
---|
| 186 | } |
---|
[1401] | 187 | else if (minimalTaskEquality == TaskEquality.SEMANTICALLY_EQUAL) { |
---|
[1256] | 188 | comparer = new SemanticalComparer(); |
---|
| 189 | } |
---|
| 190 | else { |
---|
[1401] | 191 | comparer = new DefaultComparer(this.minimalTaskEquality); |
---|
[1256] | 192 | } |
---|
| 193 | |
---|
[1401] | 194 | if (minimalTaskEquality == TaskEquality.LEXICALLY_EQUAL) { |
---|
[1256] | 195 | lexicalComparer = comparer; |
---|
| 196 | lexicalEqualityBuffer = equalityBuffer; |
---|
| 197 | } |
---|
| 198 | else { |
---|
| 199 | lexicalComparer = new LexicalComparer(); |
---|
| 200 | lexicalEqualityBuffer = new HashMap<Long, Boolean>(); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
| 205 | * <p> |
---|
| 206 | * deserialize this object and reinitialize the buffers |
---|
| 207 | * </p> |
---|
| 208 | */ |
---|
| 209 | private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { |
---|
| 210 | in.defaultReadObject(); |
---|
| 211 | init(); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | /** |
---|
[1401] | 216 | * <p> |
---|
| 217 | * interface for internally used comparers containing only a compare method |
---|
| 218 | * </p> |
---|
[1256] | 219 | */ |
---|
| 220 | private static interface Comparer { |
---|
| 221 | |
---|
| 222 | /** |
---|
[1401] | 223 | * <p> |
---|
| 224 | * returns true, if this comparator considers the provided tasks as equal, false else |
---|
| 225 | * </p> |
---|
[1256] | 226 | * |
---|
[1401] | 227 | * @param task1 the first task to compare |
---|
| 228 | * @param task2 the second task to compare |
---|
| 229 | * |
---|
| 230 | * @return as described |
---|
[1256] | 231 | */ |
---|
| 232 | boolean compare(ITask task1, ITask task2); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
[1401] | 236 | * <p> |
---|
| 237 | * comparer that performs comparisons only on the lexical level |
---|
| 238 | * </p> |
---|
[1256] | 239 | */ |
---|
| 240 | private static class LexicalComparer implements Comparer { |
---|
| 241 | |
---|
[1401] | 242 | /* (non-Javadoc) |
---|
| 243 | * @see Comparer#compare(ITask, ITask) |
---|
[1256] | 244 | */ |
---|
| 245 | public boolean compare(ITask task1, ITask task2) { |
---|
| 246 | return TaskEqualityRuleManager.getInstance().areLexicallyEqual(task1, task2); |
---|
| 247 | } |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
[1401] | 251 | * <p> |
---|
| 252 | * comparer that performs comparisons only on the syntactical level |
---|
| 253 | * </p> |
---|
[1256] | 254 | * |
---|
| 255 | */ |
---|
| 256 | private static class SyntacticalComparer implements Comparer { |
---|
| 257 | |
---|
[1401] | 258 | /* (non-Javadoc) |
---|
| 259 | * @see Comparer#compare(ITask, ITask) |
---|
[1256] | 260 | */ |
---|
| 261 | public boolean compare(ITask task1, ITask task2) { |
---|
| 262 | return TaskEqualityRuleManager.getInstance().areSyntacticallyEqual(task1, task2); |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /** |
---|
[1401] | 267 | * <p> |
---|
| 268 | * comparer that performs comparisons only on the semantical level |
---|
| 269 | * </p> |
---|
[1256] | 270 | */ |
---|
| 271 | private static class SemanticalComparer implements Comparer { |
---|
| 272 | |
---|
[1401] | 273 | /* (non-Javadoc) |
---|
| 274 | * @see Comparer#compare(ITask, ITask) |
---|
[1256] | 275 | */ |
---|
| 276 | public boolean compare(ITask task1, ITask task2) { |
---|
| 277 | return TaskEqualityRuleManager.getInstance().areSemanticallyEqual(task1, task2); |
---|
| 278 | } |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | /** |
---|
[1401] | 282 | * <p> |
---|
| 283 | * comparer that performs comparisons only on the provided level |
---|
| 284 | * </p> |
---|
[1256] | 285 | */ |
---|
| 286 | private static class DefaultComparer implements Comparer { |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | * <p> |
---|
[1401] | 290 | * the minimal task equality considered by this comparer |
---|
[1256] | 291 | * </p> |
---|
| 292 | */ |
---|
[1401] | 293 | private TaskEquality minimalTaskEquality; |
---|
[1256] | 294 | |
---|
| 295 | /** |
---|
[1401] | 296 | * <p> |
---|
| 297 | * initializes this comparer with the task equality to be considered |
---|
| 298 | * </p> |
---|
[1256] | 299 | */ |
---|
[1401] | 300 | public DefaultComparer(TaskEquality minimalTaskEquality) { |
---|
| 301 | this.minimalTaskEquality = minimalTaskEquality; |
---|
[1256] | 302 | } |
---|
| 303 | |
---|
[1401] | 304 | /* (non-Javadoc) |
---|
| 305 | * @see Comparer#compare(ITask, ITask) |
---|
[1256] | 306 | */ |
---|
| 307 | public boolean compare(ITask task1, ITask task2) { |
---|
| 308 | return TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
[1401] | 309 | (task1, task2, minimalTaskEquality); |
---|
[1256] | 310 | } |
---|
| 311 | } |
---|
[1285] | 312 | |
---|
[1256] | 313 | } |
---|