Search code examples
sparqljenasemantic-webontologyprotege

SPARQL Query Printing Enumerate Class with JENA JAVA


I made this owl model. There is two classes Sensor and Location, and Location is enumerated class.

:Sensor rdf:type owl:Class;
:Location rdf:type owl:Class ;
                 owl:equivalentClass [ rdf:type owl:Class ;
                                       owl:oneOf ( :Bathroom
                                                   :Bedroom
                                                 )
                                     ] .
:hasLocation rdf:type owl:ObjectProperty ;
                    rdfs:domain [ rdf:type owl:Class ;
                                  :Sensor
                                ] ;
                    rdfs:range :Location .
:Bathing rdf:type owl:NamedIndividual ,
                         ADLOntology:Bathing .
:Bathroom rdf:type owl:NamedIndividual ,
                          ADLOntology:Location .
:Window rdf:type owl:NamedIndividual ,
                           :Sensor ;
                  :hasLocation :Bedroom;
                  :hasId "55"^^xsd:int ; .

I am trying to get the location of each sensors with their id-number. I wrote my query in Protege and it works fine. But, on JENA it's printing null for the location. I used the Resource to print the sensor, but for the location it prints null. I couldn't figure it out the properer way to print the location.

String file = "C:/users/src/data.ttl";
Model model = FileManager.get().loadModel(file);
String queryString = "PREFIX : <http://semanticweb.org/sensor#>" +
                     "SELECT ?sensor ?location" +
                     "WHERE {?sensor :hasId \"55"\^^xsd:int." +
                             "?sensor :hasLocation ?location}";
Query query = QueryFactory.create(queryString);
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
          ResultSet result = qexec.execSelect();
          for ( ; result.hasNext(); ) {
                  QuerySolution soln = result.nextSolution();
                  Resource sensor= soln.getResource("sensor");
                  Resource location = soln.getResource("location");
                  System.out.println("Sensor" + sensor);
                  System.out.println("Location" + location);
          }
}

Solution

  • That has nothing to do with enumeration in OWL.

    The query simply looks for mappings in the RDF graph. In your example, it will work once you carefully check how the SPARQL query is generated. Note, that you concatenate the String in Java and either line breaks or spaces have to be used. Your query is missing both after the ?location variable in the SELECT part, thus, it will result in ?locationWHERE.

    Solutions:

    Add missing space, i.e.

    String queryString = "PREFIX : <http://semanticweb.org/sensor#>" +
                         "SELECT ?sensor ?location " +
                         "WHERE {?sensor :hasId \"55"\^^xsd:int." +
                                 "?sensor :hasLocation ?location}";
    

    or line break

    String queryString = "PREFIX : <http://semanticweb.org/sensor#>" +
                         "SELECT ?sensor ?location\n" +
                         "WHERE {?sensor :hasId \"55"\^^xsd:int." +
                                 "?sensor :hasLocation ?location}";