Skip to content

Commit

Permalink
Merge pull request #74 from AI-Planning/fix/der-pred
Browse files Browse the repository at this point in the history
Override equal for derived predicates
  • Loading branch information
haz authored Jun 5, 2023
2 parents dcb71de + cbe0b3d commit e9669dd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
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)})"
)

0 comments on commit e9669dd

Please sign in to comment.