plansys2_support_py.Parser module

Parser utilities for PlanSys2 PDDL structures.

class plansys2_support_py.Parser.Parser

Bases: object

Parser class for converting between strings and PlanSys2 PDDL structures.

This class provides utilities to parse and generate strings for PDDL elements such as parameters, nodes, and trees, following the same conventions as the C++ implementation in plansys2_pddl_parser.

static get_expr(expr: str) Tuple[int, int]

Return expression type and start position of an expression in a string.

Parameters

exprstr

The input string.

Returns

Tuple[int, int]

Tuple of (expression_type, start_position).

static get_fun_mod(expr: str) Tuple[int, int]

Return function modifier type and start position.

Parameters

exprstr

The input string.

Returns

Tuple[int, int]

Tuple of (modifier_type, start_position).

static get_node_type(expr: str, default_node_type: int = plansys2_msgs.msg.Node.UNKNOWN) int

Return node type corresponding to the string input.

Parameters

exprstr

The input string.

default_node_typeint, optional

Default node type if none is found.

Returns

int

The node type.

static get_parenthesis(expr: str, start: int) int

Find the matching closing parenthesis.

Parameters

exprstr

The expression string.

startint

Starting position of opening parenthesis.

Returns

int

Position of matching closing parenthesis.

static get_reduced_string(expr: str) str

Remove newlines, duplicated spaces, tabs and spaces from parenthesis.

Parameters

exprstr

The expression to be reduced.

Returns

str

The reduced expression.

static get_sub_expr(expr: str) List[str]

Extract sub-expressions from a compound expression.

Parameters

exprstr

The expression string.

Returns

List[str]

List of sub-expression strings.

static node_from_string_exists(exists: str) plansys2_msgs.msg.Node

Create a Node from an exists expression.

Parameters

existsstr

The exists expression string.

Returns

Node

Node message with EXISTS type.

static node_from_string_function(function: str) plansys2_msgs.msg.Node

Create a Node from a function string.

Parameters

functionstr

The function string (e.g., “(battery-level robot1)”).

Returns

Node

Node message with FUNCTION type.

static node_from_string_predicate(predicate: str) plansys2_msgs.msg.Node

Create a Node from a predicate string.

Parameters

predicatestr

The predicate string (e.g., “(at robot1 location1)”).

Returns

Node

Node message with PREDICATE type.

static node_to_dict(node: plansys2_msgs.msg.Node) Dict[str, Any]

Convert a plansys2_msgs.msg.Node to a dictionary.

The Node message represents elements in PDDL expression trees, including logical operators, predicates, functions, and values.

Parameters

nodeplansys2_msgs.msg.Node

The Node message to convert.

Returns

Dict[str, Any]

Dictionary representation of the Node with keys: - node_type: int - Type of node (AND, OR, NOT, PREDICATE, etc.) - node_type_name: str - Human-readable name of the node type - expression_type: int - Type of expression if applicable - expression_type_name: str - Human-readable expression type name - modifier_type: int - Type of function modifier if applicable - modifier_type_name: str - Human-readable modifier type name - node_id: int - Unique identifier for this node - children: List[int] - List of child node IDs - name: str - Node name (for predicates, functions, etc.) - parameters: List[Dict] - List of parameters (converted to dicts) - value: float - Numeric value (for NUMBER nodes) - negate: bool - Whether the node is negated

static node_to_string(node: plansys2_msgs.msg.Node) str

Convert a Node to string representation.

Parameters

nodeNode

The Node message.

Returns

str

String representation.

static param_from_string(name: str, param_type: str = '') plansys2_msgs.msg.Param

Create a Param from string representation.

Parameters

namestr

Parameter name.

param_typestr, optional

Parameter type.

Returns

Param

Param message.

static param_to_dict(param: plansys2_msgs.msg.Param) Dict[str, Any]

Convert a plansys2_msgs.msg.Param to a dictionary.

Parameters

paramplansys2_msgs.msg.Param

The Param message to convert.

Returns

Dict[str, Any]

Dictionary representation of the Param with keys: - name: str - Parameter name - type: str - Parameter type - sub_types: List[str] - List of sub-types (for complex types)

static param_to_string(param: plansys2_msgs.msg.Param) str

Convert a Param to string representation.

Parameters

paramParam

The Param message.

Returns

str

String representation.

static tree_from_string(expr: str, negate: bool = False, parent: int = plansys2_msgs.msg.Node.UNKNOWN) plansys2_msgs.msg.Tree

Create a Tree from a string expression.

Parameters

exprstr

The expression string.

negatebool, optional

Whether to negate the expression.

parentint, optional

Parent node type for context.

Returns

Tree

Tree message.

static tree_to_dict(tree: plansys2_msgs.msg.Tree) Dict[str, Any]

Convert a plansys2_msgs.msg.Tree to a dictionary.

The Tree message represents a PDDL construct as a tree structure composed of Node elements.

Parameters

treeplansys2_msgs.msg.Tree

The Tree message to convert.

Returns

Dict[str, Any]

Dictionary representation of the Tree with keys: - nodes: List[Dict] - List of nodes (converted to dicts)

static tree_to_string(tree: plansys2_msgs.msg.Tree, node_id: int = 0, negate: bool = False) str

Convert a Tree to string representation.

Parameters

treeTree

The Tree message.

node_idint, optional

Starting node ID (default: 0).

negatebool, optional

Whether to negate the expression.

Returns

str

String representation.