Skip to content

Commit

Permalink
fix issue #96 (thanks @marcofavorito) and remove old comments
Browse files Browse the repository at this point in the history
  • Loading branch information
francescofuggitti committed Oct 15, 2023
1 parent 144b615 commit 7eacf32
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
13 changes: 8 additions & 5 deletions pddl/logic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,31 +115,34 @@ class Atomic(Formula):
"""Atomic formula."""


class MonotoneOp(type):
class BinaryOpMetaclass(type):
"""Metaclass to simplify monotone operator instantiations."""

_absorbing: Optional[Formula] = None
idempotency: bool = False

def __call__(cls, *args, **kwargs):
"""Init the subclass object."""
operands = _simplify_monotone_op_operands(cls, *args)
if len(operands) == 1:
if len(operands) == 1 and cls.idempotency:
return operands[0]

return super(MonotoneOp, cls).__call__(*operands, **kwargs)
return super(BinaryOpMetaclass, cls).__call__(*operands, **kwargs)


class And(BinaryOp, metaclass=MonotoneOp):
class And(BinaryOp, metaclass=BinaryOpMetaclass):
"""And operator."""

_absorbing = False
idempotency = True
SYMBOL = "and"


class Or(BinaryOp, metaclass=MonotoneOp):
class Or(BinaryOp, metaclass=BinaryOpMetaclass):
"""Or operator."""

_absorbing = True
idempotency = True
SYMBOL = "or"


Expand Down
22 changes: 13 additions & 9 deletions pddl/logic/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pddl.custom_types import namelike, parse_function
from pddl.helpers.base import assert_
from pddl.helpers.cache_hash import cache_hash
from pddl.logic.base import Atomic, MonotoneOp
from pddl.logic.base import Atomic, BinaryOpMetaclass
from pddl.logic.terms import Term
from pddl.parser.symbols import Symbols

Expand Down Expand Up @@ -52,9 +52,6 @@ def arity(self) -> int:
"""Get the arity of the function."""
return len(self.terms)

# TODO: check whether it's a good idea...
# TODO: allow also for keyword-based replacement
# TODO: allow skip replacement with None arguments.
def __call__(self, *terms: Term):
"""Replace terms."""
assert_(len(terms) == self.arity, "Wrong number of terms.")
Expand Down Expand Up @@ -95,6 +92,7 @@ def __lt__(self, other):


@cache_hash
@functools.total_ordering
class NumericValue(FunctionExpression):
"""A class for a numeric value."""

Expand All @@ -111,6 +109,12 @@ def __eq__(self, other):
"""Compare with another object."""
return isinstance(other, NumericValue) and self.value == other.value

def __lt__(self, other):
"""Compare with another object."""
if not isinstance(other, NumericValue):
return NotImplemented
return self.value < other.value

def __hash__(self) -> int:
"""Compute the hash of the object."""
return hash(self._value)
Expand Down Expand Up @@ -210,7 +214,7 @@ def __hash__(self):
return hash((self.expression, self.optimization))


class EqualTo(BinaryFunction, metaclass=MonotoneOp):
class EqualTo(BinaryFunction, metaclass=BinaryOpMetaclass):
"""Equal to operator."""

SYMBOL = Symbols.EQUAL
Expand Down Expand Up @@ -310,7 +314,7 @@ def __init__(self, *operands: FunctionExpression):
super().__init__(*operands)


class Minus(BinaryFunction, metaclass=MonotoneOp):
class Minus(BinaryFunction, metaclass=BinaryOpMetaclass):
"""Minus operator."""

SYMBOL = Symbols.MINUS
Expand All @@ -324,7 +328,7 @@ def __init__(self, *operands: FunctionExpression):
super().__init__(*operands)


class Plus(BinaryFunction, metaclass=MonotoneOp):
class Plus(BinaryFunction, metaclass=BinaryOpMetaclass):
"""Plus operator."""

SYMBOL = Symbols.PLUS
Expand All @@ -338,7 +342,7 @@ def __init__(self, *operands: FunctionExpression):
super().__init__(*operands)


class Times(BinaryFunction, metaclass=MonotoneOp):
class Times(BinaryFunction, metaclass=BinaryOpMetaclass):
"""Times operator."""

SYMBOL = Symbols.TIMES
Expand All @@ -352,7 +356,7 @@ def __init__(self, *operands: FunctionExpression):
super().__init__(*operands)


class Divide(BinaryFunction, metaclass=MonotoneOp):
class Divide(BinaryFunction, metaclass=BinaryOpMetaclass):
"""Divide operator."""

SYMBOL = Symbols.DIVIDE
Expand Down
3 changes: 0 additions & 3 deletions pddl/logic/predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ def arity(self) -> int:
"""Get the arity of the predicate."""
return len(self.terms)

# TODO check whether it's a good idea...
# TODO allow also for keyword-based replacement
# TODO allow skip replacement with None arguments.
def __call__(self, *terms: Term):
"""Replace terms."""
assert_(len(terms) == self.arity, "Number of terms not correct.")
Expand Down

0 comments on commit 7eacf32

Please sign in to comment.