source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/utils/TaskTreeValidator.java @ 1852

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