Skip to content

ontocast.onto.util

Attributes

CONVENTIONAL_MAPPINGS = get_rdflib_namespace_mappings() module-attribute

RDFLIB_DEFAULT_NAMESPACE_URIS = frozenset(CONVENTIONAL_MAPPINGS) module-attribute

Functions:

derive_ontology_id(iri)

Derive a short ontology handle from an absolute IRI.

Prefers rdflib conventional namespace prefixes (SKOS → skos). Falls back to the last non-opaque path segment. Returns None instead of inventing garbage ids (e.g. FOAF 0.1 → would-be 01).

Source code in ontocast/onto/util.py
def derive_ontology_id(iri: str) -> str | None:
    """Derive a short ontology handle from an absolute IRI.

    Prefers rdflib conventional namespace prefixes (SKOS → ``skos``). Falls back
    to the last non-opaque path segment. Returns ``None`` instead of inventing
    garbage ids (e.g. FOAF ``0.1`` → would-be ``01``).
    """
    if not isinstance(iri, str) or not iri.strip():
        return None

    raw = strip_iri_brackets(iri.strip())
    conventional = _conventional_prefix_for(raw)
    if conventional:
        return conventional

    normalized_iri = normalize_ontology_iri(raw)
    parsed = urlparse(normalized_iri)

    candidate = (
        parsed.path.rsplit("/", 1)[-1]
        if parsed.path and "/" in parsed.path
        else parsed.netloc.split(".")[0]
        if parsed.netloc
        else normalized_iri
    )

    return _clean_derived_id(candidate)

get_rdflib_namespace_mappings()

Source code in ontocast/onto/util.py
def get_rdflib_namespace_mappings() -> dict[str, str]:
    g = Graph()
    ns_manager = NamespaceManager(g)
    return {str(uri): prefix for prefix, uri in ns_manager.namespaces()}

is_rdflib_default_namespace(namespace)

Return True when namespace is one of rdflib's built-in bindings.

Source code in ontocast/onto/util.py
def is_rdflib_default_namespace(namespace: str) -> bool:
    """Return True when *namespace* is one of rdflib's built-in bindings."""
    return namespace in RDFLIB_DEFAULT_NAMESPACE_URIS

non_domain_namespaces()

Namespaces that are never a document's domain vocabulary.

Every rdflib built-in binding (brick, csvw, xml, …), every namespace in COMMON_PREFIXES, both schema.org spellings, and the facts namespace itself. Two callers ask the same question of this set and used to each build their own copy: the delta partitioner, which must not treat a standard namespace as writable, and the prompt's "domain ontologies" clause, which must not list one. They diverge only in shape -- the partitioner compares normalized stems -- so the shape conversion lives with each caller and the membership is defined once, here.

This is deliberately not the facts-vocabulary standard set (tool/facts_validation/terms.py), which answers a different question -- which terms a render may use without being flagged unknown -- and is operator-extensible through FACTS_ADDITIONAL_STANDARD_NAMESPACES. Nor is it the aggregator's type-comparison set, which is narrower again.

Source code in ontocast/onto/util.py
def non_domain_namespaces() -> frozenset[str]:
    """Namespaces that are never a document's *domain* vocabulary.

    Every rdflib built-in binding (``brick``, ``csvw``, ``xml``, …), every
    namespace in ``COMMON_PREFIXES``, both schema.org spellings, and the facts
    namespace itself. Two callers ask the same question of this set and used to
    each build their own copy: the delta partitioner, which must not treat a
    standard namespace as writable, and the prompt's "domain ontologies" clause,
    which must not list one. They diverge only in shape -- the partitioner
    compares normalized stems -- so the shape conversion lives with each caller
    and the *membership* is defined once, here.

    This is deliberately **not** the facts-vocabulary standard set
    (``tool/facts_validation/terms.py``), which answers a different question --
    which terms a render may use without being flagged unknown -- and is
    operator-extensible through ``FACTS_ADDITIONAL_STANDARD_NAMESPACES``.
    Nor is it the aggregator's type-comparison set, which is narrower again.
    """
    from ontocast.onto.constants import COMMON_PREFIXES, DEFAULT_IRI

    return (
        RDFLIB_DEFAULT_NAMESPACE_URIS
        | frozenset(uri.strip("<>") for uri in COMMON_PREFIXES.values())
        | {"https://schema.org/", "http://schema.org/", DEFAULT_IRI}
    )

normalize_ontology_iri(iri)

Normalize an ontology IRI for catalog lookup and alias resolution.

Strips angle brackets and trailing / or # so that http://example.org/ont, …/ont/, and …/ont# compare equal as lookup keys. Author namespace delimiters for prefix bindings remain the responsibility of :func:normalize_namespace_iri.

Source code in ontocast/onto/util.py
def normalize_ontology_iri(iri: str) -> str:
    """Normalize an ontology IRI for catalog lookup and alias resolution.

    Strips angle brackets and trailing ``/`` or ``#`` so that
    ``http://example.org/ont``, ``…/ont/``, and ``…/ont#`` compare equal as
    lookup keys. Author namespace delimiters for prefix bindings remain the
    responsibility of :func:`normalize_namespace_iri`.
    """
    text = strip_iri_brackets(iri)
    return text.rstrip("/#")