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:pathsh:inversePathsh:zeroOrMorePath,sh:oneOrMorePath,sh:zeroOrOnePathsh: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 -e .
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file shacl_path_query_generator-0.1.0.tar.gz.
File metadata
- Download URL: shacl_path_query_generator-0.1.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7bfdb1f67d9dd633035e36eeeaf6274594b6404a99670ad044e6f65a7f446ff8
|
|
| MD5 |
c017f539edcc00cd65a29676dd424166
|
|
| BLAKE2b-256 |
ed704ec7c1cfe70863b5ce0ebc9b3c5e0c0951ffc67f515fa84b0dab203db0df
|
File details
Details for the file shacl_path_query_generator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: shacl_path_query_generator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a9af940a6cd95d5ef61da35b82cb79af09c0eb3d46311ac334a632024dca3e2
|
|
| MD5 |
dd70dd0d7fcdf7f6a1c68f25f9931a9a
|
|
| BLAKE2b-256 |
1ab2e05f353d668b8fdcb603fa8226695301af5c5fb7f0d779efec36d03dcb2f
|