Ontology

class pyhornedowl.Ontology(index_strategy: IndexCreationStrategy = ...)

Bases: object

Represents a loaded ontology.

add_annotation(entity: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | AnonymousIndividual, property: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool], value: Literal | str | bool | int | float | IRI | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | AnonymousIndividual, *, annotations: set[Annotation] | None = None) → bool

Adds an annotation assertion. String values are literals; use IRI or a named entity for an IRI value. Returns whether it was newly inserted.

add_axiom(ax: AnnotatedComponent | Component, *, ignore_annotations: bool = False) → bool

Synonym for add_component.

add_axioms(axioms: Sequence[AnnotatedComponent | Component], *, ignore_annotations: bool = False) → int

synonym for add_components.

add_component(component: AnnotatedComponent | Component, *, ignore_annotations: bool = False) → bool

Adds a component. Returns whether it was new.

With ignore_annotations=True, skips insertion if the body exists, regardless of its annotations.

add_components(components: Sequence[AnnotatedComponent | Component], *, ignore_annotations: bool = False) → int

Adds components and returns the number newly inserted.

With ignore_annotations=True, component which are already present are skipped.

add_prefix_mapping(iriprefix: str, mappedid: str) → None

Adds the prefix iriprefix.

annotation_property(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → AnnotationProperty

Convenience method to create an annotationProperty from an IRI.

Uses the iri method to cache native IRI instances.

anonymous_individual(name: str) → AnonymousIndividual

Convenience method to create an AnonymousIndividual from a string.

build_component_index() → None

Builds an index by component kind (ComponentMappedIndex).

build_indexes() → None

Builds indexes to allow (a quicker) access to axioms and entities.

build_iri_index() → None

Builds an index by iri (IRIMappedIndex).

class_(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → Class

Convenience method to create a Class from an IRI.

Uses the iri method to cache native IRI instances.

clazz(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → Class

Convenience method to create a Class from an IRI.

Uses the iri method to cache native IRI instances.

Deprecated since version Use: Ontology.class_ instead

contains_axiom(axiom: AnnotatedComponent | Component, *, ignore_annotations: bool = False) → bool

Checks exact membership by default. A bare axiom means no annotations. With ignore_annotations=True, compares only the axiom body.

contains_entity(entity: 'NamedEntity') → bool

Checks whether an entity occurs in the ontology.

curie(curie: str) → IRI

Creates a new IRI from CURIE string.

Use this method instead of model.IRI.parse if possible as it is more optimized using caches.

data_property(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → DataProperty

Convenience method to create a DataProperty from an IRI.

Uses the iri method to cache native IRI instances.

datatype(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → Datatype

Convenience method to create a datatype from an IRI.

Uses the iri method to cache native IRI instances.

declare(entity: 'NamedEntity') → bool

Convenience method to add a Declare(entity) axiom.

Returns whether the entity as newly declared.

declare_annotation_property(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → bool

Convenience method to add a Declare(annotationProperty(iri)) axiom.

Returns whether the annotation property as newly declared.

declare_class(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → bool

Convenience method to add a Declare(Class(iri)) axiom.

Returns whether the class as newly declared.

declare_data_property(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → bool

Convenience method to add a Declare(DataProperty(iri)) axiom.

Returns whether the data property as newly declared.

declare_datatype(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → bool

Convenience method to add a Declare(Datatype(iri)) axiom.

Returns whether the datatype as newly declared.

declare_individual(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → bool

Convenience method to add a Declare(NamedIndividual(iri)) axiom.

Returns whether the individual as newly declared.

declare_object_property(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → bool

Convenience method to add a Declare(ObjectProperty(iri)) axiom.

Returns whether the object property as newly declared.

get_ancestors(child_iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → set[str]

Gets all direct and indirect super classes of a class.

Deprecated since version Use: a reasoner instead. See :doc:reasoner for details.

get_annotation_properties() → set[AnnotationProperty]

Returns the IRIs of all declared annotation properties in the ontology.

get_annotations(subject: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | AnonymousIndividual, property: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool] | None = None) → list[AnnotationValue]

Gets all annotated value for an entity and annotation property.

Note: The order is neither necessarily the same as in the ontology neither is it stable. Get all annotation values with Ontology.get_annotations.

get_axioms() → list[AnnotatedComponent]

Returns all axioms of the ontology.

get_axioms_for_iri(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → list[AnnotatedComponent]

Gets all axioms for an entity.

get_classes() → set[Class]

Returns the IRIs of all declared classes in the ontology.

get_components() → list[AnnotatedComponent]

Returns all axioms of the ontology.

get_components_for_iri(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → list[AnnotatedComponent]

Gets all components (axiom, swrl, and meta component) for an entity.

get_data_properties() → set[DataProperty]

Returns the IRIs of all declared data properties in the ontology.

get_datatypes() → set[Datatype]

Returns the IRIs of all declared datatypes in the ontology.

get_descendants(parent_iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → set[str]

Gets all direct and indirect subclasses of a class.

Deprecated since version Use: a reasoner instead. See :doc:reasoner for details.

get_id_for_iri(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → str | None

Gets the ID of term by it IRI.

If the term does not have an ID, None is returned.

get_iri() → IRI | None

Returns the ontology iri, if it exists.

get_iri_for_id(id: str) → str | None

Gets the IRI of a term by its ID.

If the term does not have an IRI, None is returned.

get_iri_for_label(label: str) → str | None

Returns the IRI of a term by its label if it exists.

If the term does not have a label, None is returned.

get_label(entity: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | AnonymousIndividual, languages: Sequence[str] | None = None, *, default: str | None = None) → str | None

Chooses the first matching preferred language, breaking ties lexically. An empty language selects untagged strings. No implicit fallback.

get_named_individuals() → set[NamedIndividual]

Returns the IRIs of all declared named individuals in the ontology.

get_object_properties() → set[ObjectProperty]

Returns the IRIs of all declared object properties in the ontology.

get_root_classes() → set[str]

Gets all (named) root classes, i.e. all classes with no superclasses (except owl:Thing).

get_subclasses(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → set[str]

Gets all asserted (named) direct subclasses of a class.

get_superclasses(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → set[str]

Gets all asserted (named) direct superclasses of a class.

get_version_iri() → IRI | None

Returns the ontologys version iri, if it exists.

iri(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → IRI

Creates a new IRI from string.

Use this method instead of model.IRI.parse if possible as it is more optimized using caches.

is_declared(entity: 'NamedEntity') → bool

Checks whether an entity has an explicit declaration axiom.

named_individual(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → NamedIndividual

Convenience method to create a NamedIndividual from an IRI.

Uses the iri method to cache native IRI instances.

object_property(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool]) → ObjectProperty

Convenience method to create an ObjectProperty from an IRI.

Uses the iri method to cache native IRI instances.

query_components(kind: type | None = None, including: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool] | None = None) → list[AnnotatedComponent]
remove_annotation(entity: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | AnonymousIndividual, property: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool], value: Literal | str | bool | int | float | IRI | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | AnonymousIndividual) → int

Removes all annotation assertions matching subject, property and value, regardless of assertion/axiom annotations and returns the number of removed axioms.

remove_axiom(ax: AnnotatedComponent | Component, *, ignore_annotations: bool = False) → bool

Synonym for remove_component.

remove_component(component: AnnotatedComponent | Component, *, ignore_annotations: bool = False) → bool

Removes a component from the ontology and returns whether it was present.

With ignore_annotations=True, all components with the same unannotated body are removed, regardless of the annotations

remove_components(components: Sequence[AnnotatedComponent | Component], *, ignore_annotations: bool = False) → int

Removes components from the ontology and returns the number of components removed.

With ignore_annotations=True, components are removed if their unannotated body matches.

save_to_file(file_name: str, serialization: Literal2['owl', 'rdf', 'ofn', 'owx', 'omn', 'obo'] | None = None) → None

Saves the ontology to disk. If no serialization is given it is guessed by the file extension. Defaults to OWL/XML

save_to_string(serialization: Literal2['owl', 'rdf', 'ofn', 'owx', 'omn', 'obo']) → str

Saves the ontology to a UTF8 string.

set_label(iri: IRI | str | Class | ObjectProperty | DataProperty | AnnotationProperty | NamedIndividual | Datatype | tuple[str, bool], label: str, *, language: str | None = None) → None

Replaces all literal rdfs:label assertions in one language scope. The default scope is untagged literals; other languages are preserved. Language matching is case-insensitive and exact. Existing axiom annotations in the replaced scope are removed with their assertions.

signature() → Signature

Calcuates the signature of the ontology.