OWL-API重命名不会在推理器中删除旧的owl:Thing子类。

huangapple go评论72阅读模式
英文:

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

问题

使用OWL-API重命名类后,您没有获得预期的子类列表。我已经创建了一个小示例来演示。

本体包含2个类别:Dog和Frisbee。然后将Dog重命名为Cat。在重命名后,owl:Thing子类列表包含Dog和Cat。

以下是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 **");
			
			//不帮助(对于非缓冲推理器来说是不应该的)
			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编辑器。

英文:

After renaming a class using OWL-API I'm not getting the list of subclasses I expect. I have created a small example to demonstrate.

The ontology contains 2 classes: Dog and Frisbee. I then rename Dog to Cat. After the rename, the list of owl:Thing subclasses contains both Dog and Cat.

Here is the rename-test.owl file:

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

Here is the java test file:

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 = &quot;c:\\owl-tests\\rename-test.owl&quot;;
String oldUri = &quot;http://example.org/owl-api/rename/Dog&quot;;
String newUri = &quot;http://example.org/owl-api/rename/Cat&quot;;
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&lt;? extends OWLOntologyChange&gt; changes = renamer.changeIRI(
IRI.create(oldUri), 
IRI.create(newUri));
manager.applyChanges(changes);
System.out.println(&quot;** rename applied **&quot;);
//does not help (which it shouldn&#39;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(&quot;** dump all start **&quot;);
ontology.axioms().forEach(axiom -&gt; System.out.println(&quot;  axiom: &quot; + axiom));
System.out.println(&quot;** dump all end **&quot;);
}
static void dumpSubclasses(OWLOntology ontology, OWLReasoner reasoner) {
System.out.println(&quot;** owl:Thing subclasses **&quot;);
OWLClass thingClass = ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(
IRI.create(&quot;http://www.w3.org/2002/07/owl#Thing&quot;));
reasoner.getSubClasses(thingClass, true).entities().forEach(entity -&gt;
System.out.println(&quot;  &quot; + entity.toString()));
}
}

The output I get is as follows:

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

As you can see the Dog class is not in the list of axioms after renaming but the non-buffering reasoner thinks it is.

Do I need to tweak the code somehow? I tried using flush() on the reasoner which made no difference, which makes sense for a non-buffering reasoner. I can't find any other similar method to try. I don't want to save automatically as this is for an OWL editor that the user must manually save.

答案1

得分: 0

不要使用结构推理器。它并不适用于现实世界的使用,很可能会保留不应该保留的缓存。只需将本体中的公理写出来,本体中的公理数量足够少,可以这样做。

英文:

Do not use the Structural reasoner. It is not meant for real world use and is likely keeping caches it shouldn't. Just write out the axioms in the ontology, there are few enough in this ontology for that to work.

huangapple
  • 本文由 发表于 2020年9月6日 04:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63758234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定