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 java.util.ArrayList; |
---|
18 | import java.util.HashMap; |
---|
19 | import java.util.Map; |
---|
20 | import java.util.regex.Matcher; |
---|
21 | import java.util.regex.Pattern; |
---|
22 | |
---|
23 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
24 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
25 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
26 | import de.ugoe.cs.autoquest.eventcore.StringEventType; |
---|
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.IIterationInstance; |
---|
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.ISequenceInstance; |
---|
38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
44 | import de.ugoe.cs.autoquest.test.DummyGUIElement; |
---|
45 | import de.ugoe.cs.autoquest.test.DummyTextField; |
---|
46 | |
---|
47 | /** |
---|
48 | * TODO comment |
---|
49 | * |
---|
50 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
51 | * @author 2012, last modified by $Author: patrick$ |
---|
52 | */ |
---|
53 | public class TaskTreeDecoder { |
---|
54 | |
---|
55 | /** */ |
---|
56 | private static Pattern taskInstancePattern = Pattern.compile("([^{}]+)\\{|\\}"); |
---|
57 | |
---|
58 | /** */ |
---|
59 | private static Pattern taskInstanceDetailsPattern = |
---|
60 | Pattern.compile("\\s*(\\w*)\\s*([\\w\\(\\)\"]*)\\s*(\\(([\\w*\\s*]*)\\))?((\\w*)|(\".*\"))?"); |
---|
61 | |
---|
62 | /** */ |
---|
63 | private ITaskFactory taskFactory; |
---|
64 | |
---|
65 | /** */ |
---|
66 | private ITaskBuilder taskBuilder; |
---|
67 | |
---|
68 | /** */ |
---|
69 | Map<String, IEventTarget> targets = new HashMap<String, IEventTarget>(); |
---|
70 | |
---|
71 | /** */ |
---|
72 | Map<String, ITask> tasks = new HashMap<String, ITask>(); |
---|
73 | |
---|
74 | /** |
---|
75 | * |
---|
76 | */ |
---|
77 | public TaskTreeDecoder(ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
78 | super(); |
---|
79 | this.taskFactory = taskFactory; |
---|
80 | this.taskBuilder = taskBuilder; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * |
---|
85 | */ |
---|
86 | public ITaskInstanceList decode(String taskTreeSpec) { |
---|
87 | ITaskInstanceList taskInstanceList = null; |
---|
88 | |
---|
89 | Matcher taskMatcher = taskInstancePattern.matcher(taskTreeSpec); |
---|
90 | |
---|
91 | if (taskMatcher.find()) { |
---|
92 | taskInstanceList = parseTaskInstanceList(taskMatcher); |
---|
93 | } |
---|
94 | |
---|
95 | if (taskMatcher.find()) { |
---|
96 | throw new IllegalArgumentException("too many tasks specified"); |
---|
97 | } |
---|
98 | |
---|
99 | return taskInstanceList; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * |
---|
104 | */ |
---|
105 | private ITaskInstanceList parseTaskInstanceList(Matcher taskMatcher) { |
---|
106 | if ("}".equals(taskMatcher.group(1))) { |
---|
107 | throw new IllegalArgumentException("invalid task instance list specification"); |
---|
108 | } |
---|
109 | |
---|
110 | String taskDetails = taskMatcher.group(1); |
---|
111 | |
---|
112 | Matcher matcher = taskInstanceDetailsPattern.matcher(taskDetails); |
---|
113 | |
---|
114 | if (!matcher.find()) { |
---|
115 | throw new IllegalArgumentException("could not parse task details"); |
---|
116 | } |
---|
117 | |
---|
118 | String type = matcher.group(1); |
---|
119 | |
---|
120 | ITaskInstanceList list; |
---|
121 | |
---|
122 | if ("UserSession".equals(type)) { |
---|
123 | list = taskFactory.createUserSession(); |
---|
124 | } |
---|
125 | else if ("TaskInstances".equals(type)) { |
---|
126 | list = taskFactory.createNewTaskInstance(taskFactory.createNewSequence()); |
---|
127 | } |
---|
128 | else { |
---|
129 | throw new IllegalArgumentException("unknown type of task instance list: " + type); |
---|
130 | } |
---|
131 | |
---|
132 | while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) { |
---|
133 | ITaskInstance childInstance = parseTaskInstance(taskMatcher); |
---|
134 | |
---|
135 | if (!(list instanceof IUserSession)) { |
---|
136 | taskBuilder.addChild |
---|
137 | ((ISequence) ((ITaskInstance) list).getTask(), childInstance.getTask()); |
---|
138 | } |
---|
139 | |
---|
140 | taskBuilder.addTaskInstance(list, childInstance); |
---|
141 | } |
---|
142 | |
---|
143 | return list; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * |
---|
148 | */ |
---|
149 | private ITaskInstance parseTaskInstance(Matcher taskMatcher) { |
---|
150 | if ("}".equals(taskMatcher.group(1))) { |
---|
151 | throw new IllegalArgumentException("invalid task instance specification"); |
---|
152 | } |
---|
153 | |
---|
154 | String taskDetails = taskMatcher.group(1); |
---|
155 | |
---|
156 | Matcher matcher = taskInstanceDetailsPattern.matcher(taskDetails); |
---|
157 | |
---|
158 | if (!matcher.find()) { |
---|
159 | throw new IllegalArgumentException("could not parse task details"); |
---|
160 | } |
---|
161 | |
---|
162 | String type = matcher.group(1); |
---|
163 | String id = matcher.group(2); |
---|
164 | |
---|
165 | ITask task = tasks.get(id); |
---|
166 | |
---|
167 | if (task == null) { |
---|
168 | if ("Sequence".equals(type)) { |
---|
169 | task = taskFactory.createNewSequence(); |
---|
170 | } |
---|
171 | else if ("Selection".equals(type)) { |
---|
172 | task = taskFactory.createNewSelection(); |
---|
173 | } |
---|
174 | else if ("Iteration".equals(type)) { |
---|
175 | task = taskFactory.createNewIteration(); |
---|
176 | } |
---|
177 | else if ("Optional".equals(type)) { |
---|
178 | task = taskFactory.createNewOptional(); |
---|
179 | } |
---|
180 | else { |
---|
181 | task = createUserInteractionTaskInstance(matcher).getTask(); |
---|
182 | } |
---|
183 | tasks.put(id, task); |
---|
184 | } |
---|
185 | |
---|
186 | ITaskInstance instance; |
---|
187 | |
---|
188 | if (task instanceof ISequence) { |
---|
189 | instance = taskFactory.createNewTaskInstance((ISequence) task); |
---|
190 | } |
---|
191 | else if (task instanceof ISelection) { |
---|
192 | instance = taskFactory.createNewTaskInstance((ISelection) task); |
---|
193 | } |
---|
194 | else if (task instanceof IIteration) { |
---|
195 | instance = taskFactory.createNewTaskInstance((IIteration) task); |
---|
196 | } |
---|
197 | else if (task instanceof IOptional) { |
---|
198 | instance = taskFactory.createNewTaskInstance((IOptional) task); |
---|
199 | } |
---|
200 | else { |
---|
201 | instance = taskFactory.createNewTaskInstance |
---|
202 | ((IEventTask) task, |
---|
203 | ((IEventTaskInstance) task.getInstances().iterator().next()).getEvent()); |
---|
204 | } |
---|
205 | |
---|
206 | while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) { |
---|
207 | ITaskInstance childInstance = parseTaskInstance(taskMatcher); |
---|
208 | |
---|
209 | if (task instanceof ISequence) { |
---|
210 | taskBuilder.addChild((ISequence) task, childInstance.getTask()); |
---|
211 | } |
---|
212 | else if (task instanceof ISelection) { |
---|
213 | taskBuilder.addChild((ISelection) task, childInstance.getTask()); |
---|
214 | } |
---|
215 | else if (task instanceof IIteration) { |
---|
216 | if (((IIteration) task).getMarkedTask() == null) { |
---|
217 | taskBuilder.setMarkedTask((IIteration) task, childInstance.getTask()); |
---|
218 | } |
---|
219 | else if (!((IIteration) task).getMarkedTask().equals(childInstance.getTask())) { |
---|
220 | throw new IllegalArgumentException |
---|
221 | ("can not add more than one child to an iteration"); |
---|
222 | } |
---|
223 | } |
---|
224 | else if (task instanceof IOptional) { |
---|
225 | if (((IOptional) task).getMarkedTask() == null) { |
---|
226 | taskBuilder.setMarkedTask((IOptional) task, childInstance.getTask()); |
---|
227 | } |
---|
228 | else if (!((IOptional) task).getMarkedTask().equals(childInstance.getTask())) { |
---|
229 | throw new IllegalArgumentException |
---|
230 | ("can not add more than one child to an optional"); |
---|
231 | } |
---|
232 | } |
---|
233 | else { |
---|
234 | throw new IllegalArgumentException("can not add children to something that is no " + |
---|
235 | "sequence, selection, iteration, or optional"); |
---|
236 | } |
---|
237 | |
---|
238 | if (instance instanceof ISequenceInstance) { |
---|
239 | taskBuilder.addChild((ISequenceInstance) instance, childInstance); |
---|
240 | } |
---|
241 | else if (instance instanceof ISelectionInstance) { |
---|
242 | taskBuilder.setChild((ISelectionInstance) instance, childInstance); |
---|
243 | } |
---|
244 | else if (instance instanceof IIterationInstance) { |
---|
245 | taskBuilder.addChild((IIterationInstance) instance, childInstance); |
---|
246 | } |
---|
247 | else if (instance instanceof IOptionalInstance) { |
---|
248 | taskBuilder.setChild((IOptionalInstance) instance, childInstance); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | return instance; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * <p> |
---|
257 | * </p> |
---|
258 | * |
---|
259 | * @param matcher |
---|
260 | * @return |
---|
261 | */ |
---|
262 | private ITaskInstance createUserInteractionTaskInstance(Matcher matcher) { |
---|
263 | String evenType = matcher.group(1); |
---|
264 | String id = matcher.group(2); |
---|
265 | IEventTarget eventTarget = targets.get(id); |
---|
266 | if (eventTarget == null) { |
---|
267 | eventTarget = determineTarget(evenType, id, matcher.group(4)); |
---|
268 | targets.put(id, eventTarget); |
---|
269 | } |
---|
270 | IEventType eventType = determineType(evenType, matcher.group(4)); |
---|
271 | IEventTask task = taskFactory.createNewEventTask(eventType + " --> " + eventTarget); |
---|
272 | |
---|
273 | return taskFactory.createNewTaskInstance(task, new Event(eventType, eventTarget)); |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * <p> |
---|
278 | * </p> |
---|
279 | * |
---|
280 | * @param type |
---|
281 | * @param id |
---|
282 | * @param enteredText |
---|
283 | * @return |
---|
284 | */ |
---|
285 | private IEventTarget determineTarget(String type, String id, String enteredText) { |
---|
286 | if ("TextInput".equals(type) && enteredText != null) { |
---|
287 | return new DummyTextField(enteredText); |
---|
288 | } |
---|
289 | else { |
---|
290 | return new DummyGUIElement(id); |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * <p> |
---|
296 | * </p> |
---|
297 | * |
---|
298 | * @param type |
---|
299 | * @param enteredText |
---|
300 | * @return |
---|
301 | */ |
---|
302 | private IEventType determineType(String type, String enteredText) { |
---|
303 | if ("TextInput".equals(type) && enteredText != null) { |
---|
304 | return new TextInput(enteredText, new ArrayList<Event>()); |
---|
305 | } |
---|
306 | else { |
---|
307 | return new StringEventType(type); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | } |
---|