jenaapache-jena

how to get datatypeProperty and objectProperty of an instance(Individual) in jena


For example, i have a jena OntModel, with many classes, individuals and properties. I want to konw how many datatypeProperty and objectProperty are set for an specific individual.

I have code below, given an Individual, ind.listProperties() will list all properties, while i dont konw how to judge if it is a datatypeProperty or objectProperty

static void statInstance(Individual ind, Node node) {
    for (StmtIterator j = ind.listProperties(); j.hasNext(); ) {
        Statement s = j.next();
        // how to judge if s is a datatypeProperty or objectProperty
    }
}

Solution

  • to judge you can use method org.apache.jena.rdf.model.RDFNode#canAs(Class)

    if (s.getPredicate().canAs(DatatypeProperty.class)) {
        //...
    } else if (s.getPredicate().canAs(ObjectProperty.class)) {
        //...
    } else {
        //...
    }
    

    the method org.apache.jena.rdf.model#listProperties will return all statements where the specified individual is a subject.

    so your approach works only for searching object/data property assertions ("a R v" and "a1 PN a2" in terms of owl2-short-guide)