Skip to content

Commit

Permalink
Merge pull request #245 from DerwenAI/storage_plugin
Browse files Browse the repository at this point in the history
unbinding a SPARQL query, to help troubleshoot integration with Oxrdflib
  • Loading branch information
ceteri committed Mar 23, 2022
2 parents 3c66540 + 71db6f6 commit 274064e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
11 changes: 6 additions & 5 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# `kglab` changelog

## 0.5.?
## 0.6.?

2022-03-??
2022-??-??

* ???
* support for substituting the binding variables into a SPARQL query
* add a NumPy-backed `RDFlib.Store` plugin


## 0.5.3
Expand All @@ -20,7 +21,7 @@

* bump up versions of dependencies to fit Py 3.7+
* improve testing based on pytest; kudos @Mec-iS
* allow configuration for rdflib.Store plugins
* allow configuration for `RDFlib.Store` plugins


## 0.5.1
Expand Down Expand Up @@ -190,7 +191,7 @@
2020-12-06

* added `pySHACL` examples
* resolved Google Colab errors registering the `rdflib-jsonld` plugin
* resolved Google Colab errors registering the `RDFlib-jsonld` plugin
* comparing timing info across serialization methods
* simplified the example notebook naming convention

Expand Down
34 changes: 34 additions & 0 deletions kglab/kglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io
import json
import pathlib
import re
import traceback
import typing

Expand Down Expand Up @@ -1180,6 +1181,39 @@ def n3fy_row (
return bindings


@classmethod
def unbind_sparql (
cls,
sparql: str,
bindings: dict,
*,
preamble: str = "",
) -> str:
"""
Substitute the _binding variables_ into the text of a SPARQL query,
to obviate the need for binding variables specified separately.
This can be helpful for debugging, or for some query engines that
may not have full SPARQL support yet.
sparql:
text for the SPARQL query
bindings:
variable bindings
returns:
a string of the expanded SPARQL query
"""
sparql_meta, sparql_body = re.split(r"\s*WHERE\s*\{", sparql, maxsplit=1)

for var in sorted(bindings.keys(), key=lambda x: len(x), reverse=True): # pylint: disable=W0108
pattern = re.compile("(\?" + var + ")(\W)") # pylint: disable=W1401
bind_val = "<" + str(bindings[var]) + ">\\2"
sparql_body = re.sub(pattern, bind_val, sparql_body)

return "".join([preamble, sparql_meta, " WHERE {", sparql_body]).strip()


######################################################################
## SHACL validation

Expand Down

0 comments on commit 274064e

Please sign in to comment.