Skip to main content

Translate SHACL complex paths into SPARQL CONSTRUCT queries with intermediate variable expansion.

Project description

[2025-07-30 16:42+0200]

Here is your README.md tailored to your SHACL path parser and flattener package:


SHACL Path Query Generator

Parse and flatten SHACL path expressions into executable SPARQL triple patterns.

Features

  • ✅ Parses all standard SHACL path constructs:
    • sh:path
    • sh:inversePath
    • sh:zeroOrMorePath, sh:oneOrMorePath, sh:zeroOrOnePath
    • sh:alternativePath
    • sequences (rdf:List)
  • ✅ Converts parsed paths into a list of triple patterns with fresh variables.
  • ✅ Allows optional unrolling depth for repetitive paths (*, +).
  • 🧪 Fully tested with nested and edge-case paths.
  • 🧩 Outputs raw SPARQL triple patterns, ready for flexible usage (e.g. WHERE, CONSTRUCT, INSERT, etc.).

Installation

pip install shacl-path-query-generator

Requires: rdflib>=7.1.4

Example Usage

from rdflib import Graph, Namespace
from shacl_path_query import path_to_sparql_pattern

EX = Namespace("http://ex.org/")
SH = Namespace("http://www.w3.org/ns/shacl#")

turtle = """
PREFIX ex: <http://ex.org/>
PREFIX sh: <http://www.w3.org/ns/shacl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

[] a sh:NodeShape ;
   sh:targetClass ex:Thing ;
   sh:property [
     sh:path (
       ex:p1
       [ sh:inversePath ex:p2 ]
     ) ;
     sh:datatype xsd:string ;
   ] .
"""

g = Graph()
g.parse(data=turtle, format="turtle")

# Get the path node (blank node for the list)
path_node = g.value(predicate=SH.path)
pattern = path_to_sparql_pattern(path_node, g)
print(pattern)

Sample Output

?s <http://ex.org/p1> ?v0 .
?o <http://ex.org/p2> ?v0 .

Example in a SPARQL CONSTRUCT

CONSTRUCT {
  ?s <http://ex.org/p1> ?v0 .
  ?o <http://ex.org/p2> ?v0 .
}
WHERE {
  VALUES ?s { <http://ex.org/a> <http://ex.org/b> }
  ?s <http://ex.org/p1> ?v0 .
  ?o <http://ex.org/p2> ?v0 .
}

License

MIT


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shacl_path_query_generator-0.1.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

shacl_path_query_generator-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file shacl_path_query_generator-0.1.1.tar.gz.

File metadata

File hashes

Hashes for shacl_path_query_generator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 01e0be341a3d230224b019ef718942210f50f0cee9670f8c7326236ce2d80f17
MD5 62bb3486c9d9ce27593a931f906e7b0e
BLAKE2b-256 de319568fabb9ae2b31bcb32ec2b10bf4f7a3ca86c79e4ec1fc0315d33492963

See more details on using hashes here.

File details

Details for the file shacl_path_query_generator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for shacl_path_query_generator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc1448dd048d1f6bc1756af0e3c0a5e68bb78193a4f77c4156c55b8c7bde53bb
MD5 1542831cfd3705a1dbbd29c12f038f9c
BLAKE2b-256 442eaa0f9770fcb6ff995fee2781fab74fd833865db73ee2edcbbf33805095d9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page