Build one deterministic merged ontology context from reduced document artifacts.
Source code in ontocast/stategraph/context_resolver.py
| def build_merged_document_ontology_context(
state: AgentState,
) -> UnitOntologyContext | None:
"""Build one deterministic merged ontology context from reduced document artifacts."""
artifacts = [
ontology
for ontology in document_ontology_access(state).reduced_artifacts()
if not ontology.is_null() and len(ontology.graph) > 0
]
if not artifacts:
return None
sorted_artifacts = sorted(artifacts, key=lambda ontology: ontology.iri or "")
merged_graph = RDFGraph()
patch_sources: list[str] = []
for ontology in sorted_artifacts:
merged_graph += ontology.graph
if ontology.iri:
patch_sources.append(ontology.iri)
merged_graph.sanitize_prefixes_namespaces()
anchor_iri = patch_sources[0] if patch_sources else NULL_ONTOLOGY.iri
snapshot = Ontology(
ontology_id=None,
title="Merged document ontology context",
description=(
"Deterministic merge of reduced ontology artifacts used for facts context."
),
graph=merged_graph,
iri=anchor_iri,
current_domain=state.current_domain,
)
return UnitOntologyContext(
anchor_iri=anchor_iri,
ontology_snapshot=snapshot,
patch_sources=patch_sources,
assembly_mode=OntologyAssemblyMode.DOCUMENT_MERGED_REDUCED,
confidence=1.0,
)
|