0

How to get the class of individual in owl with the reasoner

        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(KOALA));


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

        OWLDataFactory factory = manager.getOWLDataFactory();

        OWLIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));

        OWLDataPropertyExpression hasConnexion= factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasConnexion"));

        OWLDataPropertyAssertionAxiom axiom = factory.getOWLDataPropertyAssertionAxiom(hasConnexion, john, 3);

        AddAxiom addAxiom = new AddAxiom(ontology, axiom);

        manager.applyChange(addAxiom);

        manager.saveOntology(ontology, new StreamDocumentTarget(System.out));


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


        OWLClass myClass= fac.getOWLClass(IRI.create("http://www.semanticweb.org/xxxxx/ontologies/2017/10/ontology#hasConnexion"));


        NodeSet<OWLNamedIndividual> individuals = reasoner.getInstances(myClass,
                false);
         for (Node<OWLNamedIndividual> i : individuals)
         {
             System.out.println(i.getClass());
         }

I expect the result to be the class of every individual but the reasoner gives no result. In protege it's work well but when i take my ontology and try to make it with owl api, i don't got any result

supp
  • 39
  • 2
  • 6

2 Answers2

1

Yes, that will not work. i.getClass() will give you the Java class rather than the asserted types of the individual. To get the asserted types of the individuals you will need to call ontology.axioms(i).collect(Collectors.toSet()). This will only return assertions that have been added to the ontology.

To get inferred types you will need to call reasoner.types(i).collect(Collectors.toSet()).

What bothers me is that you say you got no results. I would expect that you should get lots of wrong results (i.e. Java classes rather than OWL classes). Ok, so you are creating an ontology in the code snippet. The reason why you are not getting any results is that you have not added a class assertion axiom for john. You need to add a factory.getOWLClassAssertionAxiom(owlClassExpression, john) where owlClassExpression represents a class in your ontology like Person.

Henriette Harmse
  • 4,167
  • 1
  • 13
  • 22
  • Thank you for your answer but in the begining i don't know the class of my Individuals, what i want to do is get the class of individuals according to their DataProperty with the reasoner. do you understand me ? – supp Dec 04 '17 at 19:54
  • But since you never assert the type of your individual or the domain of your data property it can only infer that `john` is of type `owl:Thing`. – Henriette Harmse Dec 05 '17 at 05:08
  • To get the types of individuals in your ontology use: `ontology.individualsInSignature().forEach(p->{ Set types = reasoner.types(p).collect(Collectors.toSet()); System.out.println("Types = " + types); });` – Henriette Harmse Dec 05 '17 at 05:11
  • You may find value in checking out the [OWL API examples](https://github.com/phillord/owl-api/blob/master/contract/src/test/java/org/coode/owlapi/examples/Examples.java) – Henriette Harmse Dec 05 '17 at 05:15
  • How you are trying to get the type based on the DataProperty is wrong. What you are doing in fact is creating a OWL class `hasConnection` rather than inferring its domain. – Henriette Harmse Dec 05 '17 at 05:25
  • 1
    There are no results because the StructuralReasoner does not do any actual reasoning, it only uses asserted axioms. – Ignazio Dec 05 '17 at 06:36
  • Thank you for OWL API examples, it really helped me to understand many things. i am new in OWL and i really want to learn it more. you looks to know it well so i have one more question please. I took the class Example and i kept only the class teenager and adult. I gave and age to John but i can't see if he is Adult or Teenager. here is my code if you can tell me where i am wrong please https://drive.google.com/file/d/1093E2ZHEVA76xIoZcE9nC6UNdgeQhD8o/view?usp=sharing – supp Dec 05 '17 at 11:57
  • I think you will be better off by creating a small ontology in Protoge rather than writing code to create your ontology. Then you can quickly test in Protege whether you in fact can determine whether `john` is an adult just to ensure that your ontology created correctly. If that works write code that only reads the ontology and does the inference that you are battling with. Also, as @Ignazio pointed out you should rather use a different reasoner. – Henriette Harmse Dec 05 '17 at 12:26
  • oh thank you ! i did it in protege and it works. but how can i get adults person for example with owl in java ? – supp Dec 05 '17 at 14:33
  • Great! Now you can use the example ontology and short bit of code to ask a new question that is to the point. – Henriette Harmse Dec 05 '17 at 14:55
  • but i am new, and it the example that you gave me there isn't a code to determinat for example if john is an adult or child, how can i do it please ? – supp Dec 05 '17 at 15:03
1

Based on OWL Api Examples, here is two examples:

Get SubClasses of OWLClass Style:

OWLOntologyManager owlManager OWLManager.createOWLOntologyManager();

IRI iri = IRI.create("http://www.semanticweb.org/music-ontology#Style");

OWLClass style = owlManager.getOWLDataFactory().getOWLClass(iri);

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

NodeSet<OWLClass> subClasses = reasoner.getSubClasses(style, true);
Set<OWLClass> clses = subClasses.getFlattened();

Log.d(TAG, "Subclasses of Style: ");

for (OWLClass cls : clses) {
    String s = cls.toString();
    Log.d(TAG, s.substring(s.indexOf("#") + 1, s.length() -1));
}

Get Individuals of OWLClass Rock:

OWLOntologyManager owlManager OWLManager.createOWLOntologyManager();

IRI iri = IRI.create("http://www.semanticweb.org/music-ontology#Rock");

OWLClass rock = owlManager.getOWLDataFactory().getOWLClass(iri);

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

NodeSet<OWLNamedIndividual> individualsNodeSet = reasoner.getInstances(rock, true);
Set<OWLNamedIndividual> individuals = individualsNodeSet.getFlattened();

Log.d(TAG, "Instances of Rock: ");

for (OWLNamedIndividual ind : individuals) {
    String s = ind.toString();
    Log.d(TAG, s.substring(s.indexOf("#") + 1, s.length() -1));
}
Lenoir Zamboni
  • 139
  • 3
  • 5