source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskBuilder.java @ 1734

Last change on this file since 1734 was 1734, checked in by rkrimmel, 10 years ago

Added automatically created javadoc, still needs to be commented properly though

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