Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java	(revision 1674)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/matrix/ObjectDistanceSubstitionMatrix.java	(revision 1675)
@@ -55,4 +55,8 @@
 				eti1 = (IEventTaskInstance) obj1;
 			}
+			else {
+				//System.out.println("Not an EventTaskInstance");
+				continue;
+			}
 		
 			for (Iterator<ITaskInstance> jt = uniqueTasks.getSymbols()
@@ -62,4 +66,8 @@
 				if (obj2 instanceof IEventTaskInstance) {
 					eti2 = (IEventTaskInstance) obj2;
+				}
+				else{
+					//System.out.println("Not an EventtaskInstance");
+					continue;
 				}
 				IGUIElement first = (IGUIElement) eti1.getEvent().getTarget();
@@ -102,12 +110,4 @@
 			}
 		}
-		
-		//System.out.println(meandistance/(uniqueTasks.size()*uniqueTasks.size()));
-		//System.out.println(idmapping.toString());
-		//System.out.println(matrix.toString());
-		//System.out.println("ObjectDistanceMatrix: MaxDistance: " + maxDistance);
-		//System.out.println(idmapping.keySet().toString());
-		//System.out.println(idmapping.values().toString());
-		
 	}
 	
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java	(revision 1674)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/RuleUtils.java	(revision 1675)
@@ -15,8 +15,7 @@
 package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
 
-import java.util.Iterator;
-
-import org.apache.commons.lang.NotImplementedException;
-
+import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
+import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
@@ -129,5 +128,5 @@
 						
 					if(((IMarkingTemporalRelationship) tempTask).getMarkedTask() == parent.get(startIndex).getTask()) {
-						//System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+						System.out.println("Adding OptionalInstance " + parent.get(startIndex) + " to " + tempTask.getType());
 						IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
 						taskBuilder.setChild(optional, parent.get(startIndex));
@@ -135,5 +134,5 @@
 					}
 					else {
-						//System.out.println("Adding Empty optional, not deleting anything from the input sequence");
+						System.out.println("Adding Empty optional, not deleting anything from the input sequence");
 						IOptionalInstance optional = taskFactory.createNewTaskInstance((IOptional) tempTask);
 						taskBuilder.addChild(subsequence, optional);
@@ -143,5 +142,5 @@
 					}				
 			} else if (tempTask.getType() == "selection") {
-				//System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+				System.out.println("Adding SelectionInstance " + parent.get(startIndex) + " to " + tempTask.getType());
 				ISelectionInstance selection = taskFactory.createNewTaskInstance((ISelection) tempTask);
 				taskBuilder.setChild(selection, parent.get(startIndex) );
@@ -149,14 +148,14 @@
 
 			} else if (tempTask.getType() == "sequence") {
-				//System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask.getType());
-				//TODO: Implement this
-				throw new NotImplementedException();
+				System.out.println("Adding SequenceInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+				ISequenceInstance sequence = taskFactory.createNewTaskInstance((ISequence) tempTask);
+				taskBuilder.addChild(sequence,parent.get(startIndex));
 				
 			} else if (tempTask.getType() == "iteration") {
-				//TODO: Implement this
-				//System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType());
-				throw new NotImplementedException();
+				IIterationInstance iteration = taskFactory.createNewTaskInstance((IIteration) tempTask);
+				taskBuilder.addChild(iteration,parent.get(startIndex));
+				System.out.println("Adding IterationInstance " + parent.get(startIndex) + " to " + tempTask.getType());
 			} else {
-				//System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType());
+				System.out.println("Adding EventInstance " + parent.get(startIndex) + " to " + tempTask.getType());
 				taskBuilder.addChild(subsequence, parent.get(startIndex));
 			}
@@ -164,12 +163,4 @@
 			modelindex++;
 		}
-		/*
-		System.out.println();
-		System.out.println("Sequence instance:");
-		for(Iterator<ITaskInstance> it = subsequence.iterator();it.hasNext();) {
-			System.out.println(it.next());
-		}*/
-		//System.out.println();
-		//System.out.println();
 		
 		taskBuilder.addTaskInstance(parent, startIndex, subsequence);
