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
RevLine 
[1113]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
[922]15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
[439]16
[1126]17import java.util.List;
18
[922]19import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
[1294]20import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
[1126]21import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
[1294]22import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
[922]23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
[1294]24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
[922]25import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
[1294]26import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
[1146]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;
[439]32
33/**
[1216]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>
[439]40 *
[1216]41 * @author Patrick Harms
[439]42 */
[1146]43public class TaskBuilder implements ITaskBuilder {
[439]44
[1146]45    /* (non-Javadoc)
[1294]46     * @see ITaskBuilder#addChild(ISequenceInstance, ITaskInstance)
[1146]47     */
48    @Override
[1294]49    public void addChild(ISequenceInstance instance, ITaskInstance child)
50        throws IllegalArgumentException
[1146]51    {
[1294]52        if (!(instance instanceof SequenceInstance)) {
[1146]53            throw new IllegalArgumentException
[1294]54                ("illegal type of sequence instance provided: " + instance.getClass());
[1146]55        }
56
57        if (!(child instanceof TaskInstance)) {
58            throw new IllegalArgumentException
[1294]59                ("illegal type of task instance provided: " + child.getClass());
[1146]60        }
61       
[1294]62        /*IStructuringTemporalRelationship parentTask =
63            (IStructuringTemporalRelationship) parent.getTask();
[1146]64       
[1294]65        for (ITask parentTaskChild : parentTask.getChildren()) {
66            if (parentTaskChild.equals(child.getTask())) {
67                foundChildTask = true;
68                break;
69            }
70        }
71           
72        if (!foundChildTask) {
[1146]73            throw new IllegalArgumentException
[1294]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());
[1146]90        }
[1294]91
92        if (!(child instanceof TaskInstance)) {
93            throw new IllegalArgumentException
94                ("illegal type of task instance provided: " + child.getClass());
[1146]95        }
[1294]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;
[1146]104            }
105        }
[1294]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());
[1146]126        }
[1294]127
128        if (!(child instanceof TaskInstance)) {
129            throw new IllegalArgumentException
130                ("illegal type of task instance provided: " + child.getClass());
131        }
[1146]132       
[1294]133        /*IStructuringTemporalRelationship parentTask =
134            (IStructuringTemporalRelationship) parent.getTask();
[1146]135       
[1294]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;
[1146]209            }
210        }
211           
[1294]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());
[1146]231        }
[1294]232
233        if (!(child instanceof TaskInstance)) {
234            throw new IllegalArgumentException
235                ("illegal type of task instance provided: " + child.getClass());
236        }
[1146]237       
[1294]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           
[1146]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
[1294]253        ((OptionalInstance) instance).setChild(child);
[1146]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)
[1294]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)
[1216]292     * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, ITaskInstance)
[1146]293     */
294    @Override
295    public void addTaskInstance(ITaskInstanceList taskInstanceList, ITaskInstance taskInstance) {
[1294]296        if (taskInstanceList instanceof SequenceInstance) {
297            addChild((SequenceInstance) taskInstanceList, taskInstance);
[1146]298        }
[1294]299        else if (taskInstanceList instanceof IterationInstance) {
300            addChild((IterationInstance) taskInstanceList, taskInstance);
301        }
[1146]302        else if (taskInstanceList instanceof UserSession) {
[1294]303            addExecutedTask((UserSession) taskInstanceList, taskInstance);
[1146]304        }
305        else {
306            throw new IllegalArgumentException
307                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
308        }
309    }
310
311    /* (non-Javadoc)
[1216]312     * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, int, ITaskInstance)
[1146]313     */
314    @Override
315    public void addTaskInstance(ITaskInstanceList taskInstanceList,
316                                int               index,
317                                ITaskInstance     taskInstance)
318    {
[1294]319        if (taskInstanceList instanceof SequenceInstance) {
320            addChild((SequenceInstance) taskInstanceList, index, taskInstance);
[1146]321        }
[1294]322        else if (taskInstanceList instanceof IterationInstance) {
323            addChild((IterationInstance) taskInstanceList, index, taskInstance);
324        }
[1146]325        else if (taskInstanceList instanceof UserSession) {
[1294]326            addExecutedTask((UserSession) taskInstanceList, index, taskInstance);
[1146]327        }
328        else {
329            throw new IllegalArgumentException
330                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
331        }
332    }
333
334    /* (non-Javadoc)
[1216]335     * @see ITaskBuilder#setTaskInstance(ITaskInstanceList, int, ITaskInstance)
[1146]336     */
337    @Override
338    public void setTaskInstance(ITaskInstanceList taskInstanceList,
339                                int               index,
340                                ITaskInstance     taskInstance)
341    {
[1294]342        removeTaskInstance(taskInstanceList, index);
343        addTaskInstance(taskInstanceList, index, taskInstance);
[1146]344    }
345
346    /* (non-Javadoc)
[1216]347     * @see ITaskBuilder#setTask(ITaskInstance, ITask)
[1146]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
[1216]359    /* (non-Javadoc)
360     * @see ITaskBuilder#addChild(ISequence, ITask)
[557]361     */
362    @Override
[1146]363    public void addChild(ISequence parent, ITask child) {
[557]364        if (!(parent instanceof Sequence)) {
365            throw new IllegalArgumentException
[1146]366                ("illegal type of sequence provided: " + parent.getClass());
[557]367        }
[439]368
[1146]369        addChildInternal((Sequence) parent, -1, child);
[439]370    }
371
[1216]372    /* (non-Javadoc)
373     * @see ITaskBuilder#addChild(ISequence, int, ITask)
[557]374     */
375    @Override
[1146]376    public void addChild(ISequence parent, int index, ITask child) {
[557]377        if (!(parent instanceof Sequence)) {
378            throw new IllegalArgumentException
[1146]379                ("illegal type of sequence provided: " + parent.getClass());
[557]380        }
[439]381
[1146]382        addChildInternal((Sequence) parent, index, child);
[439]383    }
384
[1126]385    /* (non-Javadoc)
[1216]386     * @see ITaskBuilder#setChild(ISequence, int, ITask)
[1126]387     */
388    @Override
[1146]389    public void setChild(ISequence parent, int index, ITask child) {
[1126]390        if (!(parent instanceof Sequence)) {
391            throw new IllegalArgumentException
[1146]392                ("illegal type of sequence provided: " + parent.getClass());
[1126]393        }
394
[1146]395        ((Sequence) parent).removeChild(index);
396        addChildInternal((Sequence) parent, index, child);
[1126]397    }
398
[1216]399    /* (non-Javadoc)
400     * @see ITaskBuilder#addChild(ISelection, ITask)
[557]401     */
402    @Override
[1146]403    public void addChild(ISelection parent, ITask child) {
[557]404        if (!(parent instanceof Selection)) {
405            throw new IllegalArgumentException
[1146]406                ("illegal type of selection provided: " + parent.getClass());
[557]407        }
[439]408
[1146]409        addChildInternal((Selection) parent, -1, child);
[439]410    }
411
[1216]412    /* (non-Javadoc)
413     * @see ITaskBuilder#setMarkedTask(IIteration, ITask)
[557]414     */
415    @Override
[1146]416    public void setMarkedTask(IIteration iteration, ITask newChild) {
[557]417        if (!(iteration instanceof Iteration)) {
418            throw new IllegalArgumentException
419                ("illegal type of iteration provided: " + iteration.getClass());
420        }
[439]421
[1146]422        if (!(newChild instanceof Task)) {
[557]423            throw new IllegalArgumentException
[1146]424                ("illegal type of task provided: " + newChild.getClass());
[557]425        }
426
[1146]427        ((Iteration) iteration).setMarkedTask(newChild);
[439]428    }
429
[1126]430    /* (non-Javadoc)
[1216]431     * @see ITaskTreeBuilder#setChild(IOptional, ITaskTreeNode)
[1126]432     */
433    @Override
[1146]434    public void setMarkedTask(IOptional optional, ITask newChild) {
[1126]435        if (!(optional instanceof Optional)) {
436            throw new IllegalArgumentException
437                ("illegal type of optional provided: " + optional.getClass());
438        }
439
[1146]440        if (!(newChild instanceof Task)) {
[1126]441            throw new IllegalArgumentException
[1146]442                ("illegal type of task provided: " + newChild.getClass());
[1126]443        }
444
[1146]445        ((Optional) optional).setMarkedTask(newChild);
[1126]446    }
447
[1216]448    /* (non-Javadoc)
449     * @see ITaskBuilder#removeChild(ISequence, int)
[557]450     */
451    @Override
452    public void removeChild(ISequence parent, int index) {
[1146]453        if (!(parent instanceof Sequence)) {
[557]454            throw new IllegalArgumentException
[1146]455                ("illegal type of sequence provided: " + parent.getClass());
[557]456        }
[439]457
[1146]458        ((Sequence) parent).removeChild(index);
[439]459    }
460
[1216]461    /* (non-Javadoc)
462     * @see ITaskBuilder#removeChild(ISelection, ITask)
[557]463     */
464    @Override
[1146]465    public void removeChild(ISelection parent, ITask child) {
466        if (!(parent instanceof Selection)) {
[557]467            throw new IllegalArgumentException
[1146]468                ("illegal type of selection provided: " + parent.getClass());
[557]469        }
470
[1146]471        List<ITask> children = parent.getChildren();
[1126]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))))
[557]476            {
[1146]477                ((Selection) parent).removeChild(i);
[557]478                break;
479            }
480        }
[439]481    }
482
[1126]483    /* (non-Javadoc)
[1216]484     * @see ITaskBuilder#removeTaskInstance(ITaskInstanceList, int)
[1146]485     */
486    @Override
487    public void removeTaskInstance(ITaskInstanceList taskInstanceList, int index) {
[1294]488        if (taskInstanceList instanceof SequenceInstance) {
489            ((SequenceInstance) taskInstanceList).removeChild(index);
[1146]490        }
[1294]491        else if (taskInstanceList instanceof IterationInstance) {
492            ((IterationInstance) taskInstanceList).removeChild(index);
493        }
[1146]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)
[1216]504     * @see ITaskTreeBuilder#replaceChild(ISelection, ITaskTreeNode, ITaskTreeNode)
[1126]505     */
506    @Override
[1146]507    public void replaceChild(ISelection parent, ITask oldChild, ITask newChild) {
508        if (!(parent instanceof Selection)) {
[1126]509            throw new IllegalArgumentException
[1146]510                ("illegal type of selection provided: " + parent.getClass());
[1126]511        }
512
[1146]513        List<ITask> children = parent.getChildren();
[1126]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            {
[1146]519                ((Selection) parent).removeChild(i);
520                ((Selection) parent).addChild(i, newChild);
[1126]521                break;
522            }
523        }
524    }
525
[557]526    /**
[1216]527     * <p>
528     * internal convenience method for adding children to a structuring temporal relationship
529     * including a check for the child type.
530     * </p>
[988]531     */
[1146]532    private void addChildInternal(StructuringTemporalRelationship parent, int index, ITask child) {
533        if (!(child instanceof Task)) {
[557]534            throw new IllegalArgumentException
[1146]535                ("illegal type of task provided: " + child.getClass());
[557]536        }
[439]537
[557]538        if (index > -1) {
[1146]539            parent.addChild(index, child);
[557]540        }
541        else {
[1146]542            parent.addChild(child);
[557]543        }
[439]544    }
545
546}
Note: See TracBrowser for help on using the repository browser.