Antman
Antman

Reputation: 493

How to read object properties from ontology using Jena Java API

I have opened my ontology so far and now I want to read all the objects and display their properties:

I have the next code:

// Opening the ontology.
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model.read("file:C:/Users/Antonio/Desktop/myOntology.owl","OWL");


// Going through the ontology
for (Iterator<OntClass> i = model.listClasses();i.hasNext();){
    OntClass cls = i.next();
    System.out.print(cls.getLocalName()+": ");

    // here I want to show the properties
}

which just shows the name of the classes, but not their properties. I have been reading the documentation but I don't find anything useful.

Hopefully someone can help me.

Thanks in advance.

Upvotes: 0

Views: 742

Answers (2)

apoorv
apoorv

Reputation: 13

The code is printing classes because listClasses() returns classes of the ontology. For printing the object properties of the individuals, you can use OWL API

Upvotes: 0

Ozerioss Smith
Ozerioss Smith

Reputation: 90

I'm not sure why you would want all the properties but you can do that easily. First of all make sure to import Jena's OntProperty import org.apache.jena.ontology.OntProperty;

Then you can simply inside your for loop : cls.listDeclaredProperties().toList()

If you want to access the content of a specific property though you could do it this way : Check your .owl file for the URI which generally looks something like this "http://example.com/ontology#"

So your Java code is going to look like this : OntProperty nameOfProperty = model.getOntProperty("http://example.com/ontology#nameOfyourProperty");

Then inside your loop you could do for example something like this : cls.getProperty(nameOfProperty).getString()

And by the way before reading your file you might want to put it in a try catch statement. Hope that helped.

Upvotes: 1

Related Questions