使用推理器删除对象 属性 公理
remove object property axiom using a reasoner
我现在面临的情况是这样的:
individuals: A, B
object properties: R, inverseOfR
axiom A-R-B or B-inverseOfR-A
我想做的是删除 A 和 B 之间的关系,不管它是如何声明的。
我希望这会起作用:
reasoner.equivalentObjectProperties(inverseR)
.forEach(equivalent -> ontology.remove(factory.getOWLObjectPropertyAssertionAxiom(equivalent, b, a)));
不过好像没有。
我可以显式检查反函数,但它看起来更像是一种变通方法,而不是正确的解决方案。
我正在使用 owlapi-api 5.1.0 和 jfact 5.0.2。完整测试代码:
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));
}
}
这可能是 JFact 的错误。您可以尝试使用 HermiT 1.3.8.500 或 Openllet 吗?两者都与 OWLAPI 5 兼容。
我幸运地使用了 getSimplified,它恰好解决了我的问题。这是我一直在寻找的解决方案:
reasoner.equivalentObjectProperties(inverseR)
.map(equivalent -> factory.getOWLObjectPropertyAssertionAxiom(equivalent, b, a))
.map(axiom -> axiom.getSimplified())
.forEach(axiom -> ontology.remove(axiom));
我试图删除的公理:ObjectPropertyAssertion(ObjectInverseOf(<R>) <B> <A>)
不存在,getSimplified
将其转换为 ObjectPropertyAssertion(<R> <A> <B>)
我现在面临的情况是这样的:
individuals: A, B
object properties: R, inverseOfR
axiom A-R-B or B-inverseOfR-A
我想做的是删除 A 和 B 之间的关系,不管它是如何声明的。
我希望这会起作用:
reasoner.equivalentObjectProperties(inverseR)
.forEach(equivalent -> ontology.remove(factory.getOWLObjectPropertyAssertionAxiom(equivalent, b, a)));
不过好像没有。
我可以显式检查反函数,但它看起来更像是一种变通方法,而不是正确的解决方案。
我正在使用 owlapi-api 5.1.0 和 jfact 5.0.2。完整测试代码:
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));
}
}
这可能是 JFact 的错误。您可以尝试使用 HermiT 1.3.8.500 或 Openllet 吗?两者都与 OWLAPI 5 兼容。
我幸运地使用了 getSimplified,它恰好解决了我的问题。这是我一直在寻找的解决方案:
reasoner.equivalentObjectProperties(inverseR)
.map(equivalent -> factory.getOWLObjectPropertyAssertionAxiom(equivalent, b, a))
.map(axiom -> axiom.getSimplified())
.forEach(axiom -> ontology.remove(axiom));
我试图删除的公理:ObjectPropertyAssertion(ObjectInverseOf(<R>) <B> <A>)
不存在,getSimplified
将其转换为 ObjectPropertyAssertion(<R> <A> <B>)