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

Last change on this file since 1255 was 1216, checked in by pharms, 11 years ago
  • improved java doc
  • Property svn:executable set to *
File size: 14.4 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
[1146]19import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
[922]20import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
[1126]21import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
[922]22import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
[1146]24import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
[439]29
30/**
[1216]31 * <p>
32 * this is the default implementation of the interface {@link ITaskBuilder}. It
33 * does not do anything fancy except implementing the interface. In some situations, it performs
34 * a check if the model or instances to be created a valid. However, this can not be done
35 * in any situation of the creation process.
36 * </p>
[439]37 *
[1216]38 * @author Patrick Harms
[439]39 */
[1146]40public class TaskBuilder implements ITaskBuilder {
[439]41
[1146]42    /* (non-Javadoc)
[1216]43     * @see ITaskBuilder#addChild(ITaskInstance,ITaskInstance)
[1146]44     */
45    @Override
46    public void addChild(ITaskInstance parent, ITaskInstance child) throws IllegalArgumentException
47    {
48        if (!(parent instanceof TaskInstance)) {
49            throw new IllegalArgumentException
50                ("illegal type of task instance provided: " + parent.getClass());
51        }
52
53        if (!(child instanceof TaskInstance)) {
54            throw new IllegalArgumentException
55                ("illegal type of task instance provided: " + parent.getClass());
56        }
57       
58        // check, that the correct number of children for the distinct types are added
59        ITask task = parent.getTask();
60       
61        if (task instanceof IEventTask) {
62            throw new IllegalArgumentException
63                ("can not add children to a task instance of an event task");
64        }
65        else if (task instanceof ISelection) {
66            if (parent.getChildren().size() > 0) {
67                throw new IllegalArgumentException
68                    ("the instance of a selection must have at most one child");
69            }
70        }
71        else if (task instanceof IOptional) {
72            if (parent.getChildren().size() > 1) {
73                throw new IllegalArgumentException
74                    ("the instance of an optional must have at most one child");
75            }
76        }
[1216]77        /*else if (task instanceof IIteration) {
[1146]78            for (ITaskInstance childInstance : parent.getChildren()) {
79                if (!childInstance.getTask().equals(child.getTask())) {
80                    throw new IllegalArgumentException
81                        ("all children of an instance of an iteration must have exactly the " +
82                         "same type");
83                }
84            }
85        }
86       
87        boolean foundChildTask = false;
88        if (parent.getTask() instanceof IStructuringTemporalRelationship) {
89            IStructuringTemporalRelationship parentTask =
90                (IStructuringTemporalRelationship) parent.getTask();
91       
92            for (ITask parentTaskChild : parentTask.getChildren()) {
93                if (parentTaskChild.equals(child.getTask())) {
94                    foundChildTask = true;
95                    break;
96                }
97            }
98        }
99        else if (parent.getTask() instanceof IMarkingTemporalRelationship) {
100            IMarkingTemporalRelationship parentTask =
101                (IMarkingTemporalRelationship) parent.getTask();
102           
103            foundChildTask = parentTask.getMarkedTask() != null ?
104                parentTask.getMarkedTask().equals(child.getTask()) : false;
105        }
106       
107        if (!foundChildTask) {
108            throw new IllegalArgumentException
109                ("the task of the child instance to be added does not belong to the children " +
110                 "of the task of the parent instance");
111        }*/
112
113        // finally, after all checks are positive, add the child
114        ((TaskInstance) parent).addChild(child);
115    }
116
117    /* (non-Javadoc)
118     * @see ITaskBuilder#addExecutedTask(IUserSession, ITaskInstance)
119     */
120    @Override
121    public void addExecutedTask(IUserSession session, ITaskInstance taskInstance) {
122        if (!(session instanceof UserSession)) {
123            throw new IllegalArgumentException
124                ("illegal type of session provided: " + session.getClass());
125        }
126
127        if (!(taskInstance instanceof TaskInstance)) {
128            throw new IllegalArgumentException
129                ("illegal type of task instance provided: " + taskInstance.getClass());
130        }
131       
132        ((UserSession) session).addExecutedTask(taskInstance);
133    }
134
135    /* (non-Javadoc)
[1216]136     * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, ITaskInstance)
[1146]137     */
138    @Override
139    public void addTaskInstance(ITaskInstanceList taskInstanceList, ITaskInstance taskInstance) {
140        if (taskInstanceList instanceof TaskInstance) {
141            ((TaskInstance) taskInstanceList).addChild(taskInstance);
142        }
143        else if (taskInstanceList instanceof UserSession) {
144            ((UserSession) taskInstanceList).addExecutedTask(taskInstance);
145        }
146        else {
147            throw new IllegalArgumentException
148                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
149        }
150    }
151
152    /* (non-Javadoc)
[1216]153     * @see ITaskBuilder#addTaskInstance(ITaskInstanceList, int, ITaskInstance)
[1146]154     */
155    @Override
156    public void addTaskInstance(ITaskInstanceList taskInstanceList,
157                                int               index,
158                                ITaskInstance     taskInstance)
159    {
160        if (taskInstanceList instanceof TaskInstance) {
161            ((TaskInstance) taskInstanceList).addChild(index, taskInstance);
162        }
163        else if (taskInstanceList instanceof UserSession) {
164            ((UserSession) taskInstanceList).addExecutedTask(index, taskInstance);
165        }
166        else {
167            throw new IllegalArgumentException
168                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
169        }
170    }
171
172    /* (non-Javadoc)
[1216]173     * @see ITaskBuilder#setTaskInstance(ITaskInstanceList, int, ITaskInstance)
[1146]174     */
175    @Override
176    public void setTaskInstance(ITaskInstanceList taskInstanceList,
177                                int               index,
178                                ITaskInstance     taskInstance)
179    {
180        if (taskInstanceList instanceof TaskInstance) {
181            ((TaskInstance) taskInstanceList).removeChild(index);
182            ((TaskInstance) taskInstanceList).addChild(index, taskInstance);
183        }
184        else if (taskInstanceList instanceof UserSession) {
185            ((UserSession) taskInstanceList).removeExecutedTask(index);
186            ((UserSession) taskInstanceList).addExecutedTask(index, taskInstance);
187        }
188        else {
189            throw new IllegalArgumentException
190                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
191        }
192    }
193
194    /* (non-Javadoc)
[1216]195     * @see ITaskBuilder#setTask(ITaskInstance, ITask)
[1146]196     */
197    @Override
198    public void setTask(ITaskInstance taskInstance, ITask task) {
199        if (!(taskInstance instanceof TaskInstance)) {
200            throw new IllegalArgumentException
201                ("illegal type of task instance provided: " + taskInstance.getClass());
202        }
203       
204        ((TaskInstance) taskInstance).setTask(task);
205    }
206
[1216]207    /* (non-Javadoc)
208     * @see ITaskBuilder#addChild(ISequence, ITask)
[557]209     */
210    @Override
[1146]211    public void addChild(ISequence parent, ITask child) {
[557]212        if (!(parent instanceof Sequence)) {
213            throw new IllegalArgumentException
[1146]214                ("illegal type of sequence provided: " + parent.getClass());
[557]215        }
[439]216
[1146]217        addChildInternal((Sequence) parent, -1, child);
[439]218    }
219
[1216]220    /* (non-Javadoc)
221     * @see ITaskBuilder#addChild(ISequence, int, ITask)
[557]222     */
223    @Override
[1146]224    public void addChild(ISequence parent, int index, ITask child) {
[557]225        if (!(parent instanceof Sequence)) {
226            throw new IllegalArgumentException
[1146]227                ("illegal type of sequence provided: " + parent.getClass());
[557]228        }
[439]229
[1146]230        addChildInternal((Sequence) parent, index, child);
[439]231    }
232
[1126]233    /* (non-Javadoc)
[1216]234     * @see ITaskBuilder#setChild(ISequence, int, ITask)
[1126]235     */
236    @Override
[1146]237    public void setChild(ISequence parent, int index, ITask child) {
[1126]238        if (!(parent instanceof Sequence)) {
239            throw new IllegalArgumentException
[1146]240                ("illegal type of sequence provided: " + parent.getClass());
[1126]241        }
242
[1146]243        ((Sequence) parent).removeChild(index);
244        addChildInternal((Sequence) parent, index, child);
[1126]245    }
246
[1216]247    /* (non-Javadoc)
248     * @see ITaskBuilder#addChild(ISelection, ITask)
[557]249     */
250    @Override
[1146]251    public void addChild(ISelection parent, ITask child) {
[557]252        if (!(parent instanceof Selection)) {
253            throw new IllegalArgumentException
[1146]254                ("illegal type of selection provided: " + parent.getClass());
[557]255        }
[439]256
[1146]257        addChildInternal((Selection) parent, -1, child);
[439]258    }
259
[1216]260    /* (non-Javadoc)
261     * @see ITaskBuilder#setMarkedTask(IIteration, ITask)
[557]262     */
263    @Override
[1146]264    public void setMarkedTask(IIteration iteration, ITask newChild) {
[557]265        if (!(iteration instanceof Iteration)) {
266            throw new IllegalArgumentException
267                ("illegal type of iteration provided: " + iteration.getClass());
268        }
[439]269
[1146]270        if (!(newChild instanceof Task)) {
[557]271            throw new IllegalArgumentException
[1146]272                ("illegal type of task provided: " + newChild.getClass());
[557]273        }
274
[1146]275        ((Iteration) iteration).setMarkedTask(newChild);
[439]276    }
277
[1126]278    /* (non-Javadoc)
[1216]279     * @see ITaskTreeBuilder#setChild(IOptional, ITaskTreeNode)
[1126]280     */
281    @Override
[1146]282    public void setMarkedTask(IOptional optional, ITask newChild) {
[1126]283        if (!(optional instanceof Optional)) {
284            throw new IllegalArgumentException
285                ("illegal type of optional provided: " + optional.getClass());
286        }
287
[1146]288        if (!(newChild instanceof Task)) {
[1126]289            throw new IllegalArgumentException
[1146]290                ("illegal type of task provided: " + newChild.getClass());
[1126]291        }
292
[1146]293        ((Optional) optional).setMarkedTask(newChild);
[1126]294    }
295
[1216]296    /* (non-Javadoc)
297     * @see ITaskBuilder#removeChild(ISequence, int)
[557]298     */
299    @Override
300    public void removeChild(ISequence parent, int index) {
[1146]301        if (!(parent instanceof Sequence)) {
[557]302            throw new IllegalArgumentException
[1146]303                ("illegal type of sequence provided: " + parent.getClass());
[557]304        }
[439]305
[1146]306        ((Sequence) parent).removeChild(index);
[439]307    }
308
[1216]309    /* (non-Javadoc)
310     * @see ITaskBuilder#removeChild(ISelection, ITask)
[557]311     */
312    @Override
[1146]313    public void removeChild(ISelection parent, ITask child) {
314        if (!(parent instanceof Selection)) {
[557]315            throw new IllegalArgumentException
[1146]316                ("illegal type of selection provided: " + parent.getClass());
[557]317        }
318
[1146]319        List<ITask> children = parent.getChildren();
[1126]320       
321        for (int i = 0; i < children.size(); i++) {
322            if ((children.get(i) == child) ||
323                ((children.get(i) != null) && (children.get(i).equals(child))))
[557]324            {
[1146]325                ((Selection) parent).removeChild(i);
[557]326                break;
327            }
328        }
[439]329    }
330
[1126]331    /* (non-Javadoc)
[1216]332     * @see ITaskBuilder#removeTaskInstance(ITaskInstanceList, int)
[1146]333     */
334    @Override
335    public void removeTaskInstance(ITaskInstanceList taskInstanceList, int index) {
336        if (taskInstanceList instanceof TaskInstance) {
337            ((TaskInstance) taskInstanceList).removeChild(index);
338        }
339        else if (taskInstanceList instanceof UserSession) {
340            ((UserSession) taskInstanceList).removeExecutedTask(index);
341        }
342        else {
343            throw new IllegalArgumentException
344                ("illegal type of task instance list provided: " + taskInstanceList.getClass());
345        }
346    }
347
348    /* (non-Javadoc)
[1216]349     * @see ITaskTreeBuilder#replaceChild(ISelection, ITaskTreeNode, ITaskTreeNode)
[1126]350     */
351    @Override
[1146]352    public void replaceChild(ISelection parent, ITask oldChild, ITask newChild) {
353        if (!(parent instanceof Selection)) {
[1126]354            throw new IllegalArgumentException
[1146]355                ("illegal type of selection provided: " + parent.getClass());
[1126]356        }
357
[1146]358        List<ITask> children = parent.getChildren();
[1126]359       
360        for (int i = 0; i < children.size(); i++) {
361            if ((children.get(i) == oldChild) ||
362                ((children.get(i) != null) && (children.get(i).equals(oldChild))))
363            {
[1146]364                ((Selection) parent).removeChild(i);
365                ((Selection) parent).addChild(i, newChild);
[1126]366                break;
367            }
368        }
369    }
370
[1216]371    /* (non-Javadoc)
372     * @see ITaskBuilder#setDescription(ITask, java.lang.String)
[557]373     */
374    @Override
[1146]375    public void setDescription(ITask parent, String description) {
376        if (!(parent instanceof Task)) {
[557]377            throw new IllegalArgumentException
[1146]378                ("illegal type of task provided: " + parent.getClass());
[557]379        }
380
[1146]381        ((Task) parent).setDescription(description);
[439]382    }
383
[557]384    /**
[1216]385     * <p>
386     * internal convenience method for adding children to a structuring temporal relationship
387     * including a check for the child type.
388     * </p>
[988]389     */
[1146]390    private void addChildInternal(StructuringTemporalRelationship parent, int index, ITask child) {
391        if (!(child instanceof Task)) {
[557]392            throw new IllegalArgumentException
[1146]393                ("illegal type of task provided: " + child.getClass());
[557]394        }
[439]395
[557]396        if (index > -1) {
[1146]397            parent.addChild(index, child);
[557]398        }
399        else {
[1146]400            parent.addChild(child);
[557]401        }
[439]402    }
403
404}
Note: See TracBrowser for help on using the repository browser.