1

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

4

1 回答 1

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 有一个方法来确定它是否不匹配,您可以使用以下方法获取 IRI 片段OWLEntity.getIRI().getFragment()

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

于 2013-11-22T23:17:55.360 回答