diff options
author | Boris Pelakh <boris.pelakh@semanticarts.com> | 2020-12-01 16:40:01 -0500 |
---|---|---|
committer | Boris Pelakh <boris.pelakh@semanticarts.com> | 2020-12-01 16:40:01 -0500 |
commit | c27da158f7b53ad3a91b50ba1cf0326133c6e940 (patch) | |
tree | bd4e6c32b95f130d7be1f8e905873b75ae26177f | |
parent | c5ff12717c02cf21abc8dd00ed8f0ec27071ba5d (diff) | |
download | rdflib-c27da158f7b53ad3a91b50ba1cf0326133c6e940.tar.gz |
Reset graph on exit from context
-rw-r--r-- | rdflib/plugins/sparql/evaluate.py | 3 | ||||
-rw-r--r-- | test/test_sparql.py | 34 |
2 files changed, 36 insertions, 1 deletions
diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index 496bc872..f5fd86ed 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -187,6 +187,7 @@ def evalGraph(ctx, part): ctx = ctx.clone() graph = ctx[part.term] + prev_graph = ctx.graph if graph is None: for graph in ctx.dataset.contexts(): @@ -199,11 +200,13 @@ def evalGraph(ctx, part): c = c.push() graphSolution = [{part.term: graph.identifier}] for x in _join(evalPart(c, part.p), graphSolution): + x.ctx.graph = prev_graph yield x else: c = ctx.pushGraph(ctx.dataset.get_context(graph)) for x in evalPart(c, part.p): + x.ctx.graph = prev_graph yield x diff --git a/test/test_sparql.py b/test/test_sparql.py index fdf29c3c..c6af2056 100644 --- a/test/test_sparql.py +++ b/test/test_sparql.py @@ -1,4 +1,5 @@ -from rdflib import Graph, URIRef, Literal, BNode +from rdflib import Graph, URIRef, Literal, BNode, ConjunctiveGraph +from rdflib.namespace import Namespace, RDF, RDFS from rdflib.plugins.sparql import prepareQuery from rdflib.compare import isomorphic @@ -112,6 +113,37 @@ def test_sparql_update_with_bnode_serialize_parse(): assert not raised +def test_named_filter_graph_query(): + g = ConjunctiveGraph() + g.namespace_manager.bind('rdf', RDF) + g.namespace_manager.bind('rdfs', RDFS) + ex = Namespace('https://ex.com/') + g.namespace_manager.bind('ex', ex) + g.get_context(ex.g1).parse(format="turtle", data=f""" + prefix ex: <https://ex.com/> + prefix rdfs: <{str(RDFS)}> + ex:Boris rdfs:label "Boris" . + ex:Susan rdfs:label "Susan" . + """) + g.get_context(ex.g2).parse(format="turtle", data=""" + prefix ex: <https://ex.com/> + ex:Boris a ex:Person . + """) + + assert list(g.query("select ?l where { graph ex:g1 { ?a rdfs:label ?l } ?a a ?type }", + initNs={'ex': ex})) == [(Literal('Boris'),)] + assert list(g.query("select ?l where { graph ex:g1 { ?a rdfs:label ?l } filter exists { ?a a ?type }}", + initNs={'ex': ex})) == [(Literal('Boris'),)] + assert list(g.query("select ?l where { graph ex:g1 { ?a rdfs:label ?l } filter not exists { ?a a ?type }}", + initNs={'ex': ex})) == [(Literal('Susan'),)] + assert list(g.query("select ?l where { graph ?g { ?a rdfs:label ?l } ?a a ?type }", + initNs={'ex': ex})) == [(Literal('Boris'),)] + assert list(g.query("select ?l where { graph ?g { ?a rdfs:label ?l } filter exists { ?a a ?type }}", + initNs={'ex': ex})) == [(Literal('Boris'),)] + assert list(g.query("select ?l where { graph ?g { ?a rdfs:label ?l } filter not exists { ?a a ?type }}", + initNs={'ex': ex})) == [(Literal('Susan'),)] + + if __name__ == "__main__": import nose |