简体   繁体   English

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

[英]Problems to read a owl ontology using Jena

I'm trying to a read a owl ontology using Jena . 我正在尝试使用耶拿(Jena)阅读猫头鹰本体。 The ontology was created in Protégé . 本体是在Protégé中创建的。

This is (part of) my original .owl file (some terms are in Portuguese): 这是我的原始.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>

Complete file in PasteBin PasteBin中的完整文件

This is my class: 这是我的课:

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());
        }

    }

}

I'm using this class in my HTTP Servlet class like below: 我在HTTP Servlet类中使用此类,如下所示:

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

and I was receiving the following error: 并且我收到以下错误:

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

I was thinking that the problem was log4j, but the comments in this question took me to another direction. 我以为问题是log4j,但是此问题中的评论将我带到了另一个方向。

With a bit of research and attention, I saw in the Ontology API documentation that Jena does not have full support to OWL2, and apparently, this was the problem. 经过一番研究和关注,我在Ontology API文档中看到Jena没有完全支持OWL2,显然,这就是问题所在。 The answer for this another question indicated that the solution could be in the .owl file. 对于另一个问题的答案表明,解决方案可以在.owl文件中。

Finally, I saved it in RDF/XML (the file extension is still .owl) and the result is: 最后,我将其保存在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>

Complete file in PasteBin . PasteBin中的完整文件。

but the same error persists. 但是同样的错误仍然存​​在。 I found other questions with similar problem, but the solutions did not help me. 我发现其他 问题有类似的问题,但解决方案并不能帮助我。

I don't know what to do now. 我现在不知道该怎么办。 What I'm doing wrong? 我做错了什么?

Information: 信息:

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

Just to inform anyone with a similar problem than mine (or anyone that wants to read a protégé ontology using Jena), below is the process and code that I used to achieve it: 只是为了告知遇到与我的问题类似的任何人(或任何想使用Jena来阅读Protégé本体论的人),以下是我用来实现此问题的过程和代码:

1 - In Protégé, save your file as RDF/XML; 1-在Protégé中,将文件另存为RDF / XML;

2 - Copy the file to your WEB-INF directory (under any sub-directory if you want); 2-将文件复制到您的WEB-INF目录(如果需要,可以在任何子目录下);

3 - Create a class to read your ontology. 3-创建一个类来阅读您的本体。 For example: 例如:

 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 - Using a Servlet class (that extends HttpServlet), you can use the created class like below: 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM