Skip to content

Commit

Permalink
Working on RDFLib#955 Adding EmbTP to SPARQL parser, using PG mode fo…
Browse files Browse the repository at this point in the history
…r evaluation
  • Loading branch information
JervenBolleman committed May 10, 2020
1 parent 3f0401d commit 7ab7d5c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
17 changes: 16 additions & 1 deletion rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from six import iteritems, itervalues

from rdflib.plugins.sparql import CUSTOM_EVALS
from rdflib.plugins.sparql.parserutils import value
from rdflib.plugins.sparql.parserutils import value, CompValue
from rdflib.plugins.sparql.sparql import (
QueryContext, AlreadyBound, FrozenBindings, Bindings, SPARQLError)
from rdflib.plugins.sparql.evalutils import (
Expand Down Expand Up @@ -122,6 +122,21 @@ def evalUnion(ctx, union):
for x in evalPart(ctx, union.p2):
yield x

# Embedded Triple Pattern
def evalEmbTP(ctx, reif):
from rdflib import RDF
v = Variable('reif')

p = CompValue()
p.name = 'Project'
p.p = CompValue()
p.p.name = 'BGP'
p.p.triples = [[v, RDF.subject, reif[0]],
[v,RDF.predicate, reif[1]],
[v,RDF.object, reif[2]]
]
p.PV = [v]
return evalProject(ctx, p)

def evalMinus(ctx, minus):
a = evalPart(ctx, minus.p1)
Expand Down
27 changes: 21 additions & 6 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ def _hexExpand(match):
# [106] VarOrTerm ::= Var | GraphTerm
VarOrTerm = Var | GraphTerm




# [107] VarOrIri ::= Var | iri
VarOrIri = Var | iri

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

VarOrBlankNodeOrIriOrLitOrEmbTP = Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral
EmbTP = Suppress('<<') + VarOrBlankNodeOrIriOrLitOrEmbTP + Verb + VarOrBlankNodeOrIriOrLitOrEmbTP + Suppress('>>')

VarOrTermOrEmbTP = Var | GraphTerm | EmbTP

# [105] GraphNodePath ::= VarOrTerm | TriplesNodePath
GraphNodePath = VarOrTerm | TriplesNodePath
GraphNodePath = VarOrTermOrEmbTP | TriplesNodePath


# [93] PathMod ::= '?' | '*' | '+'
Expand Down Expand Up @@ -471,6 +479,12 @@ def _hexExpand(match):
# [84] VerbPath ::= Path
VerbPath = Path

# See
VarOrBlankNodeOrIriOrLitOrEmbTP = Var | BlankNode | iri | RDFLiteral | NumericLiteral | BooleanLiteral
EmbTP = Combine(Suppress('<<'), VarOrBlankNodeOrIriOrLitOrEmbTP + Verb + VarOrBlankNodeOrIriOrLitOrEmbTP, Suppress('>>'))

VarOrTermOrEmbTP = Var | GraphTerm | EmbTP

# [87] ObjectPath ::= GraphNodePath
ObjectPath = GraphNodePath

Expand All @@ -490,7 +504,7 @@ def _hexExpand(match):
CollectionPath.setParseAction(expandCollection)

# [80] Object ::= GraphNode
Object = GraphNode
Object = GraphNode | EmbTP

# [79] ObjectList ::= Object ( ',' Object )*
ObjectList = Object + ZeroOrMore(',' + Object)
Expand Down Expand Up @@ -527,7 +541,7 @@ def _hexExpand(match):
TriplesNodePath <<= (CollectionPath | BlankNodePropertyListPath)

# [75] TriplesSameSubject ::= VarOrTerm PropertyListNotEmpty | TriplesNode PropertyList
TriplesSameSubject = VarOrTerm + PropertyListNotEmpty | TriplesNode + \
TriplesSameSubject = VarOrTermOrEmbTP + PropertyListNotEmpty | TriplesNode + \
PropertyList
TriplesSameSubject.setParseAction(expandTriples)

Expand All @@ -551,7 +565,7 @@ def _hexExpand(match):
QuadData = '{' + Param('quads', Quads) + '}'

# [81] TriplesSameSubjectPath ::= VarOrTerm PropertyListPathNotEmpty | TriplesNodePath PropertyListPath
TriplesSameSubjectPath = VarOrTerm + \
TriplesSameSubjectPath = VarOrTermOrEmbTP + \
PropertyListPathNotEmpty | TriplesNodePath + PropertyListPath
TriplesSameSubjectPath.setParseAction(expandTriples)

Expand Down Expand Up @@ -916,7 +930,6 @@ def _hexExpand(match):
# [73] ConstructTemplate ::= '{' Optional(ConstructTriples) '}'
ConstructTemplate = Suppress('{') + Optional(ConstructTriples) + Suppress('}')


# [57] OptionalGraphPattern ::= 'OPTIONAL' GroupGraphPattern
OptionalGraphPattern = Comp('OptionalGraphPattern', Keyword(
'OPTIONAL') + Param('graph', GroupGraphPattern))
Expand All @@ -929,9 +942,11 @@ def _hexExpand(match):
ServiceGraphPattern = Comp('ServiceGraphPattern', Keyword(
'SERVICE') + _Silent + Param('term', VarOrIri) + Param('graph', GroupGraphPattern))

ExpressionOrEmbTP = Expression | EmbTP

# [60] Bind ::= 'BIND' '(' Expression 'AS' Var ')'
Bind = Comp('Bind', Keyword('BIND') + '(' + Param(
'expr', Expression) + Keyword('AS') + Param('var', Var) + ')')
'expr', ExpressionOrEmbTP) + Keyword('AS') + Param('var', Var) + ')')

# [61] InlineData ::= 'VALUES' DataBlock
InlineData = Comp('InlineData', Keyword('VALUES') + DataBlock)
Expand Down
9 changes: 9 additions & 0 deletions test/test_sparql_star.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rdflib import Graph



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

0 comments on commit 7ab7d5c

Please sign in to comment.