Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

SPARQL request creation examples



The SPARQLRequest class allows to create SPARQL requests easily without having to write the request "by hand".

Basic example

Suppose that we have an ontology with an Aircraft class. We want to create a request getting the list of aircrafts.

We can do:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("elt");
   request.addType("elt", "Aircraft");
   String query = request.getSPARQL();
We will generate the following query:
   SELECT ?elt
   WHERE
   {
     ?elt rdf:type sitac:Aircraft .
   }      

A more complex example

Now we have the same class, but individuals from this class have a TheLabel object property, and we want to return the associated Label individual rather than the Aircraft individuals.

We can do:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("label");
   request.addAdditionalVariable("elt");
   request.addType("elt", "Aircraft");
   request.addType("label", "Label");
   request.addPropertyRef("elt", "TheLabel", "label");
   String query = request.getSPARQL();
We will generate the following query:
   SELECT ?label
   WHERE
   {
     ?elt rdf:type sitac:Aircraft .
     ?elt rdf:type sitac:Label .      
     ?elt sitac:TheLabel ?label .
   }      

Another example

Now we will use a Waypoint class, which can be in the Flightplan if their inFlightPlan data property value is true. If we want to return the list of waypoints which are in the Flightplan, we can do:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("elt");
   request.addType("elt", "Waypoint");
   request.addPropertyValue("elt", "inFlightPlan", true);
   String query = request.getSPARQL();
We will generate the following query:
   SELECT ?elt
   WHERE
   {
     ?elt rdf:type sitac:Waypoint .
     ?elt sitac:inFlightPlan "true" .      
   }      

An example with an OPTIONAL pattern

Now we have an additional property value reference, which is an OPTIONAL pattern.

We can do:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("elt");
   request.addType("elt", "Aircraft");
   request.addPropertyValue("elt", "TheLabel", "Rafale", true);
   String query = request.getSPARQL();
We will generate the following query:
   SELECT ?elt
   WHERE
   {
     ?elt rdf:type sitac:Aircraft .
     OPTIONAL {?elt sitac:TheLabel "Rafale" }
   }      

An example with an OPTIONAL pattern with several expressions

Now we will create an OPTIONAL pattern with two properties references.

We can do:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("elt");
   request.addType("elt", "Aircraft");
   SparqlRequest.Group optional = request.addOptionalConstruct();
   optional.addPropertyValue("elt", "TheLabel", "Rafale");
   optional.addPropertyValue("elt", "isFriend", "true");
   String query = request.getSPARQL();
We will generate the following query:
   SELECT ?elt
   WHERE
   {
      ?elt rdf:type sitac:Aircraft .
      OPTIONAL {
        ?elt sitac:TheLabel "Rafale" .
        ?elt sitac:isFriend "true"      
      }
   }      

An example with an UNION pattern

Now we will create an UNION pattern.

We can do:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("elt");
   request.addType("elt", "Aircraft");
   SparqlRequest.Union union = request.addUnionConstruct();
   SparqlRequest.Group group = union.addGroup();
   group.addPropertyValue("elt", "TheLabel", "Rafale");
   group = union.addGroup();       
   optional.addPropertyValue("elt", "isFriend", "false");
   String query = request.getSPARQL();
We will generate the following query:
   SELECT ?elt
   WHERE
   {
     ?elt rdf:type sitac:Aircraft .
     { ?elt sitac:TheLabel "Rafale" }
     UNION { ?elt sitac:isFriend "false" } 
   }      

See also


Categories: Requestcreation

Copyright 2025 Dassault Aviation. All Rights Reserved. Documentation and source under the LGPL v3 licence