Skip to content

Commit

Permalink
temp: use LPAR and RPAR for parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofavorito committed Jul 9, 2023
1 parent bf3d3cf commit 5704f6f
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions plan4past/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

"""Miscellanea utilities."""
import re
from typing import Type

from pddl.logic import Predicate, constants

Expand All @@ -30,6 +31,8 @@
rf"(\"({_PDDL_NAME_REGEX})( {_PDDL_NAME_REGEX})*\")|({_PDDL_NAME_REGEX})"
)
_VAL_PREFIX = "val__"
_LEFT_PAR = "LPAR__"
_RIGHT_PAR = "__RPAR"


def add_val_prefix(name: str):
Expand Down Expand Up @@ -57,8 +60,8 @@ def replace_symbols(name: str):
"""Stringify symbols."""
return (
name.replace('"', "")
.replace("(", "")
.replace(")", "")
.replace("(", _LEFT_PAR)
.replace(")", _RIGHT_PAR)
.replace("&", "and")
.replace("|", "or")
.replace("~", "not-")
Expand All @@ -67,7 +70,9 @@ def replace_symbols(name: str):
)


def check_(condition: bool, message: str = "") -> None:
def check_(
condition: bool, message: str = "", exception_cls: Type[Exception] = AssertionError
) -> None:
"""
User-defined assert.
Expand All @@ -76,7 +81,7 @@ def check_(condition: bool, message: str = "") -> None:
https://bandit.readthedocs.io/en/1.7.5/plugins/b101_assert_used.html
"""
if not condition:
raise AssertionError(message)
raise exception_cls(message)


def parse_ground_fluent(symbol: str) -> Predicate:
Expand Down Expand Up @@ -105,11 +110,27 @@ def validate(symbol: str) -> None:
:param symbol: the symbol
"""
# remove the double quotes
symbol_unquoted = symbol.replace('"', "")

# 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_(
not symbol_unquoted.startswith(_VAL_PREFIX),
f"invalid symbol: symbol '{symbol}' cannot start with {_VAL_PREFIX}",
exception_cls=ValueError,
)
# check if the symbol does not start with the 'LPAR__' prefix
check_(
not symbol_unquoted.startswith(_LEFT_PAR),
f"invalid symbol: symbol '{symbol}' cannot start with {_LEFT_PAR}",
exception_cls=ValueError,
)
# check if the symbol does not start with the '__RPAR' prefix
check_(
not symbol_unquoted.startswith(_RIGHT_PAR),
f"invalid symbol: symbol '{symbol}' cannot start with {_RIGHT_PAR}",
exception_cls=ValueError,
)

# check if the symbol is a valid PDDL ground fluent
parse_ground_fluent(symbol)

0 comments on commit 5704f6f

Please sign in to comment.