package org.aksw.simba.qaldbench.random;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.aksw.simba.benchmark.Similarity;
import org.aksw.simba.qaldbench.featurevectors.FeatureVector;
import org.aksw.simba.qaldbench.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.RepositoryException;
import org.openrdf.rio.RDFHandlerException;

public class RandomSelection {
	static public Map<String, Double[]> normalizedVectors;	
	public static void main(String[] args) throws RepositoryException, MalformedQueryException, QueryEvaluationException, IOException, RDFHandlerException {
		int noQueries = 15;  //same as number of clusters. we need to pick one query from each cluster
		String endpointURL = "http://localhost:8890/sparql";	 //The endpoint URL containing the containment RDF data
		String queryFile = "../QALDBench-Generator/personalized-query.txt";
		String outputFile = "Random-QAGen-15-benchmark.ttl";
        getRandomQueries(noQueries,endpointURL,queryFile,outputFile);

	}

	public static void getRandomQueries(int noQueries, String endpointURL, String queryFile, String outputFile) throws RepositoryException, MalformedQueryException, QueryEvaluationException, IOException, RDFHandlerException {
		long curTime = System.currentTimeMillis();
		FeatureVector.initializeRepo(endpointURL);
		 normalizedVectors  = FeatureVector.getNormalizedFeaturesVectors(endpointURL,queryFile);
			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: Random Selection");
		
		 String queryString = FeatureVector.getQuery(queryFile);
		 TupleQuery tupleQuery = FeatureVector.con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
		  TupleQueryResult result = tupleQuery.evaluate();
		  List<String> queryIds = getQueriesIds(result); 
		  Set<String> benchmarkQueriesIds = new HashSet<String>();
		  for(int i=0;i<noQueries;i++){
		  Collections.shuffle(queryIds);
		  benchmarkQueriesIds.add(queryIds.get(0));
		  }
    		Double similarityScore = Similarity.getSimilarityScore(normalizedVectors,benchmarkQueriesIds);
			Double diversityScore = Similarity.getDiversityScore(normalizedVectors,benchmarkQueriesIds);
			String method = "RandomSelection";
			long exeTime = (System.currentTimeMillis()-curTime)/1000;
			PrintBenchmark.getRDF(benchmarkQueriesIds, 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: "+exeTime);		
	}

	

	private static List<String> getQueriesIds(TupleQueryResult result) throws QueryEvaluationException, RepositoryException, MalformedQueryException {
		List<String> queryIds = new ArrayList<String>();  
		 while(result.hasNext()){
			   BindingSet bset = result.next();
			//if(!FeatureVector.isSingleTpSPOQuery(bset)){
			  queryIds.add(bset.getBinding("qId").getValue().toString().replace("http://qald-gen.aksw.org/question#", ""));
		//	  }
		  }
		return queryIds;
	}



}
