Skip to content

Instantly share code, notes, and snippets.

@jindrichmynarz
Created October 5, 2016 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jindrichmynarz/25df5bf262530806bc7b98fa688f70df to your computer and use it in GitHub Desktop.
Save jindrichmynarz/25df5bf262530806bc7b98fa688f70df to your computer and use it in GitHub Desktop.
Blank nodes to hash-based IRIs
DELETE {
?bnode ?outP ?outO .
?inS ?inP ?bnode .
}
INSERT {
?iri ?outP ?outO .
?inS ?inP ?iri .
}
WHERE {
{
SELECT ?bnode ?iri
WHERE {
{
SELECT ?bnode
(SAMPLE(?_class) AS ?class)
(SHA1(CONCAT(GROUP_CONCAT(STR(?p); separator = ""),
GROUP_CONCAT(STR(?o); separator = ""))) AS ?hash)
WHERE {
?bnode ?p ?o .
FILTER isBlank(?bnode)
OPTIONAL {
?bnode a ?_class .
}
}
GROUP BY ?bnode
}
# Your base namespace for resources:
BIND ("http://linked.opendata.cz/resource/" AS ?ns)
# Extract the local name of the resource's class and convert it to kebab-case.
BIND (LCASE(REPLACE(COALESCE(REPLACE(STR(?class), "^.*[/#]([^/#]+)$", "$1"), "thing"),
"(\\p{Ll})(\\p{Lu})", "$1-$2"))
AS ?classLocalName)
# The newly minted hash-based IRIs follow the pattern {namespace}/{class name}/{hash}.
BIND (IRI(CONCAT(?ns, ?classLocalName, "/", ?hash)) AS ?iri)
}
}
?bnode ?outP ?outO .
OPTIONAL {
?inS ?inP ?bnode .
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment