Skip to content

Instantly share code, notes, and snippets.

@tpluscode
Last active December 14, 2021 06:43
Show Gist options
  • Save tpluscode/5edd56925468c778fd59a2ec7e7c2552 to your computer and use it in GitHub Desktop.
Save tpluscode/5edd56925468c778fd59a2ec7e7c2552 to your computer and use it in GitHub Desktop.
Annotating Hydra collection search template with SHACL Advanced Features

This is an idea for how SHACL AF Node Expression can be used to annotate a hydra search template so that values coming from the request query string would be transformed to SPARQL patterns an injeced into the resulting query

The collection resource <shapes> supports a targetNode query string parameter which should be translated to a SPARQL union like below. The $this token will represent each collection member (instance of shape). For example, when requesting shapes?targetNode=foobar, the generated patterns could be:

{
  # query the target node directly
  $this sh:targetNode <foobar>
}
UNION
{
  # or find shapes which target foobar's classes
  $this sh:targetClass/^rdf:type <foobar> .
}

This would be equivalent to this filter shape

[
  sh:filterShape [
    sh:property [
      sh:path [
        sh:union (
          [ sh:path sh:targetNode ]
          [
            sh:nodes [ sh:path sh:targetClass ] ;
            sh:path [ sh:inversePath rdf:type ] ;
          ]
        ) ;
      ] ;
      sh:hasValue <foobar> ;
    ] ;
  ] ;
] .
<shapes> a hydra:Collection ;
hydra:search [
hydra:template "{?targetNode}" ;
hydra:mapping [
hydra:variable "targetNode" ;
hydra:property sh:targetNode ;
# the object of `query:filter` must be a Node Expression
query:filter [
sh:union (
[ sh:path sh:targetNode ]
[
sh:nodes [ sh:path sh:targetClass ] ;
sh:path [ sh:inversePath rdf:type ] ;
]
) ;
] ;
] ;
] ;
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment