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