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.FileNotFoundException; |
---|
18 | import java.io.FileOutputStream; |
---|
19 | import java.io.PrintStream; |
---|
20 | import java.util.HashMap; |
---|
21 | import java.util.List; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
25 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITemporalRelationship; |
---|
40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskPath; |
---|
42 | |
---|
43 | /** |
---|
44 | * TODO comment |
---|
45 | * |
---|
46 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
47 | * @author 2012, last modified by $Author: patrick$ |
---|
48 | */ |
---|
49 | public class TaskTreeEncoder { |
---|
50 | |
---|
51 | /** |
---|
52 | * |
---|
53 | */ |
---|
54 | public void encode(List<IUserSession> userSessions) |
---|
55 | throws FileNotFoundException |
---|
56 | { |
---|
57 | PrintStream out = null; |
---|
58 | try { |
---|
59 | out = new PrintStream(new FileOutputStream("userSessions.txt")); |
---|
60 | for (IUserSession session : userSessions) { |
---|
61 | encode(session, out); |
---|
62 | } |
---|
63 | } |
---|
64 | finally { |
---|
65 | if (out != null) { |
---|
66 | out.close(); |
---|
67 | } |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * |
---|
73 | */ |
---|
74 | public void encode(ITaskInstanceList taskInstanceList) |
---|
75 | throws FileNotFoundException |
---|
76 | { |
---|
77 | PrintStream out = null; |
---|
78 | try { |
---|
79 | out = new PrintStream(new FileOutputStream("taskInstanceList.txt")); |
---|
80 | encode(taskInstanceList, out); |
---|
81 | } |
---|
82 | finally { |
---|
83 | if (out != null) { |
---|
84 | out.close(); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * |
---|
91 | */ |
---|
92 | public void encode(ITask task) |
---|
93 | throws FileNotFoundException |
---|
94 | { |
---|
95 | PrintStream out = null; |
---|
96 | try { |
---|
97 | out = new PrintStream(new FileOutputStream("task.txt")); |
---|
98 | encode(task, out); |
---|
99 | } |
---|
100 | finally { |
---|
101 | if (out != null) { |
---|
102 | out.close(); |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * |
---|
109 | */ |
---|
110 | public void encode(ITaskInstanceList taskInstanceList, PrintStream out) { |
---|
111 | encode(taskInstanceList, out, null); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * |
---|
116 | */ |
---|
117 | public void encode(ITaskInstanceList taskInstanceList, |
---|
118 | PrintStream out, |
---|
119 | List<TaskPath> excludes) |
---|
120 | { |
---|
121 | TaskPath path = new TaskPath(); |
---|
122 | if (taskInstanceList instanceof IUserSession) { |
---|
123 | out.println("UserSession {"); |
---|
124 | } |
---|
125 | else { |
---|
126 | out.println("TaskInstances {"); |
---|
127 | } |
---|
128 | |
---|
129 | if (taskInstanceList instanceof ITaskInstance) { |
---|
130 | path.add(((ITaskInstance) taskInstanceList).getTask(), 0); |
---|
131 | } |
---|
132 | |
---|
133 | int i = 0; |
---|
134 | for (ITaskInstance taskInstance : taskInstanceList) { |
---|
135 | encode(taskInstance, out, " ", i++, path, excludes); |
---|
136 | } |
---|
137 | |
---|
138 | out.println('}'); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * |
---|
143 | */ |
---|
144 | public void encode(ITaskInstance taskInstance, PrintStream out) { |
---|
145 | encode(taskInstance, out, null); |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * |
---|
150 | */ |
---|
151 | public void encode(ITaskInstance taskInstance, PrintStream out, List<TaskPath> excludes) { |
---|
152 | TaskPath path = new TaskPath(); |
---|
153 | encode(taskInstance, out, "", 0, path, excludes); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * |
---|
158 | */ |
---|
159 | public void encode(ITask task, PrintStream out) { |
---|
160 | encode(task, out, "", 0); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * |
---|
165 | */ |
---|
166 | private void encode(ITaskInstance taskInstance, |
---|
167 | PrintStream out, |
---|
168 | String indent, |
---|
169 | int index, |
---|
170 | TaskPath parentPath, |
---|
171 | List<TaskPath> excludes) |
---|
172 | { |
---|
173 | ITask task = taskInstance.getTask(); |
---|
174 | TaskPath path = parentPath; |
---|
175 | path.add(task, index); |
---|
176 | |
---|
177 | if (task instanceof ITemporalRelationship) { |
---|
178 | if (index > 0) { |
---|
179 | out.println(); |
---|
180 | } |
---|
181 | out.print(indent); |
---|
182 | |
---|
183 | if (task instanceof ISequence) { |
---|
184 | out.print("Sequence "); |
---|
185 | } |
---|
186 | else if (task instanceof ISelection) { |
---|
187 | out.print("Selection "); |
---|
188 | } |
---|
189 | else if (task instanceof IIteration) { |
---|
190 | out.print("Iteration "); |
---|
191 | } |
---|
192 | else if (task instanceof IOptional) { |
---|
193 | out.print("Optional "); |
---|
194 | } |
---|
195 | |
---|
196 | out.print(task.getId()); |
---|
197 | |
---|
198 | if (task.getDescription() != null) { |
---|
199 | out.print('('); |
---|
200 | out.print(task.getDescription()); |
---|
201 | out.print(") "); |
---|
202 | } |
---|
203 | |
---|
204 | out.print('{'); |
---|
205 | |
---|
206 | if ((excludes == null) || (!excludes.contains(path))) { |
---|
207 | out.println(); |
---|
208 | } |
---|
209 | } |
---|
210 | else if (task instanceof IEventTask) { |
---|
211 | out.print(indent); |
---|
212 | out.print(task); |
---|
213 | } |
---|
214 | else { |
---|
215 | throw new RuntimeException |
---|
216 | ("unknown type of task referred by task instance " + taskInstance); |
---|
217 | } |
---|
218 | |
---|
219 | if ((excludes == null) || (!excludes.contains(path))) { |
---|
220 | int i = 0; |
---|
221 | |
---|
222 | if (taskInstance instanceof ITaskInstanceList) { |
---|
223 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
224 | encode(child, out, indent + " ", i++, path, excludes); |
---|
225 | } |
---|
226 | } |
---|
227 | else if (taskInstance instanceof ISelectionInstance) { |
---|
228 | encode(((ISelectionInstance) taskInstance).getChild(), out, indent + " ", i++, |
---|
229 | path, excludes); |
---|
230 | } |
---|
231 | else if (taskInstance instanceof IOptionalInstance) { |
---|
232 | if (((IOptionalInstance) taskInstance).getChild() != null) { |
---|
233 | encode(((IOptionalInstance) taskInstance).getChild(), out, indent + " ", i++, |
---|
234 | path, excludes); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | if (task instanceof ITemporalRelationship) { |
---|
239 | out.print(indent); |
---|
240 | out.print("}"); |
---|
241 | } |
---|
242 | } |
---|
243 | else { |
---|
244 | if (task instanceof ITemporalRelationship) { |
---|
245 | out.print(" ... }"); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | out.println(); |
---|
250 | |
---|
251 | parentPath.removeLast(); |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * |
---|
256 | */ |
---|
257 | private void encode(ITask task, |
---|
258 | PrintStream out, |
---|
259 | String indent, |
---|
260 | int index) |
---|
261 | { |
---|
262 | if (task instanceof ITemporalRelationship) { |
---|
263 | if (index > 0) { |
---|
264 | out.println(); |
---|
265 | } |
---|
266 | out.print(indent); |
---|
267 | |
---|
268 | if (task instanceof ISequence) { |
---|
269 | out.print("Sequence "); |
---|
270 | } |
---|
271 | else if (task instanceof ISelection) { |
---|
272 | out.print("Selection "); |
---|
273 | } |
---|
274 | else if (task instanceof IIteration) { |
---|
275 | out.print("Iteration "); |
---|
276 | } |
---|
277 | else if (task instanceof IOptional) { |
---|
278 | out.print("Optional "); |
---|
279 | } |
---|
280 | |
---|
281 | out.print(task.getId()); |
---|
282 | out.println(" {"); |
---|
283 | } |
---|
284 | else if (task instanceof IEventTask) { |
---|
285 | out.print(indent); |
---|
286 | out.print(task); |
---|
287 | } |
---|
288 | else { |
---|
289 | throw new RuntimeException("unknown type of task " + task); |
---|
290 | } |
---|
291 | |
---|
292 | int i = 0; |
---|
293 | |
---|
294 | if (task instanceof IStructuringTemporalRelationship) { |
---|
295 | for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
296 | encode(child, out, indent + " ", i++); |
---|
297 | } |
---|
298 | } |
---|
299 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
300 | encode(((IMarkingTemporalRelationship) task).getMarkedTask(), out, indent + " ", i++); |
---|
301 | } |
---|
302 | |
---|
303 | if (task instanceof ITemporalRelationship) { |
---|
304 | out.print(indent); |
---|
305 | out.print("}"); |
---|
306 | } |
---|
307 | |
---|
308 | out.println(); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * |
---|
313 | */ |
---|
314 | public void dumpAsCheckString(IUserSession userSession) { |
---|
315 | int[] typeCounters = new int[4]; |
---|
316 | Map<ITask, String> taskIds = new HashMap<ITask, String>(); |
---|
317 | |
---|
318 | for (ITaskInstance taskInstance : userSession) { |
---|
319 | dumpTaskInstanceAsCheckString(taskInstance, typeCounters, taskIds, ""); |
---|
320 | } |
---|
321 | } |
---|
322 | |
---|
323 | /** |
---|
324 | * |
---|
325 | */ |
---|
326 | private void dumpTaskInstanceAsCheckString(ITaskInstance taskInstance, |
---|
327 | int[] typeCounters, |
---|
328 | Map<ITask, String> taskIds, |
---|
329 | String indent) |
---|
330 | { |
---|
331 | ITask task = taskInstance.getTask(); |
---|
332 | |
---|
333 | System.out.print(" \""); |
---|
334 | System.out.print(indent); |
---|
335 | |
---|
336 | String id = taskIds.get(task); |
---|
337 | |
---|
338 | if (task instanceof ISequence) { |
---|
339 | if (id == null) { |
---|
340 | id = "sequence" + typeCounters[0]++; |
---|
341 | } |
---|
342 | |
---|
343 | System.out.print("Sequence "); |
---|
344 | System.out.print(id); |
---|
345 | System.out.println(" {\" +"); |
---|
346 | } |
---|
347 | else if (task instanceof IIteration) { |
---|
348 | if (id == null) { |
---|
349 | id = "iteration" + typeCounters[1]++; |
---|
350 | } |
---|
351 | |
---|
352 | System.out.print("Iteration "); |
---|
353 | System.out.print(id); |
---|
354 | System.out.println(" {\" +"); |
---|
355 | } |
---|
356 | else if (task instanceof ISelection) { |
---|
357 | if (id == null) { |
---|
358 | id = "selection" + typeCounters[2]++; |
---|
359 | } |
---|
360 | |
---|
361 | System.out.print("Selection "); |
---|
362 | System.out.print(id); |
---|
363 | System.out.println(" {\" +"); |
---|
364 | } |
---|
365 | else if (task instanceof IEventTask) { |
---|
366 | Event event = ((IEventTaskInstance) taskInstance).getEvent(); |
---|
367 | if (event.getType() instanceof TextInput) { |
---|
368 | if (id == null) { |
---|
369 | id = "textInput" + typeCounters[3]++; |
---|
370 | } |
---|
371 | |
---|
372 | System.out.print("TextInputEvent "); |
---|
373 | System.out.print(id); |
---|
374 | System.out.print(" \""); |
---|
375 | System.out.print(((TextInput) event.getType()).getEnteredText()); |
---|
376 | System.out.print("\""); |
---|
377 | } |
---|
378 | else { |
---|
379 | if (id == null) { |
---|
380 | id = "event" + typeCounters[3]++; |
---|
381 | } |
---|
382 | |
---|
383 | System.out.print("Event "); |
---|
384 | System.out.print(id); |
---|
385 | System.out.print(' '); |
---|
386 | System.out.print(event.getType().getName()); |
---|
387 | } |
---|
388 | System.out.print(" {}\" +"); |
---|
389 | } |
---|
390 | else { |
---|
391 | throw new RuntimeException("unknown type of task referred by task instance " + taskInstance); |
---|
392 | } |
---|
393 | |
---|
394 | taskIds.put(task, id); |
---|
395 | |
---|
396 | if (taskInstance instanceof ITaskInstanceList) { |
---|
397 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
398 | dumpTaskInstanceAsCheckString(child, typeCounters, taskIds, indent + " "); |
---|
399 | } |
---|
400 | } |
---|
401 | else if (taskInstance instanceof ISelectionInstance) { |
---|
402 | dumpTaskInstanceAsCheckString |
---|
403 | (((ISelectionInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
404 | } |
---|
405 | else if (taskInstance instanceof IOptionalInstance) { |
---|
406 | dumpTaskInstanceAsCheckString |
---|
407 | (((IOptionalInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
408 | } |
---|
409 | |
---|
410 | if (!(task instanceof IEventTask)) { |
---|
411 | System.out.print(" \""); |
---|
412 | System.out.print(indent); |
---|
413 | System.out.print("}\" +"); |
---|
414 | } |
---|
415 | |
---|
416 | System.out.println(); |
---|
417 | } |
---|
418 | |
---|
419 | } |
---|