代码之家  ›  专栏  ›  技术社区  ›  James

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

  •  0
  • James  · 技术社区  · 8 年前

    我正在尝试使用读取猫头鹰本体 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
    

    this question 带我去了另一个方向。

    经过一番研究和关注,我发现 Ontology API documentation Jena没有完全支持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;
    • 玻璃鱼4.1;
    • 耶拿3.1.0;
    1 回复  |  直到 7 年前
        1
  •  1
  •   James    8 年前

    下面是我用来实现它的过程和代码,我只想告诉那些与我有类似问题的人(或者那些想用Jena阅读protg本体的人):

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