Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override equal for derived predicates #74

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pddl/logic/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,15 @@ def __str__(self) -> str:

def __repr__(self) -> str:
"""Get the string representation."""
return f"{type(self).__name__}({self.predicate}, {self.condition})"
return f"{type(self).__name__}({repr(self.predicate)}, {repr(self.condition)})"

def __eq__(self, other):
"""Override equal operator."""
return (
isinstance(other, DerivedPredicate)
and self.predicate == other.predicate
and self.condition == other.condition
)

def __lt__(self, other):
"""Compare with another object."""
Expand Down
8 changes: 8 additions & 0 deletions pddl/logic/terms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def type_tags(self) -> AbstractSet[name_type]:
"""Get a set of type tags for this term."""
return self._type_tags

def __eq__(self, other):
"""Compare with another term."""
return (
isinstance(other, Term)
and self.name == other.name
and self.type_tags == other.type_tags
)

def __lt__(self, other):
"""Compare with another term."""
if isinstance(other, Constant):
Expand Down
54 changes: 53 additions & 1 deletion tests/test_predicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

"""This module contains tests for PDDL predicates."""
from pddl.core import Predicate
from pddl.logic.base import And
from pddl.logic.helpers import variables
from pddl.logic.predicates import EqualTo
from pddl.logic.predicates import DerivedPredicate, EqualTo


class TestPredicateSimpleInitialisation:
Expand All @@ -36,6 +37,22 @@ def test_arity(self):
"""Test arity property."""
assert self.predicate.arity == 2

def test_to_equal(self):
"""Test to equal."""
other = Predicate("P", self.a, self.b)
assert self.predicate == other

def test_to_str(self):
"""Test to string."""
assert str(self.predicate) == f"({self.predicate.name} {self.a} {self.b})"

def test_to_repr(self):
"""Test to repr."""
assert (
repr(self.predicate)
== f"Predicate({self.predicate.name}, {self.a}, {self.b})"
)


class TestEqualToPredicate:
"""Test the eaual to predicate."""
Expand Down Expand Up @@ -65,3 +82,38 @@ def test_to_str(self):
def test_to_repr(self):
"""Test to repr."""
assert repr(self.equal_to) == f"EqualTo({repr(self.left)}, {repr(self.right)})"


class TestDerivedPredicate:
"""Test the derived predicate."""

def setup_method(self):
"""Set up the tests."""
self.a, self.b = variables("a b")
self.predicate = Predicate("dp", self.a, self.b)
self.condition = And(self.a, self.b)
self.derived = DerivedPredicate(self.predicate, self.condition)

def test_predicate(self):
"""Test predicate getter."""
assert self.derived.predicate == self.predicate

def test_condition(self):
"""Test condition getter."""
assert self.derived.condition == self.condition

def test_to_equal(self):
"""Test to equal."""
other = DerivedPredicate(self.predicate, self.condition)
assert self.derived == other

def test_to_str(self):
"""Test to string."""
assert str(self.derived) == f"(:derived {self.predicate} {self.condition})"

def test_to_repr(self):
"""Test to repr."""
assert (
repr(self.derived)
== f"DerivedPredicate({repr(self.predicate)}, {repr(self.condition)})"
)