[1767] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
| 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation.utils; |
---|
| 16 | |
---|
| 17 | import java.io.PrintStream; |
---|
| 18 | import java.util.Arrays; |
---|
| 19 | import java.util.LinkedList; |
---|
| 20 | import java.util.List; |
---|
| 21 | |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskTraversingVisitor; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
| 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskPath; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * <p> |
---|
| 32 | * TODO comment |
---|
| 33 | * </p> |
---|
| 34 | * |
---|
| 35 | * @author Patrick Harms |
---|
| 36 | */ |
---|
| 37 | public class TaskTraversal { |
---|
| 38 | |
---|
| 39 | /** */ |
---|
| 40 | private List<TaskPath> traversalPaths = new LinkedList<TaskPath>(); |
---|
| 41 | |
---|
| 42 | /** */ |
---|
| 43 | private TaskPath[] traversalPathArray = null; |
---|
[1849] | 44 | |
---|
| 45 | /** */ |
---|
| 46 | private ITask task = null; |
---|
| 47 | |
---|
[1767] | 48 | /** |
---|
| 49 | * |
---|
| 50 | */ |
---|
| 51 | public static TaskTraversal getTraversal(ITask task, |
---|
| 52 | final List<TaskPath> untraversedPaths) |
---|
| 53 | { |
---|
| 54 | final TaskTraversal result = new TaskTraversal(); |
---|
| 55 | final TaskPath currentPath = new TaskPath(); |
---|
| 56 | final int[] currentIndex = new int[1]; |
---|
| 57 | currentIndex[0] = 0; |
---|
| 58 | |
---|
| 59 | task.accept(new DefaultTaskTraversingVisitor() { |
---|
| 60 | @Override |
---|
| 61 | public void visit(IEventTask eventTask) { |
---|
| 62 | currentPath.add(eventTask, currentIndex[0]); |
---|
| 63 | result.add(new TaskPath(currentPath)); |
---|
| 64 | currentPath.removeLast(); |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | @Override |
---|
| 68 | public void visit(ISequence relationship) { |
---|
| 69 | currentPath.add(relationship, currentIndex[0]); |
---|
| 70 | |
---|
[1849] | 71 | if (pathIsInList(currentPath, untraversedPaths)) { |
---|
[1767] | 72 | result.add(new TaskPath(currentPath)); |
---|
| 73 | } |
---|
| 74 | else { |
---|
| 75 | int indexBuffer = currentIndex[0]; |
---|
| 76 | |
---|
| 77 | List<ITask> children = relationship.getChildren(); |
---|
| 78 | for (int i = 0; i < children.size(); i++) { |
---|
| 79 | currentIndex[0] = i; |
---|
| 80 | super.visit(children.get(i)); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | currentIndex[0] = indexBuffer; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | currentPath.removeLast(); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | @Override |
---|
| 90 | public void visit(ISelection relationship) { |
---|
[1849] | 91 | // selections are never traversed as a traversal can have different orders |
---|
[1767] | 92 | // for semantically identical selections |
---|
| 93 | currentPath.add(relationship, currentIndex[0]); |
---|
| 94 | result.add(new TaskPath(currentPath)); |
---|
| 95 | currentPath.removeLast(); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | @Override |
---|
| 99 | public void visit(IMarkingTemporalRelationship relationship) { |
---|
| 100 | ITask markedTask = relationship.getMarkedTask(); |
---|
| 101 | |
---|
| 102 | while (markedTask instanceof IMarkingTemporalRelationship) { |
---|
| 103 | markedTask = ((IMarkingTemporalRelationship) markedTask).getMarkedTask(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | currentPath.add(relationship, currentIndex[0]); |
---|
| 107 | |
---|
[1849] | 108 | if (pathIsInList(currentPath, untraversedPaths)) { |
---|
[1767] | 109 | result.add(new TaskPath(currentPath)); |
---|
| 110 | } |
---|
| 111 | // if the child is an event task we do not traverse this marking temporal |
---|
| 112 | // relationship |
---|
| 113 | else if (markedTask instanceof IEventTask) { |
---|
| 114 | result.add(new TaskPath(currentPath)); |
---|
| 115 | } |
---|
| 116 | else { |
---|
| 117 | int indexBuffer = currentIndex[0]; |
---|
| 118 | currentIndex[0] = 0; |
---|
| 119 | super.visit(relationship); |
---|
| 120 | currentIndex[0] = indexBuffer; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | currentPath.removeLast(); |
---|
| 124 | } |
---|
| 125 | }); |
---|
| 126 | |
---|
| 127 | result.setComplete(); |
---|
| 128 | |
---|
| 129 | return result; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * |
---|
| 134 | */ |
---|
| 135 | public int[] getIndexesRootedBy(TaskPath pathToRoot) { |
---|
| 136 | int[] indexes = new int[] { -1, -1 }; |
---|
| 137 | |
---|
| 138 | for (int i = 0; i < traversalPathArray.length; i++) { |
---|
| 139 | TaskPath path = traversalPathArray[i]; |
---|
| 140 | |
---|
| 141 | // check if the path starts with the path to the root |
---|
| 142 | if (path.size() >= pathToRoot.size()) { |
---|
| 143 | boolean startsWithPathToRoot = true; |
---|
| 144 | |
---|
| 145 | for (int j = 0; (j < pathToRoot.size()) && (j < path.size()); j++) { |
---|
| 146 | if (!pathToRoot.get(j).equals(path.get(j))) { |
---|
| 147 | startsWithPathToRoot = false; |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | if (startsWithPathToRoot) { |
---|
| 153 | if (indexes[0] < 0) { |
---|
| 154 | indexes[0] = i; |
---|
| 155 | } |
---|
| 156 | indexes[1] = i; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return indexes; |
---|
| 162 | |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | /** |
---|
| 166 | * |
---|
| 167 | */ |
---|
| 168 | public TaskPath[] subList(int fromIndex, int toIndex) { |
---|
| 169 | return Arrays.copyOfRange(traversalPathArray, fromIndex, toIndex); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * |
---|
| 174 | */ |
---|
[1849] | 175 | public ITask getTask() { |
---|
| 176 | return task; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * |
---|
| 181 | */ |
---|
[1767] | 182 | public ITask get(int i) { |
---|
| 183 | return traversalPathArray[i].getLast(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * |
---|
| 188 | */ |
---|
| 189 | public int size() { |
---|
| 190 | return traversalPathArray.length; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * @return the traversalPaths |
---|
| 195 | */ |
---|
| 196 | public TaskPath[] getTraversalPaths() { |
---|
| 197 | return traversalPathArray; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * |
---|
| 202 | */ |
---|
| 203 | public void dump(PrintStream out) { |
---|
| 204 | for (TaskPath path : traversalPathArray) { |
---|
| 205 | out.print(path.getLast().getId()); |
---|
| 206 | out.print('\t'); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | out.println(); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * |
---|
| 214 | */ |
---|
| 215 | private void add(TaskPath taskPath) { |
---|
| 216 | traversalPaths.add(taskPath); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * |
---|
| 221 | */ |
---|
| 222 | private void setComplete() { |
---|
| 223 | traversalPathArray = traversalPaths.toArray(new TaskPath[traversalPaths.size()]); |
---|
| 224 | traversalPaths = null; |
---|
[1849] | 225 | task = traversalPathArray[0].getTask(0); |
---|
[1767] | 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * |
---|
| 230 | */ |
---|
| 231 | private static boolean pathIsInList(TaskPath path, List<TaskPath> pathList) { |
---|
| 232 | if (pathList != null) { |
---|
| 233 | for (TaskPath candidate : pathList) { |
---|
| 234 | if (path.equals(candidate)) { |
---|
| 235 | return true; |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | return false; |
---|
| 241 | } |
---|
| 242 | } |
---|