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

时间:2013-11-22 11:57:16

标签: java subclass rdf owl owl-api

我正在尝试编写一个加载输入类子类的方法。 此代码与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的子类

1 个答案:

答案 0 :(得分:1)

我尝试复制您的问题,但由于某些变量在代码段中未定义,因此无法使用您的代码。我使用您正在使用的前缀管理器或类名称来解决问题。以下代码段提供了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)

都有效