使用Jena阅读猫头鹰本体的问题

时间:2016-08-23 05:10:26

标签: java jena owl ontology

我正在尝试使用Jena阅读猫头鹰本体。本体是在Protégé中创建的。

这是我的原始.owl文件(部分用葡萄牙语)的一部分:

<?xml version="1.0"?>


<!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://james.miranda.br/Onto"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     ontologyIRI="http://james.miranda.br/Onto"
     versionIRI="http://james.miranda.br/Onto/1.0.0">
    <Prefix name="" IRI="http://www.w3.org/2002/07/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#"/>
    <Annotation>
        <AnnotationProperty abbreviatedIRI="rdfs:comment"/>
        <Literal xml:lang="en" datatypeIRI="&rdf;PlainLiteral">Ontology for the decision process</Literal>
    </Annotation>
    <Annotation>
        <AnnotationProperty abbreviatedIRI="rdfs:comment"/>
        <Literal xml:lang="pt" datatypeIRI="&rdf;PlainLiteral">Ontologia para o processo de tomada de decisões.</Literal>
    </Annotation>
    <Declaration>
        <Class IRI="#AcaoDesign"/>
    </Declaration>
    <Declaration>
        <Class IRI="#Alternativa"/>
    </Declaration>

PasteBin

中的完整档案

这是我的班级:

public class ReadOntology {


    public static void run(String ontologyInFile) {

        OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, null);
        InputStream ontologyIn = FileManager.get().open(ontologyInFile);

        loadModel(model, ontologyIn);
    }


    /** 
     * @param m
     * @param ontologyIn */
    protected static void loadModel(OntModel m, InputStream ontologyIn) {
        try {
             m.read(ontologyIn, "RDF/XML");
             //also tried m.read(ontologyIn, "OWL/XML");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

}

我在HTTP Servlet类中使用此类,如下所示:

ServletContext context = this.getServletContext();
String fullPath = context.getRealPath("/WEB-INF/ontology/Onto.owl");
ReadOntology.run(fullPath);

我收到了以下错误:

log4j:WARN No appenders could be found for logger (org.apache.jena.riot.system.stream.JenaIOEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Error [line: 27, col: 64] {E201} Multiple children of property element

我原以为问题是log4j,但是this question中的评论把我带到了另一个方向。

通过一些研究和关注,我在Ontology API documentation中看到耶拿并没有完全支持OWL2,显然,这就是问题所在。 this another question的答案表明解决方案可能位于.owl文件中。

最后,我将它保存在RDF / XML中(文件扩展名仍然是.owl),结果是:

<?xml version="1.0"?>


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


<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#"
     xml:base="http://www.w3.org/2002/07/owl"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <Ontology rdf:about="http://james.miranda.br/Onto">
        <rdfs:comment xml:lang="en">Ontology for the decision making process</rdfs:comment>
        <rdfs:comment xml:lang="pt">Ontologia para o processo de tomada de decisões.</rdfs:comment>
        <versionIRI rdf:resource="http://james.miranda.br/Onto/1.0.0"/>
    </Ontology>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://james.miranda.br/Onto#atende -->

    <ObjectProperty rdf:about="http://james.miranda.br/Onto#atende">
        <rdf:type rdf:resource="&owl;InverseFunctionalProperty"/>
    </ObjectProperty>



    <!-- http://james.miranda.br/Onto#compoe -->

    <ObjectProperty rdf:about="http://james.miranda.br/Onto#compoe">
        <rdfs:subPropertyOf rdf:resource="http://james.miranda.br/Onto#ehParteDe"/>
    </ObjectProperty>



    <!-- http://james.miranda.br/Onto#conflitaCom -->

    <ObjectProperty rdf:about="http://james.miranda.br/Onto#conflitaCom">
        <rdf:type rdf:resource="&owl;ReflexiveProperty"/>
    </ObjectProperty>

PasteBin中的完整档案。

但同样的错误仍然存​​在。我发现other questions有类似的问题,但解决方案对我没有帮助。

我现在不知道该怎么办。我做错了什么?

信息:

  • Ubuntu 14.04;
  • JDK 1.8.0;
  • Netbeans 8.0.2;
  • GlassFish 4.1;
  • Jena 3.1.0;

1 个答案:

答案 0 :(得分:1)

只是为了告诉任何与我有类似问题的人(或任何想要使用Jena阅读门徒本体的人),下面是我用来实现它的过程和代码:

1 - 在Protégé中,将文件另存为RDF / XML;

2 - 将文件复制到WEB-INF目录(如果需要,可以在任何子目录下);

3 - 创建一个类来阅读本体。例如:

 import java.io.InputStream;
 import org.apache.jena.ontology.*;
 import org.apache.jena.rdf.model.ModelFactory;
 import org.apache.jena.util.FileManager;

 public class ReadOntology {

    public OntModel model;

    public static void run(String ontologyInFile) {

        model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, null);
        InputStream ontologyIn = FileManager.get().open(ontologyInFile);

        loadModel(model, ontologyIn);
    }


    /** 
     * @param m
     * @param ontologyIn */
    protected static void loadModel(OntModel m, InputStream ontologyIn) {
        try {
             m.read(ontologyIn, "RDF/XML");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

}

4 - 使用Servlet类(扩展HttpServlet),您可以使用如下所示的创建类:

ServletContext context = this.getServletContext();
String fullPath = context.getRealPath("/WEB-INF/yourOntologyName.owl");
ReadOntology.run(fullPath);
ExtendedIterator<OntClass> classIterator = ReadOntology.model.listClasses(); 
while (classIterator.hasNext()) { 
    OntClass ontClass = classIterator.next(); 
    System.out.println(ontClass.toString());
}