Repository navigation
Conversation
gen-shacl dropped minimum_value and maximum_value when they were set on an any_of branch or on a type, so out-of-range values conformed to the shapes while the JSON Schema from the same schema rejected them. A branch now gets sh:minInclusive / sh:maxInclusive on its node, like its pattern. The bounds of a type and of the slot or branch whose range it is all hold, and SHACL allows one sh:minInclusive and one sh:maxInclusive per shape, so they are merged into the tightest. Each bound is written as a valid literal that compares with the range's values: the range's own datatype where the bound is a value of it, else xsd:integer or xsd:decimal, with no exponent form. A decimal 0.1 no longer falls below a bound written as the double 0.1. A bound that SHACL cannot order against the range's values (a number on a string range, gYear, non-XSD datatypes, classes, enums) would reject every value, so it is skipped with a warning. Signed-off-by: Carlo van Driesten <carlo.van-driesten@vdl.digital>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mirror of linkml#4335, same head. Review there; this PR tracks the fork stack.
Summary
Fixes # (no issue yet; I ran into this while generating the SHACL shapes for ASCS-eV/ontology-management-base#81, where values that
linkml-validaterejects passed SHACL validation)gen-shacl drops
minimum_value/maximum_valuein two places:any_ofbranch: only the branch'spatternreaches itssh:ornode;_add_typewrites the type's datatype and pattern but not its bounds, althoughTypeDefinitionhas both slots and inherits them alongtypeof.Writing those bounds brings up two problems with how gen-shacl writes bounds in general:
sh:minInclusive;Literal(value), so its datatype comes from the Python value, not from the range. A decimal slot gets"0.1"^^xsd:double, and a numeric bound on a string slot rejects every string.In plain words
A schema can say "this number is between 0 and 100" on a type such as
Percentage, or on one option of anany_of.linkml-validateenforces that. The SHACL that gen-shacl writes for the same schema doesn't, so data thatlinkml-validaterejects passes SHACL validation. This PR writes those limits into the shapes, and writes each one in a form SHACL can compare with the data. A limit that SHACL can't compare with the data would reject every value, so gen-shacl skips it and logs a warning.Reproduce
bounds.yaml:For each case, e.g.
count-0.yamlcontainingcount: 0:gen-shacl bounds.yaml > bounds.shacl.ttl linkml-validate -s bounds.yaml -C Lane count-0.yaml linkml-convert --no-validate -s bounds.yaml -C Lane -t ttl -o count-0.ttl count-0.yaml pyshacl -s bounds.shacl.ttl count-0.ttllinkml-validatemain(fb4339c)count: 1,share: 50,occupancy: 50,opened: 2005-06-01,pass_mark: 75count: 0share: 100.5occupancy: 101opened: 2010-01-01pass_mark: 49pass_mark: 100.5code: abc¹
linkml-validategoes through JSON Schema, which has no bounds for dates.²
mainwritessh:minInclusive 1on a string property, and no string compares with1. This PR skips the bound withWARNING: Range 'string' has bound 1, which SHACL cannot compare with values of datatype http://www.w3.org/2001/XMLSchema#string; skipping it.A decimal range,
score: {range: decimal, minimum_value: 0.1, maximum_value: 0.3}, with the data asxsd:decimalliterals (ex:score 0.1), on pySHACL 0.40.2 and rudof (pyrudof 0.3.42):ex:scoremain:"0.1"^^xsd:double… pySHACLmain… rudof0.1… pySHACL0.10.3I used hand-written Turtle for this one because
linkml-convertwrites the YAML0.3as0.299999999999999988897769753748434595763683319091796875, which is a separate issue.Why this is wrong
minimum_value: "For ordinal ranges, the value must be equal to or higher than this". It is a slot ofAnonymousSlotExpression(anany_ofmember) and ofTypeDefinition, and types inherit it alongtypeof. gen-json-schema already writes both.$minInclusive <= vdoes not return true, there is a validation result with v as sh:value." §4.3.4 says the same forsh:maxInclusive.<=for numerics, simple literals andxsd:string,xsd:booleanandxsd:dateTime. Any other pair is a type error, which "does not return true", so every value is a violation. pySHACL 0.40.2 and rudof 0.3.42 both also orderxsd:dateandxsd:timeas XPath does (op:date-less-than,op:time-less-than). Forxsd:gYear, which XPath only defines equality for, they disagree: pySHACL rejects"2000"^^xsd:gYearagainstsh:minInclusive "1900"^^xsd:gYear, while rudof accepts it."1e-07"^^xsd:decimalis ill-typed.The change
any_ofbranch getssh:minInclusive/sh:maxInclusiveon its node, next to itssh:pattern._add_typetakes the bounds of the slot or branch whose range is the type, and_add_boundswrites the tightest of these and the type's own bounds, one value per predicate.bound_literalwrites each bound as a literal that compares with the range's values:xsd:integerorxsd:decimal, never in exponent form. So0.5on anintegerslot becomes"0.5"^^xsd:decimal, and0on apositiveIntegerbecomes0(xsd:integer);date,datetime,time(andxsd:dateTimeStamp): a valid literal of the range's datatype;string: a string bound, written as before;xsd:gYear, where the engines disagree.any_ofslot, are written as before.How was this tested?
test_shaclgen.py; 24 of them fail onmain:test_any_of_and_type_bounds_pyshacl_end_to_end(25 cases): values in, at and out of the bounds ofinput/shaclgen/any_of_bounds.yaml, validated with pySHACL against the generated shapes, which are themselves checked withmeta_shacl=True;test_bound_is_valid_literal_of_comparable_datatype(8 cases): the literal written for each kind of range;test_incomparable_bound_is_skipped_with_warning(7 cases).rdflib.NORMALIZE_LITERALS).tests/andexamples/,mainvs this branch: 19 outputs differ, all insh:minInclusive/sh:maxInclusive:minimum_value: 0);personinfo, 2 of an inference example) write decimal bounds asxsd:decimalinstead ofxsd:doubleorxsd:integer;1..100bounds, with the warning;uv run python -m pytest tests -n 8on Windows: 14858 passed, 8 failed, 5 errors. None of them involve gen-shacl: run on their own, all pass excepttest_datadict_personinfo(calls kroki.io, fails onmaintoo) andtest_template_noblack(requests-cache.sqlitelocked by another process).ruff checkandruff format --checkat the pre-commit pin (v0.11.13): clean.Areas of uncertainty
range: Percentage(0..100) andmaximum_value: 200on the slot, this PR keeps100, following the monotonic semantics above. gen-json-schema lets the slot's keyword replace the type's (get_value_constraints_for_slot), so it accepts 150. I think the JSON Schema side should change, but that is outside this PR.0.1and"0.1"^^xsd:doublecompare as equal, and rudof agrees. pySHACL compares the exact values and rejects (table above). Writing the bound in the range's own datatype gives the same result in both.main. I left them alone because I don't know what a bound on a class should mean.Checklist
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.