Skip to content

ontocast.tool.facts_validation.connectivity

How the aggregated facts hang together.

:func:facts_connectivity counts the connected components of the served graph and the instances linked to nothing; :func:instance_links and :func:links_lost count the links the gate's repairs removed.

Attributes

__all__ = ['FactsConnectivity', 'facts_connectivity', 'instance_links', 'links_lost'] module-attribute

Classes

FactsConnectivity

Bases: BaseModel

How the instances of a facts graph hang together.

Source code in ontocast/tool/facts_validation/connectivity.py
class FactsConnectivity(BaseModel):
    """How the instances of a facts graph hang together."""

    instances: int = 0
    #: Connected groups of instances (the document node joins what it links).
    components: int = 0
    #: Instances linked to no other instance and not to the document.
    isolated: int = 0

Attributes

components = 0 class-attribute instance-attribute
instances = 0 class-attribute instance-attribute
isolated = 0 class-attribute instance-attribute

Functions:

facts_connectivity(graph, fact_namespaces, document=None)

Count the connected components and isolated instances of graph.

Instances (:func:~ontocast.tool.facts_validation.terms.fact_instances) are joined by any link (:func:instance_links' notion), directly or through blank nodes; the document node, when given, is not an instance but joins what it links (cited works, metadata), as a reader of the graph sees it. Provenance reifiers and chunk nodes join nothing.

Source code in ontocast/tool/facts_validation/connectivity.py
def facts_connectivity(
    graph: RDFGraph, fact_namespaces: Sequence[str], document: URIRef | None = None
) -> FactsConnectivity:
    """Count the connected components and isolated instances of ``graph``.

    Instances (:func:`~ontocast.tool.facts_validation.terms.fact_instances`)
    are joined by any link (:func:`instance_links`' notion), directly or
    through blank nodes; the ``document`` node, when given, is not an instance
    but joins what it links (cited works, metadata), as a reader of the graph
    sees it. Provenance reifiers and chunk nodes join nothing.
    """
    instances = fact_instances(graph, fact_namespaces) - {document}
    if not instances:
        return FactsConnectivity()
    members: set[Node] = set(instances)
    if document is not None:
        members.add(document)
    scaffolding = scaffolding_nodes(graph)
    parent: dict[Node, Node] = {}

    def find(node: Node) -> Node:
        parent.setdefault(node, node)
        while parent[node] != node:
            parent[node] = parent[parent[node]]
            node = parent[node]
        return node

    def joinable(node: Node) -> bool:
        return node in members or isinstance(node, BNode)

    for s, p, o in graph:
        if _is_link((s, p, o), scaffolding) and joinable(s) and joinable(o) and s != o:
            parent[find(s)] = find(o)

    sizes: dict[Node, int] = {}
    for node in members:
        root = find(node)
        sizes[root] = sizes.get(root, 0) + 1
    roots_with_instances = {find(node) for node in instances}
    return FactsConnectivity(
        instances=len(instances),
        components=len(roots_with_instances),
        isolated=sum(1 for node in instances if sizes[find(node)] == 1),
    )

Triples that link an instance (or a blank node) to another node.

Typing, owl:sameAs and the pipeline's provenance scaffolding are not links, and neither is a literal value. A domain property is a link in any vocabulary, PROV-O included.

Source code in ontocast/tool/facts_validation/connectivity.py
def instance_links(
    graph: RDFGraph, fact_namespaces: Sequence[str]
) -> set[tuple[Node, Node, Node]]:
    """Triples that link an instance (or a blank node) to another node.

    Typing, ``owl:sameAs`` and the pipeline's provenance scaffolding are not
    links, and neither is a literal value. A domain property is a link in any
    vocabulary, PROV-O included.
    """
    instances = fact_instances(graph, fact_namespaces)
    scaffolding = scaffolding_nodes(graph)
    return {
        (s, p, o)
        for s, p, o in graph
        if _is_link((s, p, o), scaffolding)
        and (s in instances or isinstance(s, BNode))
        and (o in instances or isinstance(o, BNode))
    }

How many of the links before the graph after no longer holds.

Source code in ontocast/tool/facts_validation/connectivity.py
def links_lost(before: Iterable[tuple[Node, Node, Node]], after: RDFGraph) -> int:
    """How many of the links ``before`` the graph ``after`` no longer holds."""
    return sum(1 for triple in before if triple not in after)