簡體   English   中英

從java中的owl文件獲取數據

[英]getting data from owl file in java

我得到了學生.i的所有成員使用以下代碼,但我得到的結果只有我的查詢是正確的。 當我在protege 4.3中運行該查詢時,他們給出了正確的結果但是java代碼中的問題。我不明白我做錯了什么。請在我的java類中提出錯誤。

class sparql1
    {  public static void main(String[] args) {

            String filename="modified2.owl";
            Model model=ModelFactory.createDefaultModel();
            OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

            try
            {
                File file=new File(filename);
                FileInputStream reader=new FileInputStream(file);
                model.read(reader,null);

                String query1=" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX my: <http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#> SELECT  ?ind WHERE { ?ind rdf:type my:Student .}";

                com.hp.hpl.jena.query.Query query=QueryFactory.create(query1);
                QueryExecution exe=QueryExecutionFactory.create(query, model1);
                ResultSet RES=exe.execSelect();
                ResultSetFormatter.out(System.out, RES, query);
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

我的本體論是

<?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-10 "http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#"
     xml:base="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10"
     xmlns:untitled-ontology-10="http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#"
     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/rohit/ontologies/2014/4/untitled-ontology-10"/>
     <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#hasAddress -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-10;hasAddress"/>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#hasEmail -->

    <owl:DatatypeProperty rdf:about="&untitled-ontology-10;hasEmail"/>
   <owl:Class rdf:about="&untitled-ontology-10;Student"/>
    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#Student2 -->

  <owl:NamedIndividual rdf:about="&untitled-ontology-10;Student2">
        <rdf:type rdf:resource="&untitled-ontology-10;Student"/>
        <hasEmail rdf:datatype="&xsd;string">asd</hasEmail>
        <hasAddress rdf:datatype="&xsd;string">sdsad</hasAddress>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#student1 -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-10;student1">
        <rdf:type rdf:resource="&untitled-ontology-10;Student"/>
        <hasEmail rdf:datatype="&xsd;string">fhgchg</hasEmail>
        <hasAddress rdf:datatype="&xsd;string">me</hasAddress>
    </owl:NamedIndividual>
    <!-- http://www.semanticweb.org/rohit/ontologies/2014/4/untitled-ontology-10#student3 -->

    <owl:NamedIndividual rdf:about="&untitled-ontology-10;student3">
        <rdf:type rdf:resource="&untitled-ontology-10;Student"/>
        <hasEmail rdf:datatype="&xsd;string">dsfdsf</hasEmail>
        <hasAddress rdf:datatype="&xsd;string">sdfds</hasAddress>
    </owl:NamedIndividual>
</rdf:RDF>

您沒有得到任何結果,因為您正在查詢空模型model1而所有結果都包含在model 只需更改:

OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);

... 至 ...

OntModel model1=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, model);

...然后您的后續查詢將可以訪問OntModel內部的三元組。 證明可以在創建QueryExecution實例的行中找到:

QueryExecution exe=QueryExecutionFactory.create(query, model1);

在此行中,您將創建一個引用新模型的查詢,該模型沒有您從文件中讀取的三元組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM