使用对象属性Sparql Jena获取Literal

时间:2017-05-09 21:58:48

标签: sparql rdf jena owl ontology

我有两个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());
    }


}

第一个查询给出了结果,没有结果由seconde !! 我想得到每种疾病的症状......这很重要...感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

文字是从不 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资源来解决症状 - 至少当你想对这个症状做一些额外的陈述时。