From 254e164f162a3c28f162b5d18bd4b7776bc275bc Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Fri, 8 Nov 2024 17:20:04 +0100 Subject: [PATCH] RuntimeErrors are included in all restrictions if types are not matching. Hash functions are taking a tuple where the first item corresponds to the string represenntation of the class --- owlapy/class_expression/restriction.py | 63 ++++++++++++++------------ tests/test_hashing.py | 3 +- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/owlapy/class_expression/restriction.py b/owlapy/class_expression/restriction.py index dfe697db..79ed3f8f 100644 --- a/owlapy/class_expression/restriction.py +++ b/owlapy/class_expression/restriction.py @@ -276,7 +276,7 @@ def __eq__(self, other): raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") def __hash__(self): - return hash((self._filler, self._property)) + return hash(("OWLObjectSomeValuesFrom",self._filler, self._property)) def get_property(self) -> OWLObjectPropertyExpression: # documented in parent @@ -301,10 +301,10 @@ def __eq__(self, other): if type(other) is type(self): return self._filler == other._filler and self._property == other._property else: - return False + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") def __hash__(self): - return hash((self._filler, self._property)) + return hash(("OWLObjectAllValuesFrom",self._filler, self._property)) def get_property(self) -> OWLObjectPropertyExpression: # documented in parent @@ -340,10 +340,11 @@ def __eq__(self, other): if type(other) is type(self): return self._property == other._property else: - return False + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") + def __hash__(self): - return hash(self._property) + return hash(("OWLObjectHasSelf", self._property)) def __repr__(self): return f'OWLObjectHasSelf({self._property})' @@ -431,13 +432,14 @@ def as_object_union_of(self) -> OWLClassExpression: return OWLObjectUnionOf(map(lambda _: OWLObjectOneOf(_), self.individuals())) def __hash__(self): - return hash(self._values) + return hash(("OWLObjectOneOf", self._values)) def __eq__(self, other): if type(other) is type(self): return self._values == other._values else: - return False + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") + def __repr__(self): return f'OWLObjectOneOf({self._values})' @@ -500,10 +502,12 @@ def __eq__(self, other): return self._property == other._property \ and self._cardinality == other._cardinality \ and self._filler == other._filler - return NotImplemented + else: + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") + def __hash__(self): - return hash((self._property, self._cardinality, self._filler)) + return hash(("OWLDataCardinalityRestriction",self._property, self._cardinality, self._filler)) class OWLDataMinCardinality(OWLDataCardinalityRestriction): @@ -618,9 +622,10 @@ def __eq__(self, other): if type(other) is type(self): return self._filler == other._filler and self._property == other._property else: - return False + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") + def __hash__(self): - return hash((self._filler, self._property)) + return hash(("OWLDataSomeValuesFrom",self._filler, self._property)) def get_property(self) -> OWLDataPropertyExpression: # documented in parent @@ -661,10 +666,10 @@ def __eq__(self, other): if type(other) is type(self): return self._filler == other._filler and self._property == other._property else: - return False + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") def __hash__(self): - return hash((self._filler, self._property)) + return hash(("OWLDataAllValuesFrom",self._filler, self._property)) def get_property(self) -> OWLDataPropertyExpression: # documented in parent @@ -707,7 +712,7 @@ def __eq__(self, other): return NotImplemented def __hash__(self): - return hash((self._v, self._property)) + return hash(("OWLDataHasValue",self._v, self._property)) def as_some_values_from(self) -> OWLClassExpression: """A convenience method that obtains this restriction as an existential restriction with a nominal filler. @@ -736,6 +741,18 @@ def __init__(self, values: Union[OWLLiteral, Iterable[OWLLiteral]]): for _ in values: assert isinstance(_, OWLLiteral) self._values = tuple(values) + def __repr__(self): + return f'OWLDataOneOf({self._values})' + + def __hash__(self): + return hash(("OWLDataOneOf",self._values)) + + def __eq__(self, other): + if type(other) is type(self): + return {i for i in self._values} == {j for j in other._values} + else: + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") + # TODO:CD: define it as @property as the name of the class method does not correspond to an action def values(self) -> Iterable[OWLLiteral]: """Gets the values that are in the oneOf. @@ -749,18 +766,6 @@ def operands(self) -> Iterable[OWLLiteral]: # documented in parent yield from self.values() - def __hash__(self): - return hash(self._values) - - def __eq__(self, other): - if type(other) is type(self): - return {i for i in self._values} == {j for j in other._values} - else: - return False - - def __repr__(self): - return f'OWLDataOneOf({self._values})' - class OWLDatatypeRestriction(OWLDataRange): """A datatype restriction DatatypeRestriction( DT F1 lt1 ... Fn ltn ) consists of a unary datatype DT and n pairs @@ -828,10 +833,12 @@ def get_facet_value(self) -> 'OWLLiteral': def __eq__(self, other): if type(other) is type(self): return self._facet == other._facet and self._literal == other._literal - return NotImplemented + else: + raise RuntimeError(f"Invalid equality checking:{self} cannot be compared with {other}") + def __hash__(self): - return hash((self._facet, self._literal)) + return hash(("OWLFacetRestriction",self._facet, self._literal)) def __repr__(self): return f'OWLFacetRestriction({self._facet}, {repr(self._literal)})' diff --git a/tests/test_hashing.py b/tests/test_hashing.py index 22b7489e..f30dfc72 100644 --- a/tests/test_hashing.py +++ b/tests/test_hashing.py @@ -46,8 +46,7 @@ def test_el_description_logic_hash(self): for op in properties: # OWLObjectSomeValuesFrom can be used as a key. memory[OWLObjectSomeValuesFrom(property=op, filler=ac)] = OWLObjectSomeValuesFrom(property=op, filler=ac) - # TODO: https://github.com/dice-group/owlapy/issues/103 - # memory[OWLObjectAllValuesFrom(property=op, filler=ac)] = OWLObjectAllValuesFrom(property=op, filler=ac) + memory[OWLObjectAllValuesFrom(property=op, filler=ac)] = OWLObjectAllValuesFrom(property=op, filler=ac) for k, v in memory.items(): assert k == v \ No newline at end of file