OWL-API 重命名不会删除 reasoner 中的旧 owl:Thing 子类

OWL-API rename does not remove old owl:Thing subclass in reasoner

使用 OWL-API 重命名 class 后,我没有得到我期望的子 classes 列表。我创建了一个小例子来演示。

ontology 包含 2 个 classes:Dog 和 Frisbee。然后我将 Dog 重命名为 Cat。重命名后,owl:Thing subclasses 的列表同时包含 Dog 和 Cat.

这里是重命名-test.owl文件:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns="http://example.org/owl-api/rename/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
  <owl:Ontology rdf:about="http://example.org/owl-api/rename"/>
  <owl:Class rdf:about="http://example.org/owl-api/rename/Frisbee"/>
  <owl:Class rdf:about="http://example.org/owl-api/rename/Dog"/>
</rdf:RDF>

这里是 java 测试文件:

package org.example;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.FileDocumentSource;
import org.semanticweb.owlapi.io.OWLOntologyDocumentSource;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyChange;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory;
import org.semanticweb.owlapi.util.OWLEntityRenamer;

public class OwlapiRenameTest_main {
    
    public static void main(String[] args) {
        
        String owlPath = "c:\owl-tests\rename-test.owl";
        String oldUri = "http://example.org/owl-api/rename/Dog";
        String newUri = "http://example.org/owl-api/rename/Cat";
        runRenameTest(owlPath, oldUri, newUri);
    }
    
    static void runRenameTest(String owlPath, String oldUri, String newUri) {
        
        OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
        OWLOntologyDocumentSource owlFile = new FileDocumentSource(new File(owlPath));
        try {
            OWLOntology ontology = manager.loadOntologyFromOntologyDocument(owlFile);
            OWLReasoner reasoner = new StructuralReasonerFactory().createNonBufferingReasoner(ontology);
            
            dumpStmts(ontology);
            dumpSubclasses(ontology, reasoner);
            
            OWLEntityRenamer renamer = new OWLEntityRenamer(manager, Collections.singleton(ontology));
            List<? extends OWLOntologyChange> changes = renamer.changeIRI(
                    IRI.create(oldUri), 
                    IRI.create(newUri));
            manager.applyChanges(changes);
            
            System.out.println("** rename applied **");
            
            //does not help (which it shouldn't anyway for non-buffering reasoner)
            reasoner.flush();

            dumpStmts(ontology);
            dumpSubclasses(ontology, reasoner);
        } catch (OWLOntologyCreationException e) {
            e.printStackTrace();
        }
    }
    
    static void dumpStmts(OWLOntology ontology) {
        
        System.out.println("** dump all start **");
        ontology.axioms().forEach(axiom -> System.out.println("  axiom: " + axiom));
        System.out.println("** dump all end **");
    }
    
    static void dumpSubclasses(OWLOntology ontology, OWLReasoner reasoner) {
        
        System.out.println("** owl:Thing subclasses **");
        OWLClass thingClass = ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(
                IRI.create("http://www.w3.org/2002/07/owl#Thing"));
        reasoner.getSubClasses(thingClass, true).entities().forEach(entity ->
                System.out.println("  " + entity.toString()));
    }
}

我得到的输出如下:

** dump all start **
  axiom: Declaration(Class(<http://example.org/owl-api/rename/Frisbee>))
  axiom: Declaration(Class(<http://example.org/owl-api/rename/Dog>))
** dump all end **
** owl:Thing subclasses **
  <http://example.org/owl-api/rename/Frisbee>
  <http://example.org/owl-api/rename/Dog>
** rename applied **
** dump all start **
  axiom: Declaration(Class(<http://example.org/owl-api/rename/Frisbee>))
  axiom: Declaration(Class(<http://example.org/owl-api/rename/Cat>))
** dump all end **
** owl:Thing subclasses **
  <http://example.org/owl-api/rename/Frisbee>
  <http://example.org/owl-api/rename/Dog>
  <http://example.org/owl-api/rename/Cat>

如您所见,Dog class 重命名后不在公理列表中,但非缓冲推理器认为它在。

我需要以某种方式调整代码吗?我尝试在推理器上使用 flush() 没有任何区别,这对于非缓冲推理器来说是有意义的。我找不到任何其他类似的方法来尝试。我不想自动保存,因为这是用户必须手动保存的 OWL 编辑器。

不要使用结构推理器。它不适合现实世界使用,并且可能会保留它不应该的缓存。只要写出 ontology 中的公理,这个 ontology 中的公理就足够了。