Skip to content

Commit

Permalink
Merge pull request #42 from aiplan4eu/39-fix-constant-object-monitoring
Browse files Browse the repository at this point in the history
39 fix constant object monitoring
  • Loading branch information
marcvinci committed Oct 16, 2023
2 parents bba316f + c8e9207 commit 5fc8acc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 39 additions & 1 deletion tests/components/test_expression_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ast
import unittest

from unified_planning.shortcuts import * # pylint: disable=unused-wildcard-import
Expand All @@ -10,6 +9,23 @@


# Example fluents


class TestObject:
def __init__(self, value: int):
"""Test object."""
self.value = value

def __eq__(self, other):
return self.value == other.value

def __hash__(self):
return hash(self.value)

def __str__(self) -> str:
return "TestObject"


def fluent_bool_1_fun():
"""Fluent bool 1."""
return True
Expand Down Expand Up @@ -45,6 +61,11 @@ def fluent_arg_real_1_fun(arg: float):
return arg


def fluent_arg_object(arg: TestObject) -> TestObject:
"""Fluent real 1."""
return arg


class TestExpressionManager(unittest.TestCase):
"""Test conversion of FNode to AST tree."""

Expand All @@ -59,6 +80,9 @@ def setUp(self) -> None:
self._fluent_arg_int_1 = self.bridge.create_fluent_from_function(fluent_arg_int_1_fun)
self._fluent_arg_real_1 = self.bridge.create_fluent_from_function(fluent_arg_real_1_fun)

self.bridge.create_types([TestObject])
self._fluent_arg_object = self.bridge.create_fluent_from_function(fluent_arg_object)

self.ast = ExpressionManager()

def test_simple_fluents(self):
Expand Down Expand Up @@ -130,6 +154,20 @@ def test_simple_nested_fluents_with_args(self):
actual = eval(compile(result, filename="<ast>", mode="eval"))
self.assertEqual(actual, False)

obj = self.bridge.create_object("obj", TestObject(1))
result = self.ast.convert(
Equals(self._fluent_arg_object(obj), self._fluent_arg_object(obj))
)
actual = eval(compile(result, filename="<ast>", mode="eval"))
self.assertEqual(actual, True)

obj1 = self.bridge.create_object("obj1", TestObject(2))
result = self.ast.convert(
Equals(self._fluent_arg_object(obj), self._fluent_arg_object(obj1))
)
actual = eval(compile(result, filename="<ast>", mode="eval"))
self.assertEqual(actual, False)


if __name__ == "__main__":
t = TestExpressionManager()
Expand Down
2 changes: 1 addition & 1 deletion up_esb/components/expression_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _map_expression(self, expression: FNode):
elif exp.is_real_constant():
return ast.Constant(value=exp.real_constant_value())
elif exp.is_object_exp():
return ast.Constant(value=exp.object())
return ast.Name(id=str(exp.object().name), ctx=ast.Load())

raise ValueError(f"Constant `{str(exp)}` not supported.")

Expand Down

0 comments on commit 5fc8acc

Please sign in to comment.