gpt4 book ai didi

java - 如何检索 OWLapi 中专用类的子类?

转载 作者:太空宇宙 更新时间:2023-11-04 07:14:34 27 4
gpt4 key购买 nike

我正在尝试编写一个加载输入类的子类的方法。此代码在 OWL API 3.1.x(Pizza.owl) 编写的 RDF 文件中运行良好,但现在不适用于 OWL API 3.4.x 版本生成的文件。

下面是我编写的代码:

public ArrayList<String> getSubClassOf(String parentClass)
{
String seq = "";
tempList.clear();
tempClass = factory.getOWLClass(":" + parentClass, pm);
s = reasoner.getSubClasses(tempClass, false);
i = s.iterator();
while(i.hasNext()){
seq = i.next().toString();
if(!seq.contains("Nothing"))
tempList.add(seq.substring(seq.indexOf("#")+1,seq.indexOf(">")));
}
return tempList;
}

这是 OWL API 3.4.2 生成的 owl 文件:

<!DOCTYPE Ontology [
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY xml "http://www.w3.org/XML/1998/namespace" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>


<Ontology xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://localhost/CA/SmartHome/SmartHome_1113"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
ontologyIRI="http://localhost/CA/SmartHome/SmartHome_1113">
<Prefix name="" IRI="http://localhost/CA/SmartHome/SmartHome_1113.owl#"/>
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
<Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
<Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
<Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
<Declaration>
<Class IRI="#Adult"/>
</Declaration>

<Declaration>
<Class IRI="#Person"/>
</Declaration>

<SubClassOf>
<Class IRI="#Adult"/>
<Class IRI="#Person"/>
</SubClassOf>

</Ontology>

这是一个非常简单的本体,由 2 个类组成,Adult 是 Person 的子类

最佳答案

我尝试复制您的问题,但无法使用您的代码,因为代码片段中的某些变量未定义。我认为问题出在您使用的前缀管理器或类名上。以下代码片段提供了 Adult 结果:

public static void main(String[] args) throws Exception {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// load the importing ontology
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new StringDocumentSource(test_owl));
OWLReasoner r = new FaCTPlusPlusReasonerFactory().createReasoner(ontology);
OWLClass person = ontology.getOWLOntologyManager().getOWLDataFactory().getOWLClass(IRI.create("http://localhost/CA/SmartHome/SmartHome_1113#Person"));
Set<OWLClass> classes = r.getSubClasses(person, false).getFlattened();
System.out.println(classes);
}

输出为:

[<http://localhost/CA/SmartHome/SmartHome_1113#Adult>, owl:Nothing]

请注意,您不需要解析字符串即可获得所需的结果。 OWLEntity 有一个方法来确定它是否不匹配任何内容,您可以使用 OWLEntity.getIRI().getFragment() 获取 IRI 片段。

本体应该对 3.1 和 3.4.x 都有效(我使用 3.4.8)

关于java - 如何检索 OWLapi 中专用类的子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144369/

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