Search code examples
sparqlrdfjenaowlontology

get Literal using the object property Sparql Jena


I have two sparql queries:

 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}";

and this is a part of my OWL file

<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>

and this is my code in Jena:

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


}

the first query gives the results, and no result by the seconde!! I want to get the symptoms of each disease... this is important ... thanks for help.


Solution

  • A literal is never the subject of an RDF triple which in fact means that the triple pattern

    "hyperglycemia" diag:hasSymptom ?symptom

    in your second query doesn't match any data.

    You have to use the URI of the RDF resource, i.e.http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia, as the subject of the triple pattern:

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

    As a comment (I think I already told you last time): have a look at your data by using N-Triples syntax instead of RDF/XML. This directly reflects the patterns in the SPARQL query.

    Moreover, in your data everything is a string literal except the diseases. Not sure why, but if you're really modeling an ontology then I would also use RDF resources for the symptoms - at least when you want to make some additional statements about the symptom.