/*******************************************************************************
 * Copyright 2017 by the Department of Informatics (University of Oslo)
 * 
 *    This file is part of the Ontology Services Toolkit 
 *
 *******************************************************************************/
package uk.ac.ox.krr.logmap2.test.overlapping;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;

import uk.ac.ox.krr.logmap2.LogMap2_Matcher;
import uk.ac.ox.krr.logmap2.Parameters;
import uk.ac.ox.krr.logmap2.division.BasicDivision;
import uk.ac.ox.krr.logmap2.division.MatchingTask;
import uk.ac.ox.krr.logmap2.division.QualityMeasures;
import uk.ac.ox.krr.logmap2.io.LogOutput;
import uk.ac.ox.krr.logmap2.mappings.objects.MappingObjectStr;
import uk.ac.ox.krr.logmap2.oaei.reader.RDFAlignReader;
import uk.ac.ox.krr.logmap2.statistics.StatisticsTimeMappings;
import uk.ac.ox.krr.logmap2.utilities.Utilities;

/**
 * 
 * This class will evaluate the mappings generated by a given systems for a set of divisions 
 *
 * @author ernesto
 * Created on 18 Mar 2018
 *
 */
public class EvaluateMappingsForDivisionsOld {

	
	
	public static final int HP2MP2016=15;
	public static final int DOID2ORDO2016=16;
	
	public static final int HP2MP2017=17;
	
	
	
	
	private static Set<MappingObjectStr> loadMappingsRDF(String file_mappings) throws Exception{
		
		File file = new File(file_mappings);
		if (!file.exists()){
			System.err.println(file_mappings);
			return Collections.emptySet();
		}
			
		try{
			RDFAlignReader reader = new RDFAlignReader(file_mappings);
			
			return reader.getMappingObjects();
		}
		catch(Exception e){
			System.err.println("Error reading: " +  file_mappings);
			return new HashSet<MappingObjectStr>();
		}
		
	}
	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		String file_gs_rdf; 
		
		
		int ontopair = 0;
				
		Parameters.print_output = false;
		Parameters.print_output_always = false;
		
		LogOutput.showOutpuLog(Parameters.print_output);
		LogOutput.showOutpuLogAlways(Parameters.print_output_always);
				
		ontopair=Utilities.MOUSE2HUMAN;
		ontopair=Utilities.FMA2NCI;		
		//ontopair=Utilities.FMA2SNOMED;
		//ontopair=Utilities.SNOMED2NCI;
		
		//ontopair=HP2MP2016;
		//ontopair=DOID2ORDO2016;
		//ontopair=HP2MP2017;
		//ontopair=DOID2ORDO2017;
					
		String path = "/home/ernesto/Documents/OAEI_2016/LargeBio/Flagged/";
		
		String folder;
		
		String track;
		
		
		if (ontopair==Utilities.FMA2NCI){
		
			file_gs_rdf = path + "oaei_FMA2NCI_UMLS_mappings_with_flagged_repairs.rdf";
			
			folder = "fma2nci/";
				
			track="LargeBio";
																				
		}
		else if (ontopair==Utilities.FMA2SNOMED){
			
			folder = "fma2snomed/";
			
			file_gs_rdf = path + "oaei_FMA2SNOMED_UMLS_mappings_with_flagged_repairs.rdf";
			
			track="LargeBio";
					
		}
		else if (ontopair==Utilities.SNOMED2NCI){
			
			folder = "fma2nci/";
			
			
			file_gs_rdf = path + "oaei_SNOMED2NCI_UMLS_mappings_with_flagged_repairs.rdf";
				
			track="LargeBio";
			
		}
		else if (ontopair==Utilities.MOUSE2HUMAN){
			
			//task="MOUSE";
			
			folder = "mouse/";
			
			file_gs_rdf = "/home/ernesto/Documents/BackUp_Mar_20_2014/data/DataUMLS/UMLS_Onto_Versions/Anatomy/2012/reference2012.rdf";
			
			track="Mouse";
			
		}
		
		else if (ontopair==HP2MP2016){
			
			folder = "hp2mp/";
			
			String path2 = "/home/ernesto/Documents/OAEI_2016/Pistoia/OAEI_datasets/";
			String iri_path2 = "file:"+ path2;
			
			//silver 2
			file_gs_rdf = path2 + "Silver-hp-mp-2.rdf";
			
			track="phenotype";
			
		}
		
		else  if (ontopair==DOID2ORDO2016) {
			
			folder = "doid2ordo/";
			
			String path2 = "/home/ernesto/Documents/OAEI_2016/Pistoia/OAEI_datasets/";
			String iri_path2 = "file:"+ path2;
			
			//silver 2
			file_gs_rdf = path2 + "Silver-doid-ordo-2.rdf";
			
			track="phenotype";
			
		}
		else if (ontopair==HP2MP2017){
			
			folder = "hp2mp2017/";
			
			String path2 = "/home/ernesto/Documents/OAEI_2017/Pistoia/OAEI_datasets/";
			String iri_path2 = "file:"+ path2;
			
			track="phenotype";
			
			//silver 2
			file_gs_rdf = path2 + "selected/Silver-hp-mp-2.rdf";
			
		}
		
		else { //if (ontopair==DOID2ORDO2017) {
			
			folder = "doid2ordo2017/";
			
			String path2 = "/home/ernesto/Documents/OAEI_2017/Pistoia/OAEI_datasets/";
			String iri_path2 = "file:"+ path2;
			
			track="phenotype";
			
			//silver 2
			file_gs_rdf = path2 + "selected/Silver-doid-ordo-2.rdf";
			
		}
		
		
		
		
		

		try {
			//overlapping.createPartitionedMatchingTasks(uri1, uri2);
			
			
			
			
			
			
			//number of tasks
			//int[] num_tasks={1,2,5,10,20,50,100,200};
			//String[] tools={"AML", "LogMap"};
			//String[] tools={"LogMap"};
			//String[] tools={"AML2"};
			//String[] tools={"AML"};
			//String[] tools={"Mamba"};
			//String[] tools={"YAM-BIO"};
			//String[] tools={"LogMapLite"}
			//String[] tools={"FCA_Map"};
			String[] tools={"POMAP"};
			//String[] tools={"GMap"};
			//String[] tools={"KEPLER"};
			//int[] num_tasks={1,2,5,10,20, 50, 100, 200};
			//%int[] num_tasks={1, 2, 5, 10};
			//int[] num_tasks={20, 50};
			int[] num_tasks={50};
			//int[] num_tasks={5, 10};
			
			boolean naive = true;
			
			
			
			Set<MappingObjectStr> mappings_RA = new HashSet<MappingObjectStr>();
			mappings_RA.addAll(loadMappingsRDF(file_gs_rdf));
			
			
			Set<MappingObjectStr> mappings = new HashSet<MappingObjectStr>();
			
			for (String tool : tools){
				
				String mappings_path;
					if (naive)
						mappings_path = "/home/ernesto/Documents/OAEI_2017/EVAL_2017/MAPPINGS/"+track+"/"+tool+"_partitioning/";
					else
						mappings_path = "/home/ernesto/Documents/OAEI_2017/EVAL_2017/MAPPINGS/"+track+"/"+tool+"_partitioning_advanced/";
					
					
				for (int j=0; j<num_tasks.length; j++){
					
					
					for (int i=0; i<num_tasks[j]; i++){
						
						
						String m_file = mappings_path + folder + tool + "-" + num_tasks[j] +"-" + i +".rdf";
			
						//System.out.println(m_file);
						
						
						mappings.addAll(loadMappingsRDF(m_file));
						
						
					}
					
					StandardMeasures.computeStandardMeasures(mappings, mappings_RA);

					//System.out.println(tool +"\t"+ mappings.size() + "\t" + num_tasks[j]  + "\t" + StandardMeasures.getPrecision() + "\t" + StandardMeasures.getRecall()+ "\t" + StandardMeasures.getFscore());
					
					//System.out.println(StandardMeasures.getFscore());
					
					System.out.println(StandardMeasures.getPrecision() + "\t" + StandardMeasures.getRecall() + "\t"+ StandardMeasures.getFscore());
					
					
					mappings.clear();
					
					
						
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}

	
	
	
}
