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

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