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