[英]How can I retrieve subclasses of a dedicated class in OWLapi?
我正在尝试编写一种方法来加载输入类的子类。 这段代码可以与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生成的猫头鹰文件:
<!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>
这是由两个类组成的非常简单的本体,而Adult是Person的子类
我尝试复制您的问题,但是由于代码段中未定义某些变量,因此无法使用您的代码。 我的问题是您使用的前缀管理器或类名。 以下代码片段提供了“成人”的结果:
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片段OWLEntity.getIRI().getFragment()
本体对于3.1和3.4.x都应有效(我使用的是3.4.8)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.