From 13e2ca3ffa85ac8433ba18b921c5c8dd00c9268d Mon Sep 17 00:00:00 2001 From: Luke Friedrichs Date: Fri, 8 Nov 2024 09:46:23 +0100 Subject: [PATCH 1/4] removed slots --- owlapy/owl_reasoner.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/owlapy/owl_reasoner.py b/owlapy/owl_reasoner.py index 211460d9..fc9cae95 100644 --- a/owlapy/owl_reasoner.py +++ b/owlapy/owl_reasoner.py @@ -36,15 +36,6 @@ class StructuralReasoner(AbstractOWLReasoner): """Tries to check instances fast (but maybe incomplete).""" - __slots__ = '_ontology', '_world', \ - '_ind_set', '_cls_to_ind', \ - '_has_prop', 'class_cache', \ - '_objectsomevalues_cache', '_datasomevalues_cache', '_objectcardinality_cache', \ - '_property_cache', \ - '_obj_prop', '_obj_prop_inv', '_data_prop', \ - '_negation_default', '_sub_properties', \ - '__warned' - _ontology: Ontology _world: owlready2.World _cls_to_ind: Dict[OWLClass, FrozenSet[OWLNamedIndividual]] # Class => individuals From fb67bf358fed6a925a54d9465abf98d243c03677 Mon Sep 17 00:00:00 2001 From: Luke Friedrichs Date: Fri, 8 Nov 2024 10:10:21 +0100 Subject: [PATCH 2/4] lazy compute all individuals + remove LRU cache --- owlapy/owl_reasoner.py | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/owlapy/owl_reasoner.py b/owlapy/owl_reasoner.py index fc9cae95..7654844d 100644 --- a/owlapy/owl_reasoner.py +++ b/owlapy/owl_reasoner.py @@ -40,13 +40,9 @@ class StructuralReasoner(AbstractOWLReasoner): _world: owlready2.World _cls_to_ind: Dict[OWLClass, FrozenSet[OWLNamedIndividual]] # Class => individuals _has_prop: Mapping[Type[_P], LRUCache[_P, FrozenSet[OWLNamedIndividual]]] # Type => Property => individuals - _ind_set: FrozenSet[OWLNamedIndividual] # ObjectSomeValuesFrom => individuals - _objectsomevalues_cache: LRUCache[OWLClassExpression, FrozenSet[OWLNamedIndividual]] # DataSomeValuesFrom => individuals - _datasomevalues_cache: LRUCache[OWLClassExpression, FrozenSet[OWLNamedIndividual]] # ObjectCardinalityRestriction => individuals - _objectcardinality_cache: LRUCache[OWLClassExpression, FrozenSet[OWLNamedIndividual]] # ObjectProperty => { individual => individuals } _obj_prop: Dict[OWLObjectProperty, Mapping[OWLNamedIndividual, Set[OWLNamedIndividual]]] # ObjectProperty => { individual => individuals } @@ -81,12 +77,6 @@ def __init__(self, ontology: AbstractOWLOntology, *, class_cache: bool = True, self._init() def _init(self, cache_size=128): - - individuals = self._ontology.individuals_in_signature() - self._ind_set = frozenset(individuals) - self._objectsomevalues_cache = LRUCache(maxsize=cache_size) - self._datasomevalues_cache = LRUCache(maxsize=cache_size) - self._objectcardinality_cache = LRUCache(maxsize=cache_size) if self.class_cache: self._cls_to_ind = dict() @@ -96,11 +86,12 @@ def _init(self, cache_size=128): self._data_prop = dict() else: self._has_prop = MappingProxyType({ - OWLDataProperty: LRUCache(maxsize=cache_size), - OWLObjectProperty: LRUCache(maxsize=cache_size), - OWLObjectInverseOf: LRUCache(maxsize=cache_size), + OWLDataProperty: dict(), + OWLObjectProperty: dict(), + OWLObjectInverseOf: dict(), }) + def reset(self): """The reset method shall reset any cached state.""" self._init() @@ -627,7 +618,8 @@ def _lazy_cache_obj_prop(self, pe: OWLObjectPropertyExpression) -> None: opc[s] = set() opc[s] |= {o} else: - for s in self._ind_set: + all_ = frozenset(self._ontology.individuals_in_signature()) + for s in all_: individuals = set(self.object_property_values(s, pe, not self._sub_properties)) if individuals: opc[s] = individuals @@ -667,8 +659,8 @@ def _some_values_subject_index(self, pe: OWLPropertyExpression) -> FrozenSet[OWL func = self.data_property_values else: func = self.object_property_values - - for s in self._ind_set: + all_ = frozenset(self._ontology.individuals_in_signature()) + for s in all_: try: next(iter(func(s, pe, not self._sub_properties))) subs |= {s} @@ -773,9 +765,6 @@ def _(self, ce: OWLObjectIntersectionOf) -> FrozenSet[OWLNamedIndividual]: @_find_instances.register def _(self, ce: OWLObjectSomeValuesFrom) -> FrozenSet[OWLNamedIndividual]: - if ce in self._objectsomevalues_cache: - return self._objectsomevalues_cache[ce] - p = ce.get_property() assert isinstance(p, OWLObjectPropertyExpression) if not self._property_cache and ce.get_filler().is_owl_thing(): @@ -785,13 +774,12 @@ def _(self, ce: OWLObjectSomeValuesFrom) -> FrozenSet[OWLNamedIndividual]: ind = self._find_some_values(p, filler_ind) - self._objectsomevalues_cache[ce] = ind return ind @_find_instances.register def _(self, ce: OWLObjectComplementOf) -> FrozenSet[OWLNamedIndividual]: if self._negation_default: - all_ = self._ind_set + all_ = frozenset(self._ontology.individuals_in_signature()) complement_ind = self._find_instances(ce.get_operand()) return all_ ^ complement_ind else: @@ -827,7 +815,7 @@ def _(self, ce: OWLObjectMinCardinality) -> FrozenSet[OWLNamedIndividual]: @_find_instances.register def _(self, ce: OWLObjectMaxCardinality) -> FrozenSet[OWLNamedIndividual]: - all_ = self._ind_set + all_ = frozenset(self._ontology.individuals_in_signature()) min_ind = self._find_instances(OWLObjectMinCardinality(cardinality=ce.get_cardinality() + 1, property=ce.get_property(), filler=ce.get_filler())) @@ -838,9 +826,6 @@ def _(self, ce: OWLObjectExactCardinality) -> FrozenSet[OWLNamedIndividual]: return self._get_instances_object_card_restriction(ce) def _get_instances_object_card_restriction(self, ce: OWLObjectCardinalityRestriction): - if ce in self._objectcardinality_cache: - return self._objectcardinality_cache[ce] - p = ce.get_property() assert isinstance(p, OWLObjectPropertyExpression) @@ -862,14 +847,10 @@ def _get_instances_object_card_restriction(self, ce: OWLObjectCardinalityRestric ind = self._find_some_values(p, filler_ind, min_count=min_count, max_count=max_count) - self._objectcardinality_cache[ce] = ind return ind @_find_instances.register def _(self, ce: OWLDataSomeValuesFrom) -> FrozenSet[OWLNamedIndividual]: - if ce in self._datasomevalues_cache: - return self._datasomevalues_cache[ce] - pe = ce.get_property() filler = ce.get_filler() assert isinstance(pe, OWLDataProperty) @@ -958,7 +939,6 @@ def include(lv: OWLLiteral): raise ValueError r = frozenset(ind) - self._datasomevalues_cache[ce] = r return r @_find_instances.register From a4ee29930e89794067342a6a4cd256dfe33ec329 Mon Sep 17 00:00:00 2001 From: Luke Friedrichs Date: Fri, 8 Nov 2024 10:21:08 +0100 Subject: [PATCH 3/4] move type annotations into init --- owlapy/owl_reasoner.py | 59 ++++++++++++++++-------------------------- 1 file changed, 22 insertions(+), 37 deletions(-) diff --git a/owlapy/owl_reasoner.py b/owlapy/owl_reasoner.py index 7654844d..6333f2e1 100644 --- a/owlapy/owl_reasoner.py +++ b/owlapy/owl_reasoner.py @@ -36,24 +36,6 @@ class StructuralReasoner(AbstractOWLReasoner): """Tries to check instances fast (but maybe incomplete).""" - _ontology: Ontology - _world: owlready2.World - _cls_to_ind: Dict[OWLClass, FrozenSet[OWLNamedIndividual]] # Class => individuals - _has_prop: Mapping[Type[_P], LRUCache[_P, FrozenSet[OWLNamedIndividual]]] # Type => Property => individuals - # ObjectSomeValuesFrom => individuals - # DataSomeValuesFrom => individuals - # ObjectCardinalityRestriction => individuals - # ObjectProperty => { individual => individuals } - _obj_prop: Dict[OWLObjectProperty, Mapping[OWLNamedIndividual, Set[OWLNamedIndividual]]] - # ObjectProperty => { individual => individuals } - _obj_prop_inv: Dict[OWLObjectProperty, Mapping[OWLNamedIndividual, Set[OWLNamedIndividual]]] - # DataProperty => { individual => literals } - _data_prop: Dict[OWLDataProperty, Mapping[OWLNamedIndividual, Set[OWLLiteral]]] - class_cache: bool - _property_cache: bool - _negation_default: bool - _sub_properties: bool - def __init__(self, ontology: AbstractOWLOntology, *, class_cache: bool = True, property_cache: bool = True, negation_default: bool = True, sub_properties: bool = False): """Fast instance checker. @@ -67,31 +49,34 @@ def __init__(self, ontology: AbstractOWLOntology, *, class_cache: bool = True, """ super().__init__(ontology) assert isinstance(ontology, Ontology) - self._world = ontology._world - self._ontology = ontology - self.class_cache = class_cache - self._property_cache = property_cache - self._negation_default = negation_default - self._sub_properties = sub_properties - self.__warned = 0 + self._world: owlready2.World = ontology._world + self._ontology: Ontology = ontology + self.class_cache: bool = class_cache + self._property_cache: bool = property_cache + self._negation_default: bool = negation_default + self._sub_properties: bool = sub_properties + self.__warned: int = 0 self._init() - def _init(self, cache_size=128): + def _init(self): if self.class_cache: - self._cls_to_ind = dict() + # Class => individuals + self._cls_to_ind: Dict[OWLClass, FrozenSet[OWLNamedIndividual]] = {} if self._property_cache: - self._obj_prop = dict() - self._obj_prop_inv = dict() - self._data_prop = dict() + # ObjectProperty => { individual => individuals } + self._obj_prop: Dict[OWLObjectProperty, Mapping[OWLNamedIndividual, Set[OWLNamedIndividual]]] = dict() + # ObjectProperty => { individual => individuals } + self._obj_prop_inv: Dict[OWLObjectProperty, Mapping[OWLNamedIndividual, Set[OWLNamedIndividual]]] = dict() + # DataProperty => { individual => literals } + self._data_prop: Dict[OWLDataProperty, Mapping[OWLNamedIndividual, Set[OWLLiteral]]] = dict() else: - self._has_prop = MappingProxyType({ - OWLDataProperty: dict(), - OWLObjectProperty: dict(), - OWLObjectInverseOf: dict(), - }) - - + self._has_prop: Mapping[Type[_P], Dict[_P, FrozenSet[OWLNamedIndividual]]] = { + OWLDataProperty: {}, + OWLObjectProperty: {}, + OWLObjectInverseOf: {}, + } + def reset(self): """The reset method shall reset any cached state.""" self._init() From df6b6c1804171d51568ae98b62c355d342335ece Mon Sep 17 00:00:00 2001 From: Luke Friedrichs Date: Fri, 8 Nov 2024 10:25:05 +0100 Subject: [PATCH 4/4] removed LRU import --- owlapy/owl_reasoner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/owlapy/owl_reasoner.py b/owlapy/owl_reasoner.py index 6333f2e1..2589b91e 100644 --- a/owlapy/owl_reasoner.py +++ b/owlapy/owl_reasoner.py @@ -27,7 +27,7 @@ OWLPropertyExpression, OWLDataPropertyExpression from owlapy.owl_individual import OWLNamedIndividual from owlapy.owl_literal import OWLLiteral -from owlapy.utils import LRUCache, run_with_timeout +from owlapy.utils import run_with_timeout from owlapy.abstracts.abstract_owl_reasoner import AbstractOWLReasoner logger = logging.getLogger(__name__)