1 | /* |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; |
---|
5 | |
---|
6 | import java.io.Serializable; |
---|
7 | import java.util.Collection; |
---|
8 | import java.util.HashMap; |
---|
9 | import java.util.Iterator; |
---|
10 | import java.util.LinkedList; |
---|
11 | import java.util.logging.Level; |
---|
12 | |
---|
13 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
14 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentHelpers; |
---|
15 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Constants; |
---|
16 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
20 | import de.ugoe.cs.util.console.Console; |
---|
21 | |
---|
22 | // TODO: Auto-generated Javadoc |
---|
23 | /** |
---|
24 | * The Class ObjectDistanceSubstitionMatrix. |
---|
25 | */ |
---|
26 | public class ObjectDistanceSubstitionMatrix implements SubstitutionMatrix, |
---|
27 | Serializable { |
---|
28 | |
---|
29 | /** The Constant serialVersionUID. */ |
---|
30 | private static final long serialVersionUID = -4253258274617754083L; |
---|
31 | |
---|
32 | /** The idmapping. */ |
---|
33 | private final HashMap<Integer, Integer> idmapping; |
---|
34 | |
---|
35 | /** The matrix. */ |
---|
36 | private ITriangleMatrix matrix; |
---|
37 | |
---|
38 | /** The unique tasks. */ |
---|
39 | private Collection<ITask> uniqueTasks; |
---|
40 | |
---|
41 | /** The gap penalty. */ |
---|
42 | private float gapPenalty; |
---|
43 | |
---|
44 | /** The index. */ |
---|
45 | private int index = 0; |
---|
46 | |
---|
47 | /** The etis of tasks. */ |
---|
48 | private final HashMap<Integer, LinkedList<IEventTaskInstance>> etisOfTasks; |
---|
49 | |
---|
50 | /** The calculate non task instances. */ |
---|
51 | private boolean calculateNonEventTaskInstances = false; |
---|
52 | |
---|
53 | /** The first round max index. */ |
---|
54 | private int firstRoundMaxIndex = 0; |
---|
55 | |
---|
56 | /** The positive threshold. */ |
---|
57 | private final double positiveThreshold; |
---|
58 | |
---|
59 | /** |
---|
60 | * Instantiates a new object distance substition matrix. |
---|
61 | * |
---|
62 | * @param positiveThreshold the positive threshold |
---|
63 | * @param gapPenalty the gap penalty |
---|
64 | * @param calculateNonTaskInstances the calculate non task instances |
---|
65 | */ |
---|
66 | public ObjectDistanceSubstitionMatrix(float positiveThreshold, |
---|
67 | int gapPenalty, boolean calculateNonTaskInstances) { |
---|
68 | this.positiveThreshold = positiveThreshold; |
---|
69 | idmapping = new HashMap<Integer, Integer>(); |
---|
70 | etisOfTasks = new HashMap<Integer, LinkedList<IEventTaskInstance>>(); |
---|
71 | this.gapPenalty = gapPenalty; |
---|
72 | this.calculateNonEventTaskInstances = calculateNonTaskInstances; |
---|
73 | |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Compute distance. |
---|
78 | * |
---|
79 | * @param task1 the task1 |
---|
80 | * @param task2 the task2 |
---|
81 | */ |
---|
82 | private void computeDistance(ITask task1, ITask task2) { |
---|
83 | int index1 = -1; |
---|
84 | int index2 = -1; |
---|
85 | float distance = 0; |
---|
86 | ITaskInstance ti1 = null; |
---|
87 | ITaskInstance ti2 = null; |
---|
88 | // We just need to the first instance here |
---|
89 | if (task1.getInstances().size() > 0) { |
---|
90 | ti1 = task1.getInstances().iterator().next(); |
---|
91 | } |
---|
92 | if (task2.getInstances().size() > 0) { |
---|
93 | ti2 = task2.getInstances().iterator().next(); |
---|
94 | } |
---|
95 | IEventTaskInstance eti1 = null; |
---|
96 | IEventTaskInstance eti2 = null; |
---|
97 | |
---|
98 | if ((ti1 instanceof IEventTaskInstance) |
---|
99 | && (ti2 instanceof IEventTaskInstance)) { |
---|
100 | eti1 = (IEventTaskInstance) ti1; |
---|
101 | index1 = getIndex(eti1); |
---|
102 | eti2 = (IEventTaskInstance) ti2; |
---|
103 | index2 = getIndex(eti2); |
---|
104 | distance = distanceBetweenInstances(eti1, eti2); |
---|
105 | } else if ((ti1 instanceof IEventTaskInstance) |
---|
106 | && !(ti2 instanceof IEventTaskInstance)) { |
---|
107 | task1 = ti1.getTask(); |
---|
108 | index2 = getIndex(task2); |
---|
109 | eti1 = (IEventTaskInstance) ti1; |
---|
110 | index1 = getIndex(eti1); |
---|
111 | distance = distanceBetweenTaskAndInstance(task2, eti1)-3; |
---|
112 | } else if (!(ti1 instanceof IEventTaskInstance) |
---|
113 | && (ti2 instanceof IEventTaskInstance)) { |
---|
114 | index1 = getIndex(task1); |
---|
115 | eti2 = (IEventTaskInstance) ti2; |
---|
116 | index2 = getIndex(eti2); |
---|
117 | distance = distanceBetweenTaskAndInstance(task1, eti2); |
---|
118 | } else if (!(ti1 instanceof IEventTaskInstance) |
---|
119 | && !(ti2 instanceof IEventTaskInstance)) { |
---|
120 | index1 = getIndex(task1); |
---|
121 | index2 = getIndex(task2); |
---|
122 | distance = distanceBetweenTasks(task1, task2)-3; |
---|
123 | } else { |
---|
124 | System.out.println("Unknown error"); |
---|
125 | } |
---|
126 | matrix.set(index1, index2, distance); |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Distance between instances. |
---|
131 | * |
---|
132 | * @param eti1 the eti1 |
---|
133 | * @param eti2 the eti2 |
---|
134 | * @return the float |
---|
135 | */ |
---|
136 | private float distanceBetweenInstances(IEventTaskInstance eti1, |
---|
137 | IEventTaskInstance eti2) { |
---|
138 | final IGUIElement first = (IGUIElement) eti1.getEvent().getTarget(); |
---|
139 | final IGUIElement second = (IGUIElement) eti2.getEvent().getTarget(); |
---|
140 | float distance = -1 * AlignmentHelpers.distanceBetween(first, second); |
---|
141 | distance += positiveThreshold; |
---|
142 | return distance; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Distance between task and instance. |
---|
147 | * |
---|
148 | * @param task1 the task1 |
---|
149 | * @param eti1 the eti1 |
---|
150 | * @return the float |
---|
151 | */ |
---|
152 | private float distanceBetweenTaskAndInstance(ITask task1, |
---|
153 | IEventTaskInstance eti1) { |
---|
154 | if (this.calculateNonEventTaskInstances) { |
---|
155 | float tmpDistance = 0; |
---|
156 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
157 | task1.accept(etlg); |
---|
158 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
159 | .getEventlist(); |
---|
160 | |
---|
161 | for (final Iterator<IEventTaskInstance> it = eventTaskInstances |
---|
162 | .iterator(); it.hasNext();) { |
---|
163 | final IEventTaskInstance eti2 = it.next(); |
---|
164 | // int taskId1 = eti1.getTask().getId(); |
---|
165 | // int taskId2 = eti2.getTask().getId(); |
---|
166 | // if (scoreExists(taskId1, taskId2)) { |
---|
167 | // tmpDistance += getScore(taskId1, taskId2); |
---|
168 | // } else { |
---|
169 | final float dist = distanceBetweenInstances(eti1, eti2); |
---|
170 | matrix.set(getIndex(eti1), getIndex(eti2), dist); |
---|
171 | tmpDistance += dist; |
---|
172 | // } |
---|
173 | } |
---|
174 | return tmpDistance / eventTaskInstances.size(); |
---|
175 | } else { |
---|
176 | return 0; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * Distance between tasks. |
---|
182 | * |
---|
183 | * @param task1 the task1 |
---|
184 | * @param task2 the task2 |
---|
185 | * @return the float |
---|
186 | */ |
---|
187 | private float distanceBetweenTasks(ITask task1, ITask task2) { |
---|
188 | if (this.calculateNonEventTaskInstances) { |
---|
189 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
190 | task1.accept(etlg); |
---|
191 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
192 | .getEventlist(); |
---|
193 | |
---|
194 | float tmpDistance = 0; |
---|
195 | for (final Iterator<IEventTaskInstance> it = eventTaskInstances |
---|
196 | .iterator(); it.hasNext();) { |
---|
197 | final IEventTaskInstance eti1 = it.next(); |
---|
198 | tmpDistance += distanceBetweenTaskAndInstance(task2, eti1); |
---|
199 | } |
---|
200 | |
---|
201 | return tmpDistance / eventTaskInstances.size(); |
---|
202 | } else { |
---|
203 | return 0; |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | /* (non-Javadoc) |
---|
208 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#generate(java.util.HashSet) |
---|
209 | */ |
---|
210 | @Override |
---|
211 | public void generate(Collection<ITask> uniqueTasks) { |
---|
212 | this.uniqueTasks = uniqueTasks; |
---|
213 | if (this.calculateNonEventTaskInstances) { |
---|
214 | matrix = new PreallocatedDynamicTriangleMatrix(uniqueTasks.size() + 1); |
---|
215 | Console.traceln(Level.INFO, "searching EventTasks in Tasks"); |
---|
216 | //searchEventTaskInstances(); |
---|
217 | } else { |
---|
218 | matrix = new StaticTriangleMatrix(uniqueTasks.size() + 1); |
---|
219 | } |
---|
220 | matrix.initialize(0); |
---|
221 | |
---|
222 | //int count = 0; |
---|
223 | //final int size = uniqueTasks.size(); |
---|
224 | for (final Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { |
---|
225 | final ITask task1 = it.next(); |
---|
226 | //count++; |
---|
227 | //if (((count % (size / 100)) == 0)) { |
---|
228 | // Console.traceln(Level.INFO, |
---|
229 | // (Math.round(((float) count / size) * 100)) + "%"); |
---|
230 | //} |
---|
231 | for (final Iterator<ITask> jt = uniqueTasks.iterator(); jt |
---|
232 | .hasNext();) { |
---|
233 | final ITask task2 = jt.next(); |
---|
234 | computeDistance(task1, task2); |
---|
235 | } |
---|
236 | } |
---|
237 | this.firstRoundMaxIndex = index; |
---|
238 | } |
---|
239 | |
---|
240 | /* (non-Javadoc) |
---|
241 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#getGapPenalty() |
---|
242 | */ |
---|
243 | @Override |
---|
244 | public float getGapPenalty() { |
---|
245 | return gapPenalty; |
---|
246 | } |
---|
247 | |
---|
248 | /** |
---|
249 | * Gets the index. |
---|
250 | * |
---|
251 | * @param eti the eti |
---|
252 | * @return the index |
---|
253 | */ |
---|
254 | synchronized private int getIndex(IEventTaskInstance eti) { |
---|
255 | int tempindex = -1; |
---|
256 | if (!idmapping.containsKey(eti.getTask().getId())) { |
---|
257 | idmapping.put(eti.getTask().getId(), index); |
---|
258 | tempindex = index; |
---|
259 | index++; |
---|
260 | } else { |
---|
261 | tempindex = idmapping.get(eti.getTask().getId()); |
---|
262 | } |
---|
263 | return tempindex; |
---|
264 | } |
---|
265 | |
---|
266 | /** |
---|
267 | * Gets the index. |
---|
268 | * |
---|
269 | * @param task the task |
---|
270 | * @return the index |
---|
271 | */ |
---|
272 | synchronized private int getIndex(ITask task) { |
---|
273 | int tempindex = -1; |
---|
274 | |
---|
275 | if (!idmapping.containsKey(task.getId())) { |
---|
276 | |
---|
277 | idmapping.put(task.getId(), index); |
---|
278 | tempindex = index; |
---|
279 | index++; |
---|
280 | } else { |
---|
281 | tempindex = idmapping.get(task.getId()); |
---|
282 | } |
---|
283 | |
---|
284 | return tempindex; |
---|
285 | } |
---|
286 | |
---|
287 | // public boolean scoreExists(int id, int id2) { |
---|
288 | // return idmapping.containsKey(id) && idmapping.containsKey(id2); |
---|
289 | // return false; |
---|
290 | // } |
---|
291 | |
---|
292 | /* (non-Javadoc) |
---|
293 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#getScore(int, int) |
---|
294 | */ |
---|
295 | @Override |
---|
296 | public float getScore(int taskId1, int taskId2) { |
---|
297 | if ((taskId1 == Constants.GAP_SYMBOL) |
---|
298 | || (taskId1 == Constants.UNMATCHED_SYMBOL) |
---|
299 | || (taskId2 == Constants.GAP_SYMBOL) |
---|
300 | || (taskId2 == Constants.UNMATCHED_SYMBOL)) { |
---|
301 | return 0; |
---|
302 | } else if ((this.calculateNonEventTaskInstances == false) |
---|
303 | && ((taskId1 > this.firstRoundMaxIndex) || (taskId2 > this.firstRoundMaxIndex))) { |
---|
304 | return 0; |
---|
305 | } else { |
---|
306 | final Integer first = idmapping.get(taskId1); |
---|
307 | final Integer second = idmapping.get(taskId2); |
---|
308 | return matrix.get(first, second); |
---|
309 | } |
---|
310 | |
---|
311 | } |
---|
312 | |
---|
313 | // TODO: Merge this with updateEventTaskInstances |
---|
314 | /** |
---|
315 | * Search event task instances. |
---|
316 | */ |
---|
317 | private void searchEventTaskInstances() { |
---|
318 | for (final Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { |
---|
319 | final ITask task = it.next(); |
---|
320 | if (!(task instanceof IEventTask)) { |
---|
321 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
322 | task.accept(etlg); |
---|
323 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
324 | .getEventlist(); |
---|
325 | etisOfTasks.put(task.getId(), eventTaskInstances); |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Sets the gap penalty. |
---|
332 | * |
---|
333 | * @param gapPenalty the new gap penalty |
---|
334 | */ |
---|
335 | public void setGapPenalty(float gapPenalty) { |
---|
336 | this.gapPenalty = gapPenalty; |
---|
337 | }; |
---|
338 | |
---|
339 | /* (non-Javadoc) |
---|
340 | * @see java.lang.Object#toString() |
---|
341 | */ |
---|
342 | @Override |
---|
343 | public String toString() { |
---|
344 | return matrix.toString(); |
---|
345 | } |
---|
346 | |
---|
347 | // Just Calculate the distance between the new tasks and the matrix. |
---|
348 | /* (non-Javadoc) |
---|
349 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#update(java.util.LinkedList) |
---|
350 | */ |
---|
351 | @Override |
---|
352 | public void update(LinkedList<ITask> newTasks) { |
---|
353 | |
---|
354 | if (this.calculateNonEventTaskInstances) { |
---|
355 | try { |
---|
356 | matrix.increaseSize(newTasks.size()); |
---|
357 | System.out.println("Subsitution matrix size is now " |
---|
358 | + ((matrix.size() * (matrix.size() + 1)) / 2)); |
---|
359 | Console.traceln(Level.INFO, "searching EventTasks in Tasks"); |
---|
360 | } catch (final Exception e) { |
---|
361 | Console.logException(e); |
---|
362 | } |
---|
363 | //this.updateEventTaskInstances(newTasks); |
---|
364 | for (final Iterator<ITask> it = newTasks.iterator(); it.hasNext();) { |
---|
365 | final ITask task1 = it.next(); |
---|
366 | for (final Iterator<ITask> jt = uniqueTasks.iterator(); jt |
---|
367 | .hasNext();) { |
---|
368 | final ITask task2 = jt.next(); |
---|
369 | computeDistance(task1, task2); |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | /** |
---|
376 | * Update event task instances. |
---|
377 | * |
---|
378 | * @param newTasks the new tasks |
---|
379 | */ |
---|
380 | public void updateEventTaskInstances(LinkedList<ITask> newTasks) { |
---|
381 | for (final Iterator<ITask> it = newTasks.iterator(); it.hasNext();) { |
---|
382 | final ITask task = it.next(); |
---|
383 | if (!(task instanceof IEventTask)) { |
---|
384 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
385 | task.accept(etlg); |
---|
386 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
387 | .getEventlist(); |
---|
388 | etisOfTasks.put(task.getId(), eventTaskInstances); |
---|
389 | } |
---|
390 | } |
---|
391 | } |
---|
392 | } |
---|