Investigate changes in latest shacl generation code #555

Open
opened 2026-06-25 12:26:21 +00:00 by jsheunis · 2 comments
jsheunis commented 2026-06-25 12:26:21 +00:00 (Migrated from github.com)

The latest version of a shacl schema has missing parts of a property shape, see www/pool.psychoinformatics.de-ui#29

Specifically, looking at the pid property shape on any class, it looks like:

  • the sh:datatype entry is gone
  • the sh:minCount entry is gone
  • sh:nodeKind has changed to sh:IRI
The latest version of a shacl schema has missing parts of a property shape, see https://hub.psychoinformatics.de/www/pool.psychoinformatics.de-ui/issues/29 Specifically, looking at the `pid` property shape on any class, it looks like: - the `sh:datatype` entry is gone - the `sh:minCount` entry is gone - `sh:nodeKind` has changed to `sh:IRI`
jsheunis commented 2026-06-26 13:08:01 +00:00 (Migrated from github.com)

I've tested gen-shacl with several different recent versions of linkml locally and can currently confirm the following:

linkml-1.10.0

All parts that we originally expected to be there are still there (makes sense because we previously locked on >=1.10.0)

linkml-1.11.0-rc1

Introduced the changes for sh:nodeKind and sh:datatype; looking at linkml release history it seems like this change was introduced by https://github.com/linkml/linkml/pull/3291. I will still look at this in more depth. A quick note is that our pid slot has a range: uriorcurie and this is not explicitly mentioned in the PR (it rather stems from nodeidentifier type), although it could just be that the changed code also affect uriorcurie. Assuming that PR-introduced change is indeed more SHACL-compliant, we have nothing to do in datalad-concepts and rather need to fix the reported "unknown editor" issue in shacl-vue itself (by updating the URIEditor component matching criteria)

Also see part of the diff:

diff --git a/packages/linkml/src/linkml/generators/shaclgen.py b/packages/linkml/src/linkml/generators/shaclgen.py
index 1885caaa2..5425051e3 100644
--- a/packages/linkml/src/linkml/generators/shaclgen.py
+++ b/packages/linkml/src/linkml/generators/shaclgen.py
@@ -265,11 +319,38 @@ class ShaclGenerator(Generator):
         )
         func(SH["in"], pv_node)
 
+    # Type URIs denoting non-literal (IRI or blank-node) values.
+    # SHACL §4.8.1 <https://www.w3.org/TR/shacl/#NodeKindConstraintComponent>
+    # defines sh:IRI, sh:BlankNode, and sh:BlankNodeOrIRI as valid node kinds.
+    # These URIs map to sh:IRI or sh:BlankNodeOrIRI constraints (never sh:Literal).
+    _NON_LITERAL_TYPE_URIS = frozenset(
+        {
+            "xsd:anyURI",  # uri, uriorcurie → sh:IRI
+            "http://www.w3.org/ns/shex#nonLiteral",  # nodeidentifier → sh:BlankNodeOrIRI
+            "http://www.w3.org/ns/shex#iri",  # future-proofing → sh:IRI
+        }
+    )
+    # IRI-only subset: uri/uriorcurie must be strict IRI references (sh:IRI),
+    # while nodeidentifier (shex:nonLiteral) allows blank nodes too (sh:BlankNodeOrIRI).
+    # See RDF 1.1 §3.2–3.3 <https://www.w3.org/TR/rdf11-concepts/#section-IRIs>.
+    _IRI_ONLY_TYPE_URIS = frozenset(
+        {
+            "xsd:anyURI",
+        }
+    )
+
     def _add_type(self, func: Callable, r: ElementName) -> None:
-        func(SH.nodeKind, SH.Literal)
         sv = self.schemaview
         rt = sv.get_type(r)
-        if rt.uri:
+        type_uri = rt.uri
+        expanded = sv.get_uri(rt, expand=True) if type_uri else None
+        if type_uri and (type_uri in self._NON_LITERAL_TYPE_URIS or expanded in self._NON_LITERAL_TYPE_URIS):
+            if type_uri in self._IRI_ONLY_TYPE_URIS:
+                func(SH.nodeKind, SH.IRI)
+            else:
+                func(SH.nodeKind, SH.BlankNodeOrIRI)
+        elif type_uri:
+            func(SH.nodeKind, SH.Literal)
             func(SH.datatype, URIRef(sv.get_uri(rt, expand=True)))
             if rt.pattern:
                 func(SH.pattern, Literal(rt.pattern))

linkml-1.11.0-rc2: introduced the sh:minCount change

Specific PR: https://github.com/linkml/linkml/pull/3348

Diff:

git diff v1.11.0-rc1 v1.11.0-rc2 -- packages/linkml/src/linkml/generators/shaclgen.py
diff --git a/packages/linkml/src/linkml/generators/shaclgen.py b/packages/linkml/src/linkml/generators/shaclgen.py
index 5425051e3..2f2e6b5d1 100644
--- a/packages/linkml/src/linkml/generators/shaclgen.py
+++ b/packages/linkml/src/linkml/generators/shaclgen.py
@@ -173,7 +173,10 @@ class ShaclGenerator(Generator):
                     prop_pv_literal(SH.minCount, s.minimum_cardinality)
                 elif s.exact_cardinality:
                     prop_pv_literal(SH.minCount, s.exact_cardinality)
-                elif s.required:
+                # Identifiers map to the node's IRI rather than a property triple,
+                # so there's no arc to constrain with sh:minCount 1 — emitting it
+                # would cause spurious violations on every instance.
+                elif s.required and not s.identifier:
                     prop_pv_literal(SH.minCount, 1)
                 # maxCount
                 if s.maximum_cardinality:

I am not sure yet what to make of this, needs more investigation.

I've tested `gen-shacl` with several different recent versions of linkml locally and can currently confirm the following: ## linkml-1.10.0 All parts that we originally expected to be there are still there (makes sense because we previously locked on `>=1.10.0`) ## linkml-1.11.0-rc1 Introduced the changes for `sh:nodeKind` and `sh:datatype`; looking at [linkml release history](https://github.com/linkml/linkml/releases) it seems like this change was introduced by https://github.com/linkml/linkml/pull/3291. I will still look at this in more depth. A quick note is that our `pid` slot has a `range: uriorcurie` and this is not explicitly mentioned in the PR (it rather stems from [`nodeidentifier`](https://linkml.io/linkml-model/latest/docs/Nodeidentifier/) type), although it could just be that the changed code also affect `uriorcurie`. Assuming that PR-introduced change is indeed more SHACL-compliant, we have nothing to do in `datalad-concepts` and rather need to fix the reported "unknown editor" issue in `shacl-vue` itself (by updating the `URIEditor` component matching criteria) Also see part of the diff: ```diff diff --git a/packages/linkml/src/linkml/generators/shaclgen.py b/packages/linkml/src/linkml/generators/shaclgen.py index 1885caaa2..5425051e3 100644 --- a/packages/linkml/src/linkml/generators/shaclgen.py +++ b/packages/linkml/src/linkml/generators/shaclgen.py @@ -265,11 +319,38 @@ class ShaclGenerator(Generator): ) func(SH["in"], pv_node) + # Type URIs denoting non-literal (IRI or blank-node) values. + # SHACL §4.8.1 <https://www.w3.org/TR/shacl/#NodeKindConstraintComponent> + # defines sh:IRI, sh:BlankNode, and sh:BlankNodeOrIRI as valid node kinds. + # These URIs map to sh:IRI or sh:BlankNodeOrIRI constraints (never sh:Literal). + _NON_LITERAL_TYPE_URIS = frozenset( + { + "xsd:anyURI", # uri, uriorcurie → sh:IRI + "http://www.w3.org/ns/shex#nonLiteral", # nodeidentifier → sh:BlankNodeOrIRI + "http://www.w3.org/ns/shex#iri", # future-proofing → sh:IRI + } + ) + # IRI-only subset: uri/uriorcurie must be strict IRI references (sh:IRI), + # while nodeidentifier (shex:nonLiteral) allows blank nodes too (sh:BlankNodeOrIRI). + # See RDF 1.1 §3.2–3.3 <https://www.w3.org/TR/rdf11-concepts/#section-IRIs>. + _IRI_ONLY_TYPE_URIS = frozenset( + { + "xsd:anyURI", + } + ) + def _add_type(self, func: Callable, r: ElementName) -> None: - func(SH.nodeKind, SH.Literal) sv = self.schemaview rt = sv.get_type(r) - if rt.uri: + type_uri = rt.uri + expanded = sv.get_uri(rt, expand=True) if type_uri else None + if type_uri and (type_uri in self._NON_LITERAL_TYPE_URIS or expanded in self._NON_LITERAL_TYPE_URIS): + if type_uri in self._IRI_ONLY_TYPE_URIS: + func(SH.nodeKind, SH.IRI) + else: + func(SH.nodeKind, SH.BlankNodeOrIRI) + elif type_uri: + func(SH.nodeKind, SH.Literal) func(SH.datatype, URIRef(sv.get_uri(rt, expand=True))) if rt.pattern: func(SH.pattern, Literal(rt.pattern)) ``` ## linkml-1.11.0-rc2: introduced the `sh:minCount` change Specific PR: https://github.com/linkml/linkml/pull/3348 Diff: ```diff git diff v1.11.0-rc1 v1.11.0-rc2 -- packages/linkml/src/linkml/generators/shaclgen.py diff --git a/packages/linkml/src/linkml/generators/shaclgen.py b/packages/linkml/src/linkml/generators/shaclgen.py index 5425051e3..2f2e6b5d1 100644 --- a/packages/linkml/src/linkml/generators/shaclgen.py +++ b/packages/linkml/src/linkml/generators/shaclgen.py @@ -173,7 +173,10 @@ class ShaclGenerator(Generator): prop_pv_literal(SH.minCount, s.minimum_cardinality) elif s.exact_cardinality: prop_pv_literal(SH.minCount, s.exact_cardinality) - elif s.required: + # Identifiers map to the node's IRI rather than a property triple, + # so there's no arc to constrain with sh:minCount 1 — emitting it + # would cause spurious violations on every instance. + elif s.required and not s.identifier: prop_pv_literal(SH.minCount, 1) # maxCount if s.maximum_cardinality: ``` I am not sure yet what to make of this, needs more investigation.
jsheunis commented 2026-06-26 14:17:15 +00:00 (Migrated from github.com)

