From 1afb0fa7d2f28fe35a811c63a089f073eb8326df Mon Sep 17 00:00:00 2001 From: Alkid Date: Mon, 9 Sep 2024 16:00:06 +0200 Subject: [PATCH] Moved abstract classes to 'abstracts' directory --- owlapy/abstracts/abstract_owl_ontology.py | 183 ++++++++++++++++++ .../abstract_owl_ontology_manager.py | 70 +++++++ owlapy/abstracts/abstract_owl_reasoner.py | 2 +- owlapy/owl_ontology.py | 174 +---------------- owlapy/owl_ontology_manager.py | 70 +------ owlapy/owl_reasoner.py | 3 +- 6 files changed, 260 insertions(+), 242 deletions(-) create mode 100644 owlapy/abstracts/abstract_owl_ontology.py create mode 100644 owlapy/abstracts/abstract_owl_ontology_manager.py diff --git a/owlapy/abstracts/abstract_owl_ontology.py b/owlapy/abstracts/abstract_owl_ontology.py new file mode 100644 index 00000000..c24f9176 --- /dev/null +++ b/owlapy/abstracts/abstract_owl_ontology.py @@ -0,0 +1,183 @@ +from abc import ABCMeta, abstractmethod +from typing import Final, Iterable, Union, Optional + +from owlapy.class_expression import OWLClass +from owlapy.iri import IRI +from owlapy.owl_axiom import OWLEquivalentClassesAxiom, OWLClassAxiom, OWLDataPropertyDomainAxiom, \ + OWLDataPropertyRangeAxiom, OWLObjectPropertyDomainAxiom, OWLObjectPropertyRangeAxiom, OWLAxiom +from owlapy.owl_individual import OWLNamedIndividual +from owlapy.owl_object import OWLObject +from owlapy.owl_ontology import _M, OWLOntologyID +from owlapy.owl_property import OWLDataProperty, OWLObjectProperty + + +class OWLOntology(OWLObject, metaclass=ABCMeta): + """Represents an OWL 2 Ontology in the OWL 2 specification. + + An OWLOntology consists of a possibly empty set of OWLAxioms and a possibly empty set of OWLAnnotations. + An ontology can have an ontology IRI which can be used to identify the ontology. If it has an ontology IRI then + it may also have an ontology version IRI. Since OWL 2, an ontology need not have an ontology IRI. (See the OWL 2 + Structural Specification). + + An ontology cannot be modified directly. Changes must be applied via its OWLOntologyManager. + """ + __slots__ = () + type_index: Final = 1 + + @abstractmethod + def classes_in_signature(self) -> Iterable[OWLClass]: + """Gets the classes in the signature of this object. + + Returns: + Classes in the signature of this object. + """ + pass + + @abstractmethod + def data_properties_in_signature(self) -> Iterable[OWLDataProperty]: + """Get the data properties that are in the signature of this object. + + Returns: + Data properties that are in the signature of this object. + """ + pass + + @abstractmethod + def object_properties_in_signature(self) -> Iterable[OWLObjectProperty]: + """A convenience method that obtains the object properties that are in the signature of this object. + + Returns: + Object properties that are in the signature of this object. + """ + pass + + @abstractmethod + def individuals_in_signature(self) -> Iterable[OWLNamedIndividual]: + """A convenience method that obtains the individuals that are in the signature of this object. + + Returns: + Individuals that are in the signature of this object. + """ + pass + + @abstractmethod + def equivalent_classes_axioms(self, c: OWLClass) -> Iterable[OWLEquivalentClassesAxiom]: + """ Gets all of the equivalent axioms in this ontology that contain the specified class as an operand. + + Args: + c: The class for which the EquivalentClasses axioms should be retrieved. + + Returns: + EquivalentClasses axioms contained in this ontology. + """ + pass + + @abstractmethod + def general_class_axioms(self) -> Iterable[OWLClassAxiom]: + """Get the general class axioms of this ontology. This includes SubClass axioms with a complex class expression + as the sub class and EquivalentClass axioms and DisjointClass axioms with only complex class expressions. + + Returns: + General class axioms contained in this ontology. + """ + pass + + @abstractmethod + def data_property_domain_axioms(self, property: OWLDataProperty) -> Iterable[OWLDataPropertyDomainAxiom]: + """Gets the OWLDataPropertyDomainAxiom objects where the property is equal to the specified property. + + Args: + property: The property which is equal to the property of the retrieved axioms. + + Returns: + The axioms matching the search. + """ + pass + + @abstractmethod + def data_property_range_axioms(self, property: OWLDataProperty) -> Iterable[OWLDataPropertyRangeAxiom]: + """Gets the OWLDataPropertyRangeAxiom objects where the property is equal to the specified property. + + Args: + property: The property which is equal to the property of the retrieved axioms. + + Returns: + The axioms matching the search. + """ + pass + + @abstractmethod + def object_property_domain_axioms(self, property: OWLObjectProperty) -> Iterable[OWLObjectPropertyDomainAxiom]: + """Gets the OWLObjectPropertyDomainAxiom objects where the property is equal to the specified property. + + Args: + property: The property which is equal to the property of the retrieved axioms. + + Returns: + The axioms matching the search. + """ + pass + + @abstractmethod + def object_property_range_axioms(self, property: OWLObjectProperty) -> Iterable[OWLObjectPropertyRangeAxiom]: + """Gets the OWLObjectPropertyRangeAxiom objects where the property is equal to the specified property. + + Args: + property: The property which is equal to the property of the retrieved axioms. + + Returns: + The axioms matching the search. + """ + pass + + @abstractmethod + def get_owl_ontology_manager(self) -> _M: + """Gets the manager that manages this ontology.""" + pass + + @abstractmethod + def get_ontology_id(self) -> OWLOntologyID: + """Gets the OWLOntologyID belonging to this object. + + Returns: + The OWLOntologyID. + """ + pass + + def is_anonymous(self) -> bool: + """Check whether this ontology does contain an IRI or not.""" + return self.get_ontology_id().is_anonymous() + + @abstractmethod + def add_axiom(self, axiom: Union[OWLAxiom, Iterable[OWLAxiom]]): + """Add the specified axiom/axioms to the ontology. + + Args: + axiom: Can be a single axiom or a collection of axioms. + + Returns: + Nothing. + """ + pass + + @abstractmethod + def remove_axiom(self, axiom: Union[OWLAxiom, Iterable[OWLAxiom]]): + """Removes the specified axiom/axioms to the ontology. + + Args: + axiom: Can be a single axiom or a collection of axioms. + + Returns: + Nothing. + """ + pass + + def save(self, document_iri: Optional[IRI] = None): + """Saves this ontology, using its IRI to determine where/how the ontology should be + saved. + + Args: + document_iri: Whether you want to save in a different location. + + """ + pass diff --git a/owlapy/abstracts/abstract_owl_ontology_manager.py b/owlapy/abstracts/abstract_owl_ontology_manager.py new file mode 100644 index 00000000..891b29a8 --- /dev/null +++ b/owlapy/abstracts/abstract_owl_ontology_manager.py @@ -0,0 +1,70 @@ +from abc import ABCMeta, abstractmethod +from typing import Union + +from owlapy.abstracts.abstract_owl_ontology import OWLOntology +from owlapy.iri import IRI + + +class OWLOntologyChange(metaclass=ABCMeta): + """Represents an ontology change.""" + __slots__ = () + + _ont: OWLOntology + + @abstractmethod + def __init__(self, ontology: OWLOntology): + self._ont = ontology + + def get_ontology(self) -> OWLOntology: + """Gets the ontology that the change is/was applied to. + + Returns: + The ontology that the change is applicable to. + """ + return self._ont + + +class OWLOntologyManager(metaclass=ABCMeta): + """An OWLOntologyManager manages a set of ontologies. It is the main point for creating, loading and accessing + ontologies.""" + + @abstractmethod + def create_ontology(self, iri: Union[str, IRI]) -> OWLOntology: + """Creates a new (empty) ontology that that has the specified ontology IRI (and no version IRI). + + Args: + iri: The IRI of the ontology to be created, can also be a string. + + Returns: + The newly created ontology. + """ + pass + + @abstractmethod + def load_ontology(self, iri: Union[IRI, str]) -> OWLOntology: + """Loads an ontology that is assumed to have the specified ontology IRI as its IRI or version IRI. The ontology + IRI will be mapped to an ontology document IRI. + + Args: + iri: The IRI that identifies the ontology, can also be a string. + It is expected that the ontology will also have this IRI + (although the OWL API should tolerate situations where this is not the case). + + Returns: + The OWLOntology representation of the ontology that was loaded. + """ + pass + + @abstractmethod + def apply_change(self, change: OWLOntologyChange): + """A convenience method that applies just one change to an ontology. When this method is used through an + OWLOntologyManager implementation, the instance used should be the one that the ontology returns through the + get_owl_ontology_manager() call. + + Args: + change: The change to be applied. + + Raises: + ChangeApplied.UNSUCCESSFULLY: if the change was not applied successfully. + """ + pass diff --git a/owlapy/abstracts/abstract_owl_reasoner.py b/owlapy/abstracts/abstract_owl_reasoner.py index 3881d8fd..1ed1e01a 100644 --- a/owlapy/abstracts/abstract_owl_reasoner.py +++ b/owlapy/abstracts/abstract_owl_reasoner.py @@ -8,7 +8,7 @@ from owlapy.class_expression import OWLClass from owlapy.owl_data_ranges import OWLDataRange from owlapy.owl_object import OWLEntity -from owlapy.owl_ontology import OWLOntology +from owlapy.abstracts.abstract_owl_ontology import OWLOntology from owlapy.owl_property import OWLObjectPropertyExpression, OWLDataProperty, OWLObjectProperty from owlapy.owl_individual import OWLNamedIndividual from owlapy.owl_literal import OWLLiteral diff --git a/owlapy/owl_ontology.py b/owlapy/owl_ontology.py index ebd4389a..fdf7acbf 100644 --- a/owlapy/owl_ontology.py +++ b/owlapy/owl_ontology.py @@ -1,5 +1,4 @@ """OWL Ontology""" -from abc import ABCMeta, abstractmethod from functools import singledispatchmethod, singledispatch from itertools import chain, islice, combinations import types @@ -9,6 +8,7 @@ import owlready2 from pandas import Timedelta from owlapy import namespaces +from owlapy.abstracts.abstract_owl_ontology import OWLOntology from owlapy.owl_data_ranges import OWLDataRange, OWLDataComplementOf, OWLDataUnionOf, OWLDataIntersectionOf from owlapy.owl_datatype import OWLDatatype from owlapy.owl_individual import OWLNamedIndividual, OWLIndividual @@ -123,178 +123,6 @@ def __eq__(self, other): return NotImplemented -class OWLOntology(OWLObject, metaclass=ABCMeta): - """Represents an OWL 2 Ontology in the OWL 2 specification. - - An OWLOntology consists of a possibly empty set of OWLAxioms and a possibly empty set of OWLAnnotations. - An ontology can have an ontology IRI which can be used to identify the ontology. If it has an ontology IRI then - it may also have an ontology version IRI. Since OWL 2, an ontology need not have an ontology IRI. (See the OWL 2 - Structural Specification). - - An ontology cannot be modified directly. Changes must be applied via its OWLOntologyManager. - """ - __slots__ = () - type_index: Final = 1 - - @abstractmethod - def classes_in_signature(self) -> Iterable[OWLClass]: - """Gets the classes in the signature of this object. - - Returns: - Classes in the signature of this object. - """ - pass - - @abstractmethod - def data_properties_in_signature(self) -> Iterable[OWLDataProperty]: - """Get the data properties that are in the signature of this object. - - Returns: - Data properties that are in the signature of this object. - """ - pass - - @abstractmethod - def object_properties_in_signature(self) -> Iterable[OWLObjectProperty]: - """A convenience method that obtains the object properties that are in the signature of this object. - - Returns: - Object properties that are in the signature of this object. - """ - pass - - @abstractmethod - def individuals_in_signature(self) -> Iterable[OWLNamedIndividual]: - """A convenience method that obtains the individuals that are in the signature of this object. - - Returns: - Individuals that are in the signature of this object. - """ - pass - - @abstractmethod - def equivalent_classes_axioms(self, c: OWLClass) -> Iterable[OWLEquivalentClassesAxiom]: - """ Gets all of the equivalent axioms in this ontology that contain the specified class as an operand. - - Args: - c: The class for which the EquivalentClasses axioms should be retrieved. - - Returns: - EquivalentClasses axioms contained in this ontology. - """ - pass - - @abstractmethod - def general_class_axioms(self) -> Iterable[OWLClassAxiom]: - """Get the general class axioms of this ontology. This includes SubClass axioms with a complex class expression - as the sub class and EquivalentClass axioms and DisjointClass axioms with only complex class expressions. - - Returns: - General class axioms contained in this ontology. - """ - pass - - @abstractmethod - def data_property_domain_axioms(self, property: OWLDataProperty) -> Iterable[OWLDataPropertyDomainAxiom]: - """Gets the OWLDataPropertyDomainAxiom objects where the property is equal to the specified property. - - Args: - property: The property which is equal to the property of the retrieved axioms. - - Returns: - The axioms matching the search. - """ - pass - - @abstractmethod - def data_property_range_axioms(self, property: OWLDataProperty) -> Iterable[OWLDataPropertyRangeAxiom]: - """Gets the OWLDataPropertyRangeAxiom objects where the property is equal to the specified property. - - Args: - property: The property which is equal to the property of the retrieved axioms. - - Returns: - The axioms matching the search. - """ - pass - - @abstractmethod - def object_property_domain_axioms(self, property: OWLObjectProperty) -> Iterable[OWLObjectPropertyDomainAxiom]: - """Gets the OWLObjectPropertyDomainAxiom objects where the property is equal to the specified property. - - Args: - property: The property which is equal to the property of the retrieved axioms. - - Returns: - The axioms matching the search. - """ - pass - - @abstractmethod - def object_property_range_axioms(self, property: OWLObjectProperty) -> Iterable[OWLObjectPropertyRangeAxiom]: - """Gets the OWLObjectPropertyRangeAxiom objects where the property is equal to the specified property. - - Args: - property: The property which is equal to the property of the retrieved axioms. - - Returns: - The axioms matching the search. - """ - pass - - @abstractmethod - def get_owl_ontology_manager(self) -> _M: - """Gets the manager that manages this ontology.""" - pass - - @abstractmethod - def get_ontology_id(self) -> OWLOntologyID: - """Gets the OWLOntologyID belonging to this object. - - Returns: - The OWLOntologyID. - """ - pass - - def is_anonymous(self) -> bool: - """Check whether this ontology does contain an IRI or not.""" - return self.get_ontology_id().is_anonymous() - - @abstractmethod - def add_axiom(self, axiom: Union[OWLAxiom, Iterable[OWLAxiom]]): - """Add the specified axiom/axioms to the ontology. - - Args: - axiom: Can be a single axiom or a collection of axioms. - - Returns: - Nothing. - """ - pass - - @abstractmethod - def remove_axiom(self, axiom: Union[OWLAxiom, Iterable[OWLAxiom]]): - """Removes the specified axiom/axioms to the ontology. - - Args: - axiom: Can be a single axiom or a collection of axioms. - - Returns: - Nothing. - """ - pass - - def save(self, document_iri: Optional[IRI] = None): - """Saves this ontology, using its IRI to determine where/how the ontology should be - saved. - - Args: - document_iri: Whether you want to save in a different location. - - """ - pass - - def _check_expression(expr: OWLObject, ontology: OWLOntology, world: owlready2.namespace.World): """ @TODO:CD: Documentation diff --git a/owlapy/owl_ontology_manager.py b/owlapy/owl_ontology_manager.py index 2475d623..0f384f05 100644 --- a/owlapy/owl_ontology_manager.py +++ b/owlapy/owl_ontology_manager.py @@ -1,80 +1,16 @@ -from abc import ABCMeta, abstractmethod from typing import Union import jpype import owlready2 +from owlapy.abstracts.abstract_owl_ontology_manager import OWLOntologyChange, OWLOntologyManager from owlapy.iri import IRI from owlapy.meta_classes import HasIRI -from owlapy.owl_ontology import OWLOntology, Ontology, SyncOntology +from owlapy.owl_ontology import Ontology, SyncOntology +from owlapy.abstracts.abstract_owl_ontology import OWLOntology from owlapy.static_funcs import startJVM -class OWLOntologyChange(metaclass=ABCMeta): - """Represents an ontology change.""" - __slots__ = () - - _ont: OWLOntology - - @abstractmethod - def __init__(self, ontology: OWLOntology): - self._ont = ontology - - def get_ontology(self) -> OWLOntology: - """Gets the ontology that the change is/was applied to. - - Returns: - The ontology that the change is applicable to. - """ - return self._ont - - -class OWLOntologyManager(metaclass=ABCMeta): - """An OWLOntologyManager manages a set of ontologies. It is the main point for creating, loading and accessing - ontologies.""" - - @abstractmethod - def create_ontology(self, iri: Union[str, IRI]) -> OWLOntology: - """Creates a new (empty) ontology that that has the specified ontology IRI (and no version IRI). - - Args: - iri: The IRI of the ontology to be created, can also be a string. - - Returns: - The newly created ontology. - """ - pass - - @abstractmethod - def load_ontology(self, iri: Union[IRI, str]) -> OWLOntology: - """Loads an ontology that is assumed to have the specified ontology IRI as its IRI or version IRI. The ontology - IRI will be mapped to an ontology document IRI. - - Args: - iri: The IRI that identifies the ontology, can also be a string. - It is expected that the ontology will also have this IRI - (although the OWL API should tolerate situations where this is not the case). - - Returns: - The OWLOntology representation of the ontology that was loaded. - """ - pass - - @abstractmethod - def apply_change(self, change: OWLOntologyChange): - """A convenience method that applies just one change to an ontology. When this method is used through an - OWLOntologyManager implementation, the instance used should be the one that the ontology returns through the - get_owl_ontology_manager() call. - - Args: - change: The change to be applied. - - Raises: - ChangeApplied.UNSUCCESSFULLY: if the change was not applied successfully. - """ - pass - - class OWLImportsDeclaration(HasIRI): """Represents an import statement in an ontology.""" __slots__ = '_iri' diff --git a/owlapy/owl_reasoner.py b/owlapy/owl_reasoner.py index be190f7f..289deb47 100644 --- a/owlapy/owl_reasoner.py +++ b/owlapy/owl_reasoner.py @@ -20,7 +20,8 @@ from owlapy.owl_data_ranges import OWLDataRange, OWLDataComplementOf, OWLDataUnionOf, OWLDataIntersectionOf from owlapy.owl_datatype import OWLDatatype from owlapy.owl_object import OWLEntity -from owlapy.owl_ontology import OWLOntology, Ontology, _parse_concept_to_owlapy, SyncOntology +from owlapy.owl_ontology import Ontology, _parse_concept_to_owlapy, SyncOntology +from owlapy.abstracts.abstract_owl_ontology import OWLOntology from owlapy.owl_ontology_manager import SyncOntologyManager from owlapy.owl_property import OWLObjectPropertyExpression, OWLDataProperty, OWLObjectProperty, OWLObjectInverseOf, \ OWLPropertyExpression, OWLDataPropertyExpression