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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees; |
---|
16 | |
---|
17 | import static org.junit.Assert.*; |
---|
18 | |
---|
19 | import java.util.List; |
---|
20 | |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
30 | |
---|
31 | /** |
---|
32 | * <p> |
---|
33 | * TODO comment |
---|
34 | * </p> |
---|
35 | * |
---|
36 | * @author Patrick Harms |
---|
37 | */ |
---|
38 | public class TaskTreeValidator { |
---|
39 | |
---|
40 | /** |
---|
41 | * |
---|
42 | */ |
---|
43 | public void validate(List<IUserSession> userSessions) { |
---|
44 | for (IUserSession userSession : userSessions) { |
---|
45 | validate(userSession); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * |
---|
51 | */ |
---|
52 | public void validate(ITaskInstanceList taskInstances) { |
---|
53 | for (ITaskInstance taskInstance : taskInstances) { |
---|
54 | validate(taskInstance); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * |
---|
60 | */ |
---|
61 | public void validate(ITaskInstance taskInstance) { |
---|
62 | assertNotNull("task model of task instance must not be null", taskInstance.getTask()); |
---|
63 | |
---|
64 | if (taskInstance.getTask() instanceof ISequence) { |
---|
65 | ISequence task = (ISequence) taskInstance.getTask(); |
---|
66 | |
---|
67 | assertEquals("number of children of sequence instance must match sequence model", |
---|
68 | taskInstance.size(), task.getChildren().size()); |
---|
69 | |
---|
70 | for (int i = 0; i < taskInstance.size(); i++) { |
---|
71 | assertNotNull("sequence instance child " + i + " was null", taskInstance.get(i)); |
---|
72 | ITask childTask = taskInstance.get(i).getTask(); |
---|
73 | assertSame("task of sequence child " + i + " does not match sequence model", |
---|
74 | childTask, task.getChildren().get(i)); |
---|
75 | } |
---|
76 | } |
---|
77 | else if (taskInstance.getTask() instanceof ISelection) { |
---|
78 | ISelection task = (ISelection) taskInstance.getTask(); |
---|
79 | |
---|
80 | assertEquals |
---|
81 | ("number of children of selection instance must be 1", 1, taskInstance.size()); |
---|
82 | assertTrue |
---|
83 | ("number of children of selection must be larger 0", task.getChildren().size() > 0); |
---|
84 | |
---|
85 | boolean found = false; |
---|
86 | for (ITask childTask : task.getChildren()) { |
---|
87 | assertNotNull("child of selection model must not be null", childTask); |
---|
88 | assertFalse("child of selection model must not be a selection", |
---|
89 | childTask instanceof ISelection); |
---|
90 | assertFalse("child of selection model must not be an optional", |
---|
91 | childTask instanceof IOptional); |
---|
92 | if (childTask.equals(taskInstance.get(0).getTask())) { |
---|
93 | found = true; |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | assertTrue("no child of the selection model matches the model of child of the " + |
---|
99 | "selection instance", found); |
---|
100 | } |
---|
101 | else if (taskInstance.getTask() instanceof IIteration) { |
---|
102 | ITask childTask = ((IIteration) taskInstance.getTask()).getMarkedTask(); |
---|
103 | assertNotNull("child task of iteration model must not be null", childTask); |
---|
104 | assertFalse("child of iteration model must not be an iteration", |
---|
105 | childTask instanceof IIteration); |
---|
106 | assertFalse("child of iteration model must not be an optional", |
---|
107 | childTask instanceof IOptional); |
---|
108 | |
---|
109 | for (int i = 0; i < taskInstance.size(); i++) { |
---|
110 | assertNotNull("iteration instance child " + i + " was null", taskInstance.get(i)); |
---|
111 | assertSame("task of iteration child " + i + " does not match iteration model", |
---|
112 | childTask, taskInstance.get(i).getTask()); |
---|
113 | } |
---|
114 | } |
---|
115 | else if (taskInstance.getTask() instanceof IOptional) { |
---|
116 | ITask childTask = ((IOptional) taskInstance.getTask()).getMarkedTask(); |
---|
117 | assertNotNull("child task of optional model must not be null", childTask); |
---|
118 | assertFalse("child of optional model must not be an optional", |
---|
119 | childTask instanceof IOptional); |
---|
120 | |
---|
121 | assertEquals |
---|
122 | ("number of children of optional instance must be 1", 1, taskInstance.size()); |
---|
123 | |
---|
124 | assertEquals("task of optional child does not match optional model", |
---|
125 | childTask, taskInstance.get(0).getTask()); |
---|
126 | } |
---|
127 | else if (taskInstance.getTask() instanceof IEventTask) { |
---|
128 | IEventTask task = (IEventTask) taskInstance.getTask(); |
---|
129 | assertNotNull("event task model must not be null", task); |
---|
130 | assertNotNull("event type of event task model must not be null", task.getEventType()); |
---|
131 | assertNotNull |
---|
132 | ("event target of event task model must not be null", task.getEventTarget()); |
---|
133 | |
---|
134 | assertEquals("event task instance must not have children", 0, taskInstance.size()); |
---|
135 | } |
---|
136 | else { |
---|
137 | fail("unknown task model: " + taskInstance.getTask()); |
---|
138 | } |
---|
139 | |
---|
140 | for (ITaskInstance child : taskInstance) { |
---|
141 | validate(child); |
---|
142 | } |
---|
143 | } |
---|
144 | } |
---|