From bf3d3cfa5166b118b5c696603fbecc215f1cb723 Mon Sep 17 00:00:00 2001 From: Marco Favorito Date: Sun, 9 Jul 2023 22:50:19 +0200 Subject: [PATCH] fix: change prefix to val__; check no atom starts with it We now check if the prefix 'val' is in the atom name. This is because after the compilation there might be conflicts in the val predicate names. Consider the following example: '"p a b" & val-p-a-b' The formula is compiled as follows: "p a b" -> 'val-p-a-b' (:derived (val-p-a-b) (p a b)) ^ ^ derived-predicate condition (ground fluent, predicate name 'p', constants 'a' and 'b') "val-p-a-b" -> 'val-val-p-a-b' (:derived (val-val-p-a-b) (val-p-a-b)) ^ ^ derived-predicate condition (ground fluent, predicate name 'p-a-b', no constants The prefix is changed to `val__` to make the prefix less probable to occur in an atom name. --- plan4past/helpers/utils.py | 11 +++++++++-- tests/test_helpers/test_utils.py | 10 +++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/plan4past/helpers/utils.py b/plan4past/helpers/utils.py index 3175f59f..aff06719 100644 --- a/plan4past/helpers/utils.py +++ b/plan4past/helpers/utils.py @@ -29,11 +29,12 @@ _GROUND_FLUENT_REGEX = re.compile( rf"(\"({_PDDL_NAME_REGEX})( {_PDDL_NAME_REGEX})*\")|({_PDDL_NAME_REGEX})" ) +_VAL_PREFIX = "val__" def add_val_prefix(name: str): """Add the 'prime' prefix.""" - return "val-" + name.replace('"', "") + return _VAL_PREFIX + name.replace('"', "") def remove_before_prefix(name: str): @@ -49,7 +50,7 @@ def remove_before_prefix(name: str): def remove_val_prefix(name: str): """Remove the 'prime' prefix.""" - return name.replace("val-", "") if name.startswith("val-") else name + return name.replace(_VAL_PREFIX, "") if name.startswith(_VAL_PREFIX) else name def replace_symbols(name: str): @@ -104,5 +105,11 @@ def validate(symbol: str) -> None: :param symbol: the symbol """ + # check if the symbol does not start with the 'val__' prefix + if symbol.startswith(_VAL_PREFIX): + raise ValueError( + f"invalid symbol: symbol '{symbol}' cannot start with {_VAL_PREFIX}" + ) + # check if the symbol is a valid PDDL ground fluent parse_ground_fluent(symbol) diff --git a/tests/test_helpers/test_utils.py b/tests/test_helpers/test_utils.py index 755f1735..78fad0fa 100644 --- a/tests/test_helpers/test_utils.py +++ b/tests/test_helpers/test_utils.py @@ -33,10 +33,10 @@ def test_val_prefix() -> None: """Test the add_val_prefix function.""" - assert add_val_prefix("Y-foo") == "val-Y-foo" - assert add_val_prefix("Yfoo") == "val-Yfoo" - assert add_val_prefix("foo") == "val-foo" - assert add_val_prefix("Y-foo-bar") == "val-Y-foo-bar" + assert add_val_prefix("Y-foo") == "val__Y-foo" + assert add_val_prefix("Yfoo") == "val__Yfoo" + assert add_val_prefix("foo") == "val__foo" + assert add_val_prefix("Y-foo-bar") == "val__Y-foo-bar" def test_remove_before_prefix() -> None: @@ -48,7 +48,7 @@ def test_remove_before_prefix() -> None: def test_remove_val_prefix() -> None: """Test the remove_val_prefix function.""" - assert remove_val_prefix("val-foo") == "foo" + assert remove_val_prefix("val__foo") == "foo" assert remove_val_prefix("foo") == "foo"