Skip to content

Commit

Permalink
removed optional arguments from abstract methods of OWLReasoner
Browse files Browse the repository at this point in the history
  • Loading branch information
alkidbaci committed Sep 9, 2024
1 parent c12c3a7 commit e48c25d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
37 changes: 15 additions & 22 deletions owlapy/abstracts/abstract_owl_reasoner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""OWL Reasoner"""
from abc import ABCMeta, abstractmethod
from typing import Iterable

import logging

from owlapy.class_expression import OWLClassExpression
from owlapy.class_expression import OWLClass
Expand All @@ -12,6 +12,8 @@
from owlapy.owl_individual import OWLNamedIndividual
from owlapy.owl_literal import OWLLiteral

logger = logging.getLogger(__name__)


class OWLReasoner(metaclass=ABCMeta):
"""An OWLReasoner reasons over a set of axioms (the set of reasoner axioms) that is based on the imports closure of
Expand Down Expand Up @@ -79,13 +81,12 @@ def object_property_ranges(self, pe: OWLObjectProperty, direct: bool = False) ->
pass

@abstractmethod
def equivalent_classes(self, ce: OWLClassExpression, only_named: bool = True) -> Iterable[OWLClassExpression]:
def equivalent_classes(self, ce: OWLClassExpression) -> Iterable[OWLClassExpression]:
"""Gets the class expressions that are equivalent to the specified class expression with respect to the set of
reasoner axioms.
Args:
ce: The class expression whose equivalent classes are to be retrieved.
only_named: Whether to only retrieve named equivalent classes or also complex class expressions.
Returns:
All class expressions C where the root ontology imports closure entails EquivalentClasses(ce C). If ce is
Expand All @@ -96,13 +97,12 @@ def equivalent_classes(self, ce: OWLClassExpression, only_named: bool = True) ->
pass

@abstractmethod
def disjoint_classes(self, ce: OWLClassExpression, only_named: bool = True) -> Iterable[OWLClassExpression]:
def disjoint_classes(self, ce: OWLClassExpression) -> Iterable[OWLClassExpression]:
"""Gets the class expressions that are disjoint with specified class expression with respect to the set of
reasoner axioms.
Args:
ce: The class expression whose disjoint classes are to be retrieved.
only_named: Whether to only retrieve named disjoint classes or also complex class expressions.
Returns:
All class expressions D where the set of reasoner axioms entails EquivalentClasses(D ObjectComplementOf(ce))
Expand Down Expand Up @@ -167,15 +167,13 @@ def equivalent_data_properties(self, dp: OWLDataProperty) -> Iterable[OWLDataPro
pass

@abstractmethod
def data_property_values(self, e: OWLEntity, pe: OWLDataProperty, direct: bool = True) \
def data_property_values(self, e: OWLEntity, pe: OWLDataProperty) \
-> Iterable['OWLLiteral']:
"""Gets the data property values for the specified entity and data property expression.
Args:
e: The owl entity (usually an individual) that is the subject of the data property values.
pe: The data property expression whose values are to be retrieved for the specified entity.
direct: Specifies if the direct values should be retrieved (True), or if all values should be retrieved
(False), so that sub properties are taken into account.
Note: Can be used to get values, for example, of 'label' property of owl entities such as classes and properties
too (not only individuals).
Expand All @@ -187,15 +185,13 @@ def data_property_values(self, e: OWLEntity, pe: OWLDataProperty, direct: bool =
pass

@abstractmethod
def object_property_values(self, ind: OWLNamedIndividual, pe: OWLObjectPropertyExpression, direct: bool = True) \
def object_property_values(self, ind: OWLNamedIndividual, pe: OWLObjectPropertyExpression) \
-> Iterable[OWLNamedIndividual]:
"""Gets the object property values for the specified individual and object property expression.
Args:
ind: The individual that is the subject of the object property values.
pe: The object property expression whose values are to be retrieved for the specified individual.
direct: Specifies if the direct values should be retrieved (True), or if all values should be retrieved
(False), so that sub properties are taken into account.
Returns:
The named individuals such that for each individual j, the set of reasoner axioms entails
Expand All @@ -221,7 +217,7 @@ def instances(self, ce: OWLClassExpression, direct: bool = False) -> Iterable[OW
pass

@abstractmethod
def sub_classes(self, ce: OWLClassExpression, direct: bool = False, only_named: bool = True) \
def sub_classes(self, ce: OWLClassExpression, direct: bool = False) \
-> Iterable[OWLClassExpression]:
"""Gets the set of named classes that are the strict (potentially direct) subclasses of the specified class
expression with respect to the reasoner axioms.
Expand All @@ -230,7 +226,6 @@ def sub_classes(self, ce: OWLClassExpression, direct: bool = False, only_named:
ce: The class expression whose strict (direct) subclasses are to be retrieved.
direct: Specifies if the direct subclasses should be retrieved (True) or if the all subclasses
(descendant) classes should be retrieved (False).
only_named: Whether to only retrieve named sub-classes or also complex class expressions.
Returns:
If direct is True, each class C where reasoner axioms entails DirectSubClassOf(C, ce). If direct is False,
Expand Down Expand Up @@ -363,7 +358,7 @@ def get_root_ontology(self) -> OWLOntology:
pass

@abstractmethod
def super_classes(self, ce: OWLClassExpression, direct: bool = False, only_named: bool = True) \
def super_classes(self, ce: OWLClassExpression, direct: bool = False) \
-> Iterable[OWLClassExpression]:
"""Gets the stream of named classes that are the strict (potentially direct) super classes of the specified
class expression with respect to the imports closure of the root ontology.
Expand All @@ -372,14 +367,15 @@ class expression with respect to the imports closure of the root ontology.
ce: The class expression whose strict (direct) super classes are to be retrieved.
direct: Specifies if the direct super classes should be retrieved (True) or if the all super classes
(ancestors) classes should be retrieved (False).
only_named: Whether to only retrieve named super classes or also complex class expressions.
Returns:
If direct is True, each class C where the set of reasoner axioms entails DirectSubClassOf(ce, C).
If direct is False, each class C where set of reasoner axioms entails StrictSubClassOf(ce, C).
If ce is equivalent to owl:Thing then nothing will be returned.
"""
pass


class OWLReasonerEx(OWLReasoner, metaclass=ABCMeta):
"""Extra convenience methods for OWL Reasoners"""

Expand All @@ -399,24 +395,21 @@ def data_property_ranges(self, pe: OWLDataProperty, direct: bool = False) -> Ite
yield ax.get_range()
if not direct:
logger.warning("indirect not implemented")
# TODO:

# default
def all_data_property_values(self, pe: OWLDataProperty, direct: bool = True) -> Iterable[OWLLiteral]:
def all_data_property_values(self, pe: OWLDataProperty) -> Iterable[OWLLiteral]:
"""Gets all values for the given data property expression that appear in the knowledge base.
Args:
pe: The data property expression whose values are to be retrieved
direct: Specifies if only the direct values of the data property pe should be retrieved (True), or if
the values of sub properties of pe should be taken into account (False).
Returns:
A set of OWLLiterals containing literals such that for each literal l in the set, the set of reasoner
axioms entails DataPropertyAssertion(pe ind l) for any ind.
"""
onto = self.get_root_ontology()
for ind in onto.individuals_in_signature():
for lit in self.data_property_values(ind, pe, direct):
for lit in self.data_property_values(ind, pe):
yield lit

# default
Expand Down Expand Up @@ -456,7 +449,7 @@ def ind_object_properties(self, ind: OWLNamedIndividual, direct: bool = True) ->
onto = self.get_root_ontology()
for op in onto.object_properties_in_signature():
try:
next(iter(self.object_property_values(ind, op, direct)))
next(iter(self.object_property_values(ind, op)))
yield op
except StopIteration:
pass
pass
14 changes: 5 additions & 9 deletions owlapy/owl_reasoner.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@

_P = TypeVar('_P', bound=OWLPropertyExpression)

# TODO:CD:The name of the classes defined with metaclass=ABCMeta should reflect that
# TODO:CD: An instance cannot be created from those classes.
# TODO:CD: We should move those Abstract Base Classes into a respective package, e.g.
# TODO:CD: owlapy/abstract_owl_reasoner/abstract_owl_reasoner.py should contain OWLReasoner and OWLReasonerEx


class OntologyReasoner(OWLReasonerEx):
__slots__ = '_ontology', '_world'
Expand Down Expand Up @@ -1149,7 +1144,7 @@ def _retrieve_triples(self, pe: OWLPropertyExpression) -> Iterable:
yield from relations


class SyncReasoner:
class SyncReasoner(OWLReasonerEx):

def __init__(self, ontology: Union[SyncOntology, str], reasoner="HermiT"):
"""
Expand Down Expand Up @@ -1633,9 +1628,10 @@ def infer_axioms_and_save(self, output_path: str = None, output_format: str = No

def generate_and_save_inferred_class_assertion_axioms(self, output="temp.ttl", output_format: str = None):
"""
Generates inferred class assertion axioms for the ontology managed by this instance's reasoner and saves them to a file.
This function uses the OWL API to generate inferred class assertion axioms based on the ontology and reasoner
associated with this instance. The inferred axioms are saved to the specified output file in the desired format.
Generates inferred class assertion axioms for the ontology managed by this instance's reasoner and saves them
to a file. This function uses the OWL API to generate inferred class assertion axioms based on the ontology
and reasoner associated with this instance. The inferred axioms are saved to the specified output file in
the desired format.
Parameters:
-----------
output : str, optional
Expand Down

0 comments on commit e48c25d

Please sign in to comment.