Index: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java
===================================================================
--- branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1620)
+++ branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1621)
@@ -16,4 +16,6 @@
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -27,4 +29,5 @@
 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.AlignmentAlgorithmFactory;
 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.Match;
+import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.MatchOccurence;
 import de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NumberSequence;
 import de.ugoe.cs.autoquest.tasktrees.alignment.matrix.PairwiseAlignmentGenerator;
@@ -154,83 +157,147 @@
 		SymbolMap<ITaskInstance, ITask> uniqueTasks = harmonizeEventTaskInstancesModel(appData);
 
-		Console.traceln(Level.INFO,"generating substitution matrix");
+		// Generate a substitution matrix between all occuring events.
+		Console.traceln(Level.INFO, "generating substitution matrix");
 		ObjectDistanceSubstitionMatrix submat = new ObjectDistanceSubstitionMatrix(
-				uniqueTasks, 6,-3);
+				uniqueTasks, 6, -3);
 		submat.generate();
 
-		Console.traceln(Level.INFO,"generating pairwise alignments");
-		ArrayList<Match> matchseqs = new ArrayList<Match>();
-		PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator.generate(numberseqs,submat,9);
-		
-		Console.traceln(Level.INFO,"retrieving significant sequence pieces");
-		for (int i=0; i< numberseqs.size();i++) {
-			Console.traceln(Level.FINEST,"retrieving significant sequence pieces:  " + Math.round((float)i/(float)numberseqs.size()*100) + "%");
-			for(int j=0; j< numberseqs.size();j++) {
-				if(i != j) {
+		// Generate pairwise alignments
+		Console.traceln(Level.INFO, "generating pairwise alignments");
+		LinkedList<Match> matchseqs = new LinkedList<Match>();
+		PairwiseAlignmentStorage alignments = PairwiseAlignmentGenerator
+				.generate(numberseqs, submat, 9);
+
+		// Retrieve all matches reached a specific threshold
+		Console.traceln(Level.INFO, "retrieving significant sequence pieces");
+		for (int i = 0; i < numberseqs.size(); i++) {
+			Console.traceln(
+					Level.FINEST,
+					"retrieving significant sequence pieces:  "
+							+ Math.round((float) i / (float) numberseqs.size()
+									* 100) + "%");
+			for (int j = 0; j < numberseqs.size(); j++) {
+				if (i != j) {
 					matchseqs.addAll(alignments.get(i, j).getMatches());
-				}   
-			}
-		}
-		Console.traceln(Level.FINEST,"retrieving significant sequence pieces:  100%");
-		
+				}
+			}
+		}
+		Console.traceln(Level.FINEST,
+				"retrieving significant sequence pieces:  100%");
 		Console.traceln(Level.INFO, "searching for patterns occuring most");
 
-		//search this match in every other sequence
-		for(int i=0; i < matchseqs.size();i++) {
+		// search each match in every other sequence
+		for (Iterator<Match> it = matchseqs.iterator(); it.hasNext();) {
 			int sequencecount = 0;
 			int totalcount = 0;
-			Match pattern = matchseqs.get(i);
+			Match pattern = it.next();
+
+			// Skip sequences with more 0 events (scrolls) than other events.
+			// Both of the pattern sequences are equally long, so the added 0
+			// 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 < numberseqs.size(); j++) {
+				LinkedList<Integer> startpositions = numberseqs.get(j)
+						.containsPattern(pattern);
+				if (startpositions.size() > 0) {
+					sequencecount++;
+					totalcount += startpositions.size();
+					for (Iterator<Integer> jt = startpositions.iterator(); jt
+							.hasNext();) {
+						pattern.addOccurence(
+								new MatchOccurence(jt.next(), j));
+					}
+
+				}
+			}
+		}
+
+		Console.traceln(Level.INFO, "sorting results");
+		// Sort results to get the most occuring results
+		Comparator<Match> comparator = new Comparator<Match>() {
+			public int compare(Match m1, Match m2) {
+				return m2.occurenceCount() - m1.occurenceCount(); // use your
+																	// logic
+			}
+		};
+		Collections.sort(matchseqs, comparator);
+		
+
+		
+		
+		// Harmonize matches: finding double matches and merge them
+		/*
+		Console.traceln(Level.INFO, "harmonizing matches");
+		int i=0;
+		
+		while(i<matchseqs.size()) {
+			int j=i;
+			while(j<matchseqs.size()) {
+				if(i!=j) {
+					if(matchseqs.get(i).equals(matchseqs.get(j))) {
+						matchseqs.get(i).addOccurencesOf(matchseqs.get(j));
+						matchseqs.remove(j);
+					
+						//System.out.println("Sequence " + i);
+						//matchseqs.get(i).getFirstSequence().printSequence();
+						//matchseqs.get(i).getSecondSequence().printSequence();
+						//System.out.println("is equal to sequence " + j);
+						//matchseqs.get(j).getFirstSequence().printSequence();
+						//matchseqs.get(j).getSecondSequence().printSequence();
+						continue;
+					}
+				}
+				j++;
+			}
+			i++;
+		}
+		Collections.sort(matchseqs, comparator);
+		*/ 
+	
+					
 			
 		
-			
-			//Skip sequences with more 0 events (scrolls) than other events. Both of the pattern sequences are equally long, so the added 0 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;
-			Console.traceln(Level.FINEST, "searching for patterns occuring most: " + Math.round((float)i/(float)matchseqs.size()*100) + "%");
-			for(int j=0; j < numberseqs.size();j++) {
-					int tmpcount = numberseqs.get(j).containsPattern(pattern);
-					if(tmpcount > 0) {
-						sequencecount++;
-						totalcount+=tmpcount;
-					}
-			}
-			
-			if(totalcount > 1) {
-				matchseqs.get(i).getFirstSequence().printSequence(); 
+		
+		
+		
+		// Just printing the results out
+		for (int i = 0; i < matchseqs.size(); i++) {
+			// Every pattern consists of 2 sequences, therefore the minimum
+			// occurences here is 2.
+			// We just need the sequences also occuring in other sequences as
+			// well
+			if (matchseqs.get(i).occurenceCount() > 2) {
 				matchseqs.get(i).getFirstSequence().printSequence();
-				System.out.println("Found pattern in " + sequencecount +"/" + numberseqs.size() + " sequences, total of " + totalcount + " occurences");
+				matchseqs.get(i).getSecondSequence().printSequence();
+				System.out.println("Found pattern "
+						+ matchseqs.get(i).occurenceCount() + " times");
 				System.out.println();
 			}
 		}
+		
+
 		alignments = null;
-		
-		//
-		//AlignmentAlgorithmFactory.setDefaultAlgorithm("de.ugoe.cs.autoquest.tasktrees.alignment.algorithms.NeedlemanWunsch");
-		//PairwiseAlignmentStorage matchAlignments = PairwiseAlignmentGenerator.generate(matchseqs, submat);
-		//UPGMAAligningTree guidetree = new UPGMAAligningTree(matchseqs,matchAlignments,submat);
-		//System.out.println(alignments.getDistanceMatrix());
-		
-		//for(Iterator<NumberSequence> it = guidetree.getRoot().getSequences().iterator();it.hasNext();) {
-		//	NumberSequence tmp = (NumberSequence) it.next();
-		//	tmp.printSequence();
-		//}
-		
-	
+
 		/*
-		do {
-
-			// appData.getStopWatch().start("whole loop");
-			// detectAndReplaceIterations(appData);
-
-			// appData.getStopWatch().start("task replacement");
-			// detectAndReplaceTasks(appData);
-			// appData.getStopWatch().stop("task replacement");
-			// appData.getStopWatch().stop("whole loop");
-
-			// appData.getStopWatch().dumpStatistics(System.out);
-			// appData.getStopWatch().reset();
-
-		} while (appData.detectedAndReplacedTasks()); */
+		 * do {
+		 * 
+		 * // appData.getStopWatch().start("whole loop"); //
+		 * detectAndReplaceIterations(appData);
+		 * 
+		 * // appData.getStopWatch().start("task replacement"); //
+		 * detectAndReplaceTasks(appData); //
+		 * appData.getStopWatch().stop("task replacement"); //
+		 * appData.getStopWatch().stop("whole loop");
+		 * 
+		 * // appData.getStopWatch().dumpStatistics(System.out); //
+		 * appData.getStopWatch().reset();
+		 * 
+		 * } while (appData.detectedAndReplacedTasks());
+		 */
 
 		Console.println("created "
