[1734] | 1 | /* |
---|
| 2 | * |
---|
| 3 | */ |
---|
[1572] | 4 | package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; |
---|
[1324] | 5 | |
---|
[1707] | 6 | import java.io.Serializable; |
---|
[1748] | 7 | import java.util.Collection; |
---|
[1554] | 8 | import java.util.HashMap; |
---|
[1331] | 9 | import java.util.Iterator; |
---|
[1695] | 10 | import java.util.LinkedList; |
---|
| 11 | import java.util.logging.Level; |
---|
[1331] | 12 | |
---|
| 13 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[1553] | 14 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentHelpers; |
---|
[1578] | 15 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Constants; |
---|
[1695] | 16 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
[1553] | 17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
[1554] | 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[1695] | 20 | import de.ugoe.cs.util.console.Console; |
---|
[1324] | 21 | |
---|
[1734] | 22 | // TODO: Auto-generated Javadoc |
---|
| 23 | /** |
---|
| 24 | * The Class ObjectDistanceSubstitionMatrix. |
---|
| 25 | */ |
---|
[1733] | 26 | public class ObjectDistanceSubstitionMatrix implements SubstitutionMatrix, |
---|
| 27 | Serializable { |
---|
[1702] | 28 | |
---|
[1734] | 29 | /** The Constant serialVersionUID. */ |
---|
[1707] | 30 | private static final long serialVersionUID = -4253258274617754083L; |
---|
[1734] | 31 | |
---|
| 32 | /** The idmapping. */ |
---|
[1733] | 33 | private final HashMap<Integer, Integer> idmapping; |
---|
[1734] | 34 | |
---|
| 35 | /** The matrix. */ |
---|
[1702] | 36 | private ITriangleMatrix matrix; |
---|
[1734] | 37 | |
---|
| 38 | /** The unique tasks. */ |
---|
[1748] | 39 | private Collection<ITask> uniqueTasks; |
---|
[1734] | 40 | |
---|
| 41 | /** The gap penalty. */ |
---|
[1702] | 42 | private float gapPenalty; |
---|
[1734] | 43 | |
---|
| 44 | /** The index. */ |
---|
[1676] | 45 | private int index = 0; |
---|
[1734] | 46 | |
---|
| 47 | /** The etis of tasks. */ |
---|
[1733] | 48 | private final HashMap<Integer, LinkedList<IEventTaskInstance>> etisOfTasks; |
---|
[1734] | 49 | |
---|
| 50 | /** The calculate non task instances. */ |
---|
[1740] | 51 | private boolean calculateNonEventTaskInstances = false; |
---|
[1734] | 52 | |
---|
| 53 | /** The first round max index. */ |
---|
[1702] | 54 | private int firstRoundMaxIndex = 0; |
---|
[1695] | 55 | |
---|
[1734] | 56 | /** The positive threshold. */ |
---|
[1733] | 57 | private final double positiveThreshold; |
---|
[1695] | 58 | |
---|
[1734] | 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 | */ |
---|
[1733] | 66 | public ObjectDistanceSubstitionMatrix(float positiveThreshold, |
---|
| 67 | int gapPenalty, boolean calculateNonTaskInstances) { |
---|
[1558] | 68 | this.positiveThreshold = positiveThreshold; |
---|
[1554] | 69 | idmapping = new HashMap<Integer, Integer>(); |
---|
[1695] | 70 | etisOfTasks = new HashMap<Integer, LinkedList<IEventTaskInstance>>(); |
---|
[1620] | 71 | this.gapPenalty = gapPenalty; |
---|
[1740] | 72 | this.calculateNonEventTaskInstances = calculateNonTaskInstances; |
---|
[1695] | 73 | |
---|
[1324] | 74 | } |
---|
| 75 | |
---|
[1734] | 76 | /** |
---|
| 77 | * Compute distance. |
---|
| 78 | * |
---|
| 79 | * @param task1 the task1 |
---|
| 80 | * @param task2 the task2 |
---|
| 81 | */ |
---|
[1711] | 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) { |
---|
[1733] | 90 | ti1 = task1.getInstances().iterator().next(); |
---|
[1711] | 91 | } |
---|
| 92 | if (task2.getInstances().size() > 0) { |
---|
[1733] | 93 | ti2 = task2.getInstances().iterator().next(); |
---|
[1711] | 94 | } |
---|
| 95 | IEventTaskInstance eti1 = null; |
---|
| 96 | IEventTaskInstance eti2 = null; |
---|
| 97 | |
---|
[1733] | 98 | if ((ti1 instanceof IEventTaskInstance) |
---|
| 99 | && (ti2 instanceof IEventTaskInstance)) { |
---|
[1711] | 100 | eti1 = (IEventTaskInstance) ti1; |
---|
| 101 | index1 = getIndex(eti1); |
---|
| 102 | eti2 = (IEventTaskInstance) ti2; |
---|
| 103 | index2 = getIndex(eti2); |
---|
| 104 | distance = distanceBetweenInstances(eti1, eti2); |
---|
[1733] | 105 | } else if ((ti1 instanceof IEventTaskInstance) |
---|
[1711] | 106 | && !(ti2 instanceof IEventTaskInstance)) { |
---|
[1733] | 107 | task1 = ti1.getTask(); |
---|
[1711] | 108 | index2 = getIndex(task2); |
---|
| 109 | eti1 = (IEventTaskInstance) ti1; |
---|
| 110 | index1 = getIndex(eti1); |
---|
[1747] | 111 | distance = distanceBetweenTaskAndInstance(task2, eti1)-3; |
---|
[1711] | 112 | } else if (!(ti1 instanceof IEventTaskInstance) |
---|
[1733] | 113 | && (ti2 instanceof IEventTaskInstance)) { |
---|
[1711] | 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); |
---|
[1747] | 122 | distance = distanceBetweenTasks(task1, task2)-3; |
---|
[1711] | 123 | } else { |
---|
| 124 | System.out.println("Unknown error"); |
---|
| 125 | } |
---|
| 126 | matrix.set(index1, index2, distance); |
---|
| 127 | } |
---|
[1733] | 128 | |
---|
[1734] | 129 | /** |
---|
| 130 | * Distance between instances. |
---|
| 131 | * |
---|
| 132 | * @param eti1 the eti1 |
---|
| 133 | * @param eti2 the eti2 |
---|
| 134 | * @return the float |
---|
| 135 | */ |
---|
[1733] | 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; |
---|
[1324] | 143 | } |
---|
[1707] | 144 | |
---|
[1734] | 145 | /** |
---|
| 146 | * Distance between task and instance. |
---|
| 147 | * |
---|
| 148 | * @param task1 the task1 |
---|
| 149 | * @param eti1 the eti1 |
---|
| 150 | * @return the float |
---|
| 151 | */ |
---|
[1696] | 152 | private float distanceBetweenTaskAndInstance(ITask task1, |
---|
[1695] | 153 | IEventTaskInstance eti1) { |
---|
[1740] | 154 | if (this.calculateNonEventTaskInstances) { |
---|
[1695] | 155 | float tmpDistance = 0; |
---|
[1740] | 156 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
| 157 | task1.accept(etlg); |
---|
| 158 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
| 159 | .getEventlist(); |
---|
| 160 | |
---|
[1733] | 161 | for (final Iterator<IEventTaskInstance> it = eventTaskInstances |
---|
[1695] | 162 | .iterator(); it.hasNext();) { |
---|
[1733] | 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 | // } |
---|
[1695] | 173 | } |
---|
| 174 | return tmpDistance / eventTaskInstances.size(); |
---|
| 175 | } else { |
---|
| 176 | return 0; |
---|
| 177 | } |
---|
[1677] | 178 | } |
---|
[1695] | 179 | |
---|
[1734] | 180 | /** |
---|
| 181 | * Distance between tasks. |
---|
| 182 | * |
---|
| 183 | * @param task1 the task1 |
---|
| 184 | * @param task2 the task2 |
---|
| 185 | * @return the float |
---|
| 186 | */ |
---|
[1696] | 187 | private float distanceBetweenTasks(ITask task1, ITask task2) { |
---|
[1740] | 188 | if (this.calculateNonEventTaskInstances) { |
---|
| 189 | final EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); |
---|
| 190 | task1.accept(etlg); |
---|
| 191 | final LinkedList<IEventTaskInstance> eventTaskInstances = etlg |
---|
| 192 | .getEventlist(); |
---|
| 193 | |
---|
[1695] | 194 | float tmpDistance = 0; |
---|
[1733] | 195 | for (final Iterator<IEventTaskInstance> it = eventTaskInstances |
---|
[1695] | 196 | .iterator(); it.hasNext();) { |
---|
[1733] | 197 | final IEventTaskInstance eti1 = it.next(); |
---|
[1695] | 198 | tmpDistance += distanceBetweenTaskAndInstance(task2, eti1); |
---|
| 199 | } |
---|
[1677] | 200 | |
---|
[1695] | 201 | return tmpDistance / eventTaskInstances.size(); |
---|
| 202 | } else { |
---|
| 203 | return 0; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
[1734] | 207 | /* (non-Javadoc) |
---|
| 208 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#generate(java.util.HashSet) |
---|
| 209 | */ |
---|
[1733] | 210 | @Override |
---|
[1748] | 211 | public void generate(Collection<ITask> uniqueTasks) { |
---|
[1733] | 212 | this.uniqueTasks = uniqueTasks; |
---|
[1740] | 213 | if (this.calculateNonEventTaskInstances) { |
---|
[1747] | 214 | matrix = new PreallocatedDynamicTriangleMatrix(uniqueTasks.size() + 1); |
---|
[1733] | 215 | Console.traceln(Level.INFO, "searching EventTasks in Tasks"); |
---|
[1740] | 216 | //searchEventTaskInstances(); |
---|
[1695] | 217 | } else { |
---|
[1733] | 218 | matrix = new StaticTriangleMatrix(uniqueTasks.size() + 1); |
---|
[1677] | 219 | } |
---|
[1733] | 220 | matrix.initialize(0); |
---|
[1695] | 221 | |
---|
[1741] | 222 | //int count = 0; |
---|
| 223 | //final int size = uniqueTasks.size(); |
---|
[1733] | 224 | for (final Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { |
---|
| 225 | final ITask task1 = it.next(); |
---|
[1741] | 226 | //count++; |
---|
| 227 | //if (((count % (size / 100)) == 0)) { |
---|
| 228 | // Console.traceln(Level.INFO, |
---|
| 229 | // (Math.round(((float) count / size) * 100)) + "%"); |
---|
| 230 | //} |
---|
[1733] | 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; |
---|
[1677] | 238 | } |
---|
[1695] | 239 | |
---|
[1734] | 240 | /* (non-Javadoc) |
---|
| 241 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#getGapPenalty() |
---|
| 242 | */ |
---|
[1733] | 243 | @Override |
---|
| 244 | public float getGapPenalty() { |
---|
| 245 | return gapPenalty; |
---|
| 246 | } |
---|
| 247 | |
---|
[1734] | 248 | /** |
---|
| 249 | * Gets the index. |
---|
| 250 | * |
---|
| 251 | * @param eti the eti |
---|
| 252 | * @return the index |
---|
| 253 | */ |
---|
[1707] | 254 | synchronized private int getIndex(IEventTaskInstance eti) { |
---|
[1676] | 255 | int tempindex = -1; |
---|
[1695] | 256 | if (!idmapping.containsKey(eti.getTask().getId())) { |
---|
[1676] | 257 | idmapping.put(eti.getTask().getId(), index); |
---|
| 258 | tempindex = index; |
---|
| 259 | index++; |
---|
[1695] | 260 | } else { |
---|
[1676] | 261 | tempindex = idmapping.get(eti.getTask().getId()); |
---|
| 262 | } |
---|
| 263 | return tempindex; |
---|
| 264 | } |
---|
[1695] | 265 | |
---|
[1734] | 266 | /** |
---|
| 267 | * Gets the index. |
---|
| 268 | * |
---|
| 269 | * @param task the task |
---|
| 270 | * @return the index |
---|
| 271 | */ |
---|
[1733] | 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; |
---|
[1558] | 285 | } |
---|
[1554] | 286 | |
---|
[1733] | 287 | // public boolean scoreExists(int id, int id2) { |
---|
| 288 | // return idmapping.containsKey(id) && idmapping.containsKey(id2); |
---|
| 289 | // return false; |
---|
| 290 | // } |
---|
| 291 | |
---|
[1734] | 292 | /* (non-Javadoc) |
---|
| 293 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#getScore(int, int) |
---|
| 294 | */ |
---|
[1733] | 295 | @Override |
---|
[1702] | 296 | public float getScore(int taskId1, int taskId2) { |
---|
[1733] | 297 | if ((taskId1 == Constants.GAP_SYMBOL) |
---|
| 298 | || (taskId1 == Constants.UNMATCHED_SYMBOL) |
---|
| 299 | || (taskId2 == Constants.GAP_SYMBOL) |
---|
| 300 | || (taskId2 == Constants.UNMATCHED_SYMBOL)) { |
---|
[1702] | 301 | return 0; |
---|
[1740] | 302 | } else if ((this.calculateNonEventTaskInstances == false) |
---|
[1733] | 303 | && ((taskId1 > this.firstRoundMaxIndex) || (taskId2 > this.firstRoundMaxIndex))) { |
---|
[1702] | 304 | return 0; |
---|
[1695] | 305 | } else { |
---|
[1733] | 306 | final Integer first = idmapping.get(taskId1); |
---|
| 307 | final Integer second = idmapping.get(taskId2); |
---|
[1695] | 308 | return matrix.get(first, second); |
---|
[1578] | 309 | } |
---|
[1695] | 310 | |
---|
[1554] | 311 | } |
---|
[1733] | 312 | |
---|
| 313 | // TODO: Merge this with updateEventTaskInstances |
---|
[1734] | 314 | /** |
---|
| 315 | * Search event task instances. |
---|
| 316 | */ |
---|
[1733] | 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 | |
---|
[1734] | 330 | /** |
---|
| 331 | * Sets the gap penalty. |
---|
| 332 | * |
---|
| 333 | * @param gapPenalty the new gap penalty |
---|
| 334 | */ |
---|
[1733] | 335 | public void setGapPenalty(float gapPenalty) { |
---|
| 336 | this.gapPenalty = gapPenalty; |
---|
| 337 | }; |
---|
| 338 | |
---|
[1734] | 339 | /* (non-Javadoc) |
---|
| 340 | * @see java.lang.Object#toString() |
---|
| 341 | */ |
---|
[1733] | 342 | @Override |
---|
| 343 | public String toString() { |
---|
| 344 | return matrix.toString(); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | // Just Calculate the distance between the new tasks and the matrix. |
---|
[1734] | 348 | /* (non-Javadoc) |
---|
| 349 | * @see de.ugoe.cs.autoquest.tasktrees.alignment.matrix.SubstitutionMatrix#update(java.util.LinkedList) |
---|
| 350 | */ |
---|
[1733] | 351 | @Override |
---|
| 352 | public void update(LinkedList<ITask> newTasks) { |
---|
| 353 | |
---|
[1740] | 354 | if (this.calculateNonEventTaskInstances) { |
---|
[1733] | 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 | } |
---|
[1740] | 363 | //this.updateEventTaskInstances(newTasks); |
---|
[1733] | 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 | |
---|
[1734] | 375 | /** |
---|
| 376 | * Update event task instances. |
---|
| 377 | * |
---|
| 378 | * @param newTasks the new tasks |
---|
| 379 | */ |
---|
[1733] | 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 | } |
---|
[1711] | 392 | } |
---|