Search code examples
owlontologyprotegeowl-apireasoning

Get InverseObjectProperty with a reasoner


How can I query an inverse of an object property using a reasoner?

For example, a Person can have a Gun and vice versa:

<Declaration>
    <ObjectProperty IRI="#hasOwner"/>
</Declaration>
<Declaration>
    <ObjectProperty IRI="#hasGun"/>
</Declaration>
<InverseObjectProperties>
    <ObjectProperty IRI="#hasOwner"/>
    <ObjectProperty IRI="#hasGun"/>
</InverseObjectProperties>

Because there are simple axioms, I tried to come up with an uncomplicatedstar function to handle this:

OWLObjectProperty getOWLInverseRelation(OWLReasoner reasoner, OWLObjectProperty relation) {
    Set<OWLObjectPropertyExpression> inverseRelationExprs = reasoner.getInverseObjectProperties(relation).getEntities();

    if (inverseRelationExprs.size() != 2) return null;

    Iterator<OWLObjectPropertyExpression> inverseRelationExprIter = inverseRelationExprs.iterator();

    OWLObjectPropertyExpression inverseRelationExpr = inverseRelationExprIter.next();

    if (inverseRelationExpr.getNamedProperty().getIRI().getShortForm().equals(relation.getIRI().getShortForm()))
        inverseRelationExpr = inverseRelationExprIter.next();

    return inverseRelationExpr.asOWLObjectProperty();
}

Is this function a straightforward and simple way to get an inverse object property?


Solution

  • The OWLObjectPropertyExpression instances returned by reasoner.getInverseObjectProperties(relation).getEntities() are already the inverses of relation. Each one of them is inferred to be an inverse of your input - they can be equivalent to each other.