Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Variables



There are two kinds of variables in SPARQL requests:
  • Variables which are part of the SELECT expression
  • Variables which are present in the WHERE, INSERT or DELETE expression but are not part of the SELECT expression
All these variables are of the Variable class.

The names of the variables managed by the framework don't contain the "?" character, but when the request is printed, the "?" character will be automatically added.

Variables which are part of the SELECT expression

Adding a variable in the SELECT expression is performed through the SPARQLRequest.addSelect(String) method. The method will return false if the variable already exist.

For example:
   SparqlRequest request = new SparqlRequest("sitac");
   boolean isValid = request.addSelect("myVar"); 
The ISparqlRequestGroup.getVariables() method return all the variables which are part of the SELECT expression.

Removing a variable from the SELECT expression

The ISparqlRequestGroup.removeSelect(String name) method allow to remove a variable from the SELECT expression. The variable will be considered as an additional variable.

For example:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("myVar"); 
   request.removeSelect("myVar");

Variables which are not part of the SELECT expression

Adding a variable which is not part of the SELECT expression is performed through one of the following methods:
  • The ISparqlRequestGroup.addAdditionalVariable(String name) method will create a variable which is not part of the SELECT expression. The method will return false if the variable already exists
  • The ISparqlRequestGroup.addAutoVariable(String radix) method will create a variable which is not part of the SELECT expression. The name of the variable will use the specified radix, and the framework will make sure that the effective name (returned by the method) is not already existing
  • The ISparqlRequestGroup.addAutoVariable() method will create a variable which is not part of the SELECT expression. The name of the variable will be created automatically, and the framework will make sure that the effective name (returned by the method) is not already existing
The ISparqlRequestGroup.getAdditionalVars() method return all the variables which are not part of the SELECT expression.

For example:
   SparqlRequest request = new SparqlRequest("sitac");
   boolean isValid = request.addAdditionalVariable("myVar");
      
   String varName = request.addAutoVariable("myOtherVar"); // varName is "myOtherVar"
   varName = request.addAutoVariable("myOtherVar"); // varName is "myOtherVar1"  
       
      
   varName = request.addAutoVariable(); // varName is "auto_1"
   varName = request.addAutoVariable(); // varName is "auto_2"           

Iterating on all the variables

The ISparqlRequestGroup.getVariablesIterator() method will return an iterator on all the variables (bothe those which are in the SELECT part and those wich are not in the SELECT part).

The iterator will first return the variables which are in the SELECT part, then those which are not in the SELECT part.

Getting a variable of a specified name

Several methods allow to get the variable of a specified nalme:

Variable types

The VariableOrIRI.fromTypes() method return the types for which this variable is a representation.

The VariableOrIRI.getFirstType() method return the first type from which this variable is a representation.

For example, for the following SPARQL:
   SELECT ?zone
   WHERE {
     ?zone rdf:type sitac:TaskZone .
     ?zone sitac:isTasked "true" .
   }
We will have:
   Variable var = request.getVariable("zone");
   Map<String, PrefixedName> types = var.fromTypes();
   boolean contains = types.containsKey("sitac:TaskZone");     
      
   PrefixedName firstType = var.getFirstType(); // this is the prefixed name "sitac:TaskZone"

See also


Categories: Requestcreation

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