gpt4 book ai didi

java - 如何更新 OWL 本体文件以添加具有某些属性的新个体?

转载 作者:行者123 更新时间:2023-11-30 08:47:52 27 4
gpt4 key购买 nike

我有一个在 protege 4.3.0 中创建并存储在 OWL 文件中的本体。我需要向这个本体中添加一些个体并更新这个文件。由于这些人彼此非常相似,我想利用 OWL API 比手动使用程序更快地添加他们。

我的目标是:

  • 添加一个新个体#individualSimpleSubType
  • 为这个个体添加类型#SimpleSubType(#SimpleSubType#SimpleType 的子类)
  • 向此个体添加以下对象属性断言:#hasProperty1 associated to #PropertyValue1
  • 向此个体添加以下对象属性断言:#hasProperty2 associated to #PropertyValue2

本体中已经存在以下信息:

  • #SimpleType 及其子类 #SimpleSubType
  • 对象属性 #hasProperty1#hasProperty2
  • 个人 #PropertyValue1#PropertyValue2

以下是我用来尝试实现该目标的代码:

OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

try {
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File("ontology.owl"));

IRI ontologyIRI = ontology.getOntologyID().getOntologyIRI();
PrefixManager prefixManager = new DefaultPrefixManager(ontologyIRI.toString().concat("#"));

OWLDataFactory dataFactory = manager.getOWLDataFactory();

OWLReasonerFactory reasonerFactory = new StructuralReasonerFactory();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontology);
reasoner.precomputeInferences();

// Get all subclasses of SimpleType stored within the loaded ontology.
OWLClassNodeSet clsSimpleSubTypes = new OWLClassNodeSet();
OWLClass simpleTypeClass = dataFactory.getOWLClass(":SimpleType", prefixManager);
clsSimpleSubTypes.addDifferentEntities(reasoner.getSubClasses(simpleTypeClass, true).getFlattened());

// Get two object properties stored within the loaded ontology.
OWLObjectPropertyExpression objProperty1 = dataFactory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasProperty1"));
OWLObjectPropertyExpression objProperty2 = dataFactory.getOWLObjectProperty(IRI.create(ontologyIRI + "#hasProperty2"));

// Get two property values stored within the loaded ontology.
OWLNamedIndividual propertyValue1 = dataFactory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#PropertyValue1"));
OWLNamedIndividual propertyValue2 = dataFactory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#PropertyValue2"));

for (OWLClass cls : clsSimpleSubTypes.getFlattened())
{
if (cls.getIRI().toString().endsWith("#SimpleSubType")) {
// Create the new individual
OWLNamedIndividual po = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#individualSimpleSubType"));

// individualSimpleSubType is of type SimpleSubType
OWLClassAssertionAxiom assertion = dataFactory.getOWLClassAssertionAxiom(cls, po);
manager.addAxiom(ontology, assertion);

// individualSimpleSubType has object property PropertyValue1
OWLObjectPropertyAssertionAxiom objProperty1Axiom = dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty1, po, propertyValue1);
manager.addAxiom(ontology, objProperty1Axiom);

// individualSimpleSubType has object property PropertyValue2
OWLObjectPropertyAssertionAxiom objProperty2Axiom = dataFactory.getOWLObjectPropertyAssertionAxiom(objProperty2, po, propertyValue2);
manager.addAxiom(ontology, objProperty2Axiom);

break;
}
}

//File destinationFile = new File("ontology-new-data.owl");
//OWLOntologyFormat format = manager.getOntologyFormat(ontology);
//manager.saveOntology(ontology, format, IRI.create(destinationFile.toURI()));
manager.saveOntology(ontology);
} catch (OWLOntologyCreationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OWLOntologyStorageException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

但是,使用上面的代码,我得到的结果与使用 Protegé 得到的结果不同。

如果我使用 Protegé 如上所述添加新个人,OWL 文件会更新并添加以下 XML 行:

<!-- http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#individualSimpleSubType -->

<owl:NamedIndividual rdf:about="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#individualSimpleSubType">
<rdf:type rdf:resource="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#SimpleSubType"/>
<hasProperty1 rdf:resource="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#PropertyValue1"/>
<hasProperty2 rdf:resource="http://www.semanticweb.org/vincenzo/ontologies/2015/7/ontology.owl#PropertyValue2"/>
</owl:NamedIndividual>

但是,如果我使用上面的Java代码,保存本体还涉及到其他行的修改:将与上面相同的XML行添加到文件中,但也修改了其他行,如下。

以下几行写在上述Java代码保存的OWL文件的开头(但我在执行相同的修改后使用Protégé保存本体时没有写这些行)。

<!DOCTYPE rdf:RDF [
<!ENTITY terms "http://purl.org/dc/terms/" >
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY skos "http://www.w3.org/2004/02/skos/core#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>

然后,在一些标签的属性中加上一定的前缀,所以:

  1. 原始OWL文件的属性rdf:resource="&rdf;List"被替换为rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#List"
  2. 原始OWL文件的属性rdf:resource="&owl;TransitiveProperty"被替换为rdf:resource="http://www.w3.org/2002/07/owl#TransitiveProperty"
  3. 原始OWL文件的属性rdf:about="&terms;description"被替换为rdf:about="http://purl.org/dc/terms/description "
  4. 原始OWL文件的属性rdf:resource="&rdfs;Literal"被替换为rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal"
  5. 原始OWL文件的属性rdf:resource="&xsd;hexBinary"被替换为rdf:resource="http://www.w3.org/2001/XMLSchema #hexBinary"
  6. 原始OWL文件的属性rdf:resource="&xsd;string"被替换为rdf:resource="http://www.w3.org/2001/XMLSchema #字符串"
  7. 原始OWL文件的属性rdf:resource="&skos;Concept"被替换为rdf:resource="http://www.w3.org/2004/02/skos/core#Concept"
  8. 原始OWL文件的属性rdf:resource="&skos;ConceptScheme"被替换为rdf:resource="http://www.w3.org/2004/02/skos/core#ConceptScheme"

这种奇怪行为的原因是什么?

在我的 Eclipse 项目中,我导入了与 OWL API 3.4.2 相关的 jar 文件,它与 Protegé 4.3.0 中集成的版本相同。

最佳答案

问题是从文件中读取原始本体中的前缀未被保留。这已在较新版本的 OWL API 中得到修复。 3.5.2和4.0.2(4.1.0还没发布)应该可以找到本体格式对象中设置的前缀。

为了保证这些前缀也用于实体(例如 &rdf; 之类的东西)你应该调用

XMLWriterPreferences.getInstance().setUseNamespaceEntities(true);

在保存本体之前。

请注意,这些不是语义差异 - 它们只是 XML 级别的句法差异。无论前缀是否缩短为实体,本体在语义上都是等价的。

关于java - 如何更新 OWL 本体文件以添加具有某些属性的新个体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32222908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com