-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shaclgen
: Add --include-annotations
option to let annotations be part of shacl shapes
#2111
Conversation
This is for the SHACL generator in response to linkml#1618. Code is added to shaclgen.py to: - allow users to specify the --include-annotations tag if they want annotations (on classes, slots, and types) to be included in the exported SHACL shapes - determine the datatype of both annotation tag and value (a CURIE is identified by searching for the ':' character) - add the correct triples to the shacl output (to a nodeshape for classes, and to a property shape for slots and slots with typesas ranges)
…hema for shaclgen annotation tests
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2111 +/- ##
==========================================
- Coverage 79.86% 79.86% -0.01%
==========================================
Files 110 110
Lines 12318 12355 +37
Branches 3507 3520 +13
==========================================
+ Hits 9838 9867 +29
- Misses 1884 1889 +5
- Partials 596 599 +3 ☔ View full report in Codecov by Sentry. |
else: | ||
logging.error(f"No URI for type {rt.name}") | ||
|
||
def _add_annotations(self, func: Callable, item) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be moved into schemaview (but doesn't need to happen for this PR)
Add a TODO comment
Regarding:
I just came across a relevant example. Among other annotations, I want to assign
the output from [ sh:datatype xsd:string ;
sh:description "Distance of a thing from source location." ;
sh:group mycurie:BasicPropertyGroup ;
sh:nodeKind sh:Literal ;
sh:order 0,
27 ;
sh:path mycurie:distance ], i.e. the linkml/linkml/generators/shaclgen.py Lines 106 to 107 in a14e6ee
So, for my use case, I would want my annotation to overwrite whatever the code does internally. I have solved this locally with the following patch: diff --git a/linkml/generators/shaclgen.py b/linkml/generators/shaclgen.py
index dc804cb5..729d80f6 100644
--- a/linkml/generators/shaclgen.py
+++ b/linkml/generators/shaclgen.py
@@ -109,8 +109,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)
if not s.multivalued:
@@ -185,6 +183,10 @@ class ShaclGenerator(Generator):
if s.annotations and self.include_annotations:
self._add_annotations(prop_pv, s)
+ if (pnode, SH.order, None) not in g:
+ prop_pv_literal(SH.order, order)
+ order += 1
+
ifabsent_processor.process_slot(prop_pv, s, class_uri)
return g |
Thank you! yes, a limit of the current rdf testing is that it's incomplete when blank nodes are present. Override is the correct behavior in the case of clashes. We'll want to implement this same behavior in other rdf-serializing generators. It would be good to extend |
Does it make sense to make the
I can take a shot at this in a next PR 👍 |
…part of shacl shapes (linkml#2111) * Add --include-annotations option for shaclgen This is for the SHACL generator in response to linkml#1618. Code is added to shaclgen.py to: - allow users to specify the --include-annotations tag if they want annotations (on classes, slots, and types) to be included in the exported SHACL shapes - determine the datatype of both annotation tag and value (a CURIE is identified by searching for the ':' character) - add the correct triples to the shacl output (to a nodeshape for classes, and to a property shape for slots and slots with typesas ranges) * fix linting * Update snapshot data in 'test_scripts' after updating kitchen sink schema for shaclgen annotation tests * Update shaclgen.py Add a TODO comment --------- Co-authored-by: Chris Mungall <[email protected]>
* Implement equals_string and equals_string_in * Remove renaming§ * Add validation rules * Add validation for equals_string and equals_string_in in schema loader * Revert renaming * Remove obsolete code * Remove obsolete code * Fix codespell errors * Resolve flake errors * Reforamt files * Fix lint errors * fix lint errors * Add unit tests for equals_string and equals_string_in * Make quality checks happy (#2136) * Update poetry lockfile * hotwo on deprecation * `shaclgen`: Add `--include-annotations` option to let annotations be part of shacl shapes (#2111) * Add --include-annotations option for shaclgen This is for the SHACL generator in response to #1618. Code is added to shaclgen.py to: - allow users to specify the --include-annotations tag if they want annotations (on classes, slots, and types) to be included in the exported SHACL shapes - determine the datatype of both annotation tag and value (a CURIE is identified by searching for the ':' character) - add the correct triples to the shacl output (to a nodeshape for classes, and to a property shape for slots and slots with typesas ranges) * fix linting * Update snapshot data in 'test_scripts' after updating kitchen sink schema for shaclgen annotation tests * Update shaclgen.py Add a TODO comment --------- Co-authored-by: Chris Mungall <[email protected]> * Erdiagram include upstream (#2139) * Include upstream classes into ERD diagram of selected entitites Add docs for —include-upstream * Fix unit test for Py3.9 * Update poetry lockfile * Implement equals_string and equals_string_in * Resolve flake errors * fix lint errors * Fix tests for equals_string_in feature Signed-off-by: Vincent Kelleher <[email protected]> * Fix gen shacl test * Fix unit tests * Reformat code * Fix missing type * Reformt * Fix lint errors * Fix lint errors * Fix unti tests * Format imports; ensure that tox and pre-commit agree on a ruff version --------- Signed-off-by: Vincent Kelleher <[email protected]> Co-authored-by: Anja Strunk <[email protected]> Co-authored-by: Vlad Korolev <[email protected]> Co-authored-by: cmungall <[email protected]> Co-authored-by: Sierra Taylor Moxon <[email protected]> Co-authored-by: Stephan Heunis <[email protected]> Co-authored-by: Chris Mungall <[email protected]> Co-authored-by: Vincent Kelleher <[email protected]> Co-authored-by: anjastrunk <[email protected]>
* Implement equals_string and equals_string_in * Remove renaming§ * Add validation rules * Add validation for equals_string and equals_string_in in schema loader * Revert renaming * Remove obsolete code * Remove obsolete code * Fix codespell errors * Resolve flake errors * Reforamt files * Fix lint errors * fix lint errors * Add unit tests for equals_string and equals_string_in * Make quality checks happy (linkml#2136) * Update poetry lockfile * hotwo on deprecation * `shaclgen`: Add `--include-annotations` option to let annotations be part of shacl shapes (linkml#2111) * Add --include-annotations option for shaclgen This is for the SHACL generator in response to linkml#1618. Code is added to shaclgen.py to: - allow users to specify the --include-annotations tag if they want annotations (on classes, slots, and types) to be included in the exported SHACL shapes - determine the datatype of both annotation tag and value (a CURIE is identified by searching for the ':' character) - add the correct triples to the shacl output (to a nodeshape for classes, and to a property shape for slots and slots with typesas ranges) * fix linting * Update snapshot data in 'test_scripts' after updating kitchen sink schema for shaclgen annotation tests * Update shaclgen.py Add a TODO comment --------- Co-authored-by: Chris Mungall <[email protected]> * Erdiagram include upstream (linkml#2139) * Include upstream classes into ERD diagram of selected entitites Add docs for —include-upstream * Fix unit test for Py3.9 * Update poetry lockfile * Implement equals_string and equals_string_in * Resolve flake errors * fix lint errors * Fix tests for equals_string_in feature Signed-off-by: Vincent Kelleher <[email protected]> * Fix gen shacl test * Fix unit tests * Reformat code * Fix missing type * Reformt * Fix lint errors * Fix lint errors * Fix unti tests * Format imports; ensure that tox and pre-commit agree on a ruff version --------- Signed-off-by: Vincent Kelleher <[email protected]> Co-authored-by: Anja Strunk <[email protected]> Co-authored-by: Vlad Korolev <[email protected]> Co-authored-by: cmungall <[email protected]> Co-authored-by: Sierra Taylor Moxon <[email protected]> Co-authored-by: Stephan Heunis <[email protected]> Co-authored-by: Chris Mungall <[email protected]> Co-authored-by: Vincent Kelleher <[email protected]> Co-authored-by: anjastrunk <[email protected]>
This is for the SHACL generator in response to #1618.
Code is added to
shaclgen.py
to:type
andextended_float
,extended_int
,extended_str
, etcIn addition, the
test_shaclgen
test is updated to test for annotations on classes, and the kitchen sink schema is updated to include annotations on the person class. Several snapshot data files needed to be updated as well because of the change to that schema, so that existing tests would still succeed.Uncertainties:
jsonasobj2
issue, mentioned here:jsonasobj2
causes inconsistent behavior on attr assignment vs. instantiation for linkml_model classes #1665 (comment)tag
, the current way that the code is implemented means that the values will be appended, not replaced. It seems to me that the desired behaviour will differ based on the use case. So the question is whether more user-defined controls should be implemented to direct such behaviour?