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
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.IEventTask;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
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;
29
30/**
31 * TODO comment
32 *
33 * @version $Revision: $ $Date: 21.02.2012$
34 * @author 2012, last modified by $Author: patrick$
35 */
36public class TaskBuilder implements ITaskBuilder {
37
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
203    /*
204     * (non-Javadoc)
205     *
206     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#addChild(Sequence, TaskTreeNode)
207     */
208    @Override
209    public void addChild(ISequence parent, ITask child) {
210        if (!(parent instanceof Sequence)) {
211            throw new IllegalArgumentException
212                ("illegal type of sequence provided: " + parent.getClass());
213        }
214
215        addChildInternal((Sequence) parent, -1, child);
216    }
217
218    /*
219     * (non-Javadoc)
220     *
221     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#addChild(Sequence, int, TaskTreeNode)
222     */
223    @Override
224    public void addChild(ISequence parent, int index, ITask child) {
225        if (!(parent instanceof Sequence)) {
226            throw new IllegalArgumentException
227                ("illegal type of sequence provided: " + parent.getClass());
228        }
229
230        addChildInternal((Sequence) parent, index, child);
231    }
232
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
237    public void setChild(ISequence parent, int index, ITask child) {
238        if (!(parent instanceof Sequence)) {
239            throw new IllegalArgumentException
240                ("illegal type of sequence provided: " + parent.getClass());
241        }
242
243        ((Sequence) parent).removeChild(index);
244        addChildInternal((Sequence) parent, index, child);
245    }
246
247    /*
248     * (non-Javadoc)
249     *
250     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#addChild(Selection, TaskTreeNode)
251     */
252    @Override
253    public void addChild(ISelection parent, ITask child) {
254        if (!(parent instanceof Selection)) {
255            throw new IllegalArgumentException
256                ("illegal type of selection provided: " + parent.getClass());
257        }
258
259        addChildInternal((Selection) parent, -1, child);
260    }
261
262    /*
263     * (non-Javadoc)
264     *
265     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#setChild(Iteration, TaskTreeNode)
266     */
267    @Override
268    public void setMarkedTask(IIteration iteration, ITask newChild) {
269        if (!(iteration instanceof Iteration)) {
270            throw new IllegalArgumentException
271                ("illegal type of iteration provided: " + iteration.getClass());
272        }
273
274        if (!(newChild instanceof Task)) {
275            throw new IllegalArgumentException
276                ("illegal type of task provided: " + newChild.getClass());
277        }
278
279        ((Iteration) iteration).setMarkedTask(newChild);
280    }
281
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
286    public void setMarkedTask(IOptional optional, ITask newChild) {
287        if (!(optional instanceof Optional)) {
288            throw new IllegalArgumentException
289                ("illegal type of optional provided: " + optional.getClass());
290        }
291
292        if (!(newChild instanceof Task)) {
293            throw new IllegalArgumentException
294                ("illegal type of task provided: " + newChild.getClass());
295        }
296
297        ((Optional) optional).setMarkedTask(newChild);
298    }
299
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) {
307        if (!(parent instanceof Sequence)) {
308            throw new IllegalArgumentException
309                ("illegal type of sequence provided: " + parent.getClass());
310        }
311
312        ((Sequence) parent).removeChild(index);
313    }
314
315    /*
316     * (non-Javadoc)
317     *
318     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#removeChild(Selection, TaskTreeNode)
319     */
320    @Override
321    public void removeChild(ISelection parent, ITask child) {
322        if (!(parent instanceof Selection)) {
323            throw new IllegalArgumentException
324                ("illegal type of selection provided: " + parent.getClass());
325        }
326
327        List<ITask> children = parent.getChildren();
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))))
332            {
333                ((Selection) parent).removeChild(i);
334                break;
335            }
336        }
337    }
338
339    /* (non-Javadoc)
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)
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
360    public void replaceChild(ISelection parent, ITask oldChild, ITask newChild) {
361        if (!(parent instanceof Selection)) {
362            throw new IllegalArgumentException
363                ("illegal type of selection provided: " + parent.getClass());
364        }
365
366        List<ITask> children = parent.getChildren();
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            {
372                ((Selection) parent).removeChild(i);
373                ((Selection) parent).addChild(i, newChild);
374                break;
375            }
376        }
377    }
378
379    /*
380     * (non-Javadoc)
381     *
382     * @see de.ugoe.cs.tasktree.treeifc.TaskTreeBuilder#setDescription(TaskTreeNode, String)
383     */
384    @Override
385    public void setDescription(ITask parent, String description) {
386        if (!(parent instanceof Task)) {
387            throw new IllegalArgumentException
388                ("illegal type of task provided: " + parent.getClass());
389        }
390
391        ((Task) parent).setDescription(description);
392    }
393
394    /**
395     *
396     */
397    private void addChildInternal(StructuringTemporalRelationship parent, int index, ITask child) {
398        if (!(child instanceof Task)) {
399            throw new IllegalArgumentException
400                ("illegal type of task provided: " + child.getClass());
401        }
402
403        if (index > -1) {
404            parent.addChild(index, child);
405        }
406        else {
407            parent.addChild(child);
408        }
409    }
410
411}
Note: See TracBrowser for help on using the repository browser.