Index: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRule.java
===================================================================
--- trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRule.java	(revision 2131)
+++ trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRule.java	(revision 2146)
@@ -332,5 +332,9 @@
                 IIteration iteration = iterations.get(currentTask);
                 if (iteration != null) {
+                    // replacement required. Check if we already replaced a previous child, or if
+                    // we did, if the current child needs to be in the same iteration instance or
+                    // a new one.
                     if (!inReplacement || (iterationInstance.getTask() != iteration)) {
+                        // initiate a new replacement by creating the corresponding new instance
                         if (currentTask instanceof IOptional) {
                             IOptional optional = optionals.get(iteration);
@@ -347,4 +351,6 @@
                     }
                     
+                    // add the current task instance to the iteration instance that is going to
+                    // replace it
                     if (currentTask instanceof IOptional) {
                         ITaskInstance child = ((IOptionalInstance) session.get(index)).getChild();
@@ -363,7 +369,9 @@
                     }
                     
+                    // remove the task instance that was added to the replacing iteration instance
                     taskBuilder.removeTaskInstance(session, index);
                 }
                 else {
+                    // no replacement require. Finish previous replacements and continue
                     if (iterationInstance != null) {
                         iterationInstance = null;
@@ -384,5 +392,14 @@
     /**
      * <p>
-     * TODO clarify why this is done
+     * This may not be required anymore. The goal was the following. Consider two subsequent task
+     * instances which are detected to be repeating tasks on a level defined by the
+     * preparationTaskHandlingStrategy. Then the replacement of these iterated tasks by iteration
+     * instances would result in an iteration instance with two children, both of them having a
+     * different task assigned which does not match the child of the iteration model. This needs
+     * to be harmonized afterwards.
+     * </p>
+     * <p>
+     * <b>But, because we introduced the harmonization of event tasks directly at the beginning and
+     * afterwards, we compare only based on object identity, this may not be required anymore.</b>
      * </p>
      */
@@ -1217,9 +1234,4 @@
         List<Subsequence> potentialSuccessors = new LinkedList<Subsequence>();
         
-        appData.getStopWatch().start("determine locations");
-        Map<Subsequence, List<SubsequenceLocation>> allLocations =
-            getLocations(subsequences, appData);
-        appData.getStopWatch().stop("determine locations");
-        
         for (Subsequence subsequence : subsequences) {
             // Console.traceln
@@ -1240,8 +1252,8 @@
             
             List<Collision> precedingCollisions =
-                getPrecedingCollisions(subsequence, potentialPredecessors, allLocations, appData);
+                getPrecedingCollisions(subsequence, potentialPredecessors, appData);
             
             List<Collision> succeedingCollisions =
-                getSucceedingCollisions(subsequence, potentialSuccessors, allLocations, appData);
+                getSucceedingCollisions(subsequence, potentialSuccessors, appData);
             
             if ((precedingCollisions.size() <= 0) && (succeedingCollisions.size() <= 0)) {
@@ -1431,11 +1443,11 @@
      *
      */
-    private List<Collision> getPrecedingCollisions
-        (Subsequence                                 subsequence,
-         List<Subsequence>                           potentialPredecessors,
-         Map<Subsequence, List<SubsequenceLocation>> allLocations,
-         RuleApplicationData                         appData)
+    private List<Collision> getPrecedingCollisions(Subsequence         subsequence,
+                                                   List<Subsequence>   potentialPredecessors,
+                                                   RuleApplicationData appData)
     {
         List<Collision> precedingCollisions = new LinkedList<Collision>();
+        Map<Subsequence, List<SubsequenceLocation>> allLocations =
+            appData.getLastFoundSubsequenceLocations();
         
         for (SubsequenceLocation location : allLocations.get(subsequence)) {
@@ -1457,12 +1469,12 @@
      *
      */
-    private List<Collision> getSucceedingCollisions
-        (Subsequence                                 subsequence,
-         List<Subsequence>                           potentialSuccessors,
-         Map<Subsequence, List<SubsequenceLocation>> allLocations,
-         RuleApplicationData                         appData)
+    private List<Collision> getSucceedingCollisions(Subsequence         subsequence,
+                                                    List<Subsequence>   potentialSuccessors,
+                                                    RuleApplicationData appData)
     {
         List<Collision> succeedingCollisions = new LinkedList<Collision>();
-
+        Map<Subsequence, List<SubsequenceLocation>> allLocations =
+            appData.getLastFoundSubsequenceLocations();
+        
         for (SubsequenceLocation location : allLocations.get(subsequence)) {
             for (Subsequence successor : potentialSuccessors) {
@@ -1724,7 +1736,7 @@
      * @return
      */
-    private int getSubListIndex(ITaskInstanceList   list,
-                                Subsequence         subsequence,
-                                int                 startIndex)
+    private static int getSubListIndex(ITaskInstanceList   list,
+                                       Subsequence         subsequence,
+                                       int                 startIndex)
     {
         boolean matchFound;
@@ -2141,4 +2153,9 @@
          * 
          */
+        private Map<Subsequence, List<SubsequenceLocation>> lastFoundSubsequenceLocations;
+        
+        /**
+         * 
+         */
         private boolean detectedAndReplacedTasks;
         
@@ -2200,4 +2217,5 @@
         private void setLastFoundSubsequences(Subsequences lastFoundSequences) {
             this.lastFoundSubsequences = lastFoundSequences;
+            this.lastFoundSubsequenceLocations = null;
         }
 
@@ -2207,4 +2225,39 @@
         private Subsequences getLastFoundSubsequences() {
             return lastFoundSubsequences;
+        }
+
+        /**
+         * @return the lastFoundSequences
+         */
+        private Map<Subsequence, List<SubsequenceLocation>> getLastFoundSubsequenceLocations() {
+            if (lastFoundSubsequenceLocations != null) {
+                return lastFoundSubsequenceLocations;
+            }
+            
+            lastFoundSubsequenceLocations =
+                new IdentityHashMap<Subsequence, List<SubsequenceLocation>>();
+                
+            // fill the map with empty locations
+            for (Subsequence subsequence : lastFoundSubsequences) {
+                lastFoundSubsequenceLocations.put
+                    (subsequence, new LinkedList<SubsequenceLocation>());
+            }
+            
+            for (IUserSession session : sessions) {
+                for (Subsequence candidate : lastFoundSubsequences) {
+                    int index = -1;
+                    do {
+                        index = getSubListIndex(session, candidate, index + 1);
+                    
+                        if (index > -1) {
+                            lastFoundSubsequenceLocations.get
+                                (candidate).add(new SubsequenceLocation(session, index));
+                        }
+                    }
+                    while (index > -1);
+                }
+            }
+                
+            return lastFoundSubsequenceLocations;
         }
 
