Skip to content

Commit

Permalink
Dataset and graph_aware store fixes
Browse files Browse the repository at this point in the history
Add graphs when parsing, so also empty graphs are added.
  • Loading branch information
gromgull committed Jul 29, 2013
1 parent 5aaa6f4 commit 6c026d0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
39 changes: 24 additions & 15 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion rdflib/plugins/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions rdflib/plugins/sparql/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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':
Expand Down
2 changes: 1 addition & 1 deletion test/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6c026d0

Please sign in to comment.