package org.aksw.simba.sparqlbench.feasible;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.aksw.simba.benchmark.Similarity;
import org.aksw.simba.benchmark.clustring.QueryClustering;
import org.aksw.simba.sparqlbench.featurevectors.FeatureVector;
import org.aksw.simba.sparqlbench.rdf.PrintBenchmark;
import org.openrdf.query.BindingSet;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.http.HTTPRepository;
import org.openrdf.rio.RDFHandlerException;

public class FeasibleExemplars {
	static Map<String, Double[]> normalizedVectors;	
	   // public static Map<String, Double[]> plainVectors= new ConcurrentHashMap<String,Double[]>();	
		static int index = 0;
	public static void main(String[] args) throws RepositoryException, MalformedQueryException, QueryEvaluationException, IOException, RDFHandlerException {
		int noQueries = 15;
		String endpointURL = "http://localhost:8890/sparql";	 //The endpoint URL containing the containment RDF data
		String queryFile = "../QALDBench-Generator/personalized-query.txt";
		String outputFile = "FEASIBLE-Exemplars-Qald15-benchmark.ttl";
	    runFeasibleExemplars(noQueries,endpointURL, queryFile, outputFile);
	}
	public static void runFeasibleExemplars(int noQueries, String endpointURL, String queryFile, String outputFile) throws RepositoryException, MalformedQueryException, QueryEvaluationException, IOException, RDFHandlerException {
		long curTime = System.currentTimeMillis();
		System.out.println("Loading queries from endpoint...");
		 normalizedVectors  = FeatureVector.getNormalizedFeaturesVectors(endpointURL,queryFile);
		//Double [] v = plainVectors.get("aea3f3e3");
		System.out.println("Total questions to be considered: "+normalizedVectors.keySet().size());
		System.out.println("Total questions to be selected: "+noQueries);
		System.out.println("Clustering method: FEASIBLE-Exemplars");
		QueryClustering qc = new QueryClustering();
		Set<String> queryIds = qc.getExemplars(normalizedVectors,noQueries); //here only get exemplars, no clustering. 
		Set<String> benchmarkQueriesIds = new HashSet<String>(queryIds); //we need to copy the query ids here/ later being used for similarity calculation. The next function will clear the queriesIds. So we have to copy before
		Double similarityScore = Similarity.getSimilarityScore(normalizedVectors,benchmarkQueriesIds);
		Double diversityScore = Similarity.getDiversityScore(normalizedVectors,benchmarkQueriesIds);
		String method = "FEASIBLE-Exemplars";
		long exeTime = (System.currentTimeMillis()-curTime)/1000;
		PrintBenchmark.getRDF(queryIds, outputFile, endpointURL,method,similarityScore,exeTime,normalizedVectors, diversityScore);
		System.out.println("Composite Error: " + similarityScore);
		System.out.println("Diversity Score: " + diversityScore);
		System.out.println("Benchmark Generation time (sec): "+exeTime);
		
	}
	
}