Home
Categories
Dictionary
Glossary
Download
Project Details
Changes Log
What Links Here
FAQ
License

Jena SPARQL requests



There are two SPARQL request-response Service requests in the Jena module:
  • owlRequest: this service answers to SPARQL requests to the Jena framework. The service returns the results as a formatted or XML content
  • http://dassault-aviation.com/jena:owlObjectRequest: this service answers to SPARQL requests to the Jena framework and return the results as results as an object
Note that the request itself can be generated using the SparqlRequest utility.

Prefix declarations


You don't need to add the prefix declarations for your requests. The module will automatically add these declarations at the beginning of your requests, depending on the module namespace properties.

owlRequest

The owlRequest Service is a request-response Service Service which answers to SPARQL requests to the Jena framework.

The datas of the request are:
  • reqSchema: the key of the OWL schema to use for the request, must correspond to the name of one of the owlSchemas in the properties
  • query: the SPARQL query without the prefix. For example we could have:
                SELECT ?pizza 
                WHERE {
                  ?pizza a owl:Class ;
                  rdfs:subClassOf ?restriction.
                  ?restriction owl:onProperty pizza:hasTopping ;
                  owl:someValuesFrom pizza:PeperoniSausageTopping
                }
    
  • responseType: the way we want the response to be formatted. It is an enumeration with two possible values: FORMATTED for a text formatted response, and XML for an XML response


The datas of the reponse are:
  • respSchema: the key of the OWL schema which was used for the resquest
  • response: the response formatted as text if the responseType for set to FORMATTED
  • xmlResponse: the response as XML if the responseType for set to XML
  • requestStatus: the status which can be NO_SCHEMA if there was no OWL schema associated with the key, INVALID, if the request was invalid, and VALID if the request was valid

Decoding XML results

It is possible to decode XML results by using the org.da.protoframework.jena.common.JenaCommonUtilities static methods[1]
You will need to put the JenaCommon.jar Jar file on your library path
.

For example:
      public void subscribe(ServiceInstance service) {
        Data xmlResponse = service.getData("xmlResponse");
        Map>String, QueryResult.Result> results = JenaCommonUtilities.getResults(xmlResponse, true, "pizza");
      }

owlObjectRequest

Main Article: owlObjectRequest

The http://dassault-aviation.com/jena:owlObjectRequest Service is a request-response Service Service which answers to SPARQL requests to the Jena framework, bur return the results as a org.da.protoframework.jena.common.QueryResult object.

The datas of the request are:
  • reqSchema: the key of the OWL schema to use for the request, must correspond to the name of one of the owlSchemas in the properties
  • query: the SPARQL query without the prefix. For example we could have:
                SELECT ?pizza 
                WHERE {
                   ?pizza a owl:Class ;
                   rdfs:subClassOf ?restriction.
                   ?restriction owl:onProperty pizza:hasTopping ;
                   owl:someValuesFrom pizza:PeperoniSausageTopping
                }
    
  • stripPrefix: a boolean indicating if the prefix of the response results must be retained or stripped from the results
  • orderedBy: the name of the class used to order the results


The datas of the reponse are:
  • respSchema: the key of the OWL schema which was used for the request
  • response: the response as a QueryResult object
  • requestStatus: the status which can be NO_SCHEMA if there was no OWL schema associated with the key, INVALID, if the request was invalid, and VALID if the request was valid


For example:
      public void subscribe(ServiceInstance service) {
        QueryResults response = service.getData("response");
        Map<String, QueryResults.Result> results = response.getResults();
        Iterator<QueryResults.Result> it = results.values().iterator();
        while (it.hasNext()) {
          QueryResults.Result result = it.next();
          System.out.println("Key: " + result.getKey());
        }
      }

Defining SPARQL custom functions


It is possible to define SPARQL custom functions to use in the request.

For example, suppose that we have an ontology with the following class:
  • "Triangle": the concept of a circle This class has the following data properties:
    • "Hypothenuse": the length of the hypothenuse of the triangle (and xsd:decimal property)
    • "Angle": the angle of the hypothenuse (and xsd:decimal property)
Suppose that we want to get all the Triangles in our ontology and return the opposite side length. The proper formula is of course: opposite = Hypothenuse * sine(Angle).

However we can't use this in SPARQL because the sine function is not handled by SPARQL. But we will be able to define a opposite function with two arguments and then specify the following SPARQL query:
      SELECT ?triangle ?hypothenuse ?opposite
      WHERE {
         ?triangle rdf:type my:Triangle .
         ?triangle my:Hypothenuse ?hypothenuse .
         ?triangle my:Angle ?angle .
         BIND (my:opposite(?hypothenuse, ?angle) as ?opposite)     
      }

Notes

  1. ^ You will need to put the JenaCommon.jar Jar file on your library path

See also


Categories: builtin-applis

Copyright 2017-2020 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence