source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskBuilder.java @ 1294

Last change on this file since 1294 was 1294, checked in by pharms, 11 years ago
  • rework of task model to move event instance stuff to task instances
  • introduction of sequence, selection, iteration and optional instances
  • Property svn:executable set to *
File size: 19.5 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.treeimpl;
16
17import java.util.List;
18
19import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
32
33/**
34 * <p>
35 * this is the default implementation of the interface {@link ITaskBuilder}. It
36 * does not do anything fancy except implementing the interface. In some situations, it performs
37 * a check if the model or instances to be created a valid. However, this can not be done
38 * in any situation of the creation process.
39 * </p>
40 *
41 * @author Patrick Harms
42 */
43public class TaskBuilder implements ITaskBuilder {
44
45    /* (non-Javadoc)
46     * @see ITaskBuilder#addChild(ISequenceInstance, ITaskInstance)
47     */
48    @Override
49    public void addChild(ISequenceInstance instance, ITaskInstance child)
50        throws IllegalArgumentException
51    {
52        if (!(instance instanceof SequenceInstance)) {
53            throw new IllegalArgumentException
54                ("illegal type of sequence instance provided: " + instance.getClass());
55        }
56
57        if (!(child instanceof TaskInstance)) {
58            throw new IllegalArgumentException
59                ("illegal type of task instance provided: " + child.getClass());
60        }
61       
62        /*IStructuringTemporalRelationship parentTask =
63            (IStructuringTemporalRelationship) parent.getTask();
64       
65        for (ITask parentTaskChild : parentTask.getChildren()) {
66            if (parentTaskChild.equals(child.getTask())) {
67                foundChildTask = true;
68                break;
69            }
70        }
71           
72        if (!foundChildTask) {
73            throw new IllegalArgumentException
74                ("the task of the child instance to be added does not belong to the children " +
75                 "of the task of the parent instance");
76        }*/
77
78        ((SequenceInstance) instance).addChild(child);
79    }
80
81    /* (non-Javadoc)
82     * @see ITaskBuilder#addChild(ISequenceInstance, int, ITaskInstance)
83     */
84    public void addChild(ISequenceInstance instance, int index, ITaskInstance child)
85        throws IllegalArgumentException
86    {
87        if (!(instance instanceof SequenceInstance)) {
88            throw new IllegalArgumentException
89                ("illegal type of sequence instance provided: " + instance.getClass());
90        }
91
92        if (!(child instanceof TaskInstance)) {
93            throw new IllegalArgumentException
94                ("illegal type of task instance provided: " + child.getClass());
95        }
96       
97        /*IStructuringTemporalRelationship parentTask =
98            (IStructuringTemporalRelationship) parent.getTask();
99       
100        for (ITask parentTaskChild : parentTask.getChildren()) {
101            if (parentTaskChild.equals(child.getTask())) {
102                foundChildTask = true;
103                break;
104            }
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        ((SequenceInstance) instance).addChild(index, child);
114    }
115
116    /* (non-Javadoc)
117     * @see ITaskBuilder#addChild(IIterationInstance, ITaskInstance)
118     */
119    @Override
120    public void addChild(IIterationInstance instance, ITaskInstance child)
121        throws IllegalArgumentException
122    {
123        if (!(instance instanceof IterationInstance)) {
124            throw new IllegalArgumentException
125                ("illegal type of iteration instance provided: " + instance.getClass());
126        }
127
128        if (!(child instanceof TaskInstance)) {
129            throw new IllegalArgumentException
130                ("illegal type of task instance provided: " + child.getClass());
131        }
132       
133        /*IStructuringTemporalRelationship parentTask =
134            (IStructuringTemporalRelationship) parent.getTask();
135       
136        IMarkingTemporalRelationship parentTask =
137            (IMarkingTemporalRelationship) parent.getTask();
138           
139        foundChildTask = parentTask.getMarkedTask() != null ?
140            parentTask.getMarkedTask().equals(child.getTask()) : false;
141           
142        if (!foundChildTask) {
143            throw new IllegalArgumentException
144                ("the task of the child instance to be added does not belong to the children " +
145                 "of the task of the parent instance");
146        }*/
147
148        ((IterationInstance) instance).addChild(child);
149    }
150
151    /* (non-Javadoc)
152     * @see ITaskBuilder#addChild(IIterationInstance, int, ITaskInstance)
153     */
154    public void addChild(IIterationInstance instance, int index, ITaskInstance child)
155        throws IllegalArgumentException
156    {
157        if (!(instance instanceof IterationInstance)) {
158            throw new IllegalArgumentException
159                ("illegal type of iteration instance provided: " + instance.getClass());
160        }
161
162        if (!(child instanceof TaskInstance)) {
163            throw new IllegalArgumentException
164                ("illegal type of task instance provided: " + child.getClass());
165        }
166       
167        /*IStructuringTemporalRelationship parentTask =
168            (IStructuringTemporalRelationship) parent.getTask();
169       
170        IMarkingTemporalRelationship parentTask =
171            (IMarkingTemporalRelationship) parent.getTask();
172           
173        foundChildTask = parentTask.getMarkedTask() != null ?
174            parentTask.getMarkedTask().equals(child.getTask()) : false;
175           
176        if (!foundChildTask) {
177            throw new IllegalArgumentException
178                ("the task of the child instance to be added does not belong to the children " +
179                 "of the task of the parent instance");
180        }*/
181
182        ((IterationInstance) instance).addChild(index, child);
183    }
184
185    /* (non-Javadoc)
186     * @see ITaskBuilder#setChild(ISelectionInstance, ITaskInstance)
187     */
188    @Override
189    public void setChild(ISelectionInstance instance, ITaskInstance child)
190        throws IllegalArgumentException
191    {
192        if (!(instance instanceof SelectionInstance)) {
193            throw new IllegalArgumentException
194                ("illegal type of sequence instance provided: " + instance.getClass());
195        }
196
197        if (!(child instanceof TaskInstance)) {
198            throw new IllegalArgumentException
199                ("illegal type of task instance provided: " + child.getClass());
200        }
201       
202        /*IStructuringTemporalRelationship parentTask =
203            (IStructuringTemporalRelationship) parent.getTask();
204       
205        for (ITask parentTaskChild : parentTask.getChildren()) {
206            if (parentTaskChild.equals(child.getTask())) {
207                foundChildTask = true;
208                break;
209            }
210        }
211           
212        if (!foundChildTask) {
213            throw new IllegalArgumentException
214                ("the task of the child instance to be added does not belong to the children " +
215                 "of the task of the parent instance");
216        }*/
217
218        ((SelectionInstance) instance).setChild(child);
219    }
220
221    /* (non-Javadoc)
222     * @see ITaskBuilder#setChild(IOptionalInstance, ITaskInstance)
223     */
224    @Override
225    public void setChild(IOptionalInstance instance, ITaskInstance child)
226        throws IllegalArgumentException
227    {
228        if (!(instance instanceof OptionalInstance)) {
229            throw new IllegalArgumentException
230                ("illegal type of optional instance provided: " + instance.getClass());
231        }
232
233        if (!(child instanceof TaskInstance)) {
234            throw new IllegalArgumentException
235                ("illegal type of task instance provided: " + child.getClass());
236        }
237       
238        /*IStructuringTemporalRelationship parentTask =
239            (IStructuringTemporalRelationship) parent.getTask();
240       
241        IMarkingTemporalRelationship parentTask =
242            (IMarkingTemporalRelationship) parent.getTask();
243           
244        foundChildTask = parentTask.getMarkedTask() != null ?
245            parentTask.getMarkedTask().equals(child.getTask()) : false;
246           
247        if (!foundChildTask) {
248            throw new IllegalArgumentException
249                ("the task of the child instance to be added does not belong to the children " +
250                 "of the task of the parent instance");
251        }*/
252
253        ((OptionalInstance) instance).setChild(child);
254    }
255
256    /* (non-Javadoc)
257     * @see ITaskBuilder#addExecutedTask(IUserSession, ITaskInstance)
258     */
259    @Override
260    public void addExecutedTask(IUserSession session, ITaskInstance taskInstance) {
261        if (!(session instanceof UserSession)) {
262            throw new IllegalArgumentException
263                ("illegal type of session provided: " + session.getClass());
264        }
265
266        if (!(taskInstance instanceof TaskInstance)) {
267            throw new IllegalArgumentException
268                ("illegal type of task instance provided: " + taskInstance.getClass());
269        }
270       
271        ((UserSession) session).addExecutedTask(taskInstance);
272    }
273
274    /* (non-Javadoc)
275     * @see ITaskBuilder#addExecutedTask(IUserSession, int, ITaskInstance)
276     */
277    public void addExecutedTask(IUserSession session, int index, ITaskInstance taskInstance) {
278        if (!(session instanceof UserSession)) {
279            throw new IllegalArgumentException
280                ("illegal type of session provided: " + session.getClass());
281        }
282
283        if (!(taskInstance instanceof TaskInstance)) {
284            throw new IllegalArgumentException
285                ("illegal type of task instance provided: " + taskInstance.getClass());
286        }
287       
288        ((UserSession) session).addExecutedTask(index, taskInstance);
289    }
290
291    /* (non-Javadoc)
292     * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, ITaskInstance)
293     */
294    @Override
295    public void addTaskInstance(ITaskInstanceList taskInstanceList, ITaskInstance taskInstance) {
296        if (taskInstanceList instanceof SequenceInstance) {
297            addChild((SequenceInstance) taskInstanceList, taskInstance);
298        }
299        else if (taskInstanceList instanceof IterationInstance) {
300            addChild((IterationInstance) taskInstanceList, taskInstance);
301        }
302        else if (taskInstanceList instanceof UserSession) {
303            addExecutedTask((UserSession) taskInstanceList, taskInstance);
304        }
305        else {
306            throw new IllegalArgumentException
307                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
308        }
309    }
310
311    /* (non-Javadoc)
312     * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, int, ITaskInstance)
313     */
314    @Override
315    public void addTaskInstance(ITaskInstanceList taskInstanceList,
316                                int               index,
317                                ITaskInstance     taskInstance)
318    {
319        if (taskInstanceList instanceof SequenceInstance) {
320            addChild((SequenceInstance) taskInstanceList, index, taskInstance);
321        }
322        else if (taskInstanceList instanceof IterationInstance) {
323            addChild((IterationInstance) taskInstanceList, index, taskInstance);
324        }
325        else if (taskInstanceList instanceof UserSession) {
326            addExecutedTask((UserSession) taskInstanceList, index, taskInstance);
327        }
328        else {
329            throw new IllegalArgumentException
330                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
331        }
332    }
333
334    /* (non-Javadoc)
335     * @see ITaskBuilder#setTaskInstance(ITaskInstanceList, int, ITaskInstance)
336     */
337    @Override
338    public void setTaskInstance(ITaskInstanceList taskInstanceList,
339                                int               index,
340                                ITaskInstance     taskInstance)
341    {
342        removeTaskInstance(taskInstanceList, index);
343        addTaskInstance(taskInstanceList, index, taskInstance);
344    }
345
346    /* (non-Javadoc)
347     * @see ITaskBuilder#setTask(ITaskInstance, ITask)
348     */
349    @Override
350    public void setTask(ITaskInstance taskInstance, ITask task) {
351        if (!(taskInstance instanceof TaskInstance)) {
352            throw new IllegalArgumentException
353                ("illegal type of task instance provided: " + taskInstance.getClass());
354        }
355       
356        ((TaskInstance) taskInstance).setTask(task);
357    }
358
359    /* (non-Javadoc)
360     * @see ITaskBuilder#addChild(ISequence, ITask)
361     */
362    @Override
363    public void addChild(ISequence parent, ITask child) {
364        if (!(parent instanceof Sequence)) {
365            throw new IllegalArgumentException
366                ("illegal type of sequence provided: " + parent.getClass());
367        }
368
369        addChildInternal((Sequence) parent, -1, child);
370    }
371
372    /* (non-Javadoc)
373     * @see ITaskBuilder#addChild(ISequence, int, ITask)
374     */
375    @Override
376    public void addChild(ISequence parent, int index, ITask child) {
377        if (!(parent instanceof Sequence)) {
378            throw new IllegalArgumentException
379                ("illegal type of sequence provided: " + parent.getClass());
380        }
381
382        addChildInternal((Sequence) parent, index, child);
383    }
384
385    /* (non-Javadoc)
386     * @see ITaskBuilder#setChild(ISequence, int, ITask)
387     */
388    @Override
389    public void setChild(ISequence parent, int index, ITask child) {
390        if (!(parent instanceof Sequence)) {
391            throw new IllegalArgumentException
392                ("illegal type of sequence provided: " + parent.getClass());
393        }
394
395        ((Sequence) parent).removeChild(index);
396        addChildInternal((Sequence) parent, index, child);
397    }
398
399    /* (non-Javadoc)
400     * @see ITaskBuilder#addChild(ISelection, ITask)
401     */
402    @Override
403    public void addChild(ISelection parent, ITask child) {
404        if (!(parent instanceof Selection)) {
405            throw new IllegalArgumentException
406                ("illegal type of selection provided: " + parent.getClass());
407        }
408
409        addChildInternal((Selection) parent, -1, child);
410    }
411
412    /* (non-Javadoc)
413     * @see ITaskBuilder#setMarkedTask(IIteration, ITask)
414     */
415    @Override
416    public void setMarkedTask(IIteration iteration, ITask newChild) {
417        if (!(iteration instanceof Iteration)) {
418            throw new IllegalArgumentException
419                ("illegal type of iteration provided: " + iteration.getClass());
420        }
421
422        if (!(newChild instanceof Task)) {
423            throw new IllegalArgumentException
424                ("illegal type of task provided: " + newChild.getClass());
425        }
426
427        ((Iteration) iteration).setMarkedTask(newChild);
428    }
429
430    /* (non-Javadoc)
431     * @see ITaskTreeBuilder#setChild(IOptional, ITaskTreeNode)
432     */
433    @Override
434    public void setMarkedTask(IOptional optional, ITask newChild) {
435        if (!(optional instanceof Optional)) {
436            throw new IllegalArgumentException
437                ("illegal type of optional provided: " + optional.getClass());
438        }
439
440        if (!(newChild instanceof Task)) {
441            throw new IllegalArgumentException
442                ("illegal type of task provided: " + newChild.getClass());
443        }
444
445        ((Optional) optional).setMarkedTask(newChild);
446    }
447
448    /* (non-Javadoc)
449     * @see ITaskBuilder#removeChild(ISequence, int)
450     */
451    @Override
452    public void removeChild(ISequence parent, int index) {
453        if (!(parent instanceof Sequence)) {
454            throw new IllegalArgumentException
455                ("illegal type of sequence provided: " + parent.getClass());
456        }
457
458        ((Sequence) parent).removeChild(index);
459    }
460
461    /* (non-Javadoc)
462     * @see ITaskBuilder#removeChild(ISelection, ITask)
463     */
464    @Override
465    public void removeChild(ISelection parent, ITask child) {
466        if (!(parent instanceof Selection)) {
467            throw new IllegalArgumentException
468                ("illegal type of selection provided: " + parent.getClass());
469        }
470
471        List<ITask> children = parent.getChildren();
472       
473        for (int i = 0; i < children.size(); i++) {
474            if ((children.get(i) == child) ||
475                ((children.get(i) != null) && (children.get(i).equals(child))))
476            {
477                ((Selection) parent).removeChild(i);
478                break;
479            }
480        }
481    }
482
483    /* (non-Javadoc)
484     * @see ITaskBuilder#removeTaskInstance(ITaskInstanceList, int)
485     */
486    @Override
487    public void removeTaskInstance(ITaskInstanceList taskInstanceList, int index) {
488        if (taskInstanceList instanceof SequenceInstance) {
489            ((SequenceInstance) taskInstanceList).removeChild(index);
490        }
491        else if (taskInstanceList instanceof IterationInstance) {
492            ((IterationInstance) taskInstanceList).removeChild(index);
493        }
494        else if (taskInstanceList instanceof UserSession) {
495            ((UserSession) taskInstanceList).removeExecutedTask(index);
496        }
497        else {
498            throw new IllegalArgumentException
499                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
500        }
501    }
502
503    /* (non-Javadoc)
504     * @see ITaskTreeBuilder#replaceChild(ISelection, ITaskTreeNode, ITaskTreeNode)
505     */
506    @Override
507    public void replaceChild(ISelection parent, ITask oldChild, ITask newChild) {
508        if (!(parent instanceof Selection)) {
509            throw new IllegalArgumentException
510                ("illegal type of selection provided: " + parent.getClass());
511        }
512
513        List<ITask> children = parent.getChildren();
514       
515        for (int i = 0; i < children.size(); i++) {
516            if ((children.get(i) == oldChild) ||
517                ((children.get(i) != null) && (children.get(i).equals(oldChild))))
518            {
519                ((Selection) parent).removeChild(i);
520                ((Selection) parent).addChild(i, newChild);
521                break;
522            }
523        }
524    }
525
526    /**
527     * <p>
528     * internal convenience method for adding children to a structuring temporal relationship
529     * including a check for the child type.
530     * </p>
531     */
532    private void addChildInternal(StructuringTemporalRelationship parent, int index, ITask child) {
533        if (!(child instanceof Task)) {
534            throw new IllegalArgumentException
535                ("illegal type of task provided: " + child.getClass());
536        }
537
538        if (index > -1) {
539            parent.addChild(index, child);
540        }
541        else {
542            parent.addChild(child);
543        }
544    }
545
546}
Note: See TracBrowser for help on using the repository browser.