Index: /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
===================================================================
--- /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1674)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1675)
@@ -45,5 +45,4 @@
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
-import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
 import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
 import de.ugoe.cs.autoquest.usageprofiles.SymbolMap;
@@ -148,11 +147,10 @@
 		RuleApplicationData appData = new RuleApplicationData(sessions);
 
-
 		harmonizeEventTaskInstancesModel(appData);
-
+		appData.detectedAndReplacedTasks = false;
 		do {
 
 			appData.getStopWatch().start("whole loop"); //
-			//detectAndReplaceIterations(appData);
+			// detectAndReplaceIterations(appData);
 			appData.getStopWatch().start("task replacement"); //
 			detectAndReplaceTasks(appData); //
@@ -179,5 +177,24 @@
 	}
 
-	
+	private ArrayList<NumberSequence> createNumberSequences(
+			RuleApplicationData appData) {
+		ArrayList<NumberSequence> result = new ArrayList<NumberSequence>();
+		for (int i = 0; i < appData.getSessions().size(); i++) {
+			IUserSession session = appData.getSessions().get(i);
+			NumberSequence templist = new NumberSequence(session.size());
+			for (int j = 0; j < session.size(); j++) {
+				ITaskInstance taskInstance = session.get(j);
+				templist.getSequence()[j] = taskInstance.getTask().getId();
+				appData.getNumber2Task().put(templist.getSequence()[j],
+						taskInstance.getTask());
+			}
+			// Each NumberSequence is identified by its id, beginning to count
+			// at zero
+			templist.setId(i);
+			result.add(templist);
+		}
+		return result;
+	}
+
 	/**
 	 * <p>
@@ -193,13 +210,11 @@
 	 * @return Returns the unique tasks symbol map
 	 */
-	private void harmonizeEventTaskInstancesModel(
-			RuleApplicationData appData) {
+	private void harmonizeEventTaskInstancesModel(RuleApplicationData appData) {
 		Console.traceln(Level.INFO,
 				"harmonizing task model of event task instances");
 		appData.getStopWatch().start("harmonizing event tasks");
-		
-		appData.uniqueTasks = preparationTaskHandlingStrategy
-				.createSymbolMap();
-		
+
+		appData.uniqueTasks = preparationTaskHandlingStrategy.createSymbolMap();
+
 		TaskInstanceComparator comparator = preparationTaskHandlingStrategy
 				.getTaskComparator();
@@ -218,26 +233,11 @@
 
 				if (task == null) {
-					appData.getUniqueTasks().addSymbol(taskInstance, taskInstance.getTask());
-					templist.getSequence()[i] = taskInstance.getTask().getId();
-
+					appData.getUniqueTasks().addSymbol(taskInstance,
+							taskInstance.getTask());
 				} else {
 					taskBuilder.setTask(taskInstance, task);
-					templist.getSequence()[i] = task.getId();
 					unifiedTasks++;
 				}
-				appData.getNumber2Task().put(templist.getSequence()[i],
-						taskInstance.getTask());
-
-				// if(j==1) {
-				// System.out.println(i + ": TaskID: " +
-				// taskInstance.getTask().getId()+ " Numbersequence: " +
-				// templist.getSequence()[i]);
-				// }
-
-			}
-			// Each NumberSequence is identified by its id, beginning to count
-			// at zero
-			templist.setId(j);
-			appData.getNumberSequences().add(templist);
+			}
 			comparator.clearBuffers();
 		}
@@ -330,7 +330,10 @@
 
 		for (ITask iteratedTask : iteratedTasks) {
-			
-			//TODO: Add this to uniqueTasks and number2task
+
+			// TODO: Check if this is correct
 			IIteration iteration = taskFactory.createNewIteration();
+			appData.getUniqueTasks().addSymbol(
+					taskFactory.createNewTaskInstance(iteration), iteration);
+			appData.getNumber2Task().put(iteration.getId(), iteration);
 			iterations.put(iteratedTask, iteration);
 			iterationInstances.put(iteration,
@@ -406,4 +409,7 @@
 			else if (first[i] == -1 && second[i] != -1) {
 				IOptional optional = taskFactory.createNewOptional();
+				appData.uniqueTasks.addSymbol(
+						taskFactory.createNewTaskInstance(optional), optional);
+				appData.number2task.put(optional.getId(), optional);
 				taskBuilder.setMarkedTask(optional, appData.getNumber2Task()
 						.get(second[i]));
@@ -414,11 +420,20 @@
 			else if (first[i] != -1 && second[i] == -1) {
 				IOptional optional = taskFactory.createNewOptional();
+				appData.uniqueTasks.addSymbol(
+						taskFactory.createNewTaskInstance(optional), optional);
+				appData.number2task.put(optional.getId(), optional);
 				taskBuilder.setMarkedTask(optional, appData.getNumber2Task()
 						.get(first[i]));
 				taskBuilder.addChild(sequence, optional);
 			}
-			// Both tasks are unequal, we need to insert a selection here
+			// Both tasks are not equal, we need to insert a selection here
 			else {
+				System.out.println("First: " + first[i] + " Second: " + second[i]);
 				ISelection selection = taskFactory.createNewSelection();
+				appData.uniqueTasks
+						.addSymbol(
+								taskFactory.createNewTaskInstance(selection),
+								selection);
+				appData.number2task.put(selection.getId(), selection);
 				taskBuilder.addChild(selection,
 						appData.getNumber2Task().get(first[i]));
@@ -517,167 +532,174 @@
 		appData.getStopWatch().start("detecting tasks");
 
-		
+		// Create NumberSequences
+		appData.setNumberSequences(this.createNumberSequences(appData));
+
 		// Generate a substitution matrix between all occurring events.
-				Console.traceln(Level.INFO, "generating substitution matrix");
-				ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix(
-						appData.getUniqueTasks(), 6, -3);
-				submat.generate();
-
-				// Generate pairwise alignments
-				Console.traceln(Level.INFO, "generating pairwise alignments");
-				LinkedList<Match> matchseqs = new LinkedList<Match>();
-				PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator
-						.generate(appData.getNumberSequences(), submat, 9);
-
-				// Retrieve all matches reached a specific threshold
-				Console.traceln(Level.INFO, "retrieving significant sequence pieces");
-				for (int i = 0; i < appData.getNumberSequences().size(); i++) {
-					Console.traceln(
-							Level.FINEST,
-							"retrieving significant sequence pieces:  "
-									+ Math.round((float) i
-											/ (float) appData.getNumberSequences()
-													.size() * 100) + "%");
-					for (int j = 0; j < appData.getNumberSequences().size(); j++) {
-						if (i != j) {
-							matchseqs.addAll(alignments.get(i, j).getMatches());
+		Console.traceln(Level.INFO, "generating substitution matrix");
+		ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix(
+				appData.getUniqueTasks(), 6, -3);
+		submat.generate();
+
+		// Generate pairwise alignments
+		Console.traceln(Level.INFO, "generating pairwise alignments");
+		LinkedList<Match> matchseqs = new LinkedList<Match>();
+		PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator
+				.generate(appData.getNumberSequences(), submat, 9);
+
+		// Retrieve all matches reached a specific threshold
+		Console.traceln(Level.INFO, "retrieving significant sequence pieces");
+		for (int i = 0; i < appData.getNumberSequences().size(); i++) {
+			Console.traceln(
+					Level.FINEST,
+					"retrieving significant sequence pieces:  "
+							+ Math.round((float) i
+									/ (float) appData.getNumberSequences()
+											.size() * 100) + "%");
+			for (int j = 0; j < appData.getNumberSequences().size(); j++) {
+				if (i != j) {
+					matchseqs.addAll(alignments.get(i, j).getMatches());
+				}
+			}
+		}
+		if (matchseqs.size() > 0) {
+			appData.detectedAndReplacedTasks = true;
+		}
+		Console.traceln(Level.FINEST,
+				"retrieving significant sequence pieces:  100%");
+		Console.traceln(Level.INFO, "searching for patterns occuring most");
+
+		// search each match in every other sequence
+		for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) {
+			Match pattern = it.next();
+
+			// Skip sequences with more 0 events (scrolls) than other events.
+			// Both of the pattern sequences are equally long, so the zero
+			// counts just need to be smaller than the length of one sequence
+			if (pattern.getFirstSequence().eventCount(0)
+					+ pattern.getSecondSequence().eventCount(0) + 1 > pattern
+					.getFirstSequence().size())
+				continue;
+
+			for (int j = 0; j < appData.getNumberSequences().size(); j++) {
+				LinkedList<Integer> startpositions = appData
+						.getNumberSequences().get(j).containsPattern(pattern);
+				if (startpositions.size() > 0) {
+					for (Iterator<Integer> jt = startpositions.iterator(); jt
+							.hasNext();) {
+						int start = jt.next();
+						pattern.addOccurence(new MatchOccurence(start, start
+								+ pattern.size(), j));
+					}
+
+				}
+			}
+		}
+
+		Console.traceln(Level.INFO, "sorting results");
+		// Sort results to get the most occurring results
+		Comparator<Match> comparator = new Comparator<Match>() {
+			public int compare(Match m1, Match m2) {
+				return m2.occurenceCount() - m1.occurenceCount();
+
+			}
+		};
+		Collections.sort(matchseqs, comparator);
+		appData.getStopWatch().stop("detecting tasks");
+
+		appData.getStopWatch().start("replacing tasks");
+		HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>();
+		// Replace matches in the sessions
+		for (int i = 0; i < matchseqs.size(); i++) {
+			// Every pattern consists of 2 sequences, therefore the minimum
+			// occurrences here is 2.
+			// We just need the sequences also occurring in other sequences as
+			// well
+			if (matchseqs.get(i).occurenceCount() > 2) {
+
+				ISequence task = matchAsSequence(appData, matchseqs.get(i));
+				invalidOccurence: for (Iterator<MatchOccurence> it = matchseqs
+						.get(i).getOccurences().iterator(); it.hasNext();) {
+					MatchOccurence oc = it.next();
+
+					/*
+					 * System.out.println("Trying to replace sequence: ");
+					 * matchseqs.get(i).getFirstSequence().printSequence();
+					 * matchseqs.get(i).getSecondSequence().printSequence();
+					 * System.out.println(" in session number: " +
+					 * (oc.getSequenceId() + 1) + " at position " +
+					 * (oc.getStartindex()) + "-" + oc.getEndindex());
+					 * System.out.println();
+					 */
+
+					// System.out.println("Printing session: ");
+					// for (int j = 0; j <
+					// appData.getSessions().get(oc.getSequenceId()).size();
+					// j++) {
+					// System.out.println(j + ": "
+					// + appData.getSessions().get(oc.getSequenceId()).get(j));
+					// }
+
+					// Check if nothing has been replaced in the sequence we
+					// want to replace
+					if (replacedOccurences.get(oc.getSequenceId()) == null) {
+						replacedOccurences.put(oc.getSequenceId(),
+								new LinkedList<MatchOccurence>());
+					} else {
+						// check if we have any replaced occurence with indexes
+						// smaller than ours. If so, we need to adjust our start
+						// and endpoints
+						// of the replacement.
+						// Also do a check if we have replaced this specific
+						// MatchOccurence in this sequence already. Jump to the
+						// next occurence if this is the case.
+						// This is no more neccessary once the matches are
+						// harmonized.
+						for (Iterator<MatchOccurence> jt = replacedOccurences
+								.get(oc.getSequenceId()).iterator(); jt
+								.hasNext();) {
+							MatchOccurence tmpOC = jt.next();
+
+							if (oc.getStartindex() >= tmpOC.getStartindex()
+									&& oc.getStartindex() <= tmpOC
+											.getEndindex()) {
+								continue invalidOccurence;
+							}
+							if (oc.getEndindex() >= tmpOC.getStartindex()) {
+								continue invalidOccurence;
+
+							} else if (oc.getStartindex() > tmpOC.getEndindex()) {
+								int diff = tmpOC.getEndindex()
+										- tmpOC.getStartindex();
+								// Just to be sure.
+								if (diff > 0) {
+									oc.setStartindex(oc.getStartindex() - diff
+											+ 1);
+									oc.setEndindex(oc.getEndindex() - diff + 1);
+								} else {
+									Console.traceln(Level.WARNING,
+											"End index of a Match before start. This should never happen");
+								}
+							}
 						}
 					}
-				}
-				Console.traceln(Level.FINEST,
-						"retrieving significant sequence pieces:  100%");
-				Console.traceln(Level.INFO, "searching for patterns occuring most");
-
-				// search each match in every other sequence
-				for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) {
-					Match pattern = it.next();
-
-					// Skip sequences with more 0 events (scrolls) than other events.
-					// Both of the pattern sequences are equally long, so the zero
-					// counts just need to be smaller than the length of one sequence
-					if (pattern.getFirstSequence().eventCount(0)
-							+ pattern.getSecondSequence().eventCount(0) + 1 > pattern
-							.getFirstSequence().size())
-						continue;
-
-					for (int j = 0; j < appData.getNumberSequences().size(); j++) {
-						LinkedList<Integer> startpositions = appData
-								.getNumberSequences().get(j).containsPattern(pattern);
-						if (startpositions.size() > 0) {
-							for (Iterator<Integer> jt = startpositions.iterator(); jt
-									.hasNext();) {
-								int start = jt.next();
-								pattern.addOccurence(new MatchOccurence(start, start
-										+ pattern.size(), j));
-							}
-
-						}
-					}
-				}
-
-				Console.traceln(Level.INFO, "sorting results");
-				// Sort results to get the most occurring results
-				Comparator<Match> comparator = new Comparator<Match>() {
-					public int compare(Match m1, Match m2) {
-						return m2.occurenceCount() - m1.occurenceCount();
-
-					}
-				};
-				Collections.sort(matchseqs, comparator);
-				appData.getStopWatch().stop("detecting tasks");
-				
-				appData.getStopWatch().start("replacing tasks");
-				HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>();
-				// Replace matches in the sessions
-				for (int i = 0; i < matchseqs.size(); i++) {
-					// Every pattern consists of 2 sequences, therefore the minimum
-					// occurrences here is 2.
-					// We just need the sequences also occurring in other sequences as
-					// well
-					if (matchseqs.get(i).occurenceCount() > 2) {
-
-						ISequence task = matchAsSequence(appData, matchseqs.get(i));
-						invalidOccurence: for (Iterator<MatchOccurence> it = matchseqs
-								.get(i).getOccurences().iterator(); it.hasNext();) {
-							MatchOccurence oc = it.next();
-							/*
-							System.out.println("Trying to replace sequence: ");
-							matchseqs.get(i).getFirstSequence().printSequence();
-							matchseqs.get(i).getSecondSequence().printSequence();
-							System.out.println(" in session number: "
-									+ (oc.getSequenceId() + 1)
-									+ " at position "
-									+ (oc.getStartindex())
-									+ "-"
-									+ oc.getEndindex());
-							System.out.println();
-							*/
-							
-							// System.out.println("Printing session: ");
-							//for (int j = 0; j < sessions.get(oc.getSequenceId()).size(); j++) {
-							//	System.out.println(j + ": "
-							//			+ sessions.get(oc.getSequenceId()).get(j));
-							//}
-
-							// Check if nothing has been replaced in the sequence we
-							// want to replace
-							if (replacedOccurences.get(oc.getSequenceId()) == null) {
-								replacedOccurences.put(oc.getSequenceId(),
-										new LinkedList<MatchOccurence>());
-							} else {
-								// check if we have any replaced occurence with indexes
-								// smaller than ours. If so, we need to adjust our start
-								// and endpoints
-								// of the replacement.
-								// Also do a check if we have replaced this specific
-								// MatchOccurence in this sequence already. Jump to the
-								// next occurence if this is the case.
-								// This is no more neccessary once the matches are
-								// harmonized.
-								for (Iterator<MatchOccurence> jt = replacedOccurences
-										.get(oc.getSequenceId()).iterator(); jt
-										.hasNext();) {
-									MatchOccurence tmpOC = jt.next();
-									
-									if (oc.getStartindex() >= tmpOC.getStartindex() && oc.getStartindex()<=tmpOC.getEndindex()) {
-										continue invalidOccurence;
-									}
-									if (oc.getEndindex()>=tmpOC.getStartindex()) {
-										continue invalidOccurence;
-										
-									}
-									else if (oc.getStartindex()>tmpOC.getEndindex()) {
-										int diff = tmpOC.getEndindex()
-												- tmpOC.getStartindex();
-										// Just to be sure.
-										if (diff > 0) {
-											oc.setStartindex(oc.getStartindex() - diff+1);
-											oc.setEndindex(oc.getEndindex() - diff+1);
-										} else {
-											Console.traceln(Level.WARNING,
-													"End index of a Match before start. This should never happen");
-										}
-									}
-								}
-							}
-							ISequenceInstance sequenceInstances = RuleUtils
-									.createNewSubSequenceInRange(
-											appData.getSessions().get(oc.getSequenceId()),
-											oc.getStartindex(), oc.getEndindex(), task,
-											taskFactory, taskBuilder);
-							// Adjust the length of the match regarding to the length of
-							// instance. (OptionalInstances may be shorter)
-							oc.setEndindex(oc.getStartindex()
-									+ sequenceInstances.size()
-									- RuleUtils.missedOptionals);
-							replacedOccurences.get(oc.getSequenceId()).add(oc);
-						}
-					}
-				}
-
-				alignments = null;
+					ISequenceInstance sequenceInstances = RuleUtils
+							.createNewSubSequenceInRange(appData.getSessions()
+									.get(oc.getSequenceId()), oc
+									.getStartindex(), oc.getEndindex(), task,
+									taskFactory, taskBuilder);
+					// Adjust the length of the match regarding to the length of
+					// instance. (OptionalInstances may be shorter)
+					oc.setEndindex(oc.getStartindex()
+							+ sequenceInstances.size()
+							- RuleUtils.missedOptionals);
+					replacedOccurences.get(oc.getSequenceId()).add(oc);
+				}
+			}
+		}
+
+		alignments = null;
+		matchseqs = null;
 		appData.getStopWatch().stop("replacing tasks");
 	}
-
 
 	/**
@@ -751,5 +773,4 @@
 	}
 
-	
 	/**
      * 
@@ -758,6 +779,6 @@
 
 		private HashMap<Integer, ITask> number2task;
-		
-		private SymbolMap<ITaskInstance, ITask> uniqueTasks; 
+
+		private SymbolMap<ITaskInstance, ITask> uniqueTasks;
 
 		private ArrayList<NumberSequence> numberseqs;
@@ -804,13 +825,16 @@
 			return uniqueTasks;
 		}
-		
-		private void setUniqueTasks(SymbolMap<ITaskInstance, ITask> ut) {
-			this.uniqueTasks = ut;
-		}
-		
+
+		// private void setUniqueTasks(SymbolMap<ITaskInstance, ITask> ut) {
+		// this.uniqueTasks = ut;
+		// }
+
+		private void setNumberSequences(ArrayList<NumberSequence> numberseqs) {
+			this.numberseqs = numberseqs;
+		}
+
 		private ArrayList<NumberSequence> getNumberSequences() {
 			return numberseqs;
 		}
-
 
 		/**
