Search code examples
javardfowlapache-jena

How to read all object properties from Ontology in Java using Jena library?


I want to read all the Object Properties present in the OWL file. I have created that OWL file using Protege tool. I have loaded the model also but I am not able to fetch the object properties.

For Example: if I have a class in Ontology named as Car and which has several Object and Data properties linked to it, such as hasColor, hasAudioSystem,hasGps.

I want to get all the object properties linked with that particular class through Domain and Range or only through the name of the class. Please help..


Solution

  • If you want to get the list of object properties having a type declared as domain or range, one way of doing it with Jena is the following:

    public void objectPropertiesForType(Model m, final Resource type) {
        StmtIterator i = m.listStatements(new SimpleSelector() {
            @Override
            public boolean test(Statement s) {
                if (s.getPredicate().equals(RDFS.domain)
                        || s.getPredicate().equals(RDFS.range)) {
                    return (s.getObject().equals(type));
                }
                return false;
            }
        });
        while (i.hasNext()) {
            Statement s = i.next();
            System.out.println("Property: " + s.getSubject().getURI());
        }
    }