Search code examples
owl-api

remove object property axiom using a reasoner


The situation I'm facing is the following:

individuals: A, B
object properties: R, inverseOfR
axiom A-R-B or B-inverseOfR-A

What I want to do is remove the relation between A and B regardless of how it's declared.

I was hoping that this would work:

    reasoner.equivalentObjectProperties(inverseR)
            .forEach(equivalent -> ontology.remove(factory.getOWLObjectPropertyAssertionAxiom(equivalent, b, a)));

But it doesn't seem to.

I can check explicitly for an inverse instead but it seems more like a workaround than a proper solution.

I'm using owlapi-api 5.1.0 and jfact 5.0.2. The complete test code:

import java.util.Optional;

import org.junit.Assert;
import org.junit.Test;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.OWLReasoner;

import uk.ac.manchester.cs.jfact.JFactFactory;

public class RemoveAxiomTest {

    @Test
    public void test() throws Exception {
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntology ontology = manager.createOntology();
        OWLDataFactory factory = manager.getOWLDataFactory();

        OWLClass elementClass = factory.getOWLClass("element");

        OWLObjectProperty r = factory.getOWLObjectProperty("R");
        OWLObjectProperty inverseR = factory.getOWLObjectProperty("inverseR");
        manager.addAxiom(ontology, factory.getOWLInverseObjectPropertiesAxiom(r, inverseR));

        OWLNamedIndividual a = factory.getOWLNamedIndividual("A");
        OWLNamedIndividual b = factory.getOWLNamedIndividual("B");
        manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(elementClass, a));
        manager.addAxiom(ontology, factory.getOWLClassAssertionAxiom(elementClass, b));

        manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(r, a, b));

        OWLReasoner reasoner = new JFactFactory().createReasoner(ontology);
        Assert.assertTrue(reasoner.getObjectPropertyValues(a, r).containsEntity(b));
        Assert.assertTrue(reasoner.getObjectPropertyValues(b, inverseR).containsEntity(a));

        ontology.remove(factory.getOWLObjectPropertyAssertionAxiom(inverseR, b, a));
        Optional<OWLObjectPropertyExpression> optionalInverse = reasoner.inverseObjectProperties(inverseR).findAny();
        if (optionalInverse.isPresent()) {
            ontology.remove(factory.getOWLObjectPropertyAssertionAxiom(optionalInverse.get(), a, b));
        }

        reasoner.flush();
        Assert.assertFalse(reasoner.getObjectPropertyValues(a, r).containsEntity(b));
        Assert.assertFalse(reasoner.getObjectPropertyValues(b, inverseR).containsEntity(a));
    }
}

Solution

  • I lucked out on getSimplified, which happen to solve my issue. This is the solution I was looking for:

        reasoner.equivalentObjectProperties(inverseR)
                .map(equivalent -> factory.getOWLObjectPropertyAssertionAxiom(equivalent, b, a))
                .map(axiom -> axiom.getSimplified())
                .forEach(axiom -> ontology.remove(axiom));
    

    The axiom I was trying to remove : ObjectPropertyAssertion(ObjectInverseOf(<R>) <B> <A>) did not exist, getSimplified transforms it into ObjectPropertyAssertion(<R> <A> <B>)