package dk.aau.kah.bits.experiment;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.jena.query.Query;
import org.apache.jena.rdf.model.Resource;

import dk.aau.kah.bits.database.DatabaseHandler;

public abstract class AbstractExperimentHandler {
	protected DatabaseHandler databaseHandler;
	
	AbstractExperimentHandler(DatabaseHandler databaseHandler){
		this.databaseHandler = databaseHandler;
	}

	public abstract void run() throws IOException;
	
	public abstract HashMap<String, String> getResults();
	
	public abstract void getResultTimes();
	
	protected HashSet<String> getNamedGraphs(Query query) {
		HashSet<String> namedGraphs = new HashSet<String>(); 
		
		namedGraphs.add(databaseHandler.getOntologyModelURI());
		namedGraphs.add(databaseHandler.getFactModelURI());
		
		if (databaseHandler.getDimensionModelURI() == null) { // in case of #
			HashSet<Resource> levelProperties = databaseHandler.getDistinctLevelProperties();
			for (Resource resource : levelProperties) {
				Pattern p = Pattern.compile("(.)*"+resource.toString()+"(.)*", Pattern.DOTALL);
				Matcher m = p.matcher(query.getQueryPattern().toString());
				if (m.find()) { //if property is in query
					namedGraphs.add(resource.toString());
				}
			}
			
			Pattern lineitemPattern = Pattern.compile("(.)*http://lod2.eu/schemas/rdfh#lineitem(.)*", Pattern.DOTALL);
			Matcher lineitemMatcher = lineitemPattern.matcher(query.getQueryPattern().toString());
			Boolean lineitemMatch = lineitemMatcher.find();
			
			Pattern partsuppPattern = Pattern.compile("(.)*http://lod2.eu/schemas/rdfh#partsupp(.)*", Pattern.DOTALL);
			Matcher partsuppMatcher = partsuppPattern.matcher(query.getQueryPattern().toString());
			Boolean partsuppMatch = partsuppMatcher.find();
			
			Pattern orderPattern = Pattern.compile("(.)*http://lod2.eu/schemas/rdfh#orders(.)*", Pattern.DOTALL);
			Matcher orderMatcher = orderPattern.matcher(query.getQueryPattern().toString());
			Boolean orderMatch = orderMatcher.find();
			
			Pattern customerPattern = Pattern.compile("(.)*http://lod2.eu/schemas/rdfh#orders(.)*", Pattern.DOTALL);
			Matcher customerMatcher = customerPattern.matcher(query.getQueryPattern().toString());
			Boolean customerMatch = customerMatcher.find();
			
			if (lineitemMatch == false && partsuppMatch == true) {
				//Special case where cube is based on partsupp and not lineitem
				namedGraphs.add("http://lod2.eu/schemas/rdfh#has_partsupp");
				namedGraphs.remove(databaseHandler.getFactModelURI());
			} else if (lineitemMatch == false && orderMatch == true) {
				namedGraphs.add("http://lod2.eu/schemas/rdfh#has_order");
				namedGraphs.remove(databaseHandler.getFactModelURI());
			} else if (lineitemMatch == true && partsuppMatch == true) {
				namedGraphs.add("http://lod2.eu/schemas/rdfh#has_partsupp");
			} else if (lineitemMatch == false && customerMatch == true) {
				namedGraphs.add("http://lod2.eu/schemas/rdfh#has_customer");
				namedGraphs.remove(databaseHandler.getFactModelURI());
			}
		} else {
			namedGraphs.add(databaseHandler.getDimensionModelURI());
		}
		return namedGraphs;
	}
}

@@ -255,16 +255,61 @@
 ource;%0A%0A
+import da.aau.kah.bits.config.GeneralConfig;%0A
 import d
@@ -444,16 +444,56 @@
 andler;%0A
+%09protected GeneralConfig generalConfig;%0A
 %09%0A%09Abstr
@@ -589,16 +589,68 @@
 andler;%0A
+%09%09this.generalConfig = GeneralConfig.getInstance();%0A
 %09%7D%0A%0A%09pub
@@ -748,16 +748,35 @@
 esults()
+ throws IOException
 ;%0A%09%0A%09pub
@@ -792,29 +792,421 @@
 act 
-void getResultTimes()
+String getResult(String queryName) throws IOException;%0A%09%0A%09public abstract HashMap%3CString, Long%3E getResultTimes() throws IOException;%0A%09%0A%09public abstract Long getResultTime(String queryName) throws IOException;%0A%09%0A%09public abstract HashMap%3CString,Query%3E getQueries() throws IOException;%0A%09%0A%09public abstract String getQuery(String queryName) throws IOException;%0A%09%0A%09public abstract void printResults() throws IOException
 ;%0A%09%0A

Add more abstract methods