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.IIteration; |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
38 | import de.ugoe.cs.autoquest.test.DummyGUIElement; |
---|
39 | import de.ugoe.cs.autoquest.test.DummyTextField; |
---|
40 | |
---|
41 | /** |
---|
42 | * TODO comment |
---|
43 | * |
---|
44 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
45 | * @author 2012, last modified by $Author: patrick$ |
---|
46 | */ |
---|
47 | public class TaskTreeDecoder { |
---|
48 | |
---|
49 | /** */ |
---|
50 | private static Pattern taskInstancePattern = Pattern.compile("([^{}]+)\\{|\\}"); |
---|
51 | |
---|
52 | /** */ |
---|
53 | private static Pattern taskInstanceDetailsPattern = |
---|
54 | Pattern.compile("\\s*(\\w*)\\s*([\\w\\(\\)\"]*)\\s*(\\(([\\w*\\s*]*)\\))?((\\w*)|(\".*\"))?"); |
---|
55 | |
---|
56 | /** */ |
---|
57 | private ITaskFactory taskFactory; |
---|
58 | |
---|
59 | /** */ |
---|
60 | private ITaskBuilder taskBuilder; |
---|
61 | |
---|
62 | /** */ |
---|
63 | Map<String, IEventTarget> targets = new HashMap<String, IEventTarget>(); |
---|
64 | |
---|
65 | /** */ |
---|
66 | Map<String, ITask> tasks = new HashMap<String, ITask>(); |
---|
67 | |
---|
68 | /** |
---|
69 | * |
---|
70 | */ |
---|
71 | public TaskTreeDecoder(ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
72 | super(); |
---|
73 | this.taskFactory = taskFactory; |
---|
74 | this.taskBuilder = taskBuilder; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * |
---|
79 | */ |
---|
80 | public ITaskInstanceList decode(String taskTreeSpec) { |
---|
81 | ITaskInstanceList taskInstanceList = null; |
---|
82 | |
---|
83 | Matcher taskMatcher = taskInstancePattern.matcher(taskTreeSpec); |
---|
84 | |
---|
85 | if (taskMatcher.find()) { |
---|
86 | taskInstanceList = parseTaskInstanceList(taskMatcher); |
---|
87 | } |
---|
88 | |
---|
89 | if (taskMatcher.find()) { |
---|
90 | throw new IllegalArgumentException("too many tasks specified"); |
---|
91 | } |
---|
92 | |
---|
93 | return taskInstanceList; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * |
---|
98 | */ |
---|
99 | private ITaskInstanceList parseTaskInstanceList(Matcher taskMatcher) { |
---|
100 | if ("}".equals(taskMatcher.group(1))) { |
---|
101 | throw new IllegalArgumentException("invalid task instance list specification"); |
---|
102 | } |
---|
103 | |
---|
104 | String taskDetails = taskMatcher.group(1); |
---|
105 | |
---|
106 | Matcher matcher = taskInstanceDetailsPattern.matcher(taskDetails); |
---|
107 | |
---|
108 | if (!matcher.find()) { |
---|
109 | throw new IllegalArgumentException("could not parse task details"); |
---|
110 | } |
---|
111 | |
---|
112 | String type = matcher.group(1); |
---|
113 | |
---|
114 | ITaskInstanceList list; |
---|
115 | |
---|
116 | if ("UserSession".equals(type)) { |
---|
117 | list = taskFactory.createUserSession(); |
---|
118 | } |
---|
119 | else if ("TaskInstances".equals(type)) { |
---|
120 | list = taskFactory.createNewTaskInstance(taskFactory.createNewSequence()); |
---|
121 | } |
---|
122 | else { |
---|
123 | throw new IllegalArgumentException("unknown type of task instance list: " + type); |
---|
124 | } |
---|
125 | |
---|
126 | while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) { |
---|
127 | ITaskInstance childInstance = parseTaskInstance(taskMatcher); |
---|
128 | |
---|
129 | if (!(list instanceof IUserSession)) { |
---|
130 | taskBuilder.addChild |
---|
131 | ((ISequence) ((ITaskInstance) list).getTask(), childInstance.getTask()); |
---|
132 | } |
---|
133 | |
---|
134 | taskBuilder.addTaskInstance(list, childInstance); |
---|
135 | } |
---|
136 | |
---|
137 | return list; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * |
---|
142 | */ |
---|
143 | private ITaskInstance parseTaskInstance(Matcher taskMatcher) { |
---|
144 | if ("}".equals(taskMatcher.group(1))) { |
---|
145 | throw new IllegalArgumentException("invalid task instance specification"); |
---|
146 | } |
---|
147 | |
---|
148 | String taskDetails = taskMatcher.group(1); |
---|
149 | |
---|
150 | Matcher matcher = taskInstanceDetailsPattern.matcher(taskDetails); |
---|
151 | |
---|
152 | if (!matcher.find()) { |
---|
153 | throw new IllegalArgumentException("could not parse task details"); |
---|
154 | } |
---|
155 | |
---|
156 | String type = matcher.group(1); |
---|
157 | String id = matcher.group(2); |
---|
158 | |
---|
159 | ITask task = tasks.get(id); |
---|
160 | |
---|
161 | if (task == null) { |
---|
162 | if ("Sequence".equals(type)) { |
---|
163 | task = taskFactory.createNewSequence(); |
---|
164 | } |
---|
165 | else if ("Selection".equals(type)) { |
---|
166 | task = taskFactory.createNewSelection(); |
---|
167 | } |
---|
168 | else if ("Iteration".equals(type)) { |
---|
169 | task = taskFactory.createNewIteration(); |
---|
170 | } |
---|
171 | else if ("Optional".equals(type)) { |
---|
172 | task = taskFactory.createNewOptional(); |
---|
173 | } |
---|
174 | else { |
---|
175 | task = createUserInteractionTask(matcher); |
---|
176 | } |
---|
177 | tasks.put(id, task); |
---|
178 | } |
---|
179 | |
---|
180 | if ((matcher.group(5) != null) && (!"".equals(matcher.group(5).trim()))) { |
---|
181 | taskBuilder.setDescription(task, matcher.group(5).trim()); |
---|
182 | } |
---|
183 | |
---|
184 | ITaskInstance instance = taskFactory.createNewTaskInstance(task); |
---|
185 | |
---|
186 | while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) { |
---|
187 | ITaskInstance childInstance = parseTaskInstance(taskMatcher); |
---|
188 | |
---|
189 | if (task instanceof ISequence) { |
---|
190 | taskBuilder.addChild((ISequence) task, childInstance.getTask()); |
---|
191 | } |
---|
192 | else if (task instanceof ISelection) { |
---|
193 | taskBuilder.addChild((ISelection) task, childInstance.getTask()); |
---|
194 | } |
---|
195 | else if (task instanceof IIteration) { |
---|
196 | if (((IIteration) task).getMarkedTask() == null) { |
---|
197 | taskBuilder.setMarkedTask((IIteration) task, childInstance.getTask()); |
---|
198 | } |
---|
199 | else if (!((IIteration) task).getMarkedTask().equals(childInstance.getTask())) { |
---|
200 | throw new IllegalArgumentException |
---|
201 | ("can not add more than one child to an iteration"); |
---|
202 | } |
---|
203 | } |
---|
204 | else if (task instanceof IOptional) { |
---|
205 | if (((IOptional) task).getMarkedTask() == null) { |
---|
206 | taskBuilder.setMarkedTask((IOptional) task, childInstance.getTask()); |
---|
207 | } |
---|
208 | else if (!((IOptional) task).getMarkedTask().equals(childInstance.getTask())) { |
---|
209 | throw new IllegalArgumentException |
---|
210 | ("can not add more than one child to an optional"); |
---|
211 | } |
---|
212 | } |
---|
213 | else { |
---|
214 | throw new IllegalArgumentException("can not add children to something that is no " + |
---|
215 | "sequence, selection, iteration, or optional"); |
---|
216 | } |
---|
217 | |
---|
218 | taskBuilder.addChild(instance, childInstance); |
---|
219 | } |
---|
220 | |
---|
221 | return instance; |
---|
222 | } |
---|
223 | |
---|
224 | private ITask createUserInteractionTask(Matcher matcher) { |
---|
225 | String evenType = matcher.group(1); |
---|
226 | String id = matcher.group(2); |
---|
227 | IEventTarget evenTarget = targets.get(id); |
---|
228 | if(evenTarget == null) { |
---|
229 | evenTarget = determineTarget(evenType, id, matcher.group(4)); |
---|
230 | targets.put(id, evenTarget); |
---|
231 | } |
---|
232 | IEventType eventType = determineType(evenType, matcher.group(4)); |
---|
233 | return taskFactory.createNewEventTask(eventType, evenTarget); |
---|
234 | } |
---|
235 | |
---|
236 | private IEventTarget determineTarget(String type, String id, String enteredText) { |
---|
237 | if ("TextInput".equals(type) && enteredText != null) { |
---|
238 | return new DummyTextField(enteredText); |
---|
239 | } else { |
---|
240 | return new DummyGUIElement(id); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | private IEventType determineType(String type, String enteredText) { |
---|
245 | if ("TextInput".equals(type) && enteredText != null) { |
---|
246 | return new TextInput(enteredText, new ArrayList<Event>()); |
---|
247 | } else { |
---|
248 | return new StringEventType(type); |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | } |
---|