source: trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeValidator.java @ 1333

Last change on this file since 1333 was 1333, checked in by pharms, 10 years ago
  • improvement and correction of the task tree decoder to handle optionals correctly
File size: 6.9 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;
16
17import static org.junit.Assert.*;
18
19import java.util.List;
20
21import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
35
36/**
37 * <p>
38 * TODO comment
39 * </p>
40 *
41 * @author Patrick Harms
42 */
43public class TaskTreeValidator {
44
45    /**
46     *
47     */
48    public void validate(List<IUserSession> userSessions) {
49        for (IUserSession userSession : userSessions) {
50            validate(userSession);
51        }
52    }
53
54    /**
55     *
56     */
57    public void validate(ITaskInstanceList taskInstances) {
58        for (ITaskInstance taskInstance : taskInstances) {
59            validate(taskInstance);
60        }
61    }
62
63    /**
64     *
65     */
66    public void validate(ITaskInstance taskInstance) {
67        assertNotNull("task model of task instance must not be null", taskInstance.getTask());
68       
69        if (taskInstance.getTask() instanceof ISequence) {
70            ISequence task = (ISequence) taskInstance.getTask();
71           
72            assertEquals("number of children of sequence instance must match sequence model",
73                         ((ISequenceInstance) taskInstance).size(), task.getChildren().size());
74           
75            for (int i = 0; i < ((ISequenceInstance) taskInstance).size(); i++) {
76                assertNotNull("sequence instance child " + i + " was null",
77                              ((ISequenceInstance) taskInstance).get(i));
78                ITask childTask = ((ISequenceInstance) taskInstance).get(i).getTask();
79                assertSame("task of sequence child " + i + " does not match sequence model",
80                           childTask, task.getChildren().get(i));
81            }
82        }
83        else if (taskInstance.getTask() instanceof ISelection) {
84            ISelection task = (ISelection) taskInstance.getTask();
85           
86            assertNotNull("number of children of selection instance must be 1",
87                          ((ISelectionInstance) taskInstance).getChild());
88            assertTrue
89                ("number of children of selection must be larger 0", task.getChildren().size() > 0);
90           
91            boolean found = false;
92            for (ITask childTask : task.getChildren()) {
93                assertNotNull("child of selection model must not be null", childTask);
94                assertFalse("child of selection model must not be a selection",
95                            childTask instanceof ISelection);
96                assertFalse("child of selection model must not be an optional",
97                            childTask instanceof IOptional);
98                if (childTask.equals(((ISelectionInstance) taskInstance).getChild().getTask())) {
99                    found = true;
100                    break;
101                }
102            }
103           
104            assertTrue("no child of the selection model matches the model of child of the " +
105                       "selection instance", found);
106        }
107        else if (taskInstance.getTask() instanceof IIteration) {
108            ITask childTask = ((IIteration) taskInstance.getTask()).getMarkedTask();
109            assertNotNull("child task of iteration model must not be null", childTask);
110            assertFalse("child of iteration model must not be an iteration",
111                        childTask instanceof IIteration);
112            assertFalse("child of iteration model must not be an optional",
113                        childTask instanceof IOptional);
114           
115            for (int i = 0; i < ((IIterationInstance) taskInstance).size(); i++) {
116                assertNotNull("iteration instance child " + i + " was null",
117                              ((IIterationInstance) taskInstance).get(i));
118                assertSame("task of iteration child " + i + " does not match iteration model",
119                           childTask, ((IIterationInstance) taskInstance).get(i).getTask());
120            }
121        }
122        else if (taskInstance.getTask() instanceof IOptional) {
123            ITask childTask = ((IOptional) taskInstance.getTask()).getMarkedTask();
124            assertNotNull("child task of optional model must not be null", childTask);
125            assertFalse("child of optional model must not be an optional",
126                        childTask instanceof IOptional);
127           
128            if (((IOptionalInstance) taskInstance).getChild() != null) {
129                assertEquals("task of optional child does not match optional model",
130                             childTask, ((IOptionalInstance) taskInstance).getChild().getTask());
131            }
132        }
133        else if (taskInstance.getTask() instanceof IEventTask) {
134            IEventTask task = (IEventTask) taskInstance.getTask();
135            assertNotNull("event task model must not be null", task);
136            assertNotNull("event of event task instance must not be null",
137                          ((IEventTaskInstance) taskInstance).getEvent());
138        }
139        else {
140            fail("unknown task model: " + taskInstance.getTask());
141        }
142       
143        if (taskInstance instanceof ITaskInstanceList) {
144            for (ITaskInstance child : (ITaskInstanceList) taskInstance) {
145                validate(child);
146            }
147        }
148        else if (taskInstance instanceof ISelectionInstance) {
149            validate(((ISelectionInstance) taskInstance).getChild());
150        }
151        else if (taskInstance instanceof IOptionalInstance) {
152            if (((IOptionalInstance) taskInstance).getChild() != null) {
153                validate(((IOptionalInstance) taskInstance).getChild());
154            }
155        }
156    }
157}
Note: See TracBrowser for help on using the repository browser.