Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Creating an expression



The Expression is the interface for all expressions used in SPARQL Requests.

There are two ways to create an expression:
  • Using the API to combine expressions
  • Parsing an expression from a String

Using the API to combine expressions

Main Article: Expressions

One way to create expressions is to use the SPARQL request creation API to create expressions by combining:
  • The LiteralExpression class is used for literals
  • The VariableExpression class is used for expressions which only are a variable[1]
    For example for the ORDER BY ?var expression
  • The QualifiedNameExpression class is used for expressions which only encapsulate a qualified name
  • The ArithmeticExpression class is used for arithmetic expressions[2]
    For example for the BIND (?index * 2 as ?var) expression
  • The FunctionExpression class is used for functions expressions[3]
    For example for the SELECT ?zone (floor(?altitude) AS ?alt) expression
  • The ComparisonExpression class is used for comparison expressions[4]
    For example for the FILTER (?index < 5) expression
For example:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("zone");
   request.addAdditionalVariable("altitude");
   request.addPropertyRef("zone", "hasAltitude", "altitude");
   Bind bind = request.addBind("var");
   VariableExpression varExpr = bind.createVariable("altitude");
   LiteralExpression literal = bind.createLiteral(1000);
   ComparisonExpression cp = new ComparisonExpression(varExpr, ComparisonExpression.GREATER, literal);
   bind.addExpression(cp);      
Will have the following SPARQL:
   SELECT ?zone
   WHERE {
      ?zone sitac:hasAltitude ?altitude .
      BIND (?altitude > 1000 as ?var)
   }      

Parsing an expression from a String

Another way to create expressions is to use the Expression.create(String content, ISparqlRequestGroup request) static method.

This will create the expressions structure exactly as if it had been created using the combining API.

For example: For example:
   SparqlRequest request = new SparqlRequest("sitac");
   request.addSelect("zone");
   request.addAdditionalVariable("altitude");
   request.addPropertyRef("zone", "hasAltitude", "altitude");
   Bind bind = request.addBind("var");
   Expression exp = Expression.create("altitude < 1000");
   bind.addExpression(exp);      
Will have the following SPARQL:
   SELECT ?zone
   WHERE {
      ?zone sitac:hasAltitude ?altitude .
      BIND (?altitude > 1000 as ?var)
   }      

Notes

  1. ^ For example for the ORDER BY ?var expression
  2. ^ For example for the BIND (?index * 2 as ?var) expression
  3. ^ For example for the SELECT ?zone (floor(?altitude) AS ?alt) expression
  4. ^ For example for the FILTER (?index < 5) expression

See also


Categories: Requestcreation

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

Project Web Hosted by SourceForge.net Copyright 1999-2010 - Geeknet, Inc., All Rights Reserved About - Legal - Help