From 6c026d0922a392efdc1434122c081a383d9c415f Mon Sep 17 00:00:00 2001 From: Gunnar Aastrand Grimnes Date: Mon, 29 Jul 2013 11:31:20 +0200 Subject: [PATCH] Dataset and graph_aware store fixes Add graphs when parsing, so also empty graphs are added. --- rdflib/graph.py | 39 ++++++++++++++++++++------------- rdflib/plugins/memory.py | 6 ++++- rdflib/plugins/sparql/update.py | 18 ++++++++++++--- test/test_graph.py | 2 +- 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/rdflib/graph.py b/rdflib/graph.py index 232e601be..42bb7cac5 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -1308,8 +1308,9 @@ def add(self, triple_or_quad): self.store.add((s, p, o), context=c, quoted=False) - def _graph(self, c): - if not isinstance(c, Graph): + def _graph(self, c): + if c is None: return None + if not isinstance(c, Graph): return self.get_context(c) else: return c @@ -1348,10 +1349,14 @@ def triples(self, triple_or_quad, context=None): s,p,o,c = self._spoc(triple_or_quad) context = self._graph(context or c) - if not self.default_union and context is None: - context=self.default_context - - if isinstance(p, Path): + if self.default_union: + if context==self.default_context: + context = None + else: + if context is None: + context = self.default_context + + if isinstance(p, Path): if context is None: context = self @@ -1443,7 +1448,7 @@ def parse(self, source=None, publicID=None, format="xml", g_id = URIRef(g_id) context = Graph(store=self.store, identifier=g_id) - context.remove((None, None, None)) + context.remove((None, None, None)) # hmm ? context.parse(source, publicID=publicID, format=format, location=location, file=file, data=data, **args) return context @@ -1580,17 +1585,21 @@ def graph(self, identifier=None): "genid", "http://rdflib.net" + rdflib_skolem_genid, override=False) identifier = BNode().skolemize() - else: - if isinstance(identifier, BNode): - raise Exception( - "Blank nodes cannot be Graph identifiers in RDF Datasets") - if not isinstance(identifier, URIRef): - identifier = URIRef(identifier) - - g = self.get_context(identifier) + + g = self._graph(identifier) + self.store.add_graph(g) return g + def parse(self, source=None, publicID=None, format="xml", + location=None, file=None, data=None, **args): + c = ConjunctiveGraph.parse(self, source, publicID, format, location, file, data, **args) + self.graph(c) + + def add_graph(self, g): + """alias of graph for consistency""" + return self.graph(g) + def remove_graph(self, g): if not isinstance(g, Graph): g = self.get_context(g) diff --git a/rdflib/plugins/memory.py b/rdflib/plugins/memory.py index 085c0d880..cbabad097 100644 --- a/rdflib/plugins/memory.py +++ b/rdflib/plugins/memory.py @@ -368,7 +368,11 @@ def remove_graph(self, graph): Store.remove_graph(self, graph) else: self.remove((None,None,None), graph) - self.__all_contexts.remove(graph) + try: + self.__all_contexts.remove(graph) + except KeyError: + pass # we didn't know this graph, no problem + # internal utility methods below diff --git a/rdflib/plugins/sparql/update.py b/rdflib/plugins/sparql/update.py index a4607d69d..57c5bf461 100644 --- a/rdflib/plugins/sparql/update.py +++ b/rdflib/plugins/sparql/update.py @@ -62,6 +62,16 @@ def evalClear(ctx, u): for g in _graphAll(ctx, u.graphiri): g.remove((None, None, None)) +def evalDrop(ctx, u): + """ + http://www.w3.org/TR/sparql11-update/#drop + """ + if ctx.dataset.store.graph_aware: + for g in _graphAll(ctx, u.graphiri): + ctx.dataset.store.remove_graph(g) + else: + evalClear(ctx, u) + def evalInsertData(ctx, u): """ @@ -214,7 +224,10 @@ def evalMove(ctx, u): dstg += srcg - srcg.remove((None, None, None)) + if ctx.dataset.store.graph_aware: + ctx.dataset.store.remove_graph(srcg) + else: + srcg.remove((None, None, None)) def evalCopy(ctx, u): @@ -277,8 +290,7 @@ def evalUpdate(graph, update, initBindings=None): elif u.name == 'Clear': evalClear(ctx, u) elif u.name == 'Drop': - # rdflib does not record empty graphs, so clear == drop - evalClear(ctx, u) + evalDrop(ctx, u) elif u.name == 'Create': evalCreate(ctx, u) elif u.name == 'Add': diff --git a/test/test_graph.py b/test/test_graph.py index 3dfd4442b..c459694dd 100644 --- a/test/test_graph.py +++ b/test/test_graph.py @@ -178,7 +178,7 @@ def testConnected(self): def testSub(self): g1 = self.graph - g2 = Graph(store=g2.store) + g2 = Graph(store=g1.store) tarek = self.tarek # michel = self.michel