W3C home > Mailing lists > Public > semantic-web@w3.org > July 2009

Parse SPARQL to AST (was Re: ANNOUNCEMENT: Raptor RDF Parser Library 1.4.19)

From: Dave Beckett <dave@dajobe.org>
Date: Mon, 20 Jul 2009 07:04:37 -0700
Message-ID: <4A647975.6080306@dajobe.org>
To: Nicolas Chauvat <nicolas.chauvat@logilab.fr>, 'Semantic Web' <semantic-web@w3.org>
CC: Dan Brickley <danbri@danbri.org>
Nicolas Chauvat wrote:
> Hi,
> 
> On Mon, Jul 20, 2009 at 11:29:15AM +0200, Dan Brickley wrote:
>> Intriguing. Can you post more about this sometime? eg. plans for Redland  
>> and Rasqal changes too? Where are things heading?
> 
> The other day I was looking for a library available from Python that
> would parse SPARQL and return an AST (we are working on supporting
> sparql in CubicWeb). I gave a look at Rasqal but it did not appear to
> be able to provide that feature. Was my understanding correct ?
> 
> An example of what I am looking for::
> 
>   from somelib import parse
>   tree = parse(sparql_query_string)
>   # do something with the tree

I guess I answered this in another thread but here's an example of the
subversion version of rasqal turning a query into AST.  (The query engine
itself then turns that into sparql algebra).


$ cat test-queries/tr.rq
PREFIX  dc: <http://purl.org/dc/elements/1.1/>
PREFIX org: <http://www.w3.org/2001/04/roadmap/org#>
PREFIX mat: <http://www.w3.org/2002/05/matrix/vocab#>
PREFIX doc: <http://www.w3.org/2000/10/swap/pim/doc#>
PREFIX rec: <http://www.w3.org/2001/02pd/rec54#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX con: <http://www.w3.org/2000/10/swap/pim/contact#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT $wg $date $document
#FROM <http://www.w3.org/2002/01/tr-automation/tr.rdf>
FROM <tr.rdf>
WHERE {
  $document a rec:WD.
  $document org:deliveredBy $x.
  $x con:homePage $wg.
  $document dc:date $date.
  OPTIONAL {
    $document2 a rec:WD.
    $document2 dc:date $date2.
    $document2 org:deliveredBy $x2.
    $x2 con:homePage $wg.
    FILTER ($date < $date2 && $document2 != $document)
  }.
  FILTER(!BOUND($document2))
}

$ utils/roqet -n -d structure test-queries/tr.rq
roqet: Querying from file test-queries/tr.rq
Query:
query verb: SELECT
query bound variables (3): wg, date, document
query Group graph pattern[0] {
  sub-graph patterns (3) {
    Basic graph pattern[1] #0 {
      triples {
        triple #0 { triple(variable(document),
uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>,
uri<http://www.w3.org/2001/02pd/rec54#WD>) }
        triple #1 { triple(variable(document),
uri<http://www.w3.org/2001/04/roadmap/org#deliveredBy>, variable(x)) }
        triple #2 { triple(variable(x),
uri<http://www.w3.org/2000/10/swap/pim/contact#homePage>, variable(wg)) }
        triple #3 { triple(variable(document),
uri<http://purl.org/dc/elements/1.1/date>, variable(date)) }
      }
    }
    Optional graph pattern[2] #1 {
      sub-graph patterns (1) {
        Group graph pattern[3] #0 {
          sub-graph patterns (2) {
            Basic graph pattern[4] #0 {
              triples {
                triple #0 { triple(variable(document2),
uri<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>,
uri<http://www.w3.org/2001/02pd/rec54#WD>) }
                triple #1 { triple(variable(document2),
uri<http://purl.org/dc/elements/1.1/date>, variable(date2)) }
                triple #2 { triple(variable(document2),
uri<http://www.w3.org/2001/04/roadmap/org#deliveredBy>, variable(x2)) }
                triple #3 { triple(variable(x2),
uri<http://www.w3.org/2000/10/swap/pim/contact#homePage>, variable(wg)) }
              }
            }
            Filter graph pattern[5] #1 {
              filter { expr(op and(expr(op lt(expr(variable(date)),
expr(variable(date2)))), expr(op neq(expr(variable(document2)),
expr(variable(document))))))}
            }
          }
        }
      }
    }
    Filter graph pattern[6] #2 {
      filter { expr(op bang(expr(op bound(expr(variable(document2))))))}
    }
  }
}


The released rasqal does something similar but the structure is slightly
different.

The above is a dump of a walk of the constructed data structures made
available via the C API.   Garlik's 4store uses this for it's query parsing.

Dave
Received on Monday, 20 July 2009 14:05:15 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 1 March 2016 07:42:13 UTC