Skip to content

Commit

Permalink
update the >>update<< function to reuse BlankNode labels to refer to …
Browse files Browse the repository at this point in the history
…the same BlankNode
  • Loading branch information
Simaris committed Feb 1, 2021
1 parent d5da755 commit 12fc1c8
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 9 deletions.
74 changes: 65 additions & 9 deletions quit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
from pygit2 import GIT_MERGE_ANALYSIS_NORMAL
from pygit2 import GIT_SORT_REVERSE, GIT_RESET_HARD, GIT_STATUS_CURRENT

import rdflib
from rdflib import Graph, ConjunctiveGraph, BNode, Literal, URIRef
import rdflib.plugins.parsers.ntriples as ntriples

import re

from quit.conf import Feature, QuitGraphConfiguration
Expand Down Expand Up @@ -189,7 +192,12 @@ def instance(self, reference, force=False):
for blob in self.getFilesForCommit(commit):
try:
(name, oid) = blob
(f, context) = self.getFileReferenceAndContext(blob, commit)
result = self.getFileReferenceAndContext(blob, commit)
try:
(f, context, nameMap) = result
except ValueError:
print(result)

internal_identifier = context.identifier + '-' + str(oid)

if force or not self.config.hasFeature(Feature.Persistence):
Expand Down Expand Up @@ -330,13 +338,15 @@ def changeset(self, commit):
blob = (entity.name, entity.oid)

try:
f, context = self.getFileReferenceAndContext(blob, commit)
f, context, nameMap = self.getFileReferenceAndContext(blob, commit)
except KeyError:
graph = Graph(identifier=graphUri)
graph.parse(data=entity.content, format='nt')
parserGraph = ntriples.W3CNTriplesParser(ntriples.NTGraphSink(graph))
source = rdflib.parser.create_input_source(data=entity.content)
parserGraph.parse(source.getCharacterStream())

self._blobs.set(
blob, (FileReference(entity.name, entity.content), graph)
blob, (FileReference(entity.name, entity.content), graph, {})
)

private_uri = QUIT["graph-{}".format(entity.oid)]
Expand Down Expand Up @@ -413,16 +423,62 @@ def getFileReferenceAndContext(self, blob, commit):
content = commit.node(path=name).content
graphUri = self._graphconfigs.get(commit.id).getgraphuriforfile(name)
graph = Graph(identifier=URIRef(graphUri))
graph.parse(data=content, format='nt')
quitWorkingData = (FileReference(name, content), graph)
parserGraph = ntriples.W3CNTriplesParser(ntriples.NTGraphSink(graph))
source = rdflib.parser.create_input_source(data=content)
parserGraph.parse(source.getCharacterStream())
nameMap = {v: k for k, v in parserGraph._bnode_ids.items()}
quitWorkingData = (FileReference(name, content), graph, nameMap)
self._blobs.set(blob, quitWorkingData)
return quitWorkingData
return self._blobs.get(blob)


def _replaceLabledBlankNodes(self, parsedQuery, parent_commit_ref):
def replaceBlankNode(parsedQuery, nameMap):
nameMap = {v: k for k, v in nameMap.items()}
for update in parsedQuery:
for graphURI in update['quads']:
new_triples = []
for triple in update['quads'][graphURI]:
new_triple_subj = None
new_triple_obj = None
if isinstance(triple[0], rdflib.BNode):
bNode_key = triple[0].n3()
bNode_key = bNode_key[2:]
if bNode_key in nameMap:
new_triple_subj = nameMap[bNode_key]
else:
new_triple_subj = triple[0]
nameMap[bNode_key] = triple[0]
else:
new_triple_subj = triple[0]
if isinstance(triple[2], rdflib.BNode):
bNode_key = triple[2].n3()
bNode_key = bNode_key[2:]
if bNode_key in nameMap:
new_triple_obj = nameMap[bNode_key]
else:
new_triple_obj = triple[2]
nameMap[bNode_key] = triple[2]
else:
new_triple_obj = triple[2]
new_triples.append((new_triple_subj, triple[1], new_triple_obj))
update['quads'][graphURI] = new_triples

parent_commit = self.repository.revision(parent_commit_ref)
blobs = self.getFilesForCommit(parent_commit)
for blob in blobs:
(name, oid) = blob
if(name == "graph.nt"):
file_reference, context, nameMap = self.getFileReferenceAndContext(blob, parent_commit)
replaceBlankNode(parsedQuery, nameMap)


def applyQueryOnCommit(self, parsedQuery, parent_commit_ref, target_ref, query=None,
default_graph=[], named_graph=[]):
"""Apply an update query on the graph and the git repository."""
graph, commitid = self.instance(parent_commit_ref)
self._replaceLabledBlankNodes(parsedQuery, parent_commit_ref)
resultingChanges, exception = graph.update(parsedQuery)
if exception:
# TODO need to revert or invalidate the graph at this point.
Expand Down Expand Up @@ -494,7 +550,7 @@ def commit(self, graph, delta, message, parent_commit_ref, target_ref, query=Non

# Update Cache and add new contexts to store
blob = fileReference.path, index.stash[fileReference.path][0]
self._blobs.set(blob, (fileReference, graph.store.get_context(identifier)))
self._blobs.set(blob, (fileReference, graph.store.get_context(identifier), {}))
blobs_new.add(blob)
if graphconfig.mode == 'configuration':
index.add('config.ttl', new_config.graphconf.serialize(format='turtle').decode())
Expand Down Expand Up @@ -546,7 +602,7 @@ def _applyKnownGraphs(self, delta, blobs, parent_commit, index):
for blob in blobs:
(fileName, oid) = blob
try:
file_reference, context = self.getFileReferenceAndContext(blob, parent_commit)
file_reference, context, nameMap = self.getFileReferenceAndContext(blob, parent_commit)
for entry in delta:
changeset = entry['delta'].get(context.identifier, None)

Expand All @@ -558,7 +614,7 @@ def _applyKnownGraphs(self, delta, blobs, parent_commit, index):

self._blobs.remove(blob)
blob = fileName, index.stash[file_reference.path][0]
self._blobs.set(blob, (file_reference, context))
self._blobs.set(blob, (file_reference, context, nameMap))
blobs_new.add(blob)
except KeyError:
pass
Expand Down
31 changes: 31 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3841,6 +3841,37 @@ def testDeleteWithWhitespaceFile(self):
with open(path.join(repo.workdir, 'graph.nt'), 'r') as f:
self.assertEqual('\n', f.read())

def testUpdateWithBlankNode(self):
# Prepate a git Repository
graphContent = """<urn:x> <urn:y> <urn:z> .
_:a <urn:pred> _:c .
_:c <urn:pred> _:d .
"""
with TemporaryRepositoryFactory().withGraph("http://example.org/", graphContent) as repo:

# Start Quit
args = quitApp.getDefaults()
args['targetdir'] = repo.workdir
app = create_app(args).test_client()

with open(path.join(repo.workdir, 'graph.nt'), 'r') as f:
self.assertEqual(graphContent, f.read())

# execute Update query
update = 'INSERT DATA { GRAPH <http://aksw.org/> { _:c <urn:pred> _:e .}}'
result = app.post('/sparql',
content_type="application/sparql-update",
data=update)
print(result)

targetContent = """<urn:x> <urn:y> <urn:z> .
_:a <urn:pred> _:c .
_:c <urn:pred> _:d .
_:c <urn:pred> _:e .
"""
with open(path.join(repo.workdir, 'graph.nt'), 'r') as f:
self.assertEqual(targetContent, f.read())


if __name__ == '__main__':
unittest.main()

0 comments on commit 12fc1c8

Please sign in to comment.