A Python wrapper to transform SPARQL queries into ARQ algebra S-expressions.
- Java SDK 1.8+
SELECT
ASK
CONSTRUCT
(unstable)
$ ./install.sh
$ pip install -r requirements.txt
From command line:
$ python arqalgebra.py 'select ?p (count(*) as ?c) where { ?s ?p ?o } group by ?p order by desc(?c)'
The expected output is the S-expression:
(project (?p ?c)
(order ((desc ?c))
(extend ((?c ?.0))
(group (?p) ((?.0 (count)))
(bgp (triple ?s ?p ?o))))))
From Python, one can also append oneline=True
:
import arqalgebra as aa
s_exp = aa.parse('select ?p (count(*) as ?c) where { ?s ?p ?o } group by ?p order by desc(?c)', oneline=True)
# (project (?p ?c) (order ((desc ?c)) (extend ((?c ?.0)) (group (?p) ((?.0 (count))) (bgp (triple ?s ?p ?o))))))
Return a SPARQL S-expression tree using the sexpdata library.
s_exp_tree = aa.parse_to_tree('select ?p (count(*) as ?c) where { ?s ?p ?o } group by ?p order by desc(?c)')
# [Symbol('project'), [Symbol('?p'), Symbol('?c')], [Symbol('order'), [[Symbol('desc'), Symbol('?c')]], [Symbol('extend'), [[Symbol('?c'), Symbol('?.0')]], [Symbol('group'), [Symbol('?p')], [[Symbol('?.0'), [Symbol('count')]]], [Symbol('bgp'), [Symbol('triple'), Symbol('?s'), Symbol('?p'), Symbol('?o')]]]]]]
Search for a term in the parsed tree and return the parent object.
aa.search(s_exp_tree, 'bgp')
# [Symbol('bgp'), [Symbol('triple'), Symbol('?s'), Symbol('?p'), Symbol('?o')]]
$ py.test