Problems to read a owl ontology using Jena

限于喜欢 提交于 2020-03-25 17:57:18

问题


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

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>

Complete file in 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:

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.

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.

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>

Complete file in 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;
  • JDK 1.8.0;
  • Netbeans 8.0.2;
  • GlassFish 4.1;
  • Jena 3.1.0;

回答1:


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 - In Protégé, save your file as RDF/XML;

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

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


来源:https://stackoverflow.com/questions/39092829/problems-to-read-a-owl-ontology-using-jena

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!