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

Last change on this file since 2258 was 2258, checked in by pharms, 6 years ago
File size: 11.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.HashMap;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.Map;
23
24import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskInstanceTraversingVisitor;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
35import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
36import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
37import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
38import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
39
40/**
41 * <p>
42 * Convenience class to validate if certain restrictions to task trees and task tree instances
43 * apply. Restrictions are, e.g., that for every task there need to be instances or that for every
44 * sequence instance the children match the corresponding sequence model. This class is vor test and
45 * evaluation purposes only.
46 * </p>
47 *
48 * @author Patrick Harms
49 */
50public class TaskTreeValidator {
51
52    /**
53     *
54     */
55    public void validate(List<IUserSession> userSessions, boolean checkInstances) {
56        validate(userSessions);
57       
58        if (!checkInstances) {
59            return;
60        }
61       
62        Map<ITask, List<ITaskInstance>> allTasks = new HashMap<ITask, List<ITaskInstance>>();
63       
64        for (IUserSession userSession : userSessions) {
65            getAllTasksAndInstances(userSession, allTasks);
66        }
67       
68        for (Map.Entry<ITask, List<ITaskInstance>> entry : allTasks.entrySet()) {
69            assertEquals("number of task instances of task " + entry.getKey() + " in the " +
70                         "sessions is not equal to those referenced by the model",
71                         entry.getValue().size(), entry.getKey().getInstances().size());
72           
73            for (ITaskInstance candidate : entry.getValue()) {
74                boolean found = false;
75                for (ITaskInstance instance : entry.getKey().getInstances()) {
76                    if (candidate.equals(instance)) {
77                        if (!found) {
78                            found = true;
79                        }
80                        else {
81                            fail("the same instance is referred twice by the task");
82                        }
83                    }
84                }
85               
86                assertTrue("instance " + candidate + " is not referred by task", found);
87            }
88        }
89    }
90
91    /**
92     *
93     */
94    public void validate(List<IUserSession> userSessions) {
95        for (IUserSession userSession : userSessions) {
96            validate(userSession);
97        }
98    }
99
100    /**
101     *
102     */
103    public void validate(ITaskInstanceList taskInstances) {
104        for (ITaskInstance taskInstance : taskInstances) {
105            validate(taskInstance);
106        }
107    }
108
109    /**
110     *
111     */
112    public void validate(ITaskInstance taskInstance) {
113        assertNotNull("task model of task instance must not be null", taskInstance.getTask());
114       
115        if (taskInstance.getTask() instanceof ISequence) {
116            ISequence task = (ISequence) taskInstance.getTask();
117           
118            assertEquals("number of children of sequence instance must match sequence model",
119                         ((ISequenceInstance) taskInstance).size(), task.getChildren().size());
120           
121            for (int i = 0; i < ((ISequenceInstance) taskInstance).size(); i++) {
122                assertNotNull("sequence instance child " + i + " was null",
123                              ((ISequenceInstance) taskInstance).get(i));
124                ITask childTask = ((ISequenceInstance) taskInstance).get(i).getTask();
125                assertSame("task of sequence child " + i + " does not match sequence model",
126                           childTask, task.getChildren().get(i));
127            }
128        }
129        else if (taskInstance.getTask() instanceof ISelection) {
130            ISelection task = (ISelection) taskInstance.getTask();
131           
132            assertNotNull("number of children of selection instance must be 1",
133                          ((ISelectionInstance) taskInstance).getChild());
134            assertTrue
135                ("number of children of selection must be larger 0", task.getChildren().size() > 0);
136           
137            boolean found = false;
138            for (ITask childTask : task.getChildren()) {
139                assertNotNull("child of selection model must not be null", childTask);
140                assertFalse("child of selection model must not be a selection",
141                            childTask instanceof ISelection);
142                /*assertFalse("child of selection model must not be an optional",
143                            childTask instanceof IOptional);*/
144                if (childTask.equals(((ISelectionInstance) taskInstance).getChild().getTask())) {
145                    found = true;
146                    break;
147                }
148            }
149           
150            assertTrue("no child of the selection model matches the model of child of the " +
151                       "selection instance", found);
152        }
153        else if (taskInstance.getTask() instanceof IIteration) {
154            ITask childTask = ((IIteration) taskInstance.getTask()).getMarkedTask();
155            assertNotNull("child task of iteration model must not be null", childTask);
156            assertFalse("child of iteration model must not be an iteration",
157                        childTask instanceof IIteration);
158            assertFalse("child of iteration model must not be an optional",
159                        childTask instanceof IOptional);
160           
161            for (int i = 0; i < ((IIterationInstance) taskInstance).size(); i++) {
162                assertNotNull("iteration instance child " + i + " was null",
163                              ((IIterationInstance) taskInstance).get(i));
164                assertSame("task of iteration child " + i + " does not match iteration model",
165                           childTask, ((IIterationInstance) taskInstance).get(i).getTask());
166            }
167        }
168        else if (taskInstance.getTask() instanceof IOptional) {
169            ITask childTask = ((IOptional) taskInstance.getTask()).getMarkedTask();
170            assertNotNull("child task of optional model must not be null", childTask);
171            assertFalse("child of optional model must not be an optional",
172                        childTask instanceof IOptional);
173           
174            if (((IOptionalInstance) taskInstance).getChild() != null) {
175                assertEquals("task of optional child does not match optional model",
176                             childTask, ((IOptionalInstance) taskInstance).getChild().getTask());
177            }
178        }
179        else if (taskInstance.getTask() instanceof IEventTask) {
180            IEventTask task = (IEventTask) taskInstance.getTask();
181            assertNotNull("event task model must not be null", task);
182            assertNotNull("event of event task instance must not be null",
183                          ((IEventTaskInstance) taskInstance).getEvent());
184        }
185        else {
186            fail("unknown task model: " + taskInstance.getTask());
187        }
188       
189        if (taskInstance instanceof ITaskInstanceList) {
190            for (ITaskInstance child : (ITaskInstanceList) taskInstance) {
191                validate(child);
192            }
193        }
194        else if (taskInstance instanceof ISelectionInstance) {
195            validate(((ISelectionInstance) taskInstance).getChild());
196        }
197        else if (taskInstance instanceof IOptionalInstance) {
198            if (((IOptionalInstance) taskInstance).getChild() != null) {
199                validate(((IOptionalInstance) taskInstance).getChild());
200            }
201        }
202    }
203
204
205    /**
206     *
207     */
208    private void getAllTasksAndInstances(ITaskInstanceList                     taskInstances,
209                                         final Map<ITask, List<ITaskInstance>> allTasks)
210    {
211        for (ITaskInstance taskInstance : taskInstances) {
212           
213            taskInstance.accept(new DefaultTaskInstanceTraversingVisitor() {
214
215                /* (non-Javadoc)
216                 * @see DefaultTaskInstanceTraversingVisitor#visit(IOptionalInstance)
217                 */
218                @Override
219                public void visit(IOptionalInstance optionalInstance) {
220                    addToInstanceList(optionalInstance);
221                    super.visit(optionalInstance);
222                }
223
224                /* (non-Javadoc)
225                 * @see DefaultTaskInstanceTraversingVisitor#visit(ISelectionInstance)
226                 */
227                @Override
228                public void visit(ISelectionInstance selectionInstance) {
229                    addToInstanceList(selectionInstance);
230                    super.visit(selectionInstance);
231                }
232
233                /* (non-Javadoc)
234                 * @see DefaultTaskInstanceTraversingVisitor#visit(IEventTaskInstance)
235                 */
236                @Override
237                public void visit(IEventTaskInstance eventTaskInstance) {
238                    addToInstanceList(eventTaskInstance);
239                    super.visit(eventTaskInstance);
240                }
241
242                /* (non-Javadoc)
243                 * @see DefaultTaskInstanceTraversingVisitor#visit(IIterationInstance)
244                 */
245                @Override
246                public void visit(IIterationInstance iterationInstance) {
247                    addToInstanceList(iterationInstance);
248                    super.visit(iterationInstance);
249                }
250
251                /* (non-Javadoc)
252                 * @see DefaultTaskInstanceTraversingVisitor#visit(ISequenceInstance)
253                 */
254                @Override
255                public void visit(ISequenceInstance sequenceInstance) {
256                    addToInstanceList(sequenceInstance);
257                    super.visit(sequenceInstance);
258                }
259
260                private void addToInstanceList(ITaskInstance taskInstance) {
261                    List<ITaskInstance> instances = allTasks.get(taskInstance.getTask());
262                   
263                    if (instances == null) {
264                        instances = new LinkedList<ITaskInstance>();
265                        allTasks.put(taskInstance.getTask(), instances);
266                    }
267                   
268                    boolean found = false;
269                   
270                    for (ITaskInstance candidate : instances) {
271                        if (candidate.equals(taskInstance)) {
272                            found = true;
273                            break;
274                        }
275                    }
276                   
277                    assertFalse("instance " + taskInstance + " occurred twice", found);
278                   
279                    instances.add(taskInstance);
280                }
281               
282            });
283        }
284    }
285}
Note: See TracBrowser for help on using the repository browser.