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

Last change on this file since 1146 was 1146, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
File size: 8.1 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.gui.TextInput;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.ITemporalRelationship;
36import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
37
38/**
39 * TODO comment
40 *
41 * @version $Revision: $ $Date: 01.04.2012$
42 * @author 2012, last modified by $Author: patrick$
43 */
44public class TaskTreeEncoder {
45   
46    /**
47     *
48     */
49    public void encode(List<IUserSession> userSessions)
50        throws FileNotFoundException
51    {
52        PrintStream out = null;
53        try {
54            out = new PrintStream(new FileOutputStream("userSessions.txt"));
55            for (IUserSession session : userSessions) {
56                encode(session, out);
57            }
58        }
59        finally {
60            if (out != null) {
61                out.close();
62            }
63        }
64    }
65   
66    /**
67     *
68     */
69    public void encode(ITaskInstanceList taskInstanceList)
70        throws FileNotFoundException
71    {
72        PrintStream out = null;
73        try {
74            out = new PrintStream(new FileOutputStream("taskInstanceList.txt"));
75            encode(taskInstanceList, out);
76        }
77        finally {
78            if (out != null) {
79                out.close();
80            }
81        }
82    }
83
84    /**
85     *
86     */
87    public void encode(ITaskInstanceList taskInstanceList, PrintStream out) {
88        if (taskInstanceList instanceof IUserSession) {
89            out.println("UserSession {");
90        }
91        else {
92            out.println("TaskInstances {");
93        }
94       
95        for (ITaskInstance taskInstance : taskInstanceList) {
96            encode(taskInstance, out, "  ", 0);
97        }
98       
99        out.println('}');
100    }
101
102    /**
103     *
104     */
105    public void encode(ITaskInstance taskInstance, PrintStream out) {
106        encode(taskInstance, out, "", 0);
107    }
108   
109    /**
110     *
111     */
112    private void encode(ITaskInstance taskInstance,
113                        PrintStream   out,
114                        String        indent,
115                        int           index)
116    {
117        ITask task = taskInstance.getTask();
118       
119        if (task instanceof ITemporalRelationship) {
120            if (index > 0) {
121                out.println();
122            }
123            out.print(indent);
124
125            if (task instanceof ISequence) {
126                out.print("Sequence ");
127            }
128            else if (task instanceof ISelection) {
129                out.print("Selection ");
130            }           
131            else if (task instanceof IIteration) {
132                out.print("Iteration ");
133            }           
134            else if (task instanceof IOptional) {
135                out.print("Optional ");
136            }
137           
138            out.print(task.getId());
139            out.println(" {");
140        }
141        else if (task instanceof IEventTask) {
142            out.print(indent);
143            out.print(((IEventTask) task).getEventType());
144            out.print(" ");
145            out.print(((IEventTask) task).getEventTarget().getStringIdentifier());
146//            if (((IEventTask) node).getEventTarget() instanceof IGUIElement) {
147//              out.print(" ");
148//              out.print(((IGUIElement) ((IEventTask) node).getEventTarget()).getSpecification());
149//            }
150        }
151        else {
152            fail("unknown type of task referred by task instance " + taskInstance);
153        }
154
155        int i = 0;
156        for (ITaskInstance child : taskInstance.getChildren()) {
157            encode(child, out, indent + "  ", i++);
158        }
159
160        if (task instanceof ITemporalRelationship) {
161            out.print(indent);
162            out.print("}");
163        }
164
165        out.println();
166    }
167   
168    /**
169     *
170     */
171    public void dumpAsCheckString(IUserSession userSession) {
172        int[] typeCounters = new int[4];
173        Map<ITask, String> taskIds = new HashMap<ITask, String>();
174       
175        for (ITaskInstance taskInstance : userSession) {
176            dumpTaskInstanceAsCheckString(taskInstance, typeCounters, taskIds, "");
177        }
178    }
179
180    /**
181     *
182     */
183    private void dumpTaskInstanceAsCheckString(ITaskInstance      taskInstance,
184                                               int[]              typeCounters,
185                                               Map<ITask, String> taskIds,
186                                               String             indent)
187    {
188        ITask task = taskInstance.getTask();
189       
190        System.out.print("       \"");
191        System.out.print(indent);
192
193        String id = taskIds.get(task);
194       
195        if (task instanceof ISequence) {
196            if (id == null) {
197                id = "sequence" + typeCounters[0]++;
198            }
199           
200            System.out.print("Sequence ");
201            System.out.print(id);
202            System.out.println(" {\" +");
203        }
204        else if (task instanceof IIteration) {
205            if (id == null) {
206                id = "iteration" + typeCounters[1]++;
207            }
208           
209            System.out.print("Iteration ");
210            System.out.print(id);
211            System.out.println(" {\" +");
212        }
213        else if (task instanceof ISelection) {
214            if (id == null) {
215                id = "selection" + typeCounters[2]++;
216            }
217           
218            System.out.print("Selection ");
219            System.out.print(id);
220            System.out.println(" {\" +");
221        }
222        else if (task instanceof IEventTask) {
223            if (((IEventTask) task).getEventType() instanceof TextInput) {
224                if (id == null) {
225                    id = "textInput" + typeCounters[3]++;
226                }
227               
228                System.out.print("TextInputEvent ");
229                System.out.print(id);
230                System.out.print(" \"");
231                System.out.print(((TextInput) ((IEventTask) task).getEventType()).getEnteredText());
232                System.out.print("\"");
233            }
234            else {
235                if (id == null) {
236                    id = "event" + typeCounters[3]++;
237                }
238               
239                System.out.print("Event ");
240                System.out.print(id);
241                System.out.print(' ');
242                System.out.print(((IEventTask) task).getEventType().getName());
243            }
244            System.out.print(" {}\" +");
245        }
246        else {
247            fail("unknown type of task referred by task instance " + taskInstance);
248        }
249
250        taskIds.put(task, id);
251       
252        for (ITaskInstance child : taskInstance.getChildren()) {
253            dumpTaskInstanceAsCheckString(child, typeCounters, taskIds, indent + "  ");
254        }
255
256        if (!(task instanceof IEventTask)) {
257            System.out.print("       \"");
258            System.out.print(indent);
259            System.out.print("}\" +");
260        }
261
262        System.out.println();
263    }
264
265}
Note: See TracBrowser for help on using the repository browser.