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