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