package pellet;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Set;

import org.coode.owl.functionalrenderer.OWLFunctionalSyntaxRenderer;
import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.inference.OWLReasoner;
import org.semanticweb.owl.inference.OWLReasonerException;
import org.semanticweb.owl.io.OWLRendererException;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLIndividual;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyCreationException;
import org.semanticweb.owl.model.OWLOntologyManager;

public class Test01 {
    public final static String CLS = "http://www.roche.com/ontologies/SIR/RDR/classification_safety_reports#B";
    
    private OWLOntologyManager ontologyManager;
    private OWLOntology ontology;
    private OWLReasoner reasoner;
    
    public Test01(File input) throws OWLOntologyCreationException, OWLReasonerException {
        ontologyManager = OWLManager.createOWLOntologyManager();
        ontology = ontologyManager.loadOntologyFromPhysicalURI(input.toURI());
        reasoner = new org.mindswap.pellet.owlapi.Reasoner(ontologyManager);
        reasoner.loadOntologies(Collections.singleton(ontology));
        reasoner.realise();
    }
    
    public void displayOntology() throws OWLRendererException {
        new OWLFunctionalSyntaxRenderer(ontologyManager).render(ontology, System.out);
        System.out.println("");
    }
    
    public void displayInferredIndividuals(String name) throws OWLReasonerException, URISyntaxException {
        OWLClass cls = ontologyManager.getOWLDataFactory().getOWLClass(new URI(name));
        if (!ontology.getReferencedClasses().contains(cls)) {
            System.out.println("typo!");
            System.out.println("" + cls.getURI() + " not found in ");
            for (OWLClass existing  : ontology.getReferencedClasses())  {
                System.out.println("" + existing.getURI());
            }
            return;
        }
        Set<OWLIndividual> individuals = reasoner.getIndividuals(cls, false);
        if (individuals.isEmpty()) {
            System.out.println("No inferred individuals found for class " + cls);
        }
        else {
            System.out.println("Found the following inferred individuals in class " + cls);
            for (OWLIndividual individual : individuals) {
                System.out.println("\t" + individual);
            }
        }
        System.out.println("Trying again with direct = true");
        individuals = reasoner.getIndividuals(cls, true);
        if (individuals.isEmpty()) {
            System.out.println("No inferred individuals found for class " + cls);
        }
        else {
            System.out.println("Found the following inferred individuals in class " + cls);
            for (OWLIndividual individual : individuals) {
                System.out.println("\t" + individual);
            }
        }
    }
    
    public static void run(File ontology, String cls) 
    throws OWLOntologyCreationException, OWLRendererException, OWLReasonerException, URISyntaxException {
        Test01 test = new Test01(ontology);
        System.out.println("Applying pellet to ontology:");
        // test.displayOntology();
        test.displayInferredIndividuals(cls);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            System.out.println("---- Baseline case");
            run(new File("ontologies/baseline.owl"), CLS);
            System.out.println("---- Interesting case");
            run(new File("ontologies/float.owl"), CLS);
        }
        catch (Throwable t) {
            t.printStackTrace();
        }
    }

}

