source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskBuilder.java @ 1612

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