source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/utils/TaskTraversal.java @ 1767

Last change on this file since 1767 was 1767, checked in by pharms, 10 years ago
  • first version of merging similar task trees
File size: 7.2 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.temporalrelation.utils;
16
17import java.io.PrintStream;
18import java.util.Arrays;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Set;
22
23import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskTraversingVisitor;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskPath;
30
31/**
32 * <p>
33 * TODO comment
34 * </p>
35 *
36 * @author Patrick Harms
37 */
38public class TaskTraversal {
39   
40    /** */
41    private List<TaskPath> traversalPaths = new LinkedList<TaskPath>();
42   
43    /** */
44    private TaskPath[] traversalPathArray = null;
45
46    /**
47     *
48     */
49    public static TaskTraversal getTraversal(ITask                task,
50                                             final Set<ITask>     untraversedTasks,
51                                             final List<TaskPath> untraversedPaths)
52    {
53        final TaskTraversal result = new TaskTraversal();
54        final TaskPath currentPath = new TaskPath();
55        final int[] currentIndex = new int[1];
56        currentIndex[0] = 0;
57       
58        task.accept(new DefaultTaskTraversingVisitor() {
59            @Override
60            public void visit(IEventTask eventTask) {
61                currentPath.add(eventTask, currentIndex[0]);
62                result.add(new TaskPath(currentPath));
63                currentPath.removeLast();
64            }
65
66            @Override
67            public void visit(ISequence relationship) {
68                currentPath.add(relationship, currentIndex[0]);
69               
70                if (pathIsInList(currentPath, untraversedPaths) ||
71                    untraversedTasks.contains(relationship))
72                {
73                    result.add(new TaskPath(currentPath));
74                }
75                else {
76                    int indexBuffer = currentIndex[0];
77                   
78                    List<ITask> children = relationship.getChildren();
79                    for (int i = 0; i < children.size(); i++) {
80                        currentIndex[0] = i;
81                        super.visit(children.get(i));
82                    }
83                   
84                    currentIndex[0] = indexBuffer;
85                }
86               
87                currentPath.removeLast();
88            }
89
90            @Override
91            public void visit(ISelection relationship) {
92                // selections are never traversed as a traversal can have different orders for
93                // for semantically identical selections
94                currentPath.add(relationship, currentIndex[0]);
95                result.add(new TaskPath(currentPath));
96                currentPath.removeLast();
97            }
98
99            @Override
100            public void visit(IMarkingTemporalRelationship relationship) {
101                ITask markedTask = relationship.getMarkedTask();
102               
103                while (markedTask instanceof IMarkingTemporalRelationship) {
104                    markedTask = ((IMarkingTemporalRelationship) markedTask).getMarkedTask();
105                }
106               
107                currentPath.add(relationship, currentIndex[0]);
108               
109                if (pathIsInList(currentPath, untraversedPaths) ||
110                    untraversedTasks.contains(relationship))
111                {
112                    result.add(new TaskPath(currentPath));
113                }
114                // if the child is an event task we do not traverse this marking temporal
115                // relationship
116                else if (markedTask instanceof IEventTask) {
117                    result.add(new TaskPath(currentPath));
118                }
119                else {
120                    int indexBuffer = currentIndex[0];
121                    currentIndex[0] = 0;
122                    super.visit(relationship);
123                    currentIndex[0] = indexBuffer;
124                }
125               
126                currentPath.removeLast();
127            }
128        });
129       
130        result.setComplete();
131
132        return result;
133    }
134
135    /**
136     *
137     */
138    public int[] getIndexesRootedBy(TaskPath pathToRoot) {
139        int[] indexes = new int[] { -1, -1 };
140       
141        for (int i = 0; i < traversalPathArray.length; i++) {
142            TaskPath path = traversalPathArray[i];
143           
144            // check if the path starts with the path to the root
145            if (path.size() >= pathToRoot.size()) {
146                boolean startsWithPathToRoot = true;
147           
148                for (int j = 0; (j < pathToRoot.size()) && (j < path.size()); j++) {
149                    if (!pathToRoot.get(j).equals(path.get(j))) {
150                        startsWithPathToRoot = false;
151                        break;
152                    }
153                }
154               
155                if (startsWithPathToRoot) {
156                    if (indexes[0] < 0) {
157                        indexes[0] = i;
158                    }
159                    indexes[1] = i;
160                }
161            }
162        }
163       
164        return indexes;
165       
166    }
167
168    /**
169     *
170     */
171    public TaskPath[] subList(int fromIndex, int toIndex) {
172        return Arrays.copyOfRange(traversalPathArray, fromIndex, toIndex);
173    }
174
175    /**
176     *
177     */
178    public ITask get(int i) {
179        return traversalPathArray[i].getLast();
180    }
181
182    /**
183     *
184     */
185    public int size() {
186        return traversalPathArray.length;
187    }
188
189    /**
190     * @return the traversalPaths
191     */
192    public TaskPath[] getTraversalPaths() {
193        return traversalPathArray;
194    }
195
196    /**
197     *
198     */
199    public void dump(PrintStream out) {
200        for (TaskPath path : traversalPathArray) {
201            out.print(path.getLast().getId());
202            out.print('\t');
203        }
204       
205        out.println();
206    }
207
208    /**
209     *
210     */
211    private void add(TaskPath taskPath) {
212        traversalPaths.add(taskPath);
213    }
214   
215    /**
216     *
217     */
218    private void setComplete() {
219        traversalPathArray = traversalPaths.toArray(new TaskPath[traversalPaths.size()]);
220        traversalPaths = null;
221    }
222
223    /**
224     *
225     */
226    private static boolean pathIsInList(TaskPath path, List<TaskPath> pathList) {
227        if (pathList != null) {
228            for (TaskPath candidate : pathList) {
229                if (path.equals(candidate)) {
230                    return true;
231                }
232            }
233        }
234       
235        return false;
236    }
237}
Note: See TracBrowser for help on using the repository browser.