使用耶拿(Jena)阅读猫头鹰本体的问题 [英] Problems to read a owl ontology using Jena

查看:111
本文介绍了使用耶拿(Jena)阅读猫头鹰本体的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to a read a owl ontology using Jena. The ontology was created in Protégé.

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

This is (part of) my original .owl file (some terms are in Portuguese):

<?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类中使用此类,如下所示:

I'm using this class in my HTTP Servlet class like below:

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,但是

I was thinking that the problem was log4j, but the comments in this question took me to another direction.

经过一些研究和关注,我在本体API文档中看到耶拿(Jena)没有对OWL2的完全支持,显然,这就是问题所在.

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. The answer for this another question indicated that the solution could be in the .owl file.

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

Finally, I saved it in RDF/XML (the file extension is still .owl) and the result is:

<?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 中的完整文件.

,但相同的错误仍然存​​在.我找到了其他

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?

信息:

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

推荐答案

仅是告知与我有类似问题的任何人(或任何想使用耶拿(Jena)读取Protégé本体论的人),以下是我使用的过程和代码实现它:

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:

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

1 - In Protégé, save your file as RDF/XML;

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

2 - Copy the file to your WEB-INF directory (under any sub-directory if you want);

3-创建一个类来阅读您的本体.例如:

3 - Create a class to read your ontology. 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-使用Servlet类(扩展了HttpServlet),您可以使用创建的类,如下所示:

4 - Using a Servlet class (that extends HttpServlet), you can use the created class like below:

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

这篇关于使用耶拿(Jena)阅读猫头鹰本体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