Skip to content

ontocast.stategraph.context_resolver

build_merged_document_ontology_context(state)

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,
    )

resolve_effective_facts_ontology_context(state, tools, unit) async

Resolve facts context preferring merged document artifacts when available.

Source code in ontocast/stategraph/context_resolver.py
async def resolve_effective_facts_ontology_context(
    state: AgentState,
    tools: ToolBox,
    unit: SourceUnit,
) -> UnitOntologyContext:
    """Resolve facts context preferring merged document artifacts when available."""
    merged_context = build_merged_document_ontology_context(state)
    if merged_context is not None:
        return merged_context
    return await resolve_unit_ontology_context(state, tools, unit)