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

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