Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update check data action #13

Merged
merged 4 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions docs/libraries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ A common topic to record is ``/scenario_execution/snapshots`` which publishes ch
``check_data()``
^^^^^^^^^^^^^^^^

Compare received topic messages using the given ``comparison_operator``, against the specified value. Either the whole message gets compared or a member defined by ``member_name``.
Compare received topic messages using the given ``comparison_operator``, against the specified value. Either the whole message gets compared or a member defined by ``member_name``. If the ``expected_value`` is a string, set ``eval_expected_value`` to ``true``.

.. list-table::
:widths: 15 15 5 65
Expand All @@ -1359,6 +1359,14 @@ Compare received topic messages using the given ``comparison_operator``, against
- ``string``
-
- Class of the message type (e.g. ``std_msgs.msg.String``)
* - ``expected_value``
- ``string``
-
- Expected value
* - ``eval_expected_value``
- ``bool``
- ``true``
- Should the expected value get evaluated (using ``ast.literal_eval()``). Set to ``false`` if expected value is a string
* - ``qos_profile``
- ``qos_preset_profiles``
- ``qos_preset_profiles!system_default``
Expand All @@ -1367,10 +1375,6 @@ Compare received topic messages using the given ``comparison_operator``, against
- ``string``
- ``''``
- Name of the type member to check. If empty, the whole type is checked
* - ``expected_value``
- ``string``
-
- Expected value
* - ``comparison_operator``
- ``comparison_operator``
- ``comparison_operator!eq``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ def setup(self, **kwargs):

def execute(self,
expected_value: str,
eval_expected_value: bool,
comparison_operator: int,
fail_if_no_data: bool,
fail_if_bad_comparison: bool,
wait_for_first_message: bool):
self.set_expected_value(expected_value)
self.set_expected_value(expected_value, eval_expected_value)
self.comparison_operator = get_comparison_operator(comparison_operator)
self.fail_if_no_data = fail_if_no_data
self.fail_if_bad_comparison = fail_if_bad_comparison
Expand Down Expand Up @@ -123,12 +124,15 @@ def check_data(self, msg):
self.feedback_message = f"Member name not found {self.member_name}"
self.found = self.comparison_operator(value, self.expected_value)

def set_expected_value(self, expected_value_string):
def set_expected_value(self, expected_value_string, eval_expected_value):
if not isinstance(expected_value_string, str):
raise ActionError("Only string allowed as expected_value.", action=self)
error_string = ""
try:
parsed_value = literal_eval("".join(expected_value_string.split('\\')))
if eval_expected_value:
parsed_value = literal_eval("".join(expected_value_string.split('\\')))
else:
parsed_value = expected_value_string
msg = get_ros_message_type(self.topic_type)()
if self.member_name == "":
self.expected_value = msg
Expand All @@ -140,5 +144,5 @@ def set_expected_value(self, expected_value_string):
self.expected_value = parsed_value
else:
set_message_fields(self.expected_value, parsed_value)
except (TypeError, AttributeError) as e:
except (TypeError, AttributeError, ValueError) as e:
raise ActionError(f"Could not parse '{expected_value_string}'. {error_string}", action=self) from e
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ action check_data:
# Compare received topic messages using the given comparison_operator, against the specified value. Either the whole message gets compared or a member defined by member_name.
topic_name: string # name of the topic to connect to
topic_type: string # class of the message type (e.g. std_msgs.msg.String)
expected_value: string # expected value of the variable
eval_expected_value: bool = true # should the expected value get evaluated (using ast.literal_eval). Set to false for string
qos_profile: qos_preset_profiles = qos_preset_profiles!system_default # qos profile for the subscriber
member_name: string = "" # name of the member to check, if empty the whole message is checked
expected_value: string # expected value of the variable
comparison_operator: comparison_operator = comparison_operator!eq # one from the python `operator module`_
fail_if_no_data: bool = false # py_trees.common.Status.FAILURE instead of py_trees.common.Status.RUNNING if there is no data on action execution
fail_if_bad_comparison: bool = false # py_trees.common.Status.FAILURE instead of py_trees.common.Status.RUNNING if comparison failed
Expand Down
27 changes: 27 additions & 0 deletions scenario_execution_ros/test/test_check_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ def test_success_member(self):
self.execute(scenario_content)
self.assertTrue(self.scenario_execution_ros.process_results())

def test_success_member_string(self):
scenario_content = """
import osc.ros

scenario test:
do parallel:
test: serial:
wait elapsed(1s)
topic_publish(
topic_name: '/bla',
topic_type: 'std_msgs.msg.String',
value: '{\\\"data\\\": \\\"hello\\\"}')
receive: serial:
check_data(
topic_name: '/bla',
topic_type: 'std_msgs.msg.String',
member_name: 'data',
expected_value: 'hello',
eval_expected_value: false)
emit end
time_out: serial:
wait elapsed(10s)
emit fail
"""
self.execute(scenario_content)
self.assertTrue(self.scenario_execution_ros.process_results())

def test_fail_unknown_type(self):
scenario_content = """
import osc.ros
Expand Down