ontocast.tool.facts_validation.shacl¶
SHACL execution, shape assembly, autofix repairs, and the catalog lint.
shacl_catalog_contradictions cross-checks the shapes against the term
validator: a property the shapes require but the validator would flag as
unknown silently destroys extracted data.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
ShaclRepairResult
¶
Bases: BaseModel
Outcome of the LLM-free SHACL repair pass.
Source code in ontocast/tool/facts_validation/shacl.py
Attributes¶
graph
instance-attribute
¶
model_config = {'arbitrary_types_allowed': True}
class-attribute
instance-attribute
¶
passes_applied = 0
class-attribute
instance-attribute
¶
ran = False
class-attribute
instance-attribute
¶
records = Field(default_factory=list)
class-attribute
instance-attribute
¶
reverted = False
class-attribute
instance-attribute
¶
violations_after = 0
class-attribute
instance-attribute
¶
violations_before = 0
class-attribute
instance-attribute
¶
ShaclViolation
¶
Bases: BaseModel
One SHACL validation result, in the form the repair pass needs.
FactsValidationFinding is the reporting shape and deliberately flat;
this keeps the RDF terms (focus node, path, offending value, constraint
component) so a repair can act on them.
Source code in ontocast/tool/facts_validation/shacl.py
Attributes¶
component = None
class-attribute
instance-attribute
¶
focus = None
class-attribute
instance-attribute
¶
message = 'SHACL constraint violated.'
class-attribute
instance-attribute
¶
model_config = {'arbitrary_types_allowed': True}
class-attribute
instance-attribute
¶
path = None
class-attribute
instance-attribute
¶
severity = 'error'
class-attribute
instance-attribute
¶
source_shape = None
class-attribute
instance-attribute
¶
value = None
class-attribute
instance-attribute
¶
Methods:¶
as_finding()
¶
Project onto the reported finding shape.
Source code in ontocast/tool/facts_validation/shacl.py
Functions:¶
apply_shacl_repairs(graph, shapes_graph, ontology_graph, *, mode='prune', passes=1, fact_namespaces=(), code_predicates=(), inference='rdfs', advanced=True, max_triples=0, initial_violations=None)
¶
Repair SHACL violations in code, with no LLM round-trip.
Bounded validate -> repair -> revalidate loop. A pass is kept only when
it strictly reduces the violation count: a repair that trades triples for
no conformance gain is reverted, the same discipline the un-merge repair
uses.
Repairs by constraint component
sh:datatype: retype a literal that parses as the declared datatype ("2019"^^xsd:string->"2019"^^xsd:gYear).sh:class/sh:nodeKind: replace a string literal with the one catalog IRI declaring it as a surface form (qudt:unit "meV"->unit:MilliElectronVolt). Ambiguous forms are left reported.sh:minCount(modepruneonly): drop a focus node that asserts nothing beyondrdf:type/rdfs:labeland is referenced by at most one subject, together with that reference.
Everything else -- sh:maxCount (owned by the functional-violation and
un-merge machinery), sh:not, sh:qualifiedValueShape, SPARQL
constraints -- is reported, never repaired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Aggregated facts graph, repaired in place: it may be oxigraph-backed and carry RDF 1.2 triple terms, which a copied rdflib graph would silently drop. A pass that fails the accept test is rolled back triple-for-triple instead. |
required |
shapes_graph
|
RDFGraph | None
|
Shapes to validate against; |
required |
ontology_graph
|
RDFGraph | None
|
Merged ontology context, indexed for surface forms. |
required |
mode
|
str
|
|
'prune'
|
passes
|
int
|
Maximum repair rounds. |
1
|
fact_namespaces
|
Sequence[str]
|
Only nodes under these namespaces are repaired. |
()
|
code_predicates
|
Sequence[str]
|
Code-bearing predicates for surface resolution. |
()
|
inference
|
str
|
pyshacl pre-inference mode. |
'rdfs'
|
advanced
|
bool
|
Enable SHACL Advanced Features. |
True
|
max_triples
|
int
|
Skip validation above this graph size; 0 disables. |
0
|
initial_violations
|
Sequence[ShaclViolation] | None
|
Violations already computed for |
None
|
Returns:
| Type | Description |
|---|---|
ShaclRepairResult
|
The repaired graph, the applied repair records, and fact-scoped |
ShaclRepairResult
|
violation counts before and after (the population |
ShaclRepairResult
|
judged on; the loop's accept test uses the raw count internally). |
Source code in ontocast/tool/facts_validation/shacl.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
collect_shacl_shapes(ontology_graph, stored_shapes)
¶
Assemble the SHACL shapes graph for the validation gate.
Sources: the deployment's shapes partition (stored_shapes, resolved by
:class:~ontocast.tool.shapes_catalog.ShapesCatalog -- seeded from
FACTS_SHAPES_DIR and mutable over /shapes), plus the ontology
context itself when it already carries sh:NodeShape declarations inline
-- the zero-config path for catalogs that ship shapes next to their schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_graph
|
RDFGraph | None
|
Ontology context offered to the renderer. |
required |
stored_shapes
|
RDFGraph | None
|
Merged shapes graph from the shapes partition. |
required |
Returns:
| Type | Description |
|---|---|
RDFGraph | None
|
RDFGraph | None: The shapes to validate against, or |
RDFGraph | None
|
are none -- which is what keeps |
RDFGraph | None
|
("never checked") rather than reporting a clean run. |
Source code in ontocast/tool/facts_validation/shacl.py
run_shacl(graph, shapes_graph, *, ontology_graph=None, inference='rdfs', advanced=True, max_triples=0)
¶
Validate graph against shapes_graph, returning the violations.
Reaching here means shapes were found, so the caller expects validation to
happen: a missing extra or a skipped run is reported at warning level, not
debug. Silently returning "no violations" is indistinguishable from
"conforms", so those cases return None.
The ontology context is mixed in (ont_graph) rather than left out. A
facts graph states that a value uses unit:DAY; that the individual is
a qudt:Unit is stated only in the catalog. Validating the facts alone
therefore fails every sh:class constraint pointing at a catalog
individual — violations that describe the missing schema, not the data.
RDFS inference is the default for the same reason. SHACL resolves class
targets through rdfs:subClassOf on its own, but property paths carry no
entailment: a shape on obs:hasResult does not see the
life:hasStorageResult the renderer emitted, and reports the more
specific statement as a missing one, so turning inference off raises the
violation count rather than lowering it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Data graph to validate. |
required |
shapes_graph
|
RDFGraph
|
Shapes to validate against. |
required |
ontology_graph
|
RDFGraph | None
|
Schema mixed into the data graph for validation. |
None
|
inference
|
str
|
pyshacl pre-inference ( |
'rdfs'
|
advanced
|
bool
|
Enable SHACL Advanced Features. |
True
|
max_triples
|
int
|
Skip validation above this graph size; 0 disables. |
0
|
Returns:
| Type | Description |
|---|---|
list[ShaclViolation] | None
|
Violations in report order, or |
Source code in ontocast/tool/facts_validation/shacl.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
shacl_catalog_contradictions(shapes_graph, ontology_graph, *, policy=None, catalog_terms=None)
¶
Property paths the shapes require but the unit validator would flag.
A SHACL property shape with sh:minCount >= 1 demands a property that
the deterministic UNKNOWN_TERM check — same closure rules, same
exemptions — would report as not existing. Data cannot satisfy both: the
renderer is ordered to remove exactly what validation requires. Found live
in practice, where shapes required qudt:numericValue
while the validator's mandatory findings drove repair renders to delete
it. Callers log the returned IRIs as configuration errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shapes_graph
|
RDFGraph | None
|
The shapes the gate validates against. |
required |
ontology_graph
|
RDFGraph | None
|
The ontology context the facts were rendered against. |
required |
policy
|
ValidationPolicy | None
|
The exemptions the unit validator applies. Pass the same policy the unit loop uses -- including its shapes-contract exemptions -- or the check reports contradictions the validator never raises. |
None
|
catalog_terms
|
Collection[str] | None
|
The whole catalog's term inventory, when
|
None
|