ontocast.tool.ontology_validation.axiom_hygiene¶
Deterministic clean-up of a document's ontology delta before it is written.
Each rule removes triples a render should not have produced and returns how
many it removed, for ontology_reduce_metrics:
- :func:
drop_external_subjects: triples about a term of an external ontology. An external ontology is never written to; its terms are used by IRI only. - :func:
drop_example_namespace_triples: triples naming an IRI under a reserved example domain (example.org,.com,.net) outside the deployment's own domain: the namespace of prompt examples, which a model reaches for when it lacks a real one. - :func:
normalize_datatype_ranges: a property with several datatype ranges means their intersection, which is empty for two different datatypes; one range is kept. - :func:
drop_vacuous_upper_terms:owl:Thing/schema:Thingas a range or domain, or as a parent beside another parent, says nothing and reads as a modelling decision. - :func:
property_side_reasons(a check, not a rule): a minted property whose domain or range is missing or a generic upper class (:func:is_generic_class); counted for telemetry, nothing removed. - :func:
repair_undeclared_owned_references: a side or parent naming an IRI under the deployment's own domain that nothing declares was made up; it is rewritten to the one declared term with that local name, else removed. - :func:
drop_role_crossing_parents: a property's parent must be a property and a class's parent a class.rdfs:subPropertyOf schema:Propertynames a class, andrdfs:subClassOfa property or a datatype is no hierarchy. - :func:
drop_non_term_subjects: an ontology update declares terms and value sets. A subject in the facts namespace, an individual (typed, but neither as a term nor as a SKOS concept), or an untyped subject that nothing declares belongs in the facts graph, not in an ontology. - :func:
drop_terms_minted_outside_home: every new term of a document lives under its home namespace; a term newly declared under another namespace of the deployment is a second copy of one the document already named, and one declared in another vocabulary is that vocabulary's to write.
Every rule removes a blank-node structure (a restriction, a list) whole or not at all:
removing one of its statements, or every statement pointing at it, removes it
with the statements pointing at it (:func:ontocast.onto.blank_structures.remove_with_structures).
Attributes¶
DATATYPE_PRECEDENCE = (RDF.langString, XSD.string, XSD.decimal, XSD.double, XSD.float, XSD.integer, XSD.nonNegativeInteger, XSD.positiveInteger, XSD.dateTime, XSD.date, XSD.gYearMonth, XSD.gYear, XSD.boolean, XSD.anyURI)
module-attribute
¶
EXAMPLE_NAMESPACE_STEMS = tuple(f'{scheme}://{host}/' for scheme in ('http', 'https') for host in ('example.org', 'example.com', 'example.net'))
module-attribute
¶
GENERIC_CLASS_IRIS = THING_IRIS | frozenset({RDFS.Resource, SKOS.Concept, URIRef('http://www.w3.org/ns/prov#Agent'), URIRef('http://xmlns.com/foaf/0.1/Agent')})
module-attribute
¶
GENERIC_PARENT_IRIS = THING_IRIS | frozenset({RDFS.Resource, OWL.topObjectProperty, OWL.topDataProperty, SKOS.related, SKOS.semanticRelation, DCTERMS.relation, DC.relation})
module-attribute
¶
REFERENCE_AXIOMS = SIDE_PREDICATES | frozenset({RDFS.subClassOf, RDFS.subPropertyOf})
module-attribute
¶
TERM_AXIOMS = frozenset({RDFS.subClassOf, RDFS.subPropertyOf, *SIDE_PREDICATES, OWL.equivalentClass, OWL.equivalentProperty, OWL.inverseOf, OWL.disjointWith})
module-attribute
¶
TERM_TYPES = frozenset({OWL.Class, RDFS.Class, OWL.ObjectProperty, OWL.DatatypeProperty, OWL.AnnotationProperty, RDF.Property, RDFS.Datatype, OWL.Ontology, SKOS.Concept, SKOS.ConceptScheme})
module-attribute
¶
THING_IRIS = frozenset({OWL.Thing, URIRef('http://schema.org/Thing'), URIRef('https://schema.org/Thing')})
module-attribute
¶
__all__ = ['DATATYPE_PRECEDENCE', 'EXAMPLE_NAMESPACE_STEMS', 'GENERIC_CLASS_IRIS', 'GENERIC_PARENT_IRIS', 'REFERENCE_AXIOMS', 'TERM_AXIOMS', 'TERM_TYPES', 'THING_IRIS', 'is_datatype_iri', 'is_datatype_property', 'has_generic_parent', 'list_owned_sides', 'is_generic_class', 'property_side_reasons', 'drop_example_namespace_triples', 'drop_external_subjects', 'drop_non_term_subjects', 'drop_terms_minted_outside_home', 'drop_vacuous_upper_terms', 'normalize_datatype_ranges', 'repair_undeclared_owned_references']
module-attribute
¶
Functions:¶
drop_example_namespace_triples(graph, current_domain)
¶
Remove the triples that name an IRI under a reserved example domain.
IRIs under current_domain are the deployment's own and are kept even
when it is an example domain (a test or demo deployment).
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
drop_external_subjects(graph, external_stems)
¶
Remove the triples whose subject lies in an external namespace.
external_stems are namespace stems without a trailing / or #
(an ontology IRI, its namespace). A subject is external when its own
namespace stem is one of them or lies under one (<stem>/...).
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
drop_non_term_subjects(graph, *, declared, facts_namespace)
¶
Remove the triples whose subject is not a term or a value set.
A named subject stays when it is typed as one of :data:TERM_TYPES, or is
untyped and either in declared (catalog terms and ontology IRIs) or
carries one of :data:TERM_AXIOMS. A subject under facts_namespace
never stays, whatever its type.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
drop_role_crossing_parents(graph, *, catalog_classes, catalog_properties)
¶
Remove a parent of the other kind: a class above a property, or the reverse.
A parent's kind comes from the graph's own declarations first, then the
catalog, then :func:ontocast.onto.well_known.well_known_role. A
datatype is neither a class nor a property parent. A parent whose kind
is unknown is kept.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
drop_terms_minted_outside_home(graph, *, home_namespace, catalog_terms, current_domain)
¶
Remove the statements about a term newly declared outside home_namespace.
A term is newly declared when the catalog does not hold it and the graph
either types it as one of :data:TERM_TYPES (ontology headers aside) or,
untyped, gives it one of :data:TERM_AXIOMS. A term of a well-known
vocabulary is never newly declared: an axiom about one is a reuse, and
the partition decides where it goes.
Under current_domain such a term is a second copy of one the document
already named, so every triple naming it goes, and no axiom points at a
term that is not written. Outside it the term is another vocabulary's: the
statements about it go and a reference to it stays, as it would had the
render not declared it.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
drop_vacuous_upper_terms(graph)
¶
Remove Thing as a range or domain, and as a parent beside another parent.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
has_generic_parent(term, graphs)
¶
True when every parent of term is in :data:GENERIC_PARENT_IRIS.
A term with no parent is not counted: a missing parent is not a broad one.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
is_datatype_iri(iri)
¶
True for an XSD datatype, rdfs:Literal or rdf:langString.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
is_datatype_property(prop, graphs)
¶
A property declared owl:DatatypeProperty or ranging over a datatype.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
is_generic_class(iri)
¶
True for an upper class too general for a property's domain or range.
Members of :data:GENERIC_CLASS_IRIS, and any class whose local name is
exactly Agent: a vocabulary's own upper agent class.
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
list_owned_sides(graph, owned)
¶
Restate an owned property's class domain and range as expected classes.
p rdfs:domain C becomes p schema:domainIncludes C and
p rdfs:range C becomes p schema:rangeIncludes C when owned(p).
A side written for one document is a claim about every later use of the
property; a listed class claims only that it is one of the expected
kinds, and later documents add theirs. A datatype range stays
rdfs:range: it decides what a literal is. Properties not owned (the
catalog's, the well-known vocabularies') keep their authors' axioms.
Returns:
| Type | Description |
|---|---|
int
|
The number of sides restated. |
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
normalize_datatype_ranges(graph)
¶
Keep one datatype range per property; return the properties changed.
rdfs:Literal goes when a concrete datatype is present; of several
concrete ones, the first in :data:DATATYPE_PRECEDENCE stays (else the
lowest IRI).
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
property_side_reasons(prop, graphs)
¶
Why a minted property's domain or range says too little.
generic_domain / generic_range for a side whose every value is a
generic class (:func:is_generic_class; a datatype property's range is a
datatype and is never generic), no_domain / no_range for a
missing side. Empty when both sides are specific.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop
|
URIRef
|
The property. |
required |
graphs
|
Iterable[Graph]
|
Where its sides ( |
required |
Source code in ontocast/tool/ontology_validation/axiom_hygiene.py
repair_undeclared_owned_references(graphs, *, known, current_domain)
¶
Repair a side or parent naming an IRI of the deployment that nothing declares.
The deployment knows every term of its own namespaces: those in known
(the catalog, the ontology IRIs) and those graphs declare. Any other
IRI under current_domain in a side or a parent was made up by a
render. It is rewritten to the one known or declared term with the same
local name, else removed, which leaves the side or parent missing.
IRIs outside current_domain are never touched: an external
vocabulary may hold terms the catalog does not list.
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
|