I am not sure that agree with the reasoning at github.com/linkml/linkml@e29f2970b1/packages/linkml/src/linkml/generators/shaclgen.py (L206-L209), for our context, i.e.:

 # Identifiers map to the node's IRI rather than a property triple,
# so there's no arc to constrain with sh:minCount 1 — emitting it
# would cause spurious violations on every instance.
elif s.required and not s.identifier:

Why exactly should it be prevented to also have an explicit property shape for the slot that has identifier: true on a class? I don't know exactly what is meant by "[it] would cause spurious violations on every instance".

It could be a possibility that we make the pid slot required explicitly in our schema:

diff --git a/src/things/v2.yaml b/src/things/v2.yaml
index 61339e1..1d301f0 100644
--- a/src/things/v2.yaml
+++ b/src/things/v2.yaml
@@ -381,6 +381,7 @@ slots:

   pid:
     identifier: true
+    required: true
     title: Persistent identifier
     description: >-
       Persistent and globally unique identifier of a `Thing`.

I tested this locally and it brings back the sh:minCount. But do we specifically want this. I am not sure if we excluded this by design or implicitly knew that identifier: true also made it required which then mapped to sh:minCount: 1 in SHACL (which is not anymore the case in linkml-v1.11.0-rc2 and later)

Interrupting my own thought process here: while I was busy trying to change the shaclgen code to include/exclude the and not s.identifier part, in order to compare the outputs when running it locally, I saw that our shaclgen patches, or parts thereof, are rejected. Specifically, the following is rejected:

--- shaclgen.py
+++ shaclgen.py
@@ -57,6 +61,8 @@ class ShaclGenerator(Generator):
     def serialize(self, **args) -> str:
         g = self.as_graph()
         data = g.serialize(format="turtle" if self.format in ["owl", "ttl"] else self.format)
+        if self.include_stripped_prefixes:
+            data = self._add_stripped_prefixes(data)
         return data
 
     def as_graph(self) -> Graph:
@@ -118,8 +124,6 @@ class ShaclGenerator(Generator):
                         g.add((pnode, p, Literal(v)))
 
                 prop_pv(SH.path, slot_uri)
-                prop_pv_literal(SH.order, order)
-                order += 1
                 prop_pv_literal(SH.name, s.title)
                 prop_pv_literal(SH.description, s.description)
                 # minCount

This is likely because of substantial changes in the linkml source in latest vs when the patches were created. e.g. the serialize part in the source is now:

    def serialize(self, **args) -> str:
        g = self.as_graph()
        fmt = "turtle" if self.format in ["owl", "ttl"] else self.format
        return canonicalize_rdf_graph(g, output_format=fmt)

This means: need to update patches... (there will likely be some fallout because of the prefixes not being included in shacl-vue anymore)

Interrupting my own thought process AGAIN:

Exporting shacl with the latest state of linkml main rendered exported SHACL in the following format:


xyzri:ORCID a sh:NodeShape ;
	rdfs:comment "Unique persistent identifier for a person, provided by the Open Researcher and Contributor ID (ORCID) organisation." ;
	sh:closed true ;
	sh:ignoredProperties _:c14n1407 ;
	sh:property _:c14n1213 , _:c14n1258 , _:c14n1300 , _:c14n1317 , _:c14n1351 , _:c14n1597 , _:c14n1622 , _:c14n1696 , _:c14n1749 , _:c14n241 , _:c14n464 , _:c14n466 , _:c14n487 , _:c14n540 , _:c14n643 , _:c14n683 , _:c14n692 , _:c14n741 ;
	sh:targetClass xyzri:ORCID .

For v1.11.1 it is in the format:

xyzri:ORCID a sh:NodeShape ;
    rdfs:comment "Unique persistent identifier for a person, provided by the Open Researcher and Contributor ID (ORCID) organisation." ;
    sh:closed true ;
    sh:ignoredProperties ( rdf:type ) ;
    sh:property [ sh:class dlthings:Thing ;
            sh:description "An agent responsible for making an entity." ;
            sh:maxCount 1 ;
            sh:name "Creator" ;
            sh:nodeKind sh:IRI ;
            sh:path dcterms:creator ],
        [ dash:singleLine false ;
            sh:datatype xsd:string ;
            sh:description "Note that can be shown when the record is displayed as an item. This is typically longer than a label." ;
            sh:maxCount 1 ;
            sh:name "Record display note" ;
            sh:nodeKind sh:Literal ;
            sh:path skos:note ],
        [ sh:description "A list of terms from different schemas or terminology systems that have related meaning." ;
            sh:name "Related mappings" ;
            sh:nodeKind sh:IRI ;
            sh:path skos:relatedMatch ],

These are both valid TTL, but I am not sure if shacl-tulip will actually correctly read the former. It could be related to something that was a TODO since the beginning: orinoco/shacl-vue#7. Hopefully it just works, but better to check before the next linkml release and our subsequent dependency update.

I am not sure that agree with the reasoning at https://github.com/linkml/linkml/blob/e29f2970b12d6d7c9aaee6a7406616c7f2897cf1/packages/linkml/src/linkml/generators/shaclgen.py#L206-L209, for our context, i.e.: ```python # Identifiers map to the node's IRI rather than a property triple, # so there's no arc to constrain with sh:minCount 1 — emitting it # would cause spurious violations on every instance. elif s.required and not s.identifier: ``` Why exactly should it be prevented to also have an explicit property shape for the slot that has `identifier: true` on a class? I don't know exactly what is meant by _"[it] would cause spurious violations on every instance"_. It could be a possibility that we make the `pid` slot required explicitly in our schema: ```diff diff --git a/src/things/v2.yaml b/src/things/v2.yaml index 61339e1..1d301f0 100644 --- a/src/things/v2.yaml +++ b/src/things/v2.yaml @@ -381,6 +381,7 @@ slots: pid: identifier: true + required: true title: Persistent identifier description: >- Persistent and globally unique identifier of a `Thing`. ``` I tested this locally and it brings back the `sh:minCount`. But do we specifically want this. I am not sure if we excluded this by design or implicitly knew that `identifier: true` also made it required which then mapped to `sh:minCount: 1` in SHACL (which is not anymore the case in linkml-v1.11.0-rc2 and later) _Interrupting my own thought process here:_ while I was busy trying to change the shaclgen code to include/exclude the `and not s.identifier` part, in order to compare the outputs when running it locally, I saw that our shaclgen patches, or parts thereof, are rejected. Specifically, the following is rejected: ```diff --- shaclgen.py +++ shaclgen.py @@ -57,6 +61,8 @@ class ShaclGenerator(Generator): def serialize(self, **args) -> str: g = self.as_graph() data = g.serialize(format="turtle" if self.format in ["owl", "ttl"] else self.format) + if self.include_stripped_prefixes: + data = self._add_stripped_prefixes(data) return data def as_graph(self) -> Graph: @@ -118,8 +124,6 @@ class ShaclGenerator(Generator): g.add((pnode, p, Literal(v))) prop_pv(SH.path, slot_uri) - prop_pv_literal(SH.order, order) - order += 1 prop_pv_literal(SH.name, s.title) prop_pv_literal(SH.description, s.description) # minCount ``` This is likely because of substantial changes in the linkml source in latest vs when the patches were created. e.g. the serialize part in the source is now: ```python def serialize(self, **args) -> str: g = self.as_graph() fmt = "turtle" if self.format in ["owl", "ttl"] else self.format return canonicalize_rdf_graph(g, output_format=fmt) ``` This means: need to update patches... (there will likely be some fallout because of the prefixes not being included in shacl-vue anymore) _Interrupting my own thought process AGAIN_: Exporting shacl with the latest state of linkml `main` rendered exported SHACL in the following format: ```turtle xyzri:ORCID a sh:NodeShape ; rdfs:comment "Unique persistent identifier for a person, provided by the Open Researcher and Contributor ID (ORCID) organisation." ; sh:closed true ; sh:ignoredProperties _:c14n1407 ; sh:property _:c14n1213 , _:c14n1258 , _:c14n1300 , _:c14n1317 , _:c14n1351 , _:c14n1597 , _:c14n1622 , _:c14n1696 , _:c14n1749 , _:c14n241 , _:c14n464 , _:c14n466 , _:c14n487 , _:c14n540 , _:c14n643 , _:c14n683 , _:c14n692 , _:c14n741 ; sh:targetClass xyzri:ORCID . ``` For v1.11.1 it is in the format: ```turtle xyzri:ORCID a sh:NodeShape ; rdfs:comment "Unique persistent identifier for a person, provided by the Open Researcher and Contributor ID (ORCID) organisation." ; sh:closed true ; sh:ignoredProperties ( rdf:type ) ; sh:property [ sh:class dlthings:Thing ; sh:description "An agent responsible for making an entity." ; sh:maxCount 1 ; sh:name "Creator" ; sh:nodeKind sh:IRI ; sh:path dcterms:creator ], [ dash:singleLine false ; sh:datatype xsd:string ; sh:description "Note that can be shown when the record is displayed as an item. This is typically longer than a label." ; sh:maxCount 1 ; sh:name "Record display note" ; sh:nodeKind sh:Literal ; sh:path skos:note ], [ sh:description "A list of terms from different schemas or terminology systems that have related meaning." ; sh:name "Related mappings" ; sh:nodeKind sh:IRI ; sh:path skos:relatedMatch ], ``` These are both valid TTL, but I am not sure if `shacl-tulip` will actually correctly read the former. It could be related to something that was a TODO since the beginning: https://hub.psychoinformatics.de/orinoco/shacl-vue/issues/7. Hopefully it just works, but better to check before the next linkml release and our subsequent dependency update.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
orinoco/concepts#555
No description provided.