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