[1107] | 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; |
---|
[1107] | 16 | |
---|
[1127] | 17 | import java.util.HashMap; |
---|
| 18 | |
---|
[1146] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[1107] | 21 | import de.ugoe.cs.autoquest.usageprofiles.SymbolComparator; |
---|
[1129] | 22 | import de.ugoe.cs.util.StopWatch; |
---|
[1107] | 23 | |
---|
| 24 | /** |
---|
| 25 | * <p> |
---|
[1154] | 26 | * this class is a very fast implementation for the {@link SymbolComparator} interface to be |
---|
| 27 | * applied on task instances. To be efficient, it caches information about the equality of tasks. |
---|
| 28 | * Only, if it does not know a specific equality yet, it call the task equality rule manager |
---|
| 29 | * to compare two tasks. Otherwise, it returns the known equality. |
---|
[1107] | 30 | * </p> |
---|
| 31 | * |
---|
[1154] | 32 | * TODO improve documentation and remove stop watch stuff. |
---|
| 33 | * |
---|
[1107] | 34 | * @author Patrick Harms |
---|
| 35 | */ |
---|
[1146] | 36 | public class TaskComparator implements SymbolComparator<ITaskInstance> { |
---|
[1107] | 37 | |
---|
| 38 | /** |
---|
| 39 | * <p> |
---|
[1146] | 40 | * the task equality manager needed for comparing tasks with each other |
---|
[1107] | 41 | * </p> |
---|
| 42 | */ |
---|
[1146] | 43 | private TaskEqualityRuleManager taskEqualityRuleManager; |
---|
[1107] | 44 | |
---|
| 45 | /** |
---|
| 46 | * <p> |
---|
[1146] | 47 | * the minimal task equality two identified sublists need to have to consider them as equal |
---|
[1107] | 48 | * </p> |
---|
| 49 | */ |
---|
[1146] | 50 | private TaskEquality minimalNodeEquality; |
---|
[1107] | 51 | |
---|
[1154] | 52 | /** |
---|
| 53 | * <p> |
---|
| 54 | * the internally used comparer for tasks. |
---|
| 55 | * </p> |
---|
| 56 | */ |
---|
[1127] | 57 | private Comparer comparer; |
---|
[1107] | 58 | |
---|
[1154] | 59 | /** |
---|
| 60 | * <p> |
---|
| 61 | * the internally used comparer for tasks considering only lexical equality. |
---|
| 62 | * </p> |
---|
| 63 | */ |
---|
[1127] | 64 | private Comparer lexicalComparer; |
---|
[1107] | 65 | |
---|
[1146] | 66 | /** */ |
---|
[1127] | 67 | private StopWatch stopWatch = new StopWatch(); |
---|
| 68 | |
---|
[1146] | 69 | /** */ |
---|
[1127] | 70 | private HashMap<Long, Boolean> equalityBuffer = new HashMap<Long, Boolean>(); |
---|
| 71 | |
---|
[1146] | 72 | /** */ |
---|
[1127] | 73 | private HashMap<Long, Boolean> lexicalEqualityBuffer; |
---|
| 74 | |
---|
[1107] | 75 | /** |
---|
| 76 | * |
---|
| 77 | */ |
---|
[1146] | 78 | public TaskComparator(TaskEqualityRuleManager taskEqualityRuleManager, |
---|
| 79 | TaskEquality minimalNodeEquality) |
---|
[1107] | 80 | { |
---|
| 81 | super(); |
---|
[1146] | 82 | this.taskEqualityRuleManager = taskEqualityRuleManager; |
---|
[1107] | 83 | this.minimalNodeEquality = minimalNodeEquality; |
---|
[1127] | 84 | |
---|
[1146] | 85 | if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) { |
---|
[1127] | 86 | comparer = new LexicalComparer(); |
---|
| 87 | } |
---|
[1146] | 88 | else if (minimalNodeEquality == TaskEquality.SYNTACTICALLY_EQUAL) { |
---|
[1127] | 89 | comparer = new SyntacticalComparer(); |
---|
| 90 | } |
---|
[1146] | 91 | else if (minimalNodeEquality == TaskEquality.SEMANTICALLY_EQUAL) { |
---|
[1127] | 92 | comparer = new SemanticalComparer(); |
---|
| 93 | } |
---|
| 94 | else { |
---|
| 95 | comparer = new DefaultComparer(); |
---|
| 96 | } |
---|
| 97 | |
---|
[1146] | 98 | if (minimalNodeEquality == TaskEquality.LEXICALLY_EQUAL) { |
---|
[1127] | 99 | lexicalComparer = comparer; |
---|
| 100 | lexicalEqualityBuffer = equalityBuffer; |
---|
| 101 | } |
---|
| 102 | else { |
---|
| 103 | lexicalComparer = new LexicalComparer(); |
---|
| 104 | lexicalEqualityBuffer = new HashMap<Long, Boolean>(); |
---|
| 105 | } |
---|
[1107] | 106 | } |
---|
| 107 | |
---|
| 108 | /* (non-Javadoc) |
---|
| 109 | * @see de.ugoe.cs.autoquest.tasktrees.temporalrelation.SymbolComparator#equals(java.lang.Object, java.lang.Object) |
---|
| 110 | */ |
---|
| 111 | @Override |
---|
[1146] | 112 | public boolean equals(ITaskInstance taskInstance1, ITaskInstance taskInstance2) { |
---|
| 113 | return equals(taskInstance1.getTask(), taskInstance2.getTask()); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * |
---|
| 118 | */ |
---|
| 119 | public boolean equals(ITask task1, ITask task2) { |
---|
| 120 | //String id = "compare " + taskInstance1.getClass().getSimpleName() + " " + |
---|
| 121 | // taskInstance2.getClass().getSimpleName(); |
---|
[1127] | 122 | //String id = "compare"; |
---|
| 123 | //stopWatch.start(id); |
---|
| 124 | |
---|
| 125 | Boolean result; |
---|
| 126 | |
---|
[1146] | 127 | if (task1 != task2) { |
---|
| 128 | long key = ((long) System.identityHashCode(task1)) << 32; |
---|
| 129 | key += System.identityHashCode(task2); |
---|
[1127] | 130 | |
---|
| 131 | result = equalityBuffer.get(key); |
---|
| 132 | |
---|
| 133 | if (result == null) { |
---|
[1146] | 134 | result = comparer.compare(task1, task2); |
---|
[1127] | 135 | equalityBuffer.put(key, result); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | else { |
---|
| 139 | result = true; |
---|
| 140 | } |
---|
| 141 | //stopWatch.stop(id); |
---|
| 142 | |
---|
[1146] | 143 | /*boolean result2 = taskEqualityRuleManager.areAtLeastEqual(symbol1, symbol2, minimalNodeEquality); |
---|
[1127] | 144 | if (result != result2) { |
---|
| 145 | throw new IllegalStateException("implementation error"); |
---|
| 146 | }*/ |
---|
| 147 | |
---|
| 148 | return result; |
---|
[1107] | 149 | } |
---|
| 150 | |
---|
[1127] | 151 | /** |
---|
| 152 | * |
---|
| 153 | */ |
---|
[1146] | 154 | public boolean haveLexicallyEqualTasks(ITaskInstance taskInstance1, |
---|
| 155 | ITaskInstance taskInstance2) |
---|
| 156 | { |
---|
| 157 | return areLexicallyEqual(taskInstance1.getTask(), taskInstance2.getTask()); |
---|
[1127] | 158 | } |
---|
[1146] | 159 | |
---|
[1127] | 160 | |
---|
| 161 | /** |
---|
| 162 | * |
---|
| 163 | */ |
---|
[1146] | 164 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
[1127] | 165 | Boolean result; |
---|
| 166 | |
---|
[1146] | 167 | if (task1 != task2) { |
---|
| 168 | long key = ((long) System.identityHashCode(task1)) << 32; |
---|
| 169 | key += System.identityHashCode(task2); |
---|
[1127] | 170 | |
---|
| 171 | result = lexicalEqualityBuffer.get(key); |
---|
| 172 | |
---|
| 173 | if (result == null) { |
---|
[1146] | 174 | result = lexicalComparer.compare(task1, task2); |
---|
[1127] | 175 | lexicalEqualityBuffer.put(key, result); |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | else { |
---|
| 179 | result = true; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | return result; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * |
---|
| 187 | */ |
---|
[1146] | 188 | StopWatch getStopWatch() { |
---|
| 189 | return stopWatch; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /** |
---|
| 193 | * |
---|
| 194 | */ |
---|
| 195 | TaskEquality getConsideredNodeEquality() { |
---|
[1127] | 196 | return minimalNodeEquality; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * |
---|
| 201 | */ |
---|
| 202 | private interface Comparer { |
---|
| 203 | /** |
---|
| 204 | * |
---|
| 205 | */ |
---|
[1146] | 206 | boolean compare(ITask task1, ITask task2); |
---|
[1127] | 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * |
---|
| 211 | */ |
---|
| 212 | private class LexicalComparer implements Comparer { |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | * |
---|
| 216 | */ |
---|
[1146] | 217 | public boolean compare(ITask task1, ITask task2) { |
---|
| 218 | return taskEqualityRuleManager.areLexicallyEqual(task1, task2); |
---|
[1127] | 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * |
---|
| 224 | */ |
---|
| 225 | private class SyntacticalComparer implements Comparer { |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * |
---|
| 229 | */ |
---|
[1146] | 230 | public boolean compare(ITask task1, ITask task2) { |
---|
| 231 | return taskEqualityRuleManager.areSyntacticallyEqual(task1, task2); |
---|
[1127] | 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | /** |
---|
| 236 | * |
---|
| 237 | */ |
---|
| 238 | private class SemanticalComparer implements Comparer { |
---|
| 239 | |
---|
| 240 | /** |
---|
| 241 | * |
---|
| 242 | */ |
---|
[1146] | 243 | public boolean compare(ITask task1, ITask task2) { |
---|
| 244 | return taskEqualityRuleManager.areSemanticallyEqual(task1, task2); |
---|
[1127] | 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * |
---|
| 250 | */ |
---|
| 251 | private class DefaultComparer implements Comparer { |
---|
| 252 | |
---|
| 253 | /** |
---|
| 254 | * |
---|
| 255 | */ |
---|
[1146] | 256 | public boolean compare(ITask task1, ITask task2) { |
---|
| 257 | return taskEqualityRuleManager.areAtLeastEqual(task1, task2, minimalNodeEquality); |
---|
[1127] | 258 | } |
---|
| 259 | } |
---|
[1107] | 260 | } |
---|