Skip to content

ontocast.tool.agg.merge_provenance

Which multi-valued facts of a merged graph an identity merge produced.

A source subject is a subject as one unit wrote it. Values or links that a single source subject already held were together before any identity decision, so they cannot be the residue of a bad merge, and the un-merge repair, which can only undo identity decisions, must not act on them.

Classes

Functions:

build_merge_provenance(units, final_mapping)

Record which multi-valued facts of the merged graph a merge produced.

Values are compared by :func:object_value_key, as the validation gate compares them. Keys are canonicalized through final_mapping, because the gate sees the post-merge graph and a pre-merge IRI would never match.

Parameters:

Name Type Description Default
units Sequence[ContentUnit]

The content units that contributed to the merged graph.

required
final_mapping dict[URIRef, URIRef]

Source entity -> final URI, as used for the rewrite.

required

Returns:

Type Description
MergeProvenance

The (subject, predicate) pairs with at least two distinct values, and

MergeProvenance

the (subject, object) pairs linked by at least two predicates, that no

MergeProvenance

single source subject accounts for.

Source code in ontocast/tool/agg/merge_provenance.py
def build_merge_provenance(
    units: Sequence[ContentUnit],
    final_mapping: dict[URIRef, URIRef],
) -> MergeProvenance:
    """Record which multi-valued facts of the merged graph a merge produced.

    Values are compared by :func:`object_value_key`, as the validation gate
    compares them. Keys are canonicalized through ``final_mapping``, because
    the gate sees the post-merge graph and a pre-merge IRI would never match.

    Args:
        units: The content units that contributed to the merged graph.
        final_mapping: Source entity -> final URI, as used for the rewrite.

    Returns:
        The (subject, predicate) pairs with at least two distinct values, and
        the (subject, object) pairs linked by at least two predicates, that no
        single source subject accounts for.
    """
    values: dict[tuple[str, str], dict[_Source, set[str]]] = {}
    links: dict[tuple[str, str], dict[tuple[int, str, str], set[str]]] = {}
    for index, unit in enumerate(units):
        if unit.graph is None:
            continue
        for subject, predicate, obj in unit.graph:
            if not isinstance(subject, URIRef) or not isinstance(predicate, URIRef):
                continue
            final_subject = str(final_mapping.get(subject, subject))
            source = (index, str(subject))
            value = final_mapping.get(obj, obj) if isinstance(obj, URIRef) else obj
            values.setdefault((final_subject, str(predicate)), {}).setdefault(
                source, set()
            ).add(object_value_key(value))
            if isinstance(obj, URIRef):
                link = (final_subject, str(final_mapping.get(obj, obj)))
                links.setdefault(link, {}).setdefault(
                    (index, str(subject), str(obj)), set()
                ).add(str(predicate))

    return MergeProvenance(
        joined_values=sorted(
            key for key, held in values.items() if _joined(held.values())
        ),
        joined_links=sorted(
            key for key, held in links.items() if _joined(held.values())
        ),
    )