1 | /* |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; |
---|
5 | |
---|
6 | import java.io.Serializable; |
---|
7 | import java.util.HashMap; |
---|
8 | import java.util.HashSet; |
---|
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 HashSet<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 calculateNonTaskInstances = true; |
---|
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.calculateNonTaskInstances = 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); |
---|
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); |
---|
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.calculateNonTaskInstances) { |
---|
155 | float tmpDistance = 0; |
---|
156 | // System.out.println(etisOfTasks); |
---|
157 | final LinkedList<IEventTaskInstance> eventTaskInstances = etisOfTasks |
---|
158 | .get(task1.getId()); |
---|
159 | for (final Iterator<IEventTaskInstance> it = eventTaskInstances |
---|
160 | .iterator(); it.hasNext();) { |
---|
161 | final IEventTaskInstance eti2 = it.next(); |
---|
162 | // int taskId1 = eti1.getTask().getId(); |
---|
163 | // int taskId2 = eti2.getTask().getId(); |
---|
164 | // if (scoreExists(taskId1, taskId2)) { |
---|
165 | // tmpDistance += getScore(taskId1, taskId2); |
---|
166 | // } else { |
---|
167 | final float dist = distanceBetweenInstances(eti1, eti2); |
---|
168 | matrix.set(getIndex(eti1), getIndex(eti2), dist); |
---|
169 | tmpDistance += dist; |
---|
170 | // } |
---|
171 | } |
---|
172 | return tmpDistance / eventTaskInstances.size(); |
---|
173 | } else { |
---|
174 | return 0; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Distance between tasks. |
---|
180 | * |
---|
181 | * @param task1 the task1 |
---|
182 | * @param task2 the task2 |
---|
183 | * @return the float |
---|
184 | */ |
---|
185 | private float distanceBetweenTasks(ITask task1, ITask task2) { |
---|
186 | if (this.calculateNonTaskInstances) { |
---|
187 | final LinkedList<IEventTaskInstance> eventTaskInstances = etisOfTasks |
---|
188 | .get(task1.getId()); |
---|
189 | float tmpDistance = 0; |
---|
190 | for (final Iterator<IEventTaskInstance> it = eventTaskInstances |
---|
191 | .iterator(); it.hasNext();) { |
---|
192 | final IEventTaskInstance eti1 = it.next(); |
---|
193 | tmpDistance += distanceBetweenTaskAndInstance(task2, eti1); |
---|
194 | } |
---|
195 | |
---|
196 | return tmpDistance / eventTaskInstances.size(); |
---|
197 | } else { |
---|
198 | return 0; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | /* (non-Javadoc) |
---|
203 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#generate(java.util.HashSet) |
---|
204 | */ |
---|
205 | @Override |
---|
206 | public void generate(HashSet<ITask> uniqueTasks) { |
---|
207 | this.uniqueTasks = uniqueTasks; |
---|
208 | if (this.calculateNonTaskInstances) { |
---|
209 | matrix = new DynamicTriangleMatrix(uniqueTasks.size() + 1); |
---|
210 | Console.traceln(Level.INFO, "searching EventTasks in Tasks"); |
---|
211 | searchEventTaskInstances(); |
---|
212 | } else { |
---|
213 | matrix = new StaticTriangleMatrix(uniqueTasks.size() + 1); |
---|
214 | } |
---|
215 | matrix.initialize(0); |
---|
216 | |
---|
217 | int count = 0; |
---|
218 | final int size = uniqueTasks.size(); |
---|
219 | for (final Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { |
---|
220 | final ITask task1 = it.next(); |
---|
221 | count++; |
---|
222 | if (((count % (size / 100)) == 0)) { |
---|
223 | Console.traceln(Level.INFO, |
---|
224 | (Math.round(((float) count / size) * 100)) + "%"); |
---|
225 | } |
---|
226 | for (final Iterator<ITask> jt = uniqueTasks.iterator(); jt |
---|
227 | .hasNext();) { |
---|
228 | final ITask task2 = jt.next(); |
---|
229 | computeDistance(task1, task2); |
---|
230 | } |
---|
231 | } |
---|
232 | this.firstRoundMaxIndex = index; |
---|
233 | } |
---|
234 | |
---|
235 | /* (non-Javadoc) |
---|
236 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#getGapPenalty() |
---|
237 | */ |
---|
238 | @Override |
---|
239 | public float getGapPenalty() { |
---|
240 | return gapPenalty; |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Gets the index. |
---|
245 | * |
---|
246 | * @param eti the eti |
---|
247 | * @return the index |
---|
248 | */ |
---|
249 | synchronized private int getIndex(IEventTaskInstance eti) { |
---|
250 | int tempindex = -1; |
---|
251 | if (!idmapping.containsKey(eti.getTask().getId())) { |
---|
252 | idmapping.put(eti.getTask().getId(), index); |
---|
253 | tempindex = index; |
---|
254 | index++; |
---|
255 | } else { |
---|
256 | tempindex = idmapping.get(eti.getTask().getId()); |
---|
257 | } |
---|
258 | return tempindex; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * Gets the index. |
---|
263 | * |
---|
264 | * @param task the task |
---|
265 | * @return the index |
---|
266 | */ |
---|
267 | synchronized private int getIndex(ITask task) { |
---|
268 | int tempindex = -1; |
---|
269 | |
---|
270 | if (!idmapping.containsKey(task.getId())) { |
---|
271 | |
---|
272 | idmapping.put(task.getId(), index); |
---|
273 | tempindex = index; |
---|
274 | index++; |
---|
275 | } else { |
---|
276 | tempindex = idmapping.get(task.getId()); |
---|
277 | } |
---|
278 | |
---|
279 | return tempindex; |
---|
280 | } |
---|
281 | |
---|
282 | // public boolean scoreExists(int id, int id2) { |
---|
283 | // return idmapping.containsKey(id) && idmapping.containsKey(id2); |
---|
284 | // return false; |
---|
285 | // } |
---|
286 | |
---|
287 | /* (non-Javadoc) |
---|
288 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#getScore(int, int) |
---|
289 | */ |
---|
290 | @Override |
---|
291 | public float getScore(int taskId1, int taskId2) { |
---|
292 | if ((taskId1 == Constants.GAP_SYMBOL) |
---|
293 | || (taskId1 == Constants.UNMATCHED_SYMBOL) |
---|
294 | || (taskId2 == Constants.GAP_SYMBOL) |
---|
295 | || (taskId2 == Constants.UNMATCHED_SYMBOL)) { |
---|
296 | return 0; |
---|
297 | } else if ((this.calculateNonTaskInstances == false) |
---|
298 | && ((taskId1 > this.firstRoundMaxIndex) || (taskId2 > this.firstRoundMaxIndex))) { |
---|
299 | return 0; |
---|
300 | } else { |
---|
301 | final Integer first = idmapping.get(taskId1); |
---|
302 | final Integer second = idmapping.get(taskId2); |
---|
303 | return matrix.get(first, second); |
---|
304 | } |
---|
305 | |
---|
306 | } |
---|
307 | |
---|
308 | // TODO: Merge this with updateEventTaskInstances |
---|
309 | /** |
---|
310 | * Search event task instances. |
---|
311 | */ |
---|
312 | private void searchEventTaskInstances() { |
---|
313 | for (final Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { |
---|
314 | final ITask task = it.next(); |
---|
315 | if (!(task instanceof IEventTask)) { |
---|
316 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
317 | task.accept(etlg); |
---|
318 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
319 | .getEventlist(); |
---|
320 | etisOfTasks.put(task.getId(), eventTaskInstances); |
---|
321 | } |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | /** |
---|
326 | * Sets the gap penalty. |
---|
327 | * |
---|
328 | * @param gapPenalty the new gap penalty |
---|
329 | */ |
---|
330 | public void setGapPenalty(float gapPenalty) { |
---|
331 | this.gapPenalty = gapPenalty; |
---|
332 | }; |
---|
333 | |
---|
334 | /* (non-Javadoc) |
---|
335 | * @see java.lang.Object#toString() |
---|
336 | */ |
---|
337 | @Override |
---|
338 | public String toString() { |
---|
339 | return matrix.toString(); |
---|
340 | } |
---|
341 | |
---|
342 | // Just Calculate the distance between the new tasks and the matrix. |
---|
343 | /* (non-Javadoc) |
---|
344 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#update(java.util.LinkedList) |
---|
345 | */ |
---|
346 | @Override |
---|
347 | public void update(LinkedList<ITask> newTasks) { |
---|
348 | |
---|
349 | if (this.calculateNonTaskInstances) { |
---|
350 | try { |
---|
351 | matrix.increaseSize(newTasks.size()); |
---|
352 | System.out.println("Subsitution matrix size is now " |
---|
353 | + ((matrix.size() * (matrix.size() + 1)) / 2)); |
---|
354 | Console.traceln(Level.INFO, "searching EventTasks in Tasks"); |
---|
355 | } catch (final Exception e) { |
---|
356 | Console.logException(e); |
---|
357 | } |
---|
358 | this.updateEventTaskInstances(newTasks); |
---|
359 | for (final Iterator<ITask> it = newTasks.iterator(); it.hasNext();) { |
---|
360 | final ITask task1 = it.next(); |
---|
361 | for (final Iterator<ITask> jt = uniqueTasks.iterator(); jt |
---|
362 | .hasNext();) { |
---|
363 | final ITask task2 = jt.next(); |
---|
364 | computeDistance(task1, task2); |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * Update event task instances. |
---|
372 | * |
---|
373 | * @param newTasks the new tasks |
---|
374 | */ |
---|
375 | public void updateEventTaskInstances(LinkedList<ITask> newTasks) { |
---|
376 | for (final Iterator<ITask> it = newTasks.iterator(); it.hasNext();) { |
---|
377 | final ITask task = it.next(); |
---|
378 | if (!(task instanceof IEventTask)) { |
---|
379 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
380 | task.accept(etlg); |
---|
381 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
382 | .getEventlist(); |
---|
383 | etisOfTasks.put(task.getId(), eventTaskInstances); |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | } |
---|