Inferring using Jena
Asked Answered
D

1

6
InfModel infmodel = ModelFactory.createInfModel(reasoner, m);
Resource vegetarian = infmodel.getResource(source + "Vegetarian");
Resource margherita = infmodel.getResource(source + "Example-Margherita");
if (infmodel.contains(margherita, RDF., vegetarian)) {
        System.out.println("Margherita is a memberOf Vegetarian pizza");
    }

The example given above is formed by formal pizza.owl. In this owl, Example-Margherita is an individual of Margherita class. So, it is already written in owl file. However, the problem is that the reasoner should infer that margherita-example should be also an vegetarian pizza. Could anyone please give an example that shows how to find an individual's possible inferred classes like in Protege ?(Protege correctly infers that Example-Margherita is a Vegetarian Pizza. However, I can't infer programmatically)

Distributary answered 11/6, 2010 at 15:56 Comment(2)
It would be helpful if you included a pointer to the pizza.owl file (I suppose it's public somewhere) and if you also provided the code you used to set up the reasoner variable.Truckload
Thank you very much cygri for your interest. I solved my problem and provided an example, below.Distributary
D
10

I solved my question. I think there was a problem with my ontology. Therefore, I created another ontology to infer individuals. The ontology that I created contains Person and subclasses of Person : MalePerson, FemalePerson and MarriedPerson. And, there are two object properties(hasSpouse, hasSibling) and one data type property(hasAge). And, I created 3 individuals. John - MalePerson - hasAge(20) - hasSibling(Jane) Jane - FemalePerson - hasSibling(John) - hasSpouse(Bob) Bob - MalePerson - hasSpouse(Jane)

And, I put two restrictions for MalePerson and FemalePerson classes. For MalePerson : hasSpouse max 1 hasSpouse only MalePerson For FemalePerson : hasSpouse max 1 hasSpouse only FemalePerson

Lastly, I made MarriedPerson to be a defined class. Before reasoning, MarriedPerson has no individual. However, the model should infer that Jane and Bob are married. Therefore, at the end, MarriedPerson class should have 2 individuals.

When I ran this code in Java using Jena, I got 2 inferred individuals.

OntModel ontModel = ModelFactory.createOntologyModel();
    InputStream in = FileManager.get().open(inputFileName);
    if (in == null) {
        throw new IllegalArgumentException( "File: " + inputFileName + " not found");
    }
    ontModel.read(in, "");


    Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
    reasoner = reasoner.bindSchema(ontModel);
    // Obtain standard OWL-DL spec and attach the Pellet reasoner
    OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
    ontModelSpec.setReasoner(reasoner);
    // Create ontology model with reasoner support
    OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);

    // MarriedPerson has no asserted instances
    // However, if an inference engine is used, two of the three
    // individuals in the example presented here will be
    // recognized as MarriedPersons
            //ns is the uri
    OntClass marPerson = model.getOntClass(ns + "OWLClass_00000003866036241880"); // this is the uri for MarriedPerson class
    ExtendedIterator married = marPerson.listInstances();
    while(married.hasNext()) {
        OntResource mp = (OntResource)married.next();
        System.out.println(mp.getURI());
    } // this code returns 2 individuals with the help of reasoner
Distributary answered 12/6, 2010 at 12:41 Comment(2)
Thanks Mikae! Very helpful! It would be great if you could include your OWL & Turtle files too :-)Prayer
does anyone know how to extend this with own rules loaded from a file?Preordain

© 2022 - 2024 — McMap. All rights reserved.