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 1666)
+++ branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1668)
@@ -148,175 +148,15 @@
 		RuleApplicationData appData = new RuleApplicationData(sessions);
 
-		// this is the real rule application. Loop while something is replaced.
-		SymbolMap<ITaskInstance, ITask> uniqueTasks = harmonizeEventTaskInstancesModel(appData);
-
-		// Generate a substitution matrix between all occurring events.
-		Console.traceln(Level.INFO, "generating substitution matrix");
-		ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix(
-				uniqueTasks, 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.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);
-
-		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(
-									sessions.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;
+
+		harmonizeEventTaskInstancesModel(appData);
 
 		do {
 
 			appData.getStopWatch().start("whole loop"); //
-			detectAndReplaceIterations(appData);
-
+			//detectAndReplaceIterations(appData);
 			appData.getStopWatch().start("task replacement"); //
-			// detectAndReplaceTasks(appData); //
+			detectAndReplaceTasks(appData); //
 			appData.getStopWatch().stop("task replacement"); //
 			appData.getStopWatch().stop("whole loop");
-
 			appData.getStopWatch().dumpStatistics(System.out); //
 			appData.getStopWatch().reset();
@@ -353,12 +193,13 @@
 	 * @return Returns the unique tasks symbol map
 	 */
-	private SymbolMap<ITaskInstance, ITask> harmonizeEventTaskInstancesModel(
+	private void harmonizeEventTaskInstancesModel(
 			RuleApplicationData appData) {
 		Console.traceln(Level.INFO,
 				"harmonizing task model of event task instances");
 		appData.getStopWatch().start("harmonizing event tasks");
-
-		SymbolMap<ITaskInstance, ITask> uniqueTasks = preparationTaskHandlingStrategy
+		
+		appData.uniqueTasks = preparationTaskHandlingStrategy
 				.createSymbolMap();
+		
 		TaskInstanceComparator comparator = preparationTaskHandlingStrategy
 				.getTaskComparator();
@@ -374,8 +215,8 @@
 			for (int i = 0; i < session.size(); i++) {
 				ITaskInstance taskInstance = session.get(i);
-				task = uniqueTasks.getValue(taskInstance);
+				task = appData.getUniqueTasks().getValue(taskInstance);
 
 				if (task == null) {
-					uniqueTasks.addSymbol(taskInstance, taskInstance.getTask());
+					appData.getUniqueTasks().addSymbol(taskInstance, taskInstance.getTask());
 					templist.getSequence()[i] = taskInstance.getTask().getId();
 
@@ -404,10 +245,9 @@
 		appData.getStopWatch().stop("harmonizing event tasks");
 		Console.traceln(Level.INFO, "harmonized " + unifiedTasks
-				+ " task occurrences (still " + uniqueTasks.size()
+				+ " task occurrences (still " + appData.getUniqueTasks().size()
 				+ " different tasks)");
 
 		appData.getStopWatch().dumpStatistics(System.out);
 		appData.getStopWatch().reset();
-		return uniqueTasks;
 	}
 
@@ -675,49 +515,167 @@
 		appData.getStopWatch().start("detecting tasks");
 
-		// getSequencesOccuringMostOften(appData);
-
-		appData.getStopWatch().stop("detecting tasks");
-		appData.getStopWatch().start("replacing tasks");
-
-		replaceSequencesOccurringMostOften(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.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;
 		appData.getStopWatch().stop("replacing tasks");
-
-		// Console.traceln(Level.INFO, "detected and replaced "
-		// + appData.getLastFoundTasks().size() + " tasks occuring "
-		// + appData.getLastFoundTasks().getOccurrenceCount() + " times");
-	}
-
-	/**
-	 * @param appData
-	 *            the rule application data combining all data used for applying
-	 *            this rule
-	 */
-	private void replaceSequencesOccurringMostOften(RuleApplicationData appData) {
-		appData.detectedAndReplacedTasks(false);
-
-		/*
-		 * Console.traceln(Level.FINER, "replacing tasks occurrences");
-		 * 
-		 * for (List<ITaskInstance> task : appData.getLastFoundTasks()) {
-		 * ISequence sequence = taskFactory.createNewSequence();
-		 * 
-		 * Console.traceln(Level.FINEST, "replacing " + sequence.getId() + ": "
-		 * + task);
-		 * 
-		 * List<ISequenceInstance> sequenceInstances = replaceTaskOccurrences(
-		 * task, appData.getSessions(), sequence);
-		 * 
-		 * harmonizeSequenceInstancesModel(sequence, sequenceInstances,
-		 * task.size()); appData.detectedAndReplacedTasks(appData
-		 * .detectedAndReplacedTasks() || (sequenceInstances.size() > 0));
-		 * 
-		 * if (sequenceInstances.size() < appData.getLastFoundTasks()
-		 * .getOccurrenceCount()) { Console.traceln(Level.FINE, sequence.getId()
-		 * + ": replaced task only " + sequenceInstances.size() +
-		 * " times instead of expected " + appData.getLastFoundTasks()
-		 * .getOccurrenceCount()); } }
-		 */
-	}
+	}
+
 
 	/**
@@ -791,64 +749,5 @@
 	}
 
-	/**
-	 * @param tree
-	 */
-	private List<ISequenceInstance> replaceTaskOccurrences(
-			List<ITaskInstance> task, List<IUserSession> sessions,
-			ISequence temporalTaskModel) {
-		List<ISequenceInstance> sequenceInstances = new LinkedList<ISequenceInstance>();
-
-		for (IUserSession session : sessions) {
-			int index = -1;
-
-			do {
-				index = getSubListIndex(session, task, ++index);
-
-				if (index > -1) {
-					sequenceInstances.add(RuleUtils
-							.createNewSubSequenceInRange(session, index, index
-									+ task.size() - 1, temporalTaskModel,
-									taskFactory, taskBuilder));
-				}
-			} while (index > -1);
-		}
-
-		return sequenceInstances;
-	}
-
-	/**
-	 * @param trie
-	 * @param object
-	 * @return
-	 */
-	private int getSubListIndex(ITaskInstanceList list,
-			List<ITaskInstance> subList, int startIndex) {
-		boolean matchFound;
-		int result = -1;
-
-		for (int i = startIndex; i <= list.size() - subList.size(); i++) {
-			matchFound = true;
-
-			for (int j = 0; j < subList.size(); j++) {
-				// we prepared the task instances to refer to unique tasks, if
-				// they are treated
-				// as equal. Therefore, we just compare the identity of the
-				// tasks of the task
-				// instances
-				if (list.get(i + j).getTask() != subList.get(j).getTask()) {
-					matchFound = false;
-					break;
-				}
-			}
-
-			if (matchFound) {
-				result = i;
-				break;
-			}
-		}
-
-		return result;
-	}
-
+	
 	/**
      * 
@@ -857,4 +756,6 @@
 
 		private HashMap<Integer, ITask> number2task;
+		
+		private SymbolMap<ITaskInstance, ITask> uniqueTasks; 
 
 		private ArrayList<NumberSequence> numberseqs;
@@ -898,14 +799,16 @@
 		}
 
+		private SymbolMap<ITaskInstance, ITask> getUniqueTasks() {
+			return uniqueTasks;
+		}
+		
+		private void setUniqueTasks(SymbolMap<ITaskInstance, ITask> ut) {
+			this.uniqueTasks = ut;
+		}
+		
 		private ArrayList<NumberSequence> getNumberSequences() {
 			return numberseqs;
 		}
 
-		/**
-         *
-         */
-		private void detectedAndReplacedTasks(boolean detectedAndReplacedTasks) {
-			this.detectedAndReplacedTasks = detectedAndReplacedTasks;
-		}
 
 		/**
