Index: trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeValidator.java
===================================================================
--- trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeValidator.java	(revision 1333)
+++ trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeValidator.java	(revision 1638)
@@ -17,6 +17,10 @@
 import static org.junit.Assert.*;
 
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
-
+import java.util.Map;
+
+import de.ugoe.cs.autoquest.tasktrees.treeifc.DefaultTaskInstanceTraversingVisitor;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance;
@@ -46,4 +50,43 @@
      * 
      */
+    public void validate(List<IUserSession> userSessions, boolean checkInstances) {
+        validate(userSessions);
+        
+        if (!checkInstances) {
+            return;
+        }
+        
+        Map<ITask, List<ITaskInstance>> allTasks = new HashMap<ITask, List<ITaskInstance>>();
+        
+        for (IUserSession userSession : userSessions) {
+            getAllTasksAndInstances(userSession, allTasks);
+        }
+        
+        for (Map.Entry<ITask, List<ITaskInstance>> entry : allTasks.entrySet()) {
+            assertEquals("number of task instances of task " + entry.getKey() + " in the " +
+                         "sessions is not equal to those referenced by the model",
+                         entry.getValue().size(), entry.getKey().getInstances().size());
+            
+            for (ITaskInstance candidate : entry.getValue()) {
+                boolean found = false;
+                for (ITaskInstance instance : entry.getKey().getInstances()) {
+                    if (candidate.equals(instance)) {
+                        if (!found) {
+                            found = true;
+                        }
+                        else {
+                            fail("the same instance is referred twice by the task");
+                        }
+                    }
+                }
+                
+                assertTrue("instance " + candidate + " is not referred by task", found);
+            }
+        }
+    }
+
+    /**
+     * 
+     */
     public void validate(List<IUserSession> userSessions) {
         for (IUserSession userSession : userSessions) {
@@ -94,6 +137,6 @@
                 assertFalse("child of selection model must not be a selection",
                             childTask instanceof ISelection);
-                assertFalse("child of selection model must not be an optional",
-                            childTask instanceof IOptional);
+                /*assertFalse("child of selection model must not be an optional",
+                            childTask instanceof IOptional);*/
                 if (childTask.equals(((ISelectionInstance) taskInstance).getChild().getTask())) {
                     found = true;
@@ -155,3 +198,85 @@
         }
     }
+
+
+    /**
+     * 
+     */
+    private void getAllTasksAndInstances(ITaskInstanceList                     taskInstances,
+                                         final Map<ITask, List<ITaskInstance>> allTasks)
+    {
+        for (ITaskInstance taskInstance : taskInstances) {
+            
+            taskInstance.accept(new DefaultTaskInstanceTraversingVisitor() {
+
+                /* (non-Javadoc)
+                 * @see DefaultTaskInstanceTraversingVisitor#visit(IOptionalInstance)
+                 */
+                @Override
+                public void visit(IOptionalInstance optionalInstance) {
+                    addToInstanceList(optionalInstance);
+                    super.visit(optionalInstance);
+                }
+
+                /* (non-Javadoc)
+                 * @see DefaultTaskInstanceTraversingVisitor#visit(ISelectionInstance)
+                 */
+                @Override
+                public void visit(ISelectionInstance selectionInstance) {
+                    addToInstanceList(selectionInstance);
+                    super.visit(selectionInstance);
+                }
+
+                /* (non-Javadoc)
+                 * @see DefaultTaskInstanceTraversingVisitor#visit(IEventTaskInstance)
+                 */
+                @Override
+                public void visit(IEventTaskInstance eventTaskInstance) {
+                    addToInstanceList(eventTaskInstance);
+                    super.visit(eventTaskInstance);
+                }
+
+                /* (non-Javadoc)
+                 * @see DefaultTaskInstanceTraversingVisitor#visit(IIterationInstance)
+                 */
+                @Override
+                public void visit(IIterationInstance iterationInstance) {
+                    addToInstanceList(iterationInstance);
+                    super.visit(iterationInstance);
+                }
+
+                /* (non-Javadoc)
+                 * @see DefaultTaskInstanceTraversingVisitor#visit(ISequenceInstance)
+                 */
+                @Override
+                public void visit(ISequenceInstance sequenceInstance) {
+                    addToInstanceList(sequenceInstance);
+                    super.visit(sequenceInstance);
+                }
+
+                private void addToInstanceList(ITaskInstance taskInstance) {
+                    List<ITaskInstance> instances = allTasks.get(taskInstance.getTask());
+                    
+                    if (instances == null) {
+                        instances = new LinkedList<ITaskInstance>();
+                        allTasks.put(taskInstance.getTask(), instances);
+                    }
+                    
+                    boolean found = false;
+                    
+                    for (ITaskInstance candidate : instances) {
+                        if (candidate.equals(taskInstance)) {
+                            found = true;
+                            break;
+                        }
+                    }
+                    
+                    assertFalse("instance " + taskInstance + " occurred twice", found);
+                    
+                    instances.add(taskInstance);
+                }
+                
+            });
+        }
+    }
 }
