Skip to content

feat(gen-shacl): generate sh:sparql constraints from LinkML rules#11

Open
jdsika wants to merge 1 commit into
mainfrom
feat/shaclgen-rules-sparql
Open

feat(gen-shacl): generate sh:sparql constraints from LinkML rules#11
jdsika wants to merge 1 commit into
mainfrom
feat/shaclgen-rules-sparql

Conversation

@jdsika
Copy link
Copy Markdown

@jdsika jdsika commented Apr 27, 2026

Summary

Implement SHACL-SPARQL constraint generation for the boolean-guard pattern commonly used in conditional validation rules. When a LinkML class has
ules:\ blocks with preconditions (\�alue_presence: PRESENT) and postconditions (\�quals_string: true), the generator now emits \sh:SPARQLConstraint\ nodes on the corresponding \sh:NodeShape.

Features

  • New _add_rules()\ method translates recognised rule patterns to SPARQL
  • Boolean-guard pattern: if value present then flag must be true
  • Rule \description\ mapped to \sh:message\ on the constraint
  • Deactivated rules are skipped
  • Warnings emitted for \�idirectional/\open_world\ rule flags
  • New --emit-rules/--no-emit-rules\ CLI flag (default: enabled)
  • Full URI references in SPARQL (no PREFIX declarations needed)

Standards Compliance

  • W3C SHACL Section 5 (SPARQL-based Constraints)
  • Pre-bound \\ variable per Section 5.3.1
  • Validated by pyshacl with \�dvanced=True\

Tests

  • 8 new tests in \ est_shaclgen.py\ covering:
    • Boolean-guard pattern generation
    • \sh:message\ from rule description
    • Correct slot URI resolution
    • Deactivated rule skipping
    • Unsupported pattern handling
    • --no-emit-rules\ suppression
    • No-rules class (no-op)
    • Multiple rules per class

Related Issues

Refs: linkml#2464

@jdsika jdsika force-pushed the feat/shaclgen-rules-sparql branch from 2d46661 to df28d85 Compare May 12, 2026 08:36
Implement SHACL-SPARQL constraint generation for the boolean-guard
pattern commonly used in conditional validation rules. When a LinkML
class has rules: blocks with preconditions (value_presence: PRESENT)
and postconditions (equals_string: true), the generator now emits
sh:SPARQLConstraint nodes on the corresponding sh:NodeShape.

Features:
- New _add_rules() method translates recognised rule patterns to SPARQL
- Boolean-guard pattern: if value present then flag must be true
- Rule description mapped to sh:message on the constraint
- Deactivated rules are skipped
- Warnings emitted for bidirectional/open_world rule flags
- New --emit-rules/--no-emit-rules CLI flag (default: enabled)
- Full URI references in SPARQL (no PREFIX declarations needed)

The generated SPARQL follows W3C SHACL Section 5 and uses the pre-bound
\ variable per Section 5.3.1. Constraints are validated by pyshacl
with advanced=True.

Refs: linkml#2464
Signed-off-by: Carlo van Driesten <carlo.van-driesten@bmw.de>
@jdsika jdsika force-pushed the feat/shaclgen-rules-sparql branch from df28d85 to b376e52 Compare May 12, 2026 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant