Changeset 1695
- Timestamp:
- 08/27/14 16:14:00 (10 years ago)
- Location:
- branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java
r1693 r1695 1 1 package de.ugoe.cs.autoquest.tasktrees.alignment.matrix; 2 3 2 4 3 import java.util.HashMap; 5 4 import java.util.HashSet; 6 5 import java.util.Iterator; 6 import java.util.LinkedList; 7 import java.util.logging.Level; 7 8 8 9 import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; 9 10 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentHelpers; 10 11 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Constants; 12 import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; 11 13 import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; 12 14 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; 13 15 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; 14 16 import de.ugoe.cs.util.console.Console; 15 17 16 18 public class ObjectDistanceSubstitionMatrix implements SubstitutionMatrix { 17 19 18 HashMap<Integer, Integer> idmapping;20 private HashMap<Integer, Integer> idmapping; 19 21 private TriangleMatrix matrix; 20 22 private HashSet<ITask> uniqueTasks; 21 private double gapPenalty; 23 private double gapPenalty; 22 24 private int index = 0; 23 25 private HashMap<Integer, LinkedList<IEventTaskInstance>> etisOfTasks; 26 private boolean calculateNonTaskInstances = true; 27 24 28 private double positiveThreshold; 25 26 public ObjectDistanceSubstitionMatrix( 27 HashSet<ITask> uniqueTasks,float positiveThreshold, int gapPenalty) { 29 30 public ObjectDistanceSubstitionMatrix(HashSet<ITask> uniqueTasks, 31 float positiveThreshold, int gapPenalty, 32 boolean calculateNonTaskInstances) { 28 33 this.uniqueTasks = uniqueTasks; 29 34 this.positiveThreshold = positiveThreshold; 30 35 idmapping = new HashMap<Integer, Integer>(); 31 matrix = new TriangleMatrix(uniqueTasks.size()+1); 36 etisOfTasks = new HashMap<Integer, LinkedList<IEventTaskInstance>>(); 37 matrix = new TriangleMatrix(uniqueTasks.size() + 1); 32 38 this.gapPenalty = gapPenalty; 33 34 } 35 39 this.calculateNonTaskInstances = calculateNonTaskInstances; 40 41 } 36 42 37 43 public double getGapPenalty() { 38 44 return gapPenalty; 39 45 } 40 46 41 47 public void setGapPenalty(double gapPenalty) { 42 48 this.gapPenalty = gapPenalty; 43 49 } 44 50 51 private void searchEventTaskInstances() { 52 int count = 0; 53 for (Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { 54 count++; 55 56 ITask task = it.next(); 57 if (!(task instanceof IEventTask)) { 58 EventTaskInstancesListGenerator etlg = new EventTaskInstancesListGenerator(); 59 task.accept(etlg); 60 LinkedList<IEventTaskInstance> eventTaskInstances = etlg 61 .getEventlist(); 62 etisOfTasks.put(task.getId(), eventTaskInstances); 63 } 64 } 65 66 } 67 45 68 public void generate() { 46 47 int index1=-1; 48 int index2=-1; 49 float distance=0; 69 if (this.calculateNonTaskInstances) { 70 Console.traceln(Level.INFO, " searching EventTasks in Tasks"); 71 searchEventTaskInstances(); 72 } 73 Console.traceln(Level.INFO, " calculating distances"); 74 int index1 = -1; 75 int index2 = -1; 76 double distance = 0; 77 int count = 0; 50 78 for (Iterator<ITask> it = uniqueTasks.iterator(); it.hasNext();) { 79 count++; 80 51 81 ITask task1 = it.next(); 52 82 for (Iterator<ITask> jt = uniqueTasks.iterator(); jt.hasNext();) { … … 54 84 ITaskInstance ti1 = null; 55 85 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(); 86 // We just need to the first instance here 87 if (task1.getInstances().size() > 0) { 88 ti1 = (ITaskInstance) task1.getInstances().iterator() 89 .next(); 90 } 91 if (task2.getInstances().size() > 0) { 92 ti2 = (ITaskInstance) task2.getInstances().iterator() 93 .next(); 62 94 } 63 95 IEventTaskInstance eti1 = null; 64 96 IEventTaskInstance eti2 = null; 65 66 67 if (ti1 instanceof IEventTaskInstance&& ti2 instanceof IEventTaskInstance) {97 98 if (ti1 instanceof IEventTaskInstance 99 && ti2 instanceof IEventTaskInstance) { 68 100 eti1 = (IEventTaskInstance) ti1; 69 101 index1 = getIndex(eti1); 70 102 eti2 = (IEventTaskInstance) ti2; 71 103 index2 = getIndex(eti2); 72 distance = distanceBetweenInstances(eti1, eti2);73 } 74 else if(ti1 instanceof IEventTaskInstance &&!(ti2 instanceof IEventTaskInstance)) {104 distance = distanceBetweenInstances(eti1, eti2); 105 } else if (ti1 instanceof IEventTaskInstance 106 && !(ti2 instanceof IEventTaskInstance)) { 75 107 task1 = ((ITaskInstance) ti1).getTask(); 76 108 index2 = getIndex(task2); 77 109 eti1 = (IEventTaskInstance) ti1; 78 110 index1 = getIndex(eti1); 79 distance = distanceBetweenTaskAndInstance(task2, eti1);80 } 81 else if(!(ti1 instanceof IEventTaskInstance)&& ti2 instanceof IEventTaskInstance) {111 distance = distanceBetweenTaskAndInstance(task2, eti1); 112 } else if (!(ti1 instanceof IEventTaskInstance) 113 && ti2 instanceof IEventTaskInstance) { 82 114 index1 = getIndex(task1); 83 115 eti2 = (IEventTaskInstance) ti2; 84 116 index2 = getIndex(eti2); 85 distance = distanceBetweenTaskAndInstance(task1, eti2);86 } 87 else if(!(ti1 instanceof IEventTaskInstance)&& !(ti2 instanceof IEventTaskInstance)) {117 distance = distanceBetweenTaskAndInstance(task1, eti2); 118 } else if (!(ti1 instanceof IEventTaskInstance) 119 && !(ti2 instanceof IEventTaskInstance)) { 88 120 index1 = getIndex(task1); 89 121 index2 = getIndex(task2); 90 distance = distanceBetweenTasks(task1,task2); 91 } 92 else { 122 distance = distanceBetweenTasks(task1, task2); 123 } else { 93 124 System.out.println("Unknown error"); 94 125 } 95 96 matrix.set(index1, index2,distance); 97 98 } 99 } 100 } 101 102 //TODO: Calculate distance here 103 private float distanceBetweenTaskAndInstance(ITask task1, IEventTaskInstance eti) { 104 return 0; 105 } 106 107 private float distanceBetweenTasks(ITask task1, ITask task2) { 108 return 0; 109 } 110 126 127 matrix.set(index1, index2, distance); 128 129 } 130 } 131 } 132 133 private double distanceBetweenTaskAndInstance(ITask task1, 134 IEventTaskInstance eti1) { 135 if (this.calculateNonTaskInstances) { 136 float tmpDistance = 0; 137 LinkedList<IEventTaskInstance> eventTaskInstances = etisOfTasks 138 .get(task1.getId()); 139 for (Iterator<IEventTaskInstance> it = eventTaskInstances 140 .iterator(); it.hasNext();) { 141 IEventTaskInstance eti2 = it.next(); 142 int taskId1 = eti1.getTask().getId(); 143 int taskId2 = eti2.getTask().getId(); 144 if (scoreExists(taskId1, taskId2)) { 145 tmpDistance += getScore(taskId1, taskId2); 146 } else { 147 double dist = distanceBetweenInstances(eti1, eti2); 148 matrix.set(getIndex(eti1), getIndex(eti2), dist); 149 tmpDistance += dist; 150 } 151 } 152 return tmpDistance / eventTaskInstances.size(); 153 } else { 154 return 0; 155 } 156 } 157 158 public boolean scoreExists(int id, int id2) { 159 return idmapping.containsKey(id) && idmapping.containsKey(id2); 160 } 161 162 private double distanceBetweenTasks(ITask task1, ITask task2) { 163 if (this.calculateNonTaskInstances) { 164 LinkedList<IEventTaskInstance> eventTaskInstances = etisOfTasks 165 .get(task1.getId()); 166 float tmpDistance = 0; 167 for (Iterator<IEventTaskInstance> it = eventTaskInstances 168 .iterator(); it.hasNext();) { 169 IEventTaskInstance eti1 = it.next(); 170 tmpDistance += distanceBetweenTaskAndInstance(task2, eti1); 171 } 172 173 return tmpDistance / eventTaskInstances.size(); 174 } else { 175 return 0; 176 } 177 } 111 178 112 179 private int getIndex(ITask task) { 113 int tempindex = -1;114 115 if (!idmapping.containsKey(task.getId())) {180 int tempindex = -1; 181 182 if (!idmapping.containsKey(task.getId())) { 116 183 idmapping.put(task.getId(), index); 117 184 tempindex = index; 118 185 index++; 119 } 120 else { 186 } else { 121 187 tempindex = idmapping.get(task.getId()); 122 188 } 123 189 124 190 return tempindex; 125 191 } 126 192 127 193 private int getIndex(IEventTaskInstance eti) { 128 194 int tempindex = -1; 129 if (!idmapping.containsKey(eti.getTask().getId())) {195 if (!idmapping.containsKey(eti.getTask().getId())) { 130 196 idmapping.put(eti.getTask().getId(), index); 131 197 tempindex = index; 132 198 index++; 133 } 134 else { 199 } else { 135 200 tempindex = idmapping.get(eti.getTask().getId()); 136 201 } 137 202 return tempindex; 138 203 }; 139 140 private float distanceBetweenInstances(IEventTaskInstance eti1, IEventTaskInstance eti2) { 204 205 private double distanceBetweenInstances(IEventTaskInstance eti1, 206 IEventTaskInstance eti2) { 141 207 IGUIElement first = (IGUIElement) eti1.getEvent().getTarget(); 142 208 IGUIElement second = (IGUIElement) eti2.getEvent().getTarget(); 143 float distance = -1 *AlignmentHelpers.distanceBetween(first, second);209 float distance = -1 * AlignmentHelpers.distanceBetween(first, second); 144 210 distance += positiveThreshold; 145 211 return distance; 146 212 } 147 148 public String toString() {213 214 public String toString() { 149 215 return matrix.toString(); 150 216 } 151 217 152 218 public double getScore(int taskId1, int taskId2) { 153 if(taskId1 == Constants.GAP_SYMBOL || taskId1 == Constants.UNMATCHED_SYMBOL || taskId2 == Constants.GAP_SYMBOL || taskId2 == Constants.UNMATCHED_SYMBOL ) { 154 return 0.0; 155 } 156 else { 157 219 if (taskId1 == Constants.GAP_SYMBOL 220 || taskId1 == Constants.UNMATCHED_SYMBOL 221 || taskId2 == Constants.GAP_SYMBOL 222 || taskId2 == Constants.UNMATCHED_SYMBOL) { 223 return 0.0; 224 } else { 158 225 Integer first = idmapping.get(taskId1); 159 226 Integer second = idmapping.get(taskId2); 160 return matrix.get(first, second);161 } 162 227 return matrix.get(first, second); 228 } 229 163 230 } 164 231 165 232 } 166 -
branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/TriangleMatrix.java
r1572 r1695 32 32 } 33 33 34 34 35 35 36 public String toString() {
Note: See TracChangeset
for help on using the changeset viewer.