source: trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeEncoder.java @ 1638

Last change on this file since 1638 was 1638, checked in by pharms, 10 years ago
  • some bugfixes and extensions for these test utils
File size: 11.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;
16
17import static org.junit.Assert.*;
18
19import java.io.FileNotFoundException;
20import java.io.FileOutputStream;
21import java.io.PrintStream;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
26import de.ugoe.cs.autoquest.eventcore.Event;
27import de.ugoe.cs.autoquest.eventcore.gui.TextInput;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
36import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
37import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship;
38import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
39import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
40import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
41import de.ugoe.cs.autoquest.tasktrees.treeifc.ITemporalRelationship;
42import 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 */
50public 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            if (((IOptionalInstance) taskInstance).getChild() != null) {
192                encode(((IOptionalInstance) taskInstance).getChild(), out, indent + "  ", i++);
193            }
194        }
195
196        if (task instanceof ITemporalRelationship) {
197            out.print(indent);
198            out.print("}");
199        }
200
201        out.println();
202    }
203   
204    /**
205     *
206     */
207    private void encode(ITask         task,
208                        PrintStream   out,
209                        String        indent,
210                        int           index)
211    {
212        if (task instanceof ITemporalRelationship) {
213            if (index > 0) {
214                out.println();
215            }
216            out.print(indent);
217
218            if (task instanceof ISequence) {
219                out.print("Sequence ");
220            }
221            else if (task instanceof ISelection) {
222                out.print("Selection ");
223            }           
224            else if (task instanceof IIteration) {
225                out.print("Iteration ");
226            }           
227            else if (task instanceof IOptional) {
228                out.print("Optional ");
229            }
230           
231            out.print(task.getId());
232            out.println(" {");
233        }
234        else if (task instanceof IEventTask) {
235            out.print(indent);
236            out.print(task);
237        }
238        else {
239            fail("unknown type of task " + task);
240        }
241
242        int i = 0;
243       
244        if (task instanceof IStructuringTemporalRelationship) {
245            for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) {
246                encode(child, out, indent + "  ", i++);
247            }
248        }
249        else if (task instanceof IMarkingTemporalRelationship) {
250            encode(((IMarkingTemporalRelationship) task).getMarkedTask(), out, indent + "  ", i++);
251        }
252
253        if (task instanceof ITemporalRelationship) {
254            out.print(indent);
255            out.print("}");
256        }
257
258        out.println();
259    }
260   
261    /**
262     *
263     */
264    public void dumpAsCheckString(IUserSession userSession) {
265        int[] typeCounters = new int[4];
266        Map<ITask, String> taskIds = new HashMap<ITask, String>();
267       
268        for (ITaskInstance taskInstance : userSession) {
269            dumpTaskInstanceAsCheckString(taskInstance, typeCounters, taskIds, "");
270        }
271    }
272
273    /**
274     *
275     */
276    private void dumpTaskInstanceAsCheckString(ITaskInstance      taskInstance,
277                                               int[]              typeCounters,
278                                               Map<ITask, String> taskIds,
279                                               String             indent)
280    {
281        ITask task = taskInstance.getTask();
282       
283        System.out.print("       \"");
284        System.out.print(indent);
285
286        String id = taskIds.get(task);
287       
288        if (task instanceof ISequence) {
289            if (id == null) {
290                id = "sequence" + typeCounters[0]++;
291            }
292           
293            System.out.print("Sequence ");
294            System.out.print(id);
295            System.out.println(" {\" +");
296        }
297        else if (task instanceof IIteration) {
298            if (id == null) {
299                id = "iteration" + typeCounters[1]++;
300            }
301           
302            System.out.print("Iteration ");
303            System.out.print(id);
304            System.out.println(" {\" +");
305        }
306        else if (task instanceof ISelection) {
307            if (id == null) {
308                id = "selection" + typeCounters[2]++;
309            }
310           
311            System.out.print("Selection ");
312            System.out.print(id);
313            System.out.println(" {\" +");
314        }
315        else if (task instanceof IEventTask) {
316            Event event = ((IEventTaskInstance) taskInstance).getEvent();
317            if (event.getType() instanceof TextInput) {
318                if (id == null) {
319                    id = "textInput" + typeCounters[3]++;
320                }
321               
322                System.out.print("TextInputEvent ");
323                System.out.print(id);
324                System.out.print(" \"");
325                System.out.print(((TextInput) event.getType()).getEnteredText());
326                System.out.print("\"");
327            }
328            else {
329                if (id == null) {
330                    id = "event" + typeCounters[3]++;
331                }
332               
333                System.out.print("Event ");
334                System.out.print(id);
335                System.out.print(' ');
336                System.out.print(event.getType().getName());
337            }
338            System.out.print(" {}\" +");
339        }
340        else {
341            fail("unknown type of task referred by task instance " + taskInstance);
342        }
343
344        taskIds.put(task, id);
345       
346        if (taskInstance instanceof ITaskInstanceList) {
347            for (ITaskInstance child : (ITaskInstanceList) taskInstance) {
348                dumpTaskInstanceAsCheckString(child, typeCounters, taskIds, indent + "  ");
349            }
350        }
351        else if (taskInstance instanceof ISelectionInstance) {
352            dumpTaskInstanceAsCheckString
353                (((ISelectionInstance) taskInstance).getChild(), typeCounters, taskIds, indent + "  ");
354        }
355        else if (taskInstance instanceof IOptionalInstance) {
356            dumpTaskInstanceAsCheckString
357                (((IOptionalInstance) taskInstance).getChild(), typeCounters, taskIds, indent + "  ");
358        }
359
360        if (!(task instanceof IEventTask)) {
361            System.out.print("       \"");
362            System.out.print(indent);
363            System.out.print("}\" +");
364        }
365
366        System.out.println();
367    }
368
369}
Note: See TracBrowser for help on using the repository browser.