如何在eclipse上用jena查询owl文件

时间:2014-04-04 18:50:05

标签: eclipse sparql jena

我是Jena的新手,我想使用jena sparql实现查询owl文件,我的代码如下:

import com.hp.hpl.jena.ontology.OntModelSpec;;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

import org.apache.jena.atlas.io.IndentedWriter;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.vocabulary.RDF;


public class JenaTest{
        public static final String owlFile = "C:/Users/acer/workspace/project4/src/project4/onto.owl";
        public static final String NL      = System.getProperty("line.separator") ;  

        public static void main( String[] args ) {

                Model m = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RDFS_INF);


                FileManager.get().readModel( m, owlFile );
                String myOntologyName = "untitled-ontology-73";
                String myOntologyNS   = "http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#";


                String rdfPrefix        = "PREFIX rdf: <"+RDF.getURI()+">" ;
                String myOntologyPrefix = "PREFIX "+myOntologyName+": <"+myOntologyNS+">" ;



                String queryString =   myOntologyPrefix + NL
                                     + rdfPrefix + NL +
                                       "SELECT ?person" ;

                Query query = QueryFactory.create(queryString) ;

                query.serialize(new IndentedWriter(System.out,true)) ;
                System.out.println() ;



            QueryExecution qexec = QueryExecutionFactory.create(query, m) ;

            try {

                    ResultSet rs = qexec.execSelect() ;


                    for ( ; rs.hasNext() ; ){
                            QuerySolution rb = rs.nextSolution() ;
                            RDFNode y = rb.get("person");
                            System.out.print("name : "+y+"--- ");
                            Resource z = (Resource) rb.getResource("person");
                            System.out.println("plus simplement "+z.getLocalName());
                    }
            }
            finally{
              qexec.close() ;
            }
    }

}

本体论如下:

<?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#" >
    <!ENTITY untitled-ontology-73 "http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#" >
]>

<rdf:RDF xmlns="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#"
     xml:base="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"
     xmlns:untitled-ontology-73="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"/>


    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns -->

    <owl:ObjectProperty rdf:about="&untitled-ontology-73;knowns">
        <rdf:type rdf:resource="&owl;ReflexiveProperty"/>
        <rdfs:domain rdf:resource="&untitled-ontology-73;person"/>
        <rdfs:range rdf:resource="&untitled-ontology-73;person"/>
    </owl:ObjectProperty>


    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-73;name">
        <rdfs:domain rdf:resource="&untitled-ontology-73;person"/>
        <rdfs:range rdf:resource="&xsd;string"/>
    </owl:DatatypeProperty>


    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person -->

    <owl:Class rdf:about="&untitled-ontology-73;person">
        <rdfs:subClassOf>
            <owl:Restriction>
                <owl:onProperty rdf:resource="&untitled-ontology-73;knowns"/>
                <owl:someValuesFrom rdf:resource="&untitled-ontology-73;person"/>
            </owl:Restriction>
        </rdfs:subClassOf>
    </owl:Class>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-73;amina">
        <rdf:type rdf:resource="&untitled-ontology-73;person"/>
        <name>amina</name>
        <knowns rdf:resource="&untitled-ontology-73;george"/>
        <knowns rdf:resource="&untitled-ontology-73;john"/>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-73;george">
        <rdf:type rdf:resource="&untitled-ontology-73;person"/>
        <name>george</name>
        <knowns rdf:resource="&untitled-ontology-73;amina"/>
        <knowns rdf:resource="&untitled-ontology-73;nacira"/>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-73;john">
        <rdf:type rdf:resource="&untitled-ontology-73;person"/>
        <name>john</name>
    </owl:NamedIndividual>

    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-73;nacira">
        <rdf:type rdf:resource="&untitled-ontology-73;person"/>
        <name>nacira</name>
    </owl:NamedIndividual>
</rdf:RDF>

<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->

我收到以下异常: 线程“main”中的异常java.lang.Error:未解决的编译问题:在project4.JenaTest.main(JenaTest.java:22)

上面的代码有什么问题,或者其他什么原因可能不起作用? 提前谢谢你。

1 个答案:

答案 0 :(得分:0)

Unresolved compilation problem: at project4.JenaTest.main(JenaTest.java:22)

你的一个类有编译时问题。