Skip to content

Commit

Permalink
Working on RDFLib#955 Parsing passes, now into the algebra
Browse files Browse the repository at this point in the history
  • Loading branch information
JervenBolleman committed Mar 8, 2020
1 parent d09193b commit 47e2930
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
17 changes: 13 additions & 4 deletions rdflib/plugins/sparql/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def Filter(expr, p):
def Extend(p, expr, var):
return CompValue('Extend', p=p, expr=expr, var=var)

def EmpTP(p, o):
return CompValue('EmpTP', var=o)

def Values(res):
return CompValue('values', res=res)
Expand Down Expand Up @@ -133,6 +135,7 @@ def triples(l):
l = reduce(lambda x, y: x + y, l)
if (len(l) % 3) != 0:
raise Exception('these aint triples')

return reorderTriples((l[x], l[x + 1], l[x + 2])
for x in range(0, len(l), 3))

Expand Down Expand Up @@ -307,7 +310,8 @@ def translateGroupGraphPattern(graphPattern):
G = Join(p1=G, p2=p)
elif p.name == 'Bind':
G = Extend(G, p.expr, p.var)

elif p.name == "EmpTP":
G = Project(p, p.o)
else:
raise Exception('Unknown part in GroupGraphPattern: %s - %s' %
(type(p), p.name))
Expand Down Expand Up @@ -342,9 +346,11 @@ def _traverse(e, visitPre=lambda n: None, visitPost=lambda n: None):
return tuple([_traverse(x, visitPre, visitPost) for x in e])

elif isinstance(e, CompValue):
for k, val in e.items():
e[k] = _traverse(val, visitPre, visitPost)

if e.name != 'EmpTP':
for k, val in e.items():
e[k] = _traverse(val, visitPre, visitPost)
else:
raise Exception("Sparql star not tied into the algebra jet")
_e = visitPost(e)
if _e is not None:
return _e
Expand Down Expand Up @@ -682,6 +688,9 @@ def translatePrologue(p, base, initNs=None, prologue=None):
return prologue


def translateEmbTP(u):
return u

def translateQuads(quads):
if quads.triples:
alltriples = triples(quads.triples)
Expand Down
5 changes: 3 additions & 2 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import rdflib

DEBUG = False
DEBUG = True

# ---------------- ACTIONS

Expand Down Expand Up @@ -431,8 +431,9 @@ def _hexExpand(match):
# [104] GraphNode ::= VarOrTerm | TriplesNode
GraphNode = VarOrTerm | TriplesNode

#Should be recursive but it is not yet so
VarOrBlankNodeOrIriOrLitOrEmbTP = Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral
EmbTP = Suppress('<<') + VarOrBlankNodeOrIriOrLitOrEmbTP + Verb + VarOrBlankNodeOrIriOrLitOrEmbTP + Suppress('>>')
EmbTP = Suppress('<<') + Comp('EmpTP',ParamList('s', VarOrBlankNodeOrIriOrLitOrEmbTP) + ParamList('p', Verb) + ParamList('o', VarOrBlankNodeOrIriOrLitOrEmbTP)) + Suppress('>>')

VarOrTermOrEmbTP = Var | GraphTerm | EmbTP

Expand Down
50 changes: 32 additions & 18 deletions test/test_sparql_star.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
from rdflib import Graph
from rdflib.plugins.sparql.parser import VarOrBlankNodeOrIriOrLitOrEmbTP
import pyparsing
import unittest

class SparqlStarTests(unittest.TestCase):

def test_basic_sparql_star_subject():
g = Graph()
res = g.query('''SELECT * WHERE {
<< ?s ?p ?o >> ?p2 ?o2 .
}''')
@staticmethod
def test_basic_sparql_star_subject():
g = Graph()
res = g.query('''SELECT * WHERE {
<< ?s ?p ?o >> ?p2 ?o2 .
}''')

@staticmethod
def test_basic_sparql_star_object():
g = Graph()
res = g.query('''SELECT * WHERE {
?s ?p << ?s2 ?p2 ?o2 >> .
}''')

@staticmethod
def test_basic_sparql():
g = Graph()
res = g.query('''SELECT * WHERE {
?s ?p ?o .
}''')

def test_basic_sparql_star_object():
g = Graph()
res = g.query('''SELECT * WHERE {
?s ?p << ?s2 ?p2 ?o2 >> .
}''')
@staticmethod
def test_bind_sparql_star():
g = Graph()
res = g.query('''SELECT * WHERE { BIND(<< ?s2 ?p2 ?o2 >> AS ?b) }''')

def test_basic_sparql():
g = Graph()
res = g.query('''SELECT * WHERE {
?s ?p ?o .
}''')
@staticmethod
def test_parser():
assert VarOrBlankNodeOrIriOrLitOrEmbTP.matches("<<?s ?p ?o>>")


def test_bind_sparql_star():
g = Graph()
res = g.query('''SELECT * WHERE { BIND(<< ?s2 ?p2 ?o2 >> AS ?b) }''')
if __name__ == '__main__':
unittest.main()

0 comments on commit 47e2930

Please sign in to comment.