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

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

Used Eclipse code cleanup

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