Skip to content

Commit

Permalink
RuntimeErrors are included in all restrictions if types are not match…
Browse files Browse the repository at this point in the history
…ing. Hash functions are taking a tuple where the first item corresponds to the string represenntation of the class
  • Loading branch information
Demirrr committed Nov 8, 2024
1 parent 8678792 commit 254e164
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
63 changes: 35 additions & 28 deletions owlapy/class_expression/restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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})'
Expand Down Expand Up @@ -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})'
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)})'
Expand Down
3 changes: 1 addition & 2 deletions tests/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 254e164

Please sign in to comment.