Owl inference : How to get the class of Individuals after loading ontology

246 Views Asked by At

I have an ontology witch is created in protege, insid it :

I have 2 classes ( teenager and adult ).

I have the individual John with a dataProperty hasAge.

In protege i get the class of john according to his age. ( so my ontology work well)

Now i have loaded my ontology in java and i try to get all the individuals that are in the class adult ( Like John in protege ). so i did

        //manager
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

        //IRI
        String x = "file:/D:/Studies/tpOwl.owl";
        IRI ontologyIRI = IRI.create(x);

        //ontology
        OWLOntology ont = manager.createOntology(ontologyIRI);

        //factory
        OWLDataFactory factory = manager.getOWLDataFactory();


        OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
        OWLReasoner reasoner = reasonerFactory.createReasoner(ont);

        OWLClass adult = factory.getOWLClass(IRI.create(ontologyIRI + "#Adult"));
        NodeSet<OWLNamedIndividual> instancess = reasoner.getInstances(adult, true);
        for (Node<OWLNamedIndividual> i : instancess)
        {
         System.out.println(""+i);
        }

but i got nothing.

So how can i get the individuals of a specific class after loading my ontology in java ?

1

There are 1 best solutions below

4
On

There are a couple of errors in your code:

  1. You need 2 IRIs: One for loading your ontology which is prepended with file:, the other the IRI used in your ontology for uniquely identifying constructs in your ontology.
  2. You have to load your ontology, not create it.
  3. You have to use a different reasoner. So instead of using StructuralReasonerFactory use say Hermit. See Hermit. JFact also does not work.

Here are the working code using Hermit:

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

        //IRI
        Path path = Paths.get(".").toAbsolutePath().normalize();
        IRI loadDocumentIRI = IRI.create("file:/D:/Studies/tpOwl.owl");

        IRI ontologyIRI = IRI.create("http://www.semanticweb.org/akkou/ontologies/2017/10/tp-ontology");


        //ontology
        OWLOntology ont = manager.loadOntologyFromOntologyDocument(loadDocumentIRI);

        //factory
        OWLDataFactory factory = manager.getOWLDataFactory();


        OWLReasonerFactory reasonerFactory = new ReasonerFactory();
        OWLReasoner reasoner = reasonerFactory.createReasoner(ont);


        OWLClass adult = factory.getOWLClass(IRI.create(ontologyIRI + "#Adult"));
        NodeSet<OWLNamedIndividual> instancess = reasoner.getInstances(adult, true);
        for (Node<OWLNamedIndividual> i : instancess)  {
         System.out.println(""+i);
        }