owlapi重命名不删除旧的owl:thing subclass 推理机

polkgigr  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(255)

在使用owlapi重命名一个类之后,我没有得到我期望的子类列表。我创建了一个小示例来演示。
本体包含两个类:dog和frisbee。然后我把狗改名为猫。重命名后,将显示owl:thing subclasses 包含狗和猫。
以下是rename-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类在重命名后不在 axios 列表中,但非缓冲推理器认为它在 axios 列表中。
我需要调整代码吗?我尝试在推理机上使用flush(),但没有任何区别,这对于非缓冲推理机是有意义的。我找不到其他类似的方法来尝试。我不想自动保存,因为这是一个owl编辑器,用户必须手动保存。

iyfamqjs

iyfamqjs1#

不要使用结构推理器。它不适合在现实世界中使用,而且很可能会保留不应该保存的缓存。只要写出本体论中的 axios ,这个本体论中很少有足够的 axios 可以起作用。

相关问题