Skip to content

Commit 6c026d0

Browse files
committed
Dataset and graph_aware store fixes
Add graphs when parsing, so also empty graphs are added.
1 parent 5aaa6f4 commit 6c026d0

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

rdflib/graph.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,8 +1308,9 @@ def add(self, triple_or_quad):
13081308

13091309
self.store.add((s, p, o), context=c, quoted=False)
13101310

1311-
def _graph(self, c):
1312-
if not isinstance(c, Graph):
1311+
def _graph(self, c):
1312+
if c is None: return None
1313+
if not isinstance(c, Graph):
13131314
return self.get_context(c)
13141315
else:
13151316
return c
@@ -1348,10 +1349,14 @@ def triples(self, triple_or_quad, context=None):
13481349
s,p,o,c = self._spoc(triple_or_quad)
13491350
context = self._graph(context or c)
13501351

1351-
if not self.default_union and context is None:
1352-
context=self.default_context
1353-
1354-
if isinstance(p, Path):
1352+
if self.default_union:
1353+
if context==self.default_context:
1354+
context = None
1355+
else:
1356+
if context is None:
1357+
context = self.default_context
1358+
1359+
if isinstance(p, Path):
13551360
if context is None:
13561361
context = self
13571362

@@ -1443,7 +1448,7 @@ def parse(self, source=None, publicID=None, format="xml",
14431448
g_id = URIRef(g_id)
14441449

14451450
context = Graph(store=self.store, identifier=g_id)
1446-
context.remove((None, None, None))
1451+
context.remove((None, None, None)) # hmm ?
14471452
context.parse(source, publicID=publicID, format=format,
14481453
location=location, file=file, data=data, **args)
14491454
return context
@@ -1580,17 +1585,21 @@ def graph(self, identifier=None):
15801585
"genid", "http://rdflib.net" + rdflib_skolem_genid,
15811586
override=False)
15821587
identifier = BNode().skolemize()
1583-
else:
1584-
if isinstance(identifier, BNode):
1585-
raise Exception(
1586-
"Blank nodes cannot be Graph identifiers in RDF Datasets")
1587-
if not isinstance(identifier, URIRef):
1588-
identifier = URIRef(identifier)
1589-
1590-
g = self.get_context(identifier)
1588+
1589+
g = self._graph(identifier)
1590+
15911591
self.store.add_graph(g)
15921592
return g
15931593

1594+
def parse(self, source=None, publicID=None, format="xml",
1595+
location=None, file=None, data=None, **args):
1596+
c = ConjunctiveGraph.parse(self, source, publicID, format, location, file, data, **args)
1597+
self.graph(c)
1598+
1599+
def add_graph(self, g):
1600+
"""alias of graph for consistency"""
1601+
return self.graph(g)
1602+
15941603
def remove_graph(self, g):
15951604
if not isinstance(g, Graph):
15961605
g = self.get_context(g)

rdflib/plugins/memory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,11 @@ def remove_graph(self, graph):
368368
Store.remove_graph(self, graph)
369369
else:
370370
self.remove((None,None,None), graph)
371-
self.__all_contexts.remove(graph)
371+
try:
372+
self.__all_contexts.remove(graph)
373+
except KeyError:
374+
pass # we didn't know this graph, no problem
375+
372376

373377

374378
# internal utility methods below

rdflib/plugins/sparql/update.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ def evalClear(ctx, u):
6262
for g in _graphAll(ctx, u.graphiri):
6363
g.remove((None, None, None))
6464

65+
def evalDrop(ctx, u):
66+
"""
67+
http://www.w3.org/TR/sparql11-update/#drop
68+
"""
69+
if ctx.dataset.store.graph_aware:
70+
for g in _graphAll(ctx, u.graphiri):
71+
ctx.dataset.store.remove_graph(g)
72+
else:
73+
evalClear(ctx, u)
74+
6575

6676
def evalInsertData(ctx, u):
6777
"""
@@ -214,7 +224,10 @@ def evalMove(ctx, u):
214224

215225
dstg += srcg
216226

217-
srcg.remove((None, None, None))
227+
if ctx.dataset.store.graph_aware:
228+
ctx.dataset.store.remove_graph(srcg)
229+
else:
230+
srcg.remove((None, None, None))
218231

219232

220233
def evalCopy(ctx, u):
@@ -277,8 +290,7 @@ def evalUpdate(graph, update, initBindings=None):
277290
elif u.name == 'Clear':
278291
evalClear(ctx, u)
279292
elif u.name == 'Drop':
280-
# rdflib does not record empty graphs, so clear == drop
281-
evalClear(ctx, u)
293+
evalDrop(ctx, u)
282294
elif u.name == 'Create':
283295
evalCreate(ctx, u)
284296
elif u.name == 'Add':

test/test_graph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def testConnected(self):
178178

179179
def testSub(self):
180180
g1 = self.graph
181-
g2 = Graph(store=g2.store)
181+
g2 = Graph(store=g1.store)
182182

183183
tarek = self.tarek
184184
# michel = self.michel

0 commit comments

Comments
 (0)