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