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.temporalrelation.utils; |
---|
16 | |
---|
17 | import java.util.HashMap; |
---|
18 | import java.util.LinkedList; |
---|
19 | import java.util.List; |
---|
20 | import java.util.Map; |
---|
21 | |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskInstanceTraversingVisitor; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
37 | |
---|
38 | /** |
---|
39 | * <p> |
---|
40 | * Convenience class to validate if certain restrictions to task trees and task tree instances |
---|
41 | * apply. Restrictions are, e.g., that for every task there need to be instances or that for every |
---|
42 | * sequence instance the children match the corresponding sequence model. This class is vor test and |
---|
43 | * evaluation purposes only. |
---|
44 | * </p> |
---|
45 | * |
---|
46 | * @author Patrick Harms |
---|
47 | */ |
---|
48 | public class TaskTreeValidator { |
---|
49 | |
---|
50 | /** |
---|
51 | * |
---|
52 | */ |
---|
53 | public void validate(List<IUserSession> userSessions, boolean checkInstances) { |
---|
54 | validate(userSessions); |
---|
55 | |
---|
56 | if (!checkInstances) { |
---|
57 | return; |
---|
58 | } |
---|
59 | |
---|
60 | Map<ITask, List<ITaskInstance>> allTasks = new HashMap<ITask, List<ITaskInstance>>(); |
---|
61 | |
---|
62 | for (IUserSession userSession : userSessions) { |
---|
63 | getAllTasksAndInstances(userSession, allTasks); |
---|
64 | } |
---|
65 | |
---|
66 | for (Map.Entry<ITask, List<ITaskInstance>> entry : allTasks.entrySet()) { |
---|
67 | assertEquals(entry.getKey(), "number of task instances of task " + entry.getKey() + |
---|
68 | " in the sessions is not equal to those referenced by the model", |
---|
69 | entry.getValue().size(), entry.getKey().getInstances().size()); |
---|
70 | |
---|
71 | for (ITaskInstance candidate : entry.getValue()) { |
---|
72 | boolean found = false; |
---|
73 | for (ITaskInstance instance : entry.getKey().getInstances()) { |
---|
74 | if (candidate.equals(instance)) { |
---|
75 | if (!found) { |
---|
76 | found = true; |
---|
77 | } |
---|
78 | else { |
---|
79 | fail(entry.getKey(), "the same instance is referred twice by the task"); |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | assertTrue(candidate.getTask(), "instance " + candidate + |
---|
85 | " is not referred by task", found); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * |
---|
92 | */ |
---|
93 | public void validate(List<IUserSession> userSessions) { |
---|
94 | for (IUserSession userSession : userSessions) { |
---|
95 | validate(userSession); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * |
---|
101 | */ |
---|
102 | public void validate(ITaskInstanceList taskInstances) { |
---|
103 | for (ITaskInstance taskInstance : taskInstances) { |
---|
104 | validate(taskInstance); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * |
---|
110 | */ |
---|
111 | public void validate(ITask task) { |
---|
112 | assertTrue(task, "task has no instances", task.getInstances().size() > 0); |
---|
113 | assertTrue(task, "task has no instances", task.getInstances().iterator().hasNext()); |
---|
114 | assertNotNull(task, "task has no instances", task.getInstances().iterator().next()); |
---|
115 | |
---|
116 | for (ITaskInstance taskInstance : task.getInstances()) { |
---|
117 | assertSame(task, "task of instance does not match task", task, taskInstance.getTask()); |
---|
118 | validate(taskInstance); |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * |
---|
124 | */ |
---|
125 | public void validate(ITaskInstance taskInstance) { |
---|
126 | ITask task = taskInstance.getTask(); |
---|
127 | assertNotNull(task, "task model of task instance must not be null", task); |
---|
128 | |
---|
129 | if (task instanceof ISequence) { |
---|
130 | ISequence seq = (ISequence) taskInstance.getTask(); |
---|
131 | |
---|
132 | assertEquals(seq, "number of children of sequence instance must match sequence model", |
---|
133 | ((ISequenceInstance) taskInstance).size(), seq.getChildren().size()); |
---|
134 | |
---|
135 | for (int i = 0; i < ((ISequenceInstance) taskInstance).size(); i++) { |
---|
136 | assertNotNull(seq, "sequence instance child " + i + " was null", |
---|
137 | ((ISequenceInstance) taskInstance).get(i)); |
---|
138 | ITask childTask = ((ISequenceInstance) taskInstance).get(i).getTask(); |
---|
139 | assertSame(seq, "task of sequence instance child " + i + "(" + |
---|
140 | ((ISequenceInstance) taskInstance).get(i) + ") does not match " + |
---|
141 | "sequence model (" + seq.getChildren().get(i) + ")", |
---|
142 | childTask, seq.getChildren().get(i)); |
---|
143 | } |
---|
144 | } |
---|
145 | else if (task instanceof ISelection) { |
---|
146 | ISelection sel = (ISelection) task; |
---|
147 | |
---|
148 | assertNotNull(sel, "number of children of selection instance must be 1", |
---|
149 | ((ISelectionInstance) taskInstance).getChild()); |
---|
150 | |
---|
151 | assertTrue(sel, "number of children of selection must be larger 0", |
---|
152 | sel.getChildren().size() > 0); |
---|
153 | |
---|
154 | boolean found = false; |
---|
155 | for (ITask childTask : sel.getChildren()) { |
---|
156 | assertNotNull(sel, "child of selection model must not be null", childTask); |
---|
157 | assertFalse(sel, "child of selection model must not be a selection", |
---|
158 | childTask instanceof ISelection); |
---|
159 | /*assertFalse("child of selection model must not be an optional", |
---|
160 | childTask instanceof IOptional);*/ |
---|
161 | if (childTask.equals(((ISelectionInstance) taskInstance).getChild().getTask())) { |
---|
162 | found = true; |
---|
163 | break; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | assertTrue(sel, "no child of the selection model matches the model of child of the " + |
---|
168 | "selection instance", found); |
---|
169 | } |
---|
170 | else if (task instanceof IIteration) { |
---|
171 | ITask childTask = ((IIteration) task).getMarkedTask(); |
---|
172 | assertNotNull(task, "child task of iteration model must not be null", childTask); |
---|
173 | assertFalse(task, "child of iteration model must not be an iteration", |
---|
174 | childTask instanceof IIteration); |
---|
175 | assertFalse(task, "child of iteration model must not be an optional", |
---|
176 | childTask instanceof IOptional); |
---|
177 | |
---|
178 | for (int i = 0; i < ((IIterationInstance) taskInstance).size(); i++) { |
---|
179 | assertNotNull(task, "iteration instance child " + i + " was null", |
---|
180 | ((IIterationInstance) taskInstance).get(i)); |
---|
181 | assertSame(task, "task of iteration child " + i + " does not match iteration model", |
---|
182 | childTask, ((IIterationInstance) taskInstance).get(i).getTask()); |
---|
183 | } |
---|
184 | } |
---|
185 | else if (task instanceof IOptional) { |
---|
186 | ITask childTask = ((IOptional) task).getMarkedTask(); |
---|
187 | assertNotNull(task, "child task of optional model must not be null", childTask); |
---|
188 | assertFalse(task, "child of optional model must not be an optional", |
---|
189 | childTask instanceof IOptional); |
---|
190 | |
---|
191 | if (((IOptionalInstance) taskInstance).getChild() != null) { |
---|
192 | assertSame(task, "task of optional child does not match optional model", childTask, |
---|
193 | ((IOptionalInstance) taskInstance).getChild().getTask()); |
---|
194 | } |
---|
195 | } |
---|
196 | else if (task instanceof IEventTask) { |
---|
197 | IEventTask eventTask = (IEventTask) task; |
---|
198 | assertNotNull(task, "event task model must not be null", eventTask); |
---|
199 | assertNotNull(task, "event of event task instance must not be null", |
---|
200 | ((IEventTaskInstance) taskInstance).getEvent()); |
---|
201 | } |
---|
202 | else { |
---|
203 | fail(task, "unknown task model: " + task); |
---|
204 | } |
---|
205 | |
---|
206 | if (taskInstance instanceof ITaskInstanceList) { |
---|
207 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
208 | validate(child); |
---|
209 | } |
---|
210 | } |
---|
211 | else if (taskInstance instanceof ISelectionInstance) { |
---|
212 | validate(((ISelectionInstance) taskInstance).getChild()); |
---|
213 | } |
---|
214 | else if (taskInstance instanceof IOptionalInstance) { |
---|
215 | if (((IOptionalInstance) taskInstance).getChild() != null) { |
---|
216 | validate(((IOptionalInstance) taskInstance).getChild()); |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * |
---|
223 | */ |
---|
224 | private void getAllTasksAndInstances(ITaskInstanceList taskInstances, |
---|
225 | final Map<ITask, List<ITaskInstance>> allTasks) |
---|
226 | { |
---|
227 | for (ITaskInstance taskInstance : taskInstances) { |
---|
228 | |
---|
229 | taskInstance.accept(new DefaultTaskInstanceTraversingVisitor() { |
---|
230 | |
---|
231 | /* (non-Javadoc) |
---|
232 | * @see DefaultTaskInstanceTraversingVisitor#visit(IOptionalInstance) |
---|
233 | */ |
---|
234 | @Override |
---|
235 | public void visit(IOptionalInstance optionalInstance) { |
---|
236 | addToInstanceList(optionalInstance); |
---|
237 | super.visit(optionalInstance); |
---|
238 | } |
---|
239 | |
---|
240 | /* (non-Javadoc) |
---|
241 | * @see DefaultTaskInstanceTraversingVisitor#visit(ISelectionInstance) |
---|
242 | */ |
---|
243 | @Override |
---|
244 | public void visit(ISelectionInstance selectionInstance) { |
---|
245 | addToInstanceList(selectionInstance); |
---|
246 | super.visit(selectionInstance); |
---|
247 | } |
---|
248 | |
---|
249 | /* (non-Javadoc) |
---|
250 | * @see DefaultTaskInstanceTraversingVisitor#visit(IEventTaskInstance) |
---|
251 | */ |
---|
252 | @Override |
---|
253 | public void visit(IEventTaskInstance eventTaskInstance) { |
---|
254 | addToInstanceList(eventTaskInstance); |
---|
255 | super.visit(eventTaskInstance); |
---|
256 | } |
---|
257 | |
---|
258 | /* (non-Javadoc) |
---|
259 | * @see DefaultTaskInstanceTraversingVisitor#visit(IIterationInstance) |
---|
260 | */ |
---|
261 | @Override |
---|
262 | public void visit(IIterationInstance iterationInstance) { |
---|
263 | addToInstanceList(iterationInstance); |
---|
264 | super.visit(iterationInstance); |
---|
265 | } |
---|
266 | |
---|
267 | /* (non-Javadoc) |
---|
268 | * @see DefaultTaskInstanceTraversingVisitor#visit(ISequenceInstance) |
---|
269 | */ |
---|
270 | @Override |
---|
271 | public void visit(ISequenceInstance sequenceInstance) { |
---|
272 | addToInstanceList(sequenceInstance); |
---|
273 | super.visit(sequenceInstance); |
---|
274 | } |
---|
275 | |
---|
276 | private void addToInstanceList(ITaskInstance taskInstance) { |
---|
277 | List<ITaskInstance> instances = allTasks.get(taskInstance.getTask()); |
---|
278 | |
---|
279 | if (instances == null) { |
---|
280 | instances = new LinkedList<ITaskInstance>(); |
---|
281 | allTasks.put(taskInstance.getTask(), instances); |
---|
282 | } |
---|
283 | |
---|
284 | boolean found = false; |
---|
285 | |
---|
286 | for (ITaskInstance candidate : instances) { |
---|
287 | if (candidate.equals(taskInstance)) { |
---|
288 | found = true; |
---|
289 | break; |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | assertFalse(taskInstance.getTask(), "instance " + taskInstance + |
---|
294 | " occurred twice", found); |
---|
295 | |
---|
296 | instances.add(taskInstance); |
---|
297 | } |
---|
298 | |
---|
299 | }); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * <p> |
---|
305 | * convenience method to log throw issues found during the evaluation |
---|
306 | * </p> |
---|
307 | */ |
---|
308 | private void fail(ITask task, String message) { |
---|
309 | System.err.println("failing validation for task " + task); |
---|
310 | System.err.println("message: " + message); |
---|
311 | new TaskTreeEncoder().encode(task, System.err); |
---|
312 | |
---|
313 | throw new RuntimeException(message); |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | * <p> |
---|
318 | * convenience method to log throw issues found during the evaluation |
---|
319 | * </p> |
---|
320 | */ |
---|
321 | private void assertNotNull(ITask task, String message, Object object) { |
---|
322 | if (object == null) { |
---|
323 | System.err.println("failing validation for task " + task); |
---|
324 | System.err.println("message: " + message); |
---|
325 | new TaskTreeEncoder().encode(task, System.err); |
---|
326 | |
---|
327 | throw new RuntimeException(message); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * <p> |
---|
333 | * convenience method to log throw issues found during the evaluation |
---|
334 | * </p> |
---|
335 | */ |
---|
336 | private void assertTrue(ITask task, String message, boolean value) { |
---|
337 | if (!value) { |
---|
338 | System.err.println("failing validation for task " + task); |
---|
339 | System.err.println("message: " + message); |
---|
340 | new TaskTreeEncoder().encode(task, System.err); |
---|
341 | |
---|
342 | throw new RuntimeException(message); |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * <p> |
---|
348 | * convenience method to log throw issues found during the evaluation |
---|
349 | * </p> |
---|
350 | */ |
---|
351 | private void assertFalse(ITask task, String message, boolean value) { |
---|
352 | if (value) { |
---|
353 | System.err.println("failing validation for task " + task); |
---|
354 | System.err.println("message: " + message); |
---|
355 | new TaskTreeEncoder().encode(task, System.err); |
---|
356 | |
---|
357 | throw new RuntimeException(message); |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | /** |
---|
362 | * <p> |
---|
363 | * convenience method to log throw issues found during the evaluation |
---|
364 | * </p> |
---|
365 | */ |
---|
366 | private void assertEquals(ITask task, String message, Object object1, Object object2) { |
---|
367 | if (!object1.equals(object2)) { |
---|
368 | System.err.println("failing validation for task " + task); |
---|
369 | System.err.println("message: " + message); |
---|
370 | new TaskTreeEncoder().encode(task, System.err); |
---|
371 | |
---|
372 | throw new RuntimeException(message); |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * <p> |
---|
378 | * convenience method to log throw issues found during the evaluation |
---|
379 | * </p> |
---|
380 | */ |
---|
381 | private void assertSame(ITask task, String message, Object object1, Object object2) { |
---|
382 | if (object1 != object2) { |
---|
383 | System.err.println("failing validation for task " + task); |
---|
384 | System.err.println("message: " + message); |
---|
385 | new TaskTreeEncoder().encode(task, System.err); |
---|
386 | |
---|
387 | throw new RuntimeException(message); |
---|
388 | } |
---|
389 | } |
---|
390 | } |
---|