Re: Template language for SPARQL?

Thanks to Scott Henninger, Paul Hermans, Bill Roberts, Martynas
Jusevicius, Gunnar Aastrand Grimnes, Andy Seaborne, Mark Wallace, Graham
Klyne, James Leigh and Ivan Mikhailov for your comments on SPARQL
template conventions (a few weeks ago).

There doesn't seem to be any sort of community consensus on preferred
syntax for indicating parameters in a SPARQL template.  Several syntaxes
were mentioned:

  {?foo}  %{foo}  %2  $foo  ${foo}

I decided to be agnostic in the template processor that I wrote for the
RDF Pipeline Framework, by allowing template authors to specify the
names of their parameters using a #inputs(...) template directive.  Here
is an example, using all five of the syntaxes above, with parameter
names separated by whitespace:

  #inputs( {?monkey} %{gorilla} %3 $ape ${chimp} )
  SELECT ?s 
  WHERE { { ?s a {?monkey} }
    UNION { ?s a %{gorilla} }
    UNION { ?s a %3        }
    UNION { ?s a $ape }
    UNION { ?s a ${chimp} } }

The template processor knows nothing about SPARQL syntax -- it merely
does blind text substitution -- so this also works:

  #inputs( Fred )
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  SELECT ?person
  WHERE { ?person foaf:givenName "Fred" }

Since a SPARQL server will ignore the #inputs directive as a comment, a
template can also be fully functional SPARQL code before template
processing, such as:

  #inputs( <http://example/in> <http://example/out> )
  PREFIX inGraph:  <http://example/in>
  PREFIX outGraph: <http://example/out>
  INSERT { GRAPH outGraph: { ?s ?p ?v } }
  WHERE  { GRAPH inGraph:  { ?s ?p ?v } }

This also allows the template to be tested without performing parameter
substitution, which can be handy during development.

For convenience, an environment variable foo can be accessing without
being declared, using the fixed syntax $ENV{var} .  And URI query string
parameters can be accessed as well, but must be declared using the
#parameters(...) directive.  For example, given a URI
http://example/path?firstName=Bill and environment variable $LASTNAME
containing "Clinton", the template:

  #parameters( $firstName )
  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  SELECT ?president
  WHERE { ?president foaf:givenName "$firstName" ;
                     foaf:familyName "$ENV{LASTNAME}" . }

will be expanded to:

  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  SELECT ?president
  WHERE { ?president foaf:givenName "Bill" ;
                     foaf:familyName "Clinton" . }

The code for RDF::Pipeline::Template is available here:
http://code.google.com/p/rdf-pipeline/source/browse/trunk/RDF-Pipeline/lib/RDF/Pipeline/Template.pm
And a simple script for invoking it from the command line is here:
http://code.google.com/p/rdf-pipeline/source/browse/trunk/tools/ste.perl 
A snapshot of the perldoc man page for the module is here:
http://code.google.com/p/rdf-pipeline/wiki/TemplateProcessor 

Comments and suggestions are welcomed.

Thanks!
David


On Thu, 2012-05-31 at 21:45 -0400, David Booth wrote: 
> [Resending, as my previous message never showed up.]
> 
> I would like to be able to write something like this:
> 
>   SELECT ?s
>   WHERE { ?s a ${CLASS} . }
> 
> and have ${CLASS} substituted in from an environment variable or other
> parameter, but SPARQL does not currently provide anything like this, and
> it's clearly beyond what the working group could put in SPARQL 1.1.  
> So I'm wondering . . . 
> 
> What have others been using as a template language for SPARQL?  
> 
> I know TopQuadrant uses an RDF representation for SPARQL CONSTRUCT
> queries in conjunction with SPIN, 
> http://www.w3.org/Submission/2011/02/ 
> but it shreds the SPARQL query into tiny RDF pieces, which is rather
> unfriendly for a human to read.  It would be much nicer to have
> something like the above.
> 
> How are others handling the need to parameterize their SPARQL queries
> like this?
> 
> 
> 

-- 
David Booth, Ph.D.
http://dbooth.org/

Opinions expressed herein are those of the author and do not necessarily
reflect those of his employer.

Received on Thursday, 26 July 2012 20:06:59 UTC