有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OWLAPI重命名不会删除reasoner中的旧owl:Thing子类

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

本体包含两个类:狗和飞盘。然后我把狗改名为猫。重命名后,owl:Thing子类的列表同时包含Dog和Cat

这是重命名测试。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类在重命名后不在公理列表中,但非缓冲推理器认为它是

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


共 (1) 个答案

  1. # 1 楼答案

    不要使用结构推理机。它不适合在现实世界中使用,而且可能会保留不应该使用的缓存。只需在本体中写出公理,这个本体中几乎没有足够的公理来工作