繁体   English   中英

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

[英]Problems to read a owl ontology using Jena

我正在尝试使用耶拿(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,但是此问题中的评论将我带到了另一个方向。

经过一番研究和关注,我在Ontology API文档中看到Jena没有完全支持OWL2,显然,这就是问题所在。 对于另一个问题的答案表明,解决方案可以在.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中的完整文件。

但是同样的错误仍然存​​在。 我发现其他 问题有类似的问题,但解决方案并不能帮助我。

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

信息:

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

只是为了告知遇到与我的问题类似的任何人(或任何想使用Jena来阅读Protégé本体论的人),以下是我用来实现此问题的过程和代码:

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

暂无
暂无

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

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