[1572] | 1 | package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; |
---|
[1324] | 2 | |
---|
[1570] | 3 | |
---|
[1554] | 4 | import java.util.HashMap; |
---|
[1692] | 5 | import java.util.HashSet; |
---|
[1331] | 6 | import java.util.Iterator; |
---|
| 7 | |
---|
| 8 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[1553] | 9 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentHelpers; |
---|
[1578] | 10 | import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Constants; |
---|
[1553] | 11 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
[1554] | 12 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 13 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[1324] | 14 | |
---|
[1570] | 15 | |
---|
[1324] | 16 | public class ObjectDistanceSubstitionMatrix implements SubstitutionMatrix { |
---|
| 17 | |
---|
[1554] | 18 | HashMap<Integer, Integer> idmapping; |
---|
| 19 | private TriangleMatrix matrix; |
---|
[1692] | 20 | private HashSet<ITask> uniqueTasks; |
---|
[1568] | 21 | private double gapPenalty; |
---|
[1676] | 22 | private int index = 0; |
---|
[1615] | 23 | |
---|
[1568] | 24 | private double positiveThreshold; |
---|
[1555] | 25 | |
---|
[1554] | 26 | public ObjectDistanceSubstitionMatrix( |
---|
[1692] | 27 | HashSet<ITask> uniqueTasks,float positiveThreshold, int gapPenalty) { |
---|
[1554] | 28 | this.uniqueTasks = uniqueTasks; |
---|
[1558] | 29 | this.positiveThreshold = positiveThreshold; |
---|
[1554] | 30 | idmapping = new HashMap<Integer, Integer>(); |
---|
| 31 | matrix = new TriangleMatrix(uniqueTasks.size()+1); |
---|
[1620] | 32 | this.gapPenalty = gapPenalty; |
---|
[1558] | 33 | |
---|
[1324] | 34 | } |
---|
| 35 | |
---|
[1615] | 36 | |
---|
[1568] | 37 | public double getGapPenalty() { |
---|
[1555] | 38 | return gapPenalty; |
---|
[1324] | 39 | } |
---|
[1615] | 40 | |
---|
| 41 | public void setGapPenalty(double gapPenalty) { |
---|
| 42 | this.gapPenalty = gapPenalty; |
---|
| 43 | } |
---|
[1324] | 44 | |
---|
[1554] | 45 | public void generate() { |
---|
[1578] | 46 | |
---|
[1677] | 47 | int index1=-1; |
---|
| 48 | int index2=-1; |
---|
| 49 | float distance=0; |
---|
[1692] | 50 | for (Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { |
---|
| 51 | ITask task1 = it.next(); |
---|
| 52 | for (Iterator<ITask> jt = uniqueTasks.iterator(); jt.hasNext();) { |
---|
| 53 | ITask task2 = jt.next(); |
---|
| 54 | ITaskInstance ti1 = null; |
---|
| 55 | ITaskInstance ti2 = null; |
---|
| 56 | //TODO This seems ugly |
---|
| 57 | if(task1.getInstances().size() > 0) { |
---|
| 58 | ti1 = (ITaskInstance) task1.getInstances().iterator().next(); |
---|
| 59 | } |
---|
| 60 | if(task2.getInstances().size() > 0) { |
---|
| 61 | ti2 = (ITaskInstance) task2.getInstances().iterator().next(); |
---|
| 62 | } |
---|
[1677] | 63 | IEventTaskInstance eti1 = null; |
---|
[1554] | 64 | IEventTaskInstance eti2 = null; |
---|
[1677] | 65 | |
---|
| 66 | |
---|
[1692] | 67 | if (ti1 instanceof IEventTaskInstance && ti2 instanceof IEventTaskInstance) { |
---|
| 68 | eti1 = (IEventTaskInstance) ti1; |
---|
[1677] | 69 | index1 = getIndex(eti1); |
---|
[1692] | 70 | eti2 = (IEventTaskInstance) ti2; |
---|
[1677] | 71 | index2 = getIndex(eti2); |
---|
| 72 | distance = distanceBetweenInstances(eti1,eti2); |
---|
[1331] | 73 | } |
---|
[1692] | 74 | else if(ti1 instanceof IEventTaskInstance && !(ti2 instanceof IEventTaskInstance)) { |
---|
| 75 | task1 = ((ITaskInstance) ti1).getTask(); |
---|
[1677] | 76 | index2 = getIndex(task2); |
---|
[1692] | 77 | eti1 = (IEventTaskInstance) ti1; |
---|
[1677] | 78 | index1 = getIndex(eti1); |
---|
| 79 | distance = distanceBetweenTaskAndInstance(task2,eti1); |
---|
[1675] | 80 | } |
---|
[1692] | 81 | else if(!(ti1 instanceof IEventTaskInstance) && ti2 instanceof IEventTaskInstance) { |
---|
[1677] | 82 | index1 = getIndex(task1); |
---|
[1692] | 83 | eti2 = (IEventTaskInstance) ti2; |
---|
[1677] | 84 | index2 = getIndex(eti2); |
---|
| 85 | distance = distanceBetweenTaskAndInstance(task1,eti2); |
---|
| 86 | } |
---|
[1692] | 87 | else if(!(ti1 instanceof IEventTaskInstance) && !(ti2 instanceof IEventTaskInstance)) { |
---|
[1677] | 88 | index1 = getIndex(task1); |
---|
| 89 | index2 = getIndex(task2); |
---|
| 90 | distance = distanceBetweenTasks(task1,task2); |
---|
| 91 | } |
---|
| 92 | else { |
---|
| 93 | System.out.println("Unknown error"); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | matrix.set(index1, index2,distance); |
---|
[1578] | 97 | |
---|
[1554] | 98 | } |
---|
[1331] | 99 | } |
---|
[1324] | 100 | } |
---|
[1677] | 101 | |
---|
[1692] | 102 | //TODO: Calculate distance here |
---|
[1677] | 103 | private float distanceBetweenTaskAndInstance(ITask task1, IEventTaskInstance eti) { |
---|
| 104 | return 0; |
---|
| 105 | } |
---|
| 106 | |
---|
[1692] | 107 | //TODO: Calculate distance here |
---|
[1677] | 108 | private float distanceBetweenTasks(ITask task1, ITask task2) { |
---|
| 109 | return 0; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | private int getIndex(ITask task) { |
---|
| 114 | int tempindex =-1; |
---|
[1679] | 115 | |
---|
[1677] | 116 | if(!idmapping.containsKey(task.getId())) { |
---|
| 117 | idmapping.put(task.getId(), index); |
---|
| 118 | tempindex = index; |
---|
| 119 | index++; |
---|
| 120 | } |
---|
| 121 | else { |
---|
| 122 | tempindex = idmapping.get(task.getId()); |
---|
| 123 | } |
---|
[1679] | 124 | |
---|
[1677] | 125 | return tempindex; |
---|
| 126 | } |
---|
| 127 | |
---|
[1676] | 128 | private int getIndex(IEventTaskInstance eti) { |
---|
| 129 | int tempindex = -1; |
---|
[1677] | 130 | if(!idmapping.containsKey(eti.getTask().getId())) { |
---|
[1676] | 131 | idmapping.put(eti.getTask().getId(), index); |
---|
| 132 | tempindex = index; |
---|
| 133 | index++; |
---|
| 134 | } |
---|
[1677] | 135 | else { |
---|
[1676] | 136 | tempindex = idmapping.get(eti.getTask().getId()); |
---|
| 137 | } |
---|
| 138 | return tempindex; |
---|
[1677] | 139 | }; |
---|
[1676] | 140 | |
---|
| 141 | private float distanceBetweenInstances(IEventTaskInstance eti1, IEventTaskInstance eti2) { |
---|
| 142 | IGUIElement first = (IGUIElement) eti1.getEvent().getTarget(); |
---|
| 143 | IGUIElement second = (IGUIElement) eti2.getEvent().getTarget(); |
---|
| 144 | float distance = -1*AlignmentHelpers.distanceBetween(first, second); |
---|
| 145 | distance += positiveThreshold; |
---|
| 146 | return distance; |
---|
| 147 | } |
---|
| 148 | |
---|
[1558] | 149 | public String toString(){ |
---|
| 150 | return matrix.toString(); |
---|
| 151 | } |
---|
[1554] | 152 | |
---|
[1578] | 153 | public double getScore(int taskId1, int taskId2) { |
---|
| 154 | if(taskId1 == Constants.GAP_SYMBOL || taskId1 == Constants.UNMATCHED_SYMBOL || taskId2 == Constants.GAP_SYMBOL || taskId2 == Constants.UNMATCHED_SYMBOL ) { |
---|
| 155 | return 0.0; |
---|
| 156 | } |
---|
| 157 | else { |
---|
[1692] | 158 | |
---|
[1687] | 159 | Integer first = idmapping.get(taskId1); |
---|
| 160 | Integer second = idmapping.get(taskId2); |
---|
[1677] | 161 | return matrix.get(first,second); |
---|
[1578] | 162 | } |
---|
| 163 | |
---|
[1554] | 164 | } |
---|
| 165 | |
---|
[1324] | 166 | } |
---|
[1554] | 167 | |
---|