ontocast.stategraph.facts_gate¶
The post-aggregation facts invariant gate, shared by both entry paths.
Two callers reach this gate with the same contract and different capabilities:
- the document graph, at
VALIDATE_FACTS-- many units, so the un-merge repair is meaningful; - the single-unit path (
/process_unitand the CLI--use-unit-pipelinebatch), which does not run the graph and calls in after aggregation.
Un-merging re-aggregates retained units against each other, which has no
meaning for one unit, so the single-unit caller passes merge_repair=False.
Everything else -- validation arguments, SHACL autofix, conformance summary and
the emitted metrics -- is identical, and used to be written out twice; a new
validate_aggregated_facts argument added to one copy silently skipped the
other entry path.
Attributes¶
MERGE_SIGNATURE_KINDS = frozenset({FactsValidationFindingKind.FUNCTIONAL_VIOLATION, FactsValidationFindingKind.SUSPECT_MULTI_VALUE, FactsValidationFindingKind.DEGENERATE_COREFERENCE})
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions:¶
drop_detached_values(graph, isolated, value_classes, fact_namespaces)
¶
Remove every isolated instance typed as a value, with the parts it owns.
A value belongs to the statement that carries it; its type is one of
value_classes (see
:meth:~ontocast.tool.facts_validation.terms.CatalogRoles.value_classes).
One linked to no instance nor the document has lost its statement,
whatever numbers it holds, so it is not a fact. A value linked to
anything, in either direction, is kept.
isolated is computed before values are named, so a value's parts are
still blank nodes under it. Its parts go with it: the instances and blank
nodes it reaches that nothing else points at.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Served facts graph, mutated in place. |
required |
isolated
|
Sequence[str]
|
IRIs of the instances linked to nothing. |
required |
value_classes
|
frozenset[str]
|
The classes whose instances are values. |
required |
fact_namespaces
|
Sequence[str]
|
Namespaces holding minted instances. |
required |
Returns:
| Type | Description |
|---|---|
list[GraphRepairRecord]
|
One |
list[GraphRepairRecord]
|
label as target, and the triples removed with it. |
Source code in ontocast/stategraph/facts_gate.py
drop_domain_violations(graph, findings)
¶
Remove every statement a mandatory domain_violation finding names.
The finding names a subject and a property whose declared domain the subject's types contradict; every statement of that pair goes, with the blank nodes only it reached and the reifiers quoting it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
A unit's facts graph, mutated in place. |
required |
findings
|
Sequence[FactsUnitFinding]
|
The unit's findings; other kinds are ignored. |
required |
Returns:
| Type | Description |
|---|---|
list[GraphRepairRecord]
|
One |
list[GraphRepairRecord]
|
the subject as source, the property as target. |
Source code in ontocast/stategraph/facts_gate.py
drop_self_loops(graph, ontology_graph)
¶
Remove every statement x p x whose p is not declared reflexive.
A text never relates a thing to itself through an ordinary property: such
a statement is a slip (the subject written twice) or two nodes a merge
joined. It states nothing, so it goes; owl:sameAs, rdf:type and an
owl:ReflexiveProperty the context declares are kept. A reifier goes
with its statement.
Returns:
| Type | Description |
|---|---|
list[GraphRepairRecord]
|
One |
Source code in ontocast/stategraph/facts_gate.py
drop_unlinked_instances(graph, fact_namespaces, document)
¶
Remove every instance that states nothing, and every value no statement carries.
Served facts are statements: an instance linked to no other instance nor
to document, holding no value of its own
(:func:~ontocast.tool.facts_validation.connectivity.silent_instances),
is a type and a name, a mention rather than a fact; a blank-node value
nothing points to has lost its statement. Runs on the aggregated graph,
so a mention another unit links has merged first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Aggregated facts graph, mutated in place. |
required |
fact_namespaces
|
Sequence[str]
|
Namespaces holding minted instances. |
required |
document
|
URIRef | None
|
The document node; a link to it counts. |
required |
Returns:
| Type | Description |
|---|---|
list[GraphRepairRecord]
|
One |
list[GraphRepairRecord]
|
value (its source the blank node). |
Source code in ontocast/stategraph/facts_gate.py
drop_untyped_instances(graph, fact_namespaces)
¶
Remove every instance that has no concrete type, with its triples.
Runs on the aggregated graph so a type asserted in any unit counts. An
untyped instance (:func:untyped_instances: a fact-namespace IRI in
subject or IRI-object position whose rdf:type names no class outside
the fact namespaces other than a metaclass or owl:Thing) loses every
triple it is the subject or the object of; blank nodes reachable only
through those triples go with them, and so do the RDF 1.2 reifiers
quoting any removed statement. A typed neighbour keeps everything else.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Aggregated facts graph, mutated in place. |
required |
fact_namespaces
|
Sequence[str]
|
Namespaces holding minted instances. |
required |
Returns:
| Type | Description |
|---|---|
list[GraphRepairRecord]
|
One |
Source code in ontocast/stategraph/facts_gate.py
run_facts_gate(state, ontology_graph, tools, *, merge_repair, document_metadata=None)
¶
Validate the aggregated facts and apply the LLM-free repair stages.
Literal variants differing only in language tag or datatype are collapsed
first (FACTS_LITERAL_VARIANT_DEDUPE), so validation counts values
rather than spellings.
When merge_repair is set, merge-signature error findings whose subject
resulted from an identity merge turn the offending cluster into pair vetoes
and the retained facts units are re-aggregated, up to
FACTS_MERGE_REPAIR_PASSES times. Then, on both paths, SHACL autofix
(FACTS_SHACL_AUTOFIX) repairs constraint violations in code, instances
still untyped take the types their links' domains and ranges entail in the
whole ontology context (facts_untyped_inferred), and those still
untyped are dropped (:func:drop_untyped_instances;
facts_untyped_dropped), and so are instances that state nothing
(:func:drop_unlinked_instances; facts_unlinked_dropped). The links
these repairs removed
(facts_links_removed), how the served instances hang together
(facts_components, facts_isolated) and the labels several of them
share (facts_twin_labels) are recorded. Residual findings stay on the
state as telemetry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
AgentState
|
Document state carrying |
required |
ontology_graph
|
RDFGraph
|
Ontology context the facts are validated against. |
required |
tools
|
ToolBox
|
Dependency container; only the aggregator is used, and only when
|
required |
merge_repair
|
bool
|
Whether to run the un-merge repair loop. False for the single-unit path, where re-aggregating one unit is a no-op. |
required |
document_metadata
|
dict | None
|
Provenance metadata for re-aggregation. Required
when |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in ontocast/stategraph/facts_gate.py
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 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 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 | |
stored_pairs(pairs)
¶
Entity pairs as the state stores them: sorted (left, right) strings.
Source code in ontocast/stategraph/facts_gate.py
vetoes_from_findings(findings, clusters, units=())
¶
Pair vetoes for merge-signature error findings.
A finding implicates the cluster whose merge could have produced it. A
merged subject collects the objects of all its members, so
FUNCTIONAL_VIOLATION and SUSPECT_MULTI_VALUE implicate the subject; its
objects are innocent (merging objects can only reduce the values a subject
carries). DEGENERATE_COREFERENCE reports the pointing node as subject and
the over-merged endpoint in values (range1 hasLowerBound v1 ;
hasUpperBound v1); either merge can produce it, so it implicates both.
Within an implicated cluster only the members that disagree are split:
two members are vetoed when every value one carried on the finding's
predicate, in its own unit, conflicts with every value the other carried
(for DEGENERATE_COREFERENCE: when they use, or are reached by, different
ones of its predicates). Members that carried nothing there keep their
merges. When units cannot attribute the disagreement, or are not
given, every pair of the implicated cluster is vetoed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
findings
|
list[FactsValidationFinding]
|
Error findings from the validation report. |
required |
clusters
|
dict[str, list[str]]
|
Identity-merge clusters keyed by final IRI; members are the entity IRIs of the unit graphs. |
required |
units
|
Sequence[ContentUnit]
|
The aggregated units, whose graphs say which member carried which value. |
()
|
Returns:
| Type | Description |
|---|---|
set[frozenset[URIRef]]
|
Pairs of IRIs that must not be merged on a re-aggregation pass. |
Source code in ontocast/stategraph/facts_gate.py
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |