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.treeimpl; |
---|
16 | |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
29 | |
---|
30 | /** |
---|
31 | * <p> |
---|
32 | * this is the default implementation of the interface {@link ITaskBuilder}. It |
---|
33 | * does not do anything fancy except implementing the interface. In some situations, it performs |
---|
34 | * a check if the model or instances to be created a valid. However, this can not be done |
---|
35 | * in any situation of the creation process. |
---|
36 | * </p> |
---|
37 | * |
---|
38 | * @author Patrick Harms |
---|
39 | */ |
---|
40 | public class TaskBuilder implements ITaskBuilder { |
---|
41 | |
---|
42 | /* (non-Javadoc) |
---|
43 | * @see ITaskBuilder#addChild(ITaskInstance,ITaskInstance) |
---|
44 | */ |
---|
45 | @Override |
---|
46 | public void addChild(ITaskInstance parent, ITaskInstance child) throws IllegalArgumentException |
---|
47 | { |
---|
48 | if (!(parent instanceof TaskInstance)) { |
---|
49 | throw new IllegalArgumentException |
---|
50 | ("illegal type of task instance provided: " + parent.getClass()); |
---|
51 | } |
---|
52 | |
---|
53 | if (!(child instanceof TaskInstance)) { |
---|
54 | throw new IllegalArgumentException |
---|
55 | ("illegal type of task instance provided: " + parent.getClass()); |
---|
56 | } |
---|
57 | |
---|
58 | // check, that the correct number of children for the distinct types are added |
---|
59 | ITask task = parent.getTask(); |
---|
60 | |
---|
61 | if (task instanceof IEventTask) { |
---|
62 | throw new IllegalArgumentException |
---|
63 | ("can not add children to a task instance of an event task"); |
---|
64 | } |
---|
65 | else if (task instanceof ISelection) { |
---|
66 | if (parent.getChildren().size() > 0) { |
---|
67 | throw new IllegalArgumentException |
---|
68 | ("the instance of a selection must have at most one child"); |
---|
69 | } |
---|
70 | } |
---|
71 | else if (task instanceof IOptional) { |
---|
72 | if (parent.getChildren().size() > 1) { |
---|
73 | throw new IllegalArgumentException |
---|
74 | ("the instance of an optional must have at most one child"); |
---|
75 | } |
---|
76 | } |
---|
77 | /*else if (task instanceof IIteration) { |
---|
78 | for (ITaskInstance childInstance : parent.getChildren()) { |
---|
79 | if (!childInstance.getTask().equals(child.getTask())) { |
---|
80 | throw new IllegalArgumentException |
---|
81 | ("all children of an instance of an iteration must have exactly the " + |
---|
82 | "same type"); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | boolean foundChildTask = false; |
---|
88 | if (parent.getTask() instanceof IStructuringTemporalRelationship) { |
---|
89 | IStructuringTemporalRelationship parentTask = |
---|
90 | (IStructuringTemporalRelationship) parent.getTask(); |
---|
91 | |
---|
92 | for (ITask parentTaskChild : parentTask.getChildren()) { |
---|
93 | if (parentTaskChild.equals(child.getTask())) { |
---|
94 | foundChildTask = true; |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | else if (parent.getTask() instanceof IMarkingTemporalRelationship) { |
---|
100 | IMarkingTemporalRelationship parentTask = |
---|
101 | (IMarkingTemporalRelationship) parent.getTask(); |
---|
102 | |
---|
103 | foundChildTask = parentTask.getMarkedTask() != null ? |
---|
104 | parentTask.getMarkedTask().equals(child.getTask()) : false; |
---|
105 | } |
---|
106 | |
---|
107 | if (!foundChildTask) { |
---|
108 | throw new IllegalArgumentException |
---|
109 | ("the task of the child instance to be added does not belong to the children " + |
---|
110 | "of the task of the parent instance"); |
---|
111 | }*/ |
---|
112 | |
---|
113 | // finally, after all checks are positive, add the child |
---|
114 | ((TaskInstance) parent).addChild(child); |
---|
115 | } |
---|
116 | |
---|
117 | /* (non-Javadoc) |
---|
118 | * @see ITaskBuilder#addExecutedTask(IUserSession, ITaskInstance) |
---|
119 | */ |
---|
120 | @Override |
---|
121 | public void addExecutedTask(IUserSession session, ITaskInstance taskInstance) { |
---|
122 | if (!(session instanceof UserSession)) { |
---|
123 | throw new IllegalArgumentException |
---|
124 | ("illegal type of session provided: " + session.getClass()); |
---|
125 | } |
---|
126 | |
---|
127 | if (!(taskInstance instanceof TaskInstance)) { |
---|
128 | throw new IllegalArgumentException |
---|
129 | ("illegal type of task instance provided: " + taskInstance.getClass()); |
---|
130 | } |
---|
131 | |
---|
132 | ((UserSession) session).addExecutedTask(taskInstance); |
---|
133 | } |
---|
134 | |
---|
135 | /* (non-Javadoc) |
---|
136 | * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, ITaskInstance) |
---|
137 | */ |
---|
138 | @Override |
---|
139 | public void addTaskInstance(ITaskInstanceList taskInstanceList, ITaskInstance taskInstance) { |
---|
140 | if (taskInstanceList instanceof TaskInstance) { |
---|
141 | ((TaskInstance) taskInstanceList).addChild(taskInstance); |
---|
142 | } |
---|
143 | else if (taskInstanceList instanceof UserSession) { |
---|
144 | ((UserSession) taskInstanceList).addExecutedTask(taskInstance); |
---|
145 | } |
---|
146 | else { |
---|
147 | throw new IllegalArgumentException |
---|
148 | ("illegal type of task instance list provided: " + taskInstanceList.getClass()); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /* (non-Javadoc) |
---|
153 | * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, int, ITaskInstance) |
---|
154 | */ |
---|
155 | @Override |
---|
156 | public void addTaskInstance(ITaskInstanceList taskInstanceList, |
---|
157 | int index, |
---|
158 | ITaskInstance taskInstance) |
---|
159 | { |
---|
160 | if (taskInstanceList instanceof TaskInstance) { |
---|
161 | ((TaskInstance) taskInstanceList).addChild(index, taskInstance); |
---|
162 | } |
---|
163 | else if (taskInstanceList instanceof UserSession) { |
---|
164 | ((UserSession) taskInstanceList).addExecutedTask(index, taskInstance); |
---|
165 | } |
---|
166 | else { |
---|
167 | throw new IllegalArgumentException |
---|
168 | ("illegal type of task instance list provided: " + taskInstanceList.getClass()); |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | /* (non-Javadoc) |
---|
173 | * @see ITaskBuilder#setTaskInstance(ITaskInstanceList, int, ITaskInstance) |
---|
174 | */ |
---|
175 | @Override |
---|
176 | public void setTaskInstance(ITaskInstanceList taskInstanceList, |
---|
177 | int index, |
---|
178 | ITaskInstance taskInstance) |
---|
179 | { |
---|
180 | if (taskInstanceList instanceof TaskInstance) { |
---|
181 | ((TaskInstance) taskInstanceList).removeChild(index); |
---|
182 | ((TaskInstance) taskInstanceList).addChild(index, taskInstance); |
---|
183 | } |
---|
184 | else if (taskInstanceList instanceof UserSession) { |
---|
185 | ((UserSession) taskInstanceList).removeExecutedTask(index); |
---|
186 | ((UserSession) taskInstanceList).addExecutedTask(index, taskInstance); |
---|
187 | } |
---|
188 | else { |
---|
189 | throw new IllegalArgumentException |
---|
190 | ("illegal type of task instance list provided: " + taskInstanceList.getClass()); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | /* (non-Javadoc) |
---|
195 | * @see ITaskBuilder#setTask(ITaskInstance, ITask) |
---|
196 | */ |
---|
197 | @Override |
---|
198 | public void setTask(ITaskInstance taskInstance, ITask task) { |
---|
199 | if (!(taskInstance instanceof TaskInstance)) { |
---|
200 | throw new IllegalArgumentException |
---|
201 | ("illegal type of task instance provided: " + taskInstance.getClass()); |
---|
202 | } |
---|
203 | |
---|
204 | ((TaskInstance) taskInstance).setTask(task); |
---|
205 | } |
---|
206 | |
---|
207 | /* (non-Javadoc) |
---|
208 | * @see ITaskBuilder#addChild(ISequence, ITask) |
---|
209 | */ |
---|
210 | @Override |
---|
211 | public void addChild(ISequence parent, ITask child) { |
---|
212 | if (!(parent instanceof Sequence)) { |
---|
213 | throw new IllegalArgumentException |
---|
214 | ("illegal type of sequence provided: " + parent.getClass()); |
---|
215 | } |
---|
216 | |
---|
217 | addChildInternal((Sequence) parent, -1, child); |
---|
218 | } |
---|
219 | |
---|
220 | /* (non-Javadoc) |
---|
221 | * @see ITaskBuilder#addChild(ISequence, int, ITask) |
---|
222 | */ |
---|
223 | @Override |
---|
224 | public void addChild(ISequence parent, int index, ITask child) { |
---|
225 | if (!(parent instanceof Sequence)) { |
---|
226 | throw new IllegalArgumentException |
---|
227 | ("illegal type of sequence provided: " + parent.getClass()); |
---|
228 | } |
---|
229 | |
---|
230 | addChildInternal((Sequence) parent, index, child); |
---|
231 | } |
---|
232 | |
---|
233 | /* (non-Javadoc) |
---|
234 | * @see ITaskBuilder#setChild(ISequence, int, ITask) |
---|
235 | */ |
---|
236 | @Override |
---|
237 | public void setChild(ISequence parent, int index, ITask child) { |
---|
238 | if (!(parent instanceof Sequence)) { |
---|
239 | throw new IllegalArgumentException |
---|
240 | ("illegal type of sequence provided: " + parent.getClass()); |
---|
241 | } |
---|
242 | |
---|
243 | ((Sequence) parent).removeChild(index); |
---|
244 | addChildInternal((Sequence) parent, index, child); |
---|
245 | } |
---|
246 | |
---|
247 | /* (non-Javadoc) |
---|
248 | * @see ITaskBuilder#addChild(ISelection, ITask) |
---|
249 | */ |
---|
250 | @Override |
---|
251 | public void addChild(ISelection parent, ITask child) { |
---|
252 | if (!(parent instanceof Selection)) { |
---|
253 | throw new IllegalArgumentException |
---|
254 | ("illegal type of selection provided: " + parent.getClass()); |
---|
255 | } |
---|
256 | |
---|
257 | addChildInternal((Selection) parent, -1, child); |
---|
258 | } |
---|
259 | |
---|
260 | /* (non-Javadoc) |
---|
261 | * @see ITaskBuilder#setMarkedTask(IIteration, ITask) |
---|
262 | */ |
---|
263 | @Override |
---|
264 | public void setMarkedTask(IIteration iteration, ITask newChild) { |
---|
265 | if (!(iteration instanceof Iteration)) { |
---|
266 | throw new IllegalArgumentException |
---|
267 | ("illegal type of iteration provided: " + iteration.getClass()); |
---|
268 | } |
---|
269 | |
---|
270 | if (!(newChild instanceof Task)) { |
---|
271 | throw new IllegalArgumentException |
---|
272 | ("illegal type of task provided: " + newChild.getClass()); |
---|
273 | } |
---|
274 | |
---|
275 | ((Iteration) iteration).setMarkedTask(newChild); |
---|
276 | } |
---|
277 | |
---|
278 | /* (non-Javadoc) |
---|
279 | * @see ITaskTreeBuilder#setChild(IOptional, ITaskTreeNode) |
---|
280 | */ |
---|
281 | @Override |
---|
282 | public void setMarkedTask(IOptional optional, ITask newChild) { |
---|
283 | if (!(optional instanceof Optional)) { |
---|
284 | throw new IllegalArgumentException |
---|
285 | ("illegal type of optional provided: " + optional.getClass()); |
---|
286 | } |
---|
287 | |
---|
288 | if (!(newChild instanceof Task)) { |
---|
289 | throw new IllegalArgumentException |
---|
290 | ("illegal type of task provided: " + newChild.getClass()); |
---|
291 | } |
---|
292 | |
---|
293 | ((Optional) optional).setMarkedTask(newChild); |
---|
294 | } |
---|
295 | |
---|
296 | /* (non-Javadoc) |
---|
297 | * @see ITaskBuilder#removeChild(ISequence, int) |
---|
298 | */ |
---|
299 | @Override |
---|
300 | public void removeChild(ISequence parent, int index) { |
---|
301 | if (!(parent instanceof Sequence)) { |
---|
302 | throw new IllegalArgumentException |
---|
303 | ("illegal type of sequence provided: " + parent.getClass()); |
---|
304 | } |
---|
305 | |
---|
306 | ((Sequence) parent).removeChild(index); |
---|
307 | } |
---|
308 | |
---|
309 | /* (non-Javadoc) |
---|
310 | * @see ITaskBuilder#removeChild(ISelection, ITask) |
---|
311 | */ |
---|
312 | @Override |
---|
313 | public void removeChild(ISelection parent, ITask child) { |
---|
314 | if (!(parent instanceof Selection)) { |
---|
315 | throw new IllegalArgumentException |
---|
316 | ("illegal type of selection provided: " + parent.getClass()); |
---|
317 | } |
---|
318 | |
---|
319 | List<ITask> children = parent.getChildren(); |
---|
320 | |
---|
321 | for (int i = 0; i < children.size(); i++) { |
---|
322 | if ((children.get(i) == child) || |
---|
323 | ((children.get(i) != null) && (children.get(i).equals(child)))) |
---|
324 | { |
---|
325 | ((Selection) parent).removeChild(i); |
---|
326 | break; |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | /* (non-Javadoc) |
---|
332 | * @see ITaskBuilder#removeTaskInstance(ITaskInstanceList, int) |
---|
333 | */ |
---|
334 | @Override |
---|
335 | public void removeTaskInstance(ITaskInstanceList taskInstanceList, int index) { |
---|
336 | if (taskInstanceList instanceof TaskInstance) { |
---|
337 | ((TaskInstance) taskInstanceList).removeChild(index); |
---|
338 | } |
---|
339 | else if (taskInstanceList instanceof UserSession) { |
---|
340 | ((UserSession) taskInstanceList).removeExecutedTask(index); |
---|
341 | } |
---|
342 | else { |
---|
343 | throw new IllegalArgumentException |
---|
344 | ("illegal type of task instance list provided: " + taskInstanceList.getClass()); |
---|
345 | } |
---|
346 | } |
---|
347 | |
---|
348 | /* (non-Javadoc) |
---|
349 | * @see ITaskTreeBuilder#replaceChild(ISelection, ITaskTreeNode, ITaskTreeNode) |
---|
350 | */ |
---|
351 | @Override |
---|
352 | public void replaceChild(ISelection parent, ITask oldChild, ITask newChild) { |
---|
353 | if (!(parent instanceof Selection)) { |
---|
354 | throw new IllegalArgumentException |
---|
355 | ("illegal type of selection provided: " + parent.getClass()); |
---|
356 | } |
---|
357 | |
---|
358 | List<ITask> children = parent.getChildren(); |
---|
359 | |
---|
360 | for (int i = 0; i < children.size(); i++) { |
---|
361 | if ((children.get(i) == oldChild) || |
---|
362 | ((children.get(i) != null) && (children.get(i).equals(oldChild)))) |
---|
363 | { |
---|
364 | ((Selection) parent).removeChild(i); |
---|
365 | ((Selection) parent).addChild(i, newChild); |
---|
366 | break; |
---|
367 | } |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | /* (non-Javadoc) |
---|
372 | * @see ITaskBuilder#setDescription(ITask, java.lang.String) |
---|
373 | */ |
---|
374 | @Override |
---|
375 | public void setDescription(ITask parent, String description) { |
---|
376 | if (!(parent instanceof Task)) { |
---|
377 | throw new IllegalArgumentException |
---|
378 | ("illegal type of task provided: " + parent.getClass()); |
---|
379 | } |
---|
380 | |
---|
381 | ((Task) parent).setDescription(description); |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * <p> |
---|
386 | * internal convenience method for adding children to a structuring temporal relationship |
---|
387 | * including a check for the child type. |
---|
388 | * </p> |
---|
389 | */ |
---|
390 | private void addChildInternal(StructuringTemporalRelationship parent, int index, ITask child) { |
---|
391 | if (!(child instanceof Task)) { |
---|
392 | throw new IllegalArgumentException |
---|
393 | ("illegal type of task provided: " + child.getClass()); |
---|
394 | } |
---|
395 | |
---|
396 | if (index > -1) { |
---|
397 | parent.addChild(index, child); |
---|
398 | } |
---|
399 | else { |
---|
400 | parent.addChild(child); |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | } |
---|