如何从HermiT推理器获取含ObjectComplementOf的否定推理结果?
如何从HermiT推理器获取包含否定(ObjectComplementOf)的推理断言?
我的尝试代码
OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); dataFactory = manager.getOWLDataFactory(); IRI iri = IRI.create("http://www.test.owl"); OWLOntology ontology = manager.createOntology(iri); OWLClass clsA = dataFactory.getOWLClass(IRI.create(iri + "#A")); OWLClass clsB = dataFactory.getOWLClass(IRI.create(iri + "#B")); OWLAxiom axiom = dataFactory.getOWLSubClassOfAxiom(clsA, clsB.getComplementNNF()); OWLIndividual john = dataFactory.getOWLNamedIndividual(IRI.create(iri + "#JOHN")); OWLClassAssertionAxiom assertionAxiom = dataFactory.getOWLClassAssertionAxiom(clsA, john); ontology.add(axiom); ontology.add(assertionAxiom); OWLReasonerFactory reasoner_factory = new ReasonerFactory(); OWLReasoner reasoner = reasoner_factory.createReasoner(ontology); OWLOntology inferred_ontology = manager.createOntology(); // Create an inferred axiom generator, and add the generators of choice. List<InferredAxiomGenerator<? extends OWLAxiom>> gens = new ArrayList<>(); gens.add(new InferredSubClassAxiomGenerator()); gens.add(new InferredClassAssertionAxiomGenerator()); gens.add(new InferredDisjointClassesAxiomGenerator()); gens.add(new InferredEquivalentClassAxiomGenerator()); // Create the inferred ontology generator, and fill the empty ontology. InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner, gens); iog.fillOntology(dataFactory, inferred_ontology);
整理后的推理结果
//KB: A SubClassOf not(B), A(JOHN) ENTAILMENTS:{ SubClassOf(A owl:Thing), SubClassOf(B owl:Thing), DisjointClasses(A owl:Nothing), DisjointClasses(B owl:Nothing), DisjointClasses(A B), ClassAssertion(owl:Thing JOHN), ClassAssertion(A JOHN) }
问题
如何获取ClassAssertion(ObjectComplementOf(B) JOHN)这一断言?
解决方案
默认的InferredClassAssertionAxiomGenerator仅生成命名类的实例断言,不会处理ObjectComplementOf(B)这类匿名否定类。要获取目标断言,可通过以下两种方式实现:
方式一:直接调用推理器API验证并手动添加
构造否定类表达式后,调用推理器的isInstanceOf方法验证个体归属,再手动将断言添加到推理后的本体中:
// 构造否定类表达式:not(B) OWLClassExpression notB = dataFactory.getOWLObjectComplementOf(clsB); // 验证JOHN是否属于not(B) boolean isJohnNotB = reasoner.isInstanceOf(john, notB); if (isJohnNotB) { // 生成并添加目标断言 OWLClassAssertionAxiom inferredAxiom = dataFactory.getOWLClassAssertionAxiom(notB, john); inferred_ontology.add(inferredAxiom); }
方式二:自定义推断公理生成器
若需批量生成所有否定类的实例断言,可自定义InferredAxiomGenerator,遍历个体和类并验证实例关系:
class InferredNegatedClassAssertionGenerator extends InferredAxiomGenerator<OWLClassAssertionAxiom> { @Override protected void addAxioms(OWLReasoner reasoner, OWLDataFactory dataFactory, Set<OWLClassAssertionAxiom> axioms) { // 遍历本体中的所有个体 for (OWLIndividual individual : reasoner.getRootOntology().getIndividualsInSignature()) { // 遍历所有命名类,生成其否定类并验证实例关系 for (OWLClass cls : reasoner.getRootOntology().getClassesInSignature()) { OWLClassExpression notCls = dataFactory.getOWLObjectComplementOf(cls); if (reasoner.isInstanceOf(individual, notCls)) { axioms.add(dataFactory.getOWLClassAssertionAxiom(notCls, individual)); } } } } @Override public String getLabel() { return "Inferred Negated Class Assertions"; } }
将自定义生成器添加到生成器列表即可:
gens.add(new InferredNegatedClassAssertionGenerator());
内容的提问来源于stack exchange,提问作者SteliosKts
相关产品推荐
相关产品推荐

