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

@jdsika jdsika commented Apr 27, 2026

Copy link
Copy Markdown

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 ^Gdvanced=True\

How was this tested?

  • 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 and stack

Refs: linkml#2464

This PR is the base of a stacked series: #19 (presence-implies-value) → #20 (compositional fallback M1–M5) → #21 (hardening) → #22 (audit fixes). Integration branch: #18.

Areas of uncertainty

Checklist

  • My code follows the contributor guidelines
  • I have added tests that prove my fix/feature works
  • Existing tests pass locally with my changes

AI Assistance

If you used AI tools while preparing this PR, you are still the author and responsible for understanding, verifying, and defending your submission. Please engage with reviewers personally rather than through your agent during feedback and revisions. See our AI Covenant for details.

@jdsika jdsika force-pushed the feat/shaclgen-rules-sparql branch 2 times, most recently from df28d85 to b376e52 Compare May 12, 2026 10:49
@rmessaou rmessaou force-pushed the feat/shaclgen-rules-sparql branch 2 times, most recently from 0e10b85 to b576ecf Compare July 8, 2026 08:34
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>
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