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 1728)
+++ /branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/SequenceForTaskDetectionRuleAlignment.java	(revision 1729)
@@ -79,8 +79,7 @@
  */
 public class SequenceForTaskDetectionRuleAlignment implements ISessionScopeRule {
-	
-	public static int nThreads = Runtime.getRuntime().availableProcessors()-1;
-
-	
+
+	public static int nThreads = Runtime.getRuntime().availableProcessors() - 1;
+
 	private int iteration = 0;
 	/**
@@ -98,5 +97,4 @@
 	 */
 	private ITaskBuilder taskBuilder;
-	
 
 	/**
@@ -161,9 +159,9 @@
 		}
 	}
-	
+
 	public RuleApplicationData loadAppData(String name) {
 		String objectName = name;
 		String filename = name + ".dat";
-	
+
 		Object data = null;
 		FileInputStream fis = null;
@@ -182,7 +180,8 @@
 			CommandHelpers.dataOverwritten(objectName);
 		}
-		return (RuleApplicationData) GlobalDataContainer.getInstance().getData(name);
-	}
-	
+		return (RuleApplicationData) GlobalDataContainer.getInstance().getData(
+				name);
+	}
+
 	/*
 	 * (non-Javadoc)
@@ -195,34 +194,34 @@
 	public RuleApplicationResult apply(List<IUserSession> sessions) {
 		RuleApplicationData appData = new RuleApplicationData(sessions);
-		//File harmonized = new File("harmonized.dat");
-		//if(harmonized.exists() && !harmonized.isDirectory()) { 
-		//	Console.traceln(Level.INFO,"loading harmonized sessions from file");
-		//	appData = loadAppData("harmonized");
-		//}
-		//else {
-			//appData.getStopWatch().start("harmonization");
-			harmonizeEventTaskInstancesModel(appData);
-			//appData.getStopWatch().stop("harmonization");
-			GlobalDataContainer.getInstance().addData("harmonized", appData);
-			//Saving intermediate results to file
-			Console.traceln(Level.INFO,"saving substitution matrix to file");
-			//saveAppData("harmonized");
-		//}
-		
-		//File substitution = new File("substitution.dat");
-		//if(!(substitution.exists() && !substitution.isDirectory())) { 
-			Console.traceln(Level.INFO, "generating substitution matrix from " + appData.getUniqueTasks().size() + " unique tasks");
-			appData.getStopWatch().start("substitution matrix");
-			appData.getSubmat().generate(appData.getUniqueTasks());
-			appData.getStopWatch().stop("substitution matrix");
-		//	GlobalDataContainer.getInstance().addData("substitution", appData);
-		//	saveAppData("substitution");
-		//}
-		//else {
-		//	Console.traceln(Level.INFO,"loading substitution matrix from file");
-		//	appData = loadAppData("substitution");
-		//}
-		
-		
+		// File harmonized = new File("harmonized.dat");
+		// if(harmonized.exists() && !harmonized.isDirectory()) {
+		// Console.traceln(Level.INFO,"loading harmonized sessions from file");
+		// appData = loadAppData("harmonized");
+		// }
+		// else {
+		// appData.getStopWatch().start("harmonization");
+		harmonizeEventTaskInstancesModel(appData);
+		// appData.getStopWatch().stop("harmonization");
+		GlobalDataContainer.getInstance().addData("harmonized", appData);
+		// Saving intermediate results to file
+		Console.traceln(Level.INFO, "saving substitution matrix to file");
+		// saveAppData("harmonized");
+		// }
+
+		// File substitution = new File("substitution.dat");
+		// if(!(substitution.exists() && !substitution.isDirectory())) {
+		Console.traceln(Level.INFO, "generating substitution matrix from "
+				+ appData.getUniqueTasks().size() + " unique tasks");
+		appData.getStopWatch().start("substitution matrix");
+		appData.getSubmat().generate(appData.getUniqueTasks());
+		appData.getStopWatch().stop("substitution matrix");
+		// GlobalDataContainer.getInstance().addData("substitution", appData);
+		// saveAppData("substitution");
+		// }
+		// else {
+		// Console.traceln(Level.INFO,"loading substitution matrix from file");
+		// appData = loadAppData("substitution");
+		// }
+
 		Console.traceln(Level.INFO, "Starting main loop");
 		do {
@@ -256,5 +255,4 @@
 		return appData.getResult();
 	}
-
 
 	private ArrayList<NumberSequence> createNumberSequences(
@@ -531,5 +529,5 @@
 	 * @return
 	 */
-	public ISequence matchAsSequence(RuleApplicationData appData, Match m) {	
+	public ISequence matchAsSequence(RuleApplicationData appData, Match m) {
 		ISequence sequence = taskFactory.createNewSequence();
 		appData.newTaskCreated(sequence);
@@ -598,5 +596,5 @@
 					taskBuilder.addChild(selection, subsequence1);
 					taskBuilder.addChild(selection, subsequence2);
-					taskBuilder.addChild(sequence,selection);
+					taskBuilder.addChild(sequence, selection);
 					while (i < first.length - 1 && selectionfound) {
 						selectionfound = false;
@@ -608,11 +606,10 @@
 								&& second[i + 1] != -1) {
 							selectionfound = true;
-						}
-						else{
+						} else {
 							continue;
 						}
 						i++;
 					}
-					if(i==first.length-1 && selectionfound) {
+					if (i == first.length - 1 && selectionfound) {
 						taskBuilder.addChild(subsequence1, appData
 								.getNumber2Task().get(first[i]));
@@ -621,6 +618,5 @@
 					}
 				}
-			}
-			else {
+			} else {
 				if ((first[i] != second[i])) {
 
@@ -651,14 +647,15 @@
 		// Create NumberSequences
 		appData.setNumberSequences(this.createNumberSequences(appData));
-		
+
 		// Generate pairwise alignments
-		//appData.setMatchseqs(generatePairwiseAlignments(appData));
+		// appData.setMatchseqs(generatePairwiseAlignments(appData));
 		generatePairwiseAlignments(appData);
 
-		//Searching each match in all other sessions, counting its occurences
+		// Searching each match in all other sessions, counting its occurences
 		searchMatchesInAllSessions(appData);
-		
+
 		// Sort results to get the most occurring results
-		Console.traceln(Level.INFO, "sorting " + appData.getMatchseqs().size() + " results");
+		Console.traceln(Level.INFO, "sorting " + appData.getMatchseqs().size()
+				+ " results");
 		Comparator<Match> comparator = new Comparator<Match>() {
 			public int compare(Match m1, Match m2) {
@@ -667,5 +664,5 @@
 			}
 		};
-		
+
 		Collections.sort(appData.getMatchseqs(), comparator);
 		appData.getStopWatch().stop("detecting tasks");
@@ -676,5 +673,5 @@
 		HashMap<Integer, List<MatchOccurence>> replacedOccurences = new HashMap<Integer, List<MatchOccurence>>();
 		for (int i = 0; i < appData.getMatchseqs().size(); i++) {
-		
+
 			// Every pattern consists of 2 sequences, therefore the minimum
 			// occurrences here is 2.
@@ -683,9 +680,11 @@
 			if (appData.getMatchseqs().get(i).occurenceCount() > 2) {
 				appData.detectedAndReplacedTasks = true;
-				ISequence task = matchAsSequence(appData, appData.getMatchseqs().get(i));
-				invalidOccurence: for (Iterator<MatchOccurence> it = appData.getMatchseqs()
-						.get(i).getOccurences().iterator(); it.hasNext();) {
+				ISequence task = matchAsSequence(appData, appData
+						.getMatchseqs().get(i));
+				invalidOccurence: for (Iterator<MatchOccurence> it = appData
+						.getMatchseqs().get(i).getOccurences().iterator(); it
+						.hasNext();) {
 					MatchOccurence oc = it.next();
-					
+
 					// Check if nothing has been replaced in the sequence we
 					// want to replace
@@ -731,5 +730,6 @@
 						}
 					}
-					System.out.println("Replacing in sequence" + oc.getSequenceId());
+					System.out.println("Replacing in sequence"
+							+ oc.getSequenceId());
 					ISequenceInstance sequenceInstances = RuleUtils
 							.createNewSubSequenceInRange(appData.getSessions()
@@ -746,30 +746,35 @@
 			}
 		}
-		//appData.setMatchseqs(null);
+		// appData.setMatchseqs(null);
 		appData.getStopWatch().stop("replacing tasks");
 	}
-	
-	
-	
+
+	private void replaceMatches() {
+
+	}
 
 	private void searchMatchesInAllSessions(RuleApplicationData appData) {
-		Console.traceln(Level.INFO, "searching for patterns occuring most with " + nThreads + " threads");
-		//Prepare parallel search of matchseqs
+		Console.traceln(Level.INFO,
+				"searching for patterns occuring most with " + nThreads
+						+ " threads");
+		// Prepare parallel search of matchseqs
 		ExecutorService executor = Executors.newFixedThreadPool(nThreads);
-		int matchSeqSize=appData.getMatchseqs().size();
-		int interval = Math.round(matchSeqSize/nThreads);
+		int matchSeqSize = appData.getMatchseqs().size();
+		int interval = matchSeqSize / nThreads;
 		int rest = matchSeqSize % nThreads;
-		
-		for(int i =0;i<matchSeqSize-interval;i+=interval) {
-			int offset =0;
-			if(rest!=0) {
-				offset=1;
+
+		for (int i = 0; i < matchSeqSize - interval; i += interval) {
+			int offset = 0;
+			if (rest != 0) {
+				offset = 1;
 				rest--;
 			}
 			int from = i;
-			int to = i+interval+offset;
-			System.out.println("Creating thread with matches from " + from + " to " + to);
+			int to = i + interval + offset;
+			System.out.println("Creating thread with matches from " + from
+					+ " to " + to);
 			// search each match in every other sequence
-			ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder(appData,from,to);
+			ParallelMatchOcurrencesFinder finder = new ParallelMatchOcurrencesFinder(
+					appData, from, to);
 			executor.execute(finder);
 		}
@@ -781,144 +786,153 @@
 			e.printStackTrace();
 		}
-		
-	}
-
-	//Print out the progress
-	 private static void printProgressPercentage(int count, int size) {
-		if(size>100) {
-			if((count%(size/100)==0)) {	
-				//Console.traceln(Level.INFO,("Thread" + Thread.currentThread().getName() + ": " + Math.round((float) count/size*100))+ "%");
-				System.out.println("Thread" + Thread.currentThread().getName() + ": " + Math.round((float) count/size*100)+ "%");
-			}
-		}
-		else {
-			//Console.traceln(Level.INFO,("Thread" + Thread.currentThread().getName() + ": " +Math.round((float) count/size*100))+ "%");
-			System.out.println("Thread" + Thread.currentThread().getName() + ": " +Math.round((float) count/size*100)+ "%");
-			
-		}
-	}
-	
-	 
-	 
-	
+
+	}
+
+	// Print out the progress
+	private static void printProgressPercentage(int count, int size) {
+		if (size > 100) {
+			if ((count % (size / 100) == 0)) {
+				// Console.traceln(Level.INFO,("Thread" +
+				// Thread.currentThread().getName() + ": " + Math.round((float)
+				// count/size*100))+ "%");
+				System.out.println("Thread" + Thread.currentThread().getName()
+						+ ": " + Math.round((float) count / size * 100) + "%");
+			}
+		} else {
+			// Console.traceln(Level.INFO,("Thread" +
+			// Thread.currentThread().getName() + ": " +Math.round((float)
+			// count/size*100))+ "%");
+			System.out.println("Thread" + Thread.currentThread().getName()
+					+ ": " + Math.round((float) count / size * 100) + "%");
+
+		}
+	}
+
+	private class ParallelMatchReplacer implements Runnable {
+
+		@Override
+		public void run() {
+
+		}
+
+	}
+
 	private class ParallelMatchOcurrencesFinder implements Runnable {
-        private final RuleApplicationData appData;
-        private final int from;
-        private final int to;
-            ParallelMatchOcurrencesFinder(RuleApplicationData appData, int from, int to) {
-            this.appData = appData;
-            this.from = from;
-            this.to = to;
-        }
-        
-        @Override
-        public void run() {
-        	int count = 0;
-    		int size=to-from;
-    		
-    		for (int i=from; i<to;i++) { 
-    			Match pattern = appData.getMatchseqs().get(i);
-    			count++;
-    			printProgressPercentage(count,size);
-    			// 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));
-    					}
-    				}
-    			}
-    		}  
-        }
-	}
-	
-	
+		private final RuleApplicationData appData;
+		private final int from;
+		private final int to;
+
+		ParallelMatchOcurrencesFinder(RuleApplicationData appData, int from,
+				int to) {
+			this.appData = appData;
+			this.from = from;
+			this.to = to;
+		}
+
+		@Override
+		public void run() {
+			int count = 0;
+			int size = to - from;
+
+			for (int i = from; i < to; i++) {
+				Match pattern = appData.getMatchseqs().get(i);
+				count++;
+				printProgressPercentage(count, size);
+				// 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));
+						}
+					}
+				}
+			}
+		}
+	}
+
 	private class ParallelPairwiseAligner implements Runnable {
-        private final RuleApplicationData appData;
-        private final int from;
-        private final int to;
-            ParallelPairwiseAligner(RuleApplicationData appData, int from, int to) {
-            this.appData = appData;
-            this.from = from;
-            this.to = to;
-        }
-        
-        @Override
-        public void run() {
-        	int count = 0;
-    		int size=to-from;
-    		
-    		for (int i=from; i<to;i++) {
-    			NumberSequence ns1 = appData.getNumberSequences().get(i);
+		private final RuleApplicationData appData;
+		private final int from;
+		private final int to;
+
+		ParallelPairwiseAligner(RuleApplicationData appData, int from, int to) {
+			this.appData = appData;
+			this.from = from;
+			this.to = to;
+		}
+
+		@Override
+		public void run() {
+			int count = 0;
+			int size = to - from;
+
+			for (int i = from; i < to; i++) {
+				NumberSequence ns1 = appData.getNumberSequences().get(i);
 				count++;
-				printProgressPercentage(count,size);
+				printProgressPercentage(count, size);
 				for (int j = 0; j < appData.getNumberSequences().size(); j++) {
 					NumberSequence ns2 = appData.getNumberSequences().get(j);
 					if (i != j) {
-						AlignmentAlgorithm aa = AlignmentAlgorithmFactory.create();
-						aa.align(ns1, ns2, appData.getSubmat(),9);
-						synchronized(appData.getMatchseqs()) {
+						AlignmentAlgorithm aa = AlignmentAlgorithmFactory
+								.create();
+						aa.align(ns1, ns2, appData.getSubmat(), 9);
+						synchronized (appData.getMatchseqs()) {
 							appData.getMatchseqs().addAll(aa.getMatches());
 						}
 					}
 				}
-    		}  
-    	}
-	}
-
-	//private LinkedList<Match> generatePairwiseAlignments(RuleApplicationData appData) {
+			}
+		}
+	}
+
+	// private LinkedList<Match> generatePairwiseAlignments(RuleApplicationData
+	// appData) {
 	private void generatePairwiseAlignments(RuleApplicationData appData) {
 		int numberSeqSize = appData.getNumberSequences().size();
 		appData.matchseqs = new LinkedList<Match>();
-		//Checking if i have an already calculated file result of this action in the working directory 
-		//File aligned = new File("aligned" + iteration + ".dat");
-		//if(!(aligned.exists() && !aligned.isDirectory())) {
-			Console.traceln(Level.INFO, "generating pairwise alignments from " + numberSeqSize + " sessions with " + nThreads + " threads");
-			ExecutorService executor = Executors.newFixedThreadPool(nThreads);
-			int interval = Math.round(numberSeqSize/nThreads);
-			int rest = numberSeqSize % nThreads;
-			
-			for (int i = 0; i < numberSeqSize-interval; i+=interval) {
-				int offset = 0;
-				if(rest!=0) {
-					offset=1;
-					rest--;
-				}
-				int from = i;
-				int to = i+interval+offset;
-				System.out.println("Creating thread for sessions " + from + " till " + to);
-				ParallelPairwiseAligner aligner = new ParallelPairwiseAligner(appData,from,to);
-				executor.execute(aligner);
-			}
-			executor.shutdown();
-			try {
-				executor.awaitTermination(10, TimeUnit.MINUTES);
-			} catch (InterruptedException e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
-			}
-	
-			//GlobalDataContainer.getInstance().addData("aligned" + iteration, appData);
-			//saveAppData("aligned" + iteration);
-		//	return result;
-		}
-		//else {
-		//	Console.traceln(Level.INFO,"loading matches from file");
-		//	appData = loadAppData("aligned"+iteration);
-		//	return appData.getMatchseqs();
-		//}
-	
+		Console.traceln(Level.INFO, "generating pairwise alignments from "
+				+ numberSeqSize + " sessions with " + nThreads + " threads");
+		ExecutorService executor = Executors.newFixedThreadPool(nThreads);
+		int interval = numberSeqSize / nThreads;
+		int rest = numberSeqSize % nThreads;
+
+		for (int i = 0; i < numberSeqSize - interval; i += interval) {
+			int offset = 0;
+			if (rest != 0) {
+				offset = 1;
+				rest--;
+			}
+			int from = i;
+			int to = i + interval + offset;
+			System.out.println("Creating thread for sessions " + from
+					+ " till " + to);
+			ParallelPairwiseAligner aligner = new ParallelPairwiseAligner(
+					appData, from, to);
+			executor.execute(aligner);
+		}
+		executor.shutdown();
+		try {
+			executor.awaitTermination(2, TimeUnit.HOURS);
+		} catch (InterruptedException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+	}
 
 	/**
@@ -934,15 +948,18 @@
 		private HashMap<Integer, ITask> number2task;
 
-		// TODO: We Actually just need number2task here, this structure can be removed in the future.
+		// TODO: We Actually just need number2task here, this structure can be
+		// removed in the future.
 		private HashSet<ITask> uniqueTasks;
+
+		ObjectDistanceSubstitionMatrix submat;
 		
-		ObjectDistanceSubstitionMatrix submat; 
 		
+
 		LinkedList<Match> matchseqs;
-		
+
 		private ArrayList<NumberSequence> numberseqs;
 
 		private LinkedList<ITask> newTasks;
-		
+
 		/**
          * 
@@ -975,5 +992,5 @@
 			stopWatch = new StopWatch();
 			result = new RuleApplicationResult();
-			submat= new ObjectDistanceSubstitionMatrix(6,-3,false);
+			submat = new ObjectDistanceSubstitionMatrix(6, -3, false);
 			newTasks = new LinkedList<ITask>();
 			this.detectedAndReplacedTasks = true;
@@ -984,14 +1001,13 @@
 		}
 
-
 		private ObjectDistanceSubstitionMatrix getSubmat() {
 			return submat;
 		}
-		
+
 		private void resetNewlyCreatedTasks() {
 			uniqueTasks.addAll(newTasks);
 			newTasks.clear();
 		}
-		
+
 		private void newTaskCreated(ITask task) {
 			number2task.put(task.getId(), task);
@@ -1023,6 +1039,4 @@
 		}
 
-		
-		
 		/**
          *
@@ -1042,5 +1056,5 @@
 			return newTasks;
 		}
-		
+
 		/**
 		 * @return the stopWatch
