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

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