Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initBindings in SPARQL #692

Merged
merged 1 commit into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 4 additions & 32 deletions rdflib/plugins/sparql/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,41 +446,13 @@ def evalConstructQuery(ctx, query):


def evalQuery(graph, query, initBindings, base=None):
ctx = QueryContext(graph)

ctx.prologue = query.prologue
initBindings = dict( ( Variable(k),v ) for k,v in initBindings.iteritems() )

main = query.algebra
ctx = QueryContext(graph, initBindings=initBindings)

if initBindings:
# add initBindings as a values clause

values = {} # no dict comprehension in 2.6 :(
for k,v in initBindings.iteritems():
if not isinstance(k, Variable):
k = Variable(k)
values[k] = v

main = main.clone() # clone to not change prepared q
main['p'] = main.p.clone()
# Find the right place to insert MultiSet join
repl = main.p
if repl.name == 'Slice':
repl['p'] = repl.p.clone()
repl = repl.p
if repl.name == 'Distinct':
repl['p'] = repl.p.clone()
repl = repl.p
if repl.p.name == 'OrderBy':
repl['p'] = repl.p.clone()
repl = repl.p
if repl.p.name == 'Extend':
repl['p'] = repl.p.clone()
repl = repl.p

repl['p'] = Join(repl.p, ToMultiSet(Values([values])))

# TODO: Vars?
ctx.prologue = query.prologue
main = query.algebra

if main.datasetClause:
if ctx.dataset is None:
Expand Down
13 changes: 7 additions & 6 deletions rdflib/plugins/sparql/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def forget(self, before, _except=None):
since before
"""
if not _except : _except = []

return FrozenBindings(self.ctx, (x for x in self.iteritems() if x[0] in _except or before[x[0]] is None))
# bindings from initBindings are newer forgotten
return FrozenBindings(self.ctx, (x for x in self.iteritems() if x[0] in _except or x[0] in self.ctx.initBindings or before[x[0]] is None))

def remember(self, these):
"""
Expand All @@ -213,8 +213,10 @@ class QueryContext(object):
Query context - passed along when evaluating the query
"""

def __init__(self, graph=None, bindings=None):
self.bindings = bindings or Bindings()
def __init__(self, graph=None, bindings=None, initBindings=None):
self.initBindings = initBindings
self.bindings = Bindings(d=bindings or [])
if initBindings: self.bindings.update(initBindings)

if isinstance(graph, ConjunctiveGraph):
self._dataset = graph
Expand All @@ -233,9 +235,8 @@ def __init__(self, graph=None, bindings=None):

def clone(self, bindings=None):
r = QueryContext(
self._dataset if self._dataset is not None else self.graph)
self._dataset if self._dataset is not None else self.graph, bindings or self.bindings, initBindings=self.initBindings)
r.prologue = self.prologue
r.bindings.update(bindings or self.bindings)
r.graph = self.graph
r.bnodes = self.bnodes
return r
Expand Down
12 changes: 4 additions & 8 deletions rdflib/plugins/sparql/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def evalCopy(ctx, u):
dstg += srcg


def evalUpdate(graph, update, initBindings=None):
def evalUpdate(graph, update, initBindings={}):
"""

http://www.w3.org/TR/sparql11-update/#updateLanguage
Expand All @@ -274,15 +274,11 @@ def evalUpdate(graph, update, initBindings=None):

for u in update:

ctx = QueryContext(graph)
initBindings = dict( ( Variable(k),v ) for k,v in initBindings.iteritems() )

ctx = QueryContext(graph, initBindings=initBindings)
ctx.prologue = u.prologue

if initBindings:
for k, v in initBindings.iteritems():
if not isinstance(k, Variable):
k = Variable(k)
ctx[k] = v
# ctx.push() # nescessary?

try:
if u.name == 'Load':
Expand Down
3 changes: 3 additions & 0 deletions test/test_initbindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def testVariableKeyWithQuestionMark():
results = list(g2.query("SELECT ?o WHERE { ?s :p ?o }", initBindings={Variable("?s"): EX['s1']}))
assert len(results) == 1, results

def testFilter():
results = list(g2.query("SELECT ?o WHERE { ?s :p ?o FILTER (?s = ?x)}", initBindings={Variable("?x"): EX['s1']}))
assert len(results) == 1, results

if __name__ == "__main__":

Expand Down