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.
ShaclRepairResult
¶
Bases: BaseModel
Outcome of the LLM-free SHACL repair pass.
Source code in ontocast/tool/facts_validation/shacl.py
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
as_finding()
¶
Project onto the reported finding shape.
Source code in ontocast/tool/facts_validation/shacl.py
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
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 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 | |
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)
¶
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.