簡體   English   中英

使用對象屬性Sparql Jena獲取文字

[英]get Literal using the object property Sparql Jena

我有兩個sparql查詢:

 public static String query2
        = "PREFIX diag: <file:/D:/onto/owl_ontologies/diagnostic1.owl#> "
        + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
        + "PREFIX owl:<http://www.w3.org/2002/07/owl#>"
        + "SELECT ?disease ?symptom"
        + "WHERE { ?disease diag:hasSymptom ?symptom}";

String moreSymptomsQuery
            = "PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#> "
            + "SELECT ?symptom"
            + "WHERE {\"hyperglycemia\" diag:hasSymptom ?symptom}";

這是我的OWL文件的一部分

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:diag="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#">
  <owl:Ontology rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl"/>
  <owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
  <owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
  <owl:ObjectProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hasSymptom">
    <rdfs:range rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
    <rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
  </owl:ObjectProperty>
  <owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesId">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
  </owl:DatatypeProperty>
  <owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympId">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
  </owl:DatatypeProperty>
  <owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesLabel">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
  </owl:DatatypeProperty>
  <owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympLabel">
    <rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    <rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
  </owl:DatatypeProperty>

<!--this is an individual "desease" hasSymptom "Symptom" -->

     <diag:Desease rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia">
        <diag:hasSymptom>hypergammaglobulinemia</diag:hasSymptom>
        <diag:DesId>DES:000004</diag:DesId>
        <diag:DesLabel>hyperglycemia</diag:DesLabel>
     </diag:Desease>

這是我在耶拿的代碼:

 public void SelectQuesry() {
    Query query = QueryFactory.create(queryName);
    QueryExecution executeQuery = QueryExecutionFactory.create(query, modelDiag);
    org.apache.jena.query.ResultSet res = executeQuery.execSelect();

    while (res.hasNext()) {
        QuerySolution qs = res.nextSolution();
        Literal symp = qs.getLiteral("symptom");

    System.out.println(symp.toString());
    }


}

第一個查詢給出結果,而第二個則沒有結果! 我想知道每種疾病的症狀...這很重要...謝謝您的幫助。

文字絕不是RDF三元組的主題,實際上這意味着三元組模式

"hyperglycemia" diag:hasSymptom ?symptom

您的第二個查詢中沒有任何數據。

您必須使用RDF資源的URI,即http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia ,作為三重模式的主題:

PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#>
SELECT ?symptom
WHERE {
  diag:hyperglycemia diag:hasSymptom ?symptom
}

作為評論(我想我上次已經告訴過您):通過使用N-Triples語法而不是RDF / XML來查看您的數據。 這直接反映了SPARQL查詢中的模式。

而且,在您的數據中,除疾病外,所有內容都是字符串文字。 不知道為什么,但是如果您實際上是在建模本體,那么我還將使用RDF資源來解決症狀-至少在您要對症狀做出其他聲明時。

暫無
暫無

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

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