SPARQL/Extensions/SPARQLScript

From W3C Wiki

SPARQLScript - SPARQL-based scripting

Several SPARQL toolkits provide Update functionality and move from simple query operations to more powerful data processing. The idea behind SPARQLScript is to go another step further and to enable or simplify the implementation of

  • semantic Mashups,
  • certain rules and inference tasks (e.g. resource consolidation) in a SPARQL-based syntax (without the need to learn another language for rules where SPARQL's CONSTRUCT or the proposed INSERT and DELETE extensions could already serve most needs)
  • SPARQL-powered widgets ("sparqlets")
  • non-declarative, RDF-driven Yahoo! Pipes-like systems
  • Templating for SPARQL query results
  • portable transformations/mappings of RDF data

SPARQLScript was initially motivated by feedback and feature requests by SPARQLBot users who asked for simple functionality improvements such as being able to combine multiple SPARQL+ queries in a single command, e.g. a LOAD followed by a SELECT, or an ASK-based check before a DELETE query is executed, or the ability to switch the target endpoint that a particular query should be run against.

SPARQL provides a number of native result types such as record sets (SELECT), RDF graphs (CONSTRUCT, DESCRIBE), and TRUE/FALSE (ASK). SPARQLScript should allow the intuitive combination and processing of these types.

This page can be used for ideas, reports, feedback, and related work.

Implementations

 BenjaminNowack is working on an implementation for ARC. The new SPARQLBot site is planned to be used for tests and demos.

Use cases, examples, and (incremental) SPARQL Grammar Extensions

Query Chaining

Example:

LOAD <http://example.com/g1>
LOAD <http://example.com/g2>
SELECT * WHERE { ... }


Grammar extension:


Script ::= ( Query )*


Shared/script-scoped prefix declarations

Example:

PREFIX foaf: <http://...>
SELECT ?name WHERE { ?s foaf:name ?name}
SELECT ?nick WHERE { ?s foaf:nick ?nick}


Grammar extension:


Script ::= ( Query | PrefixDecl )*


Endpoint Declarations

Example:

ENDPOINT <http://dbpedia.org/sparql>
SELECT ...


Grammar extension:


Script ::= ( Query | PrefixDecl | EndpointDecl )*
EndpointDecl ::= 'ENDPOINT' IRI_REF


Variable Assignments

Example:

$doc := CONSTRUCT ...


Grammar extension:


Script ::= ( Query | PrefixDecl | EndpointDecl | Assignment )*
Assignment ::= Var (':=' | '=') ( Query | String | Var ) ';'?


Branches

Example:

$proceed := ASK ...
IF ($proceed) {
 ...
}


Grammar extension:


Script ::= ( Query | PrefixDecl | EndpointDecl | Assignment | IFBlock )*
IFBlock ::= 'IF' BrackettedExpression '{' Script '}' ( 'ELSE' '{' Script '}')?


Iterations

Example:

$entries := SELECT ?name WHERE ...
FOR ($entry IN $entries) {  
 $name := $entry.name    
 ...
}


Grammar extension:


Script ::= ( Query | PrefixDecl | EndpointDecl | Assignment | IFBlock | FORBlock)*
FORBlock ::= 'FOR' '(' Var 'IN' Var ')' '{' Script '}' 


Placeholders

It should be possible to use placeholders that get filled from a previous query or script block. Not sure about the exact syntax to use here. Example:

...
$name := ...
SELECT ?person WHERE { ?person foaf:name "${name}" }


Grammar extension:


Placeholder ::= '$' '{' VARNAME '}'


Placeholders in literals don't need parser extensions, but more complicated use cases (local parts of qnames, or query fragments) need grammar changes.

Output Templating

A script sets and changes variables in the current scope. It should be possible to generate output such as status messages, or to fill text templates (e.g. for HTML clients). While most template engines allow executed code to be embedded in a textual template, SPARQLScript works the other way round: output literals (with optional placeholders) should be embeddable in the processed script code. Re-using SPARQL/Turtle literal delimiters to indicate output could be an intuitive approach. Explicit commands such as print(...) could work as well.

Example:

# query
$items = SELECT ?title WHERE ...
# stand-alone literal => output
"Found ${items.size} items: <ul>"
#loop
FOR ($item in $items) {
  "<li>${item.title}</li>"
}
"</ul>"


Grammar extension:


Script ::= ( Query | PrefixDecl | EndpointDecl | Assignment | IFBlock | FORBlock | String )*


Function calls

A Script should be able to re-use externally defined Scripts/Functions

Example:

@@todo


Grammar extension:

@@todo


Related Work References