Skip to content

Commit

Permalink
fix: change prefix to val__; check no atom starts with it
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marcofavorito committed Jul 9, 2023
1 parent 0d19b23 commit bf3d3cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions plan4past/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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(

Check warning on line 110 in plan4past/helpers/utils.py

View check run for this annotation

Codecov / codecov/patch

plan4past/helpers/utils.py#L110

Added line #L110 was not covered by tests
f"invalid symbol: symbol '{symbol}' cannot start with {_VAL_PREFIX}"
)

# check if the symbol is a valid PDDL ground fluent
parse_ground_fluent(symbol)
10 changes: 5 additions & 5 deletions tests/test_helpers/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"


Expand Down

0 comments on commit bf3d3cf

Please sign in to comment.