Skip to content

Add conditional#13

Merged
erichlf merged 8 commits intohumblefrom
conditional
Apr 3, 2025
Merged

Add conditional#13
erichlf merged 8 commits intohumblefrom
conditional

Conversation

@erichlf
Copy link
Collaborator

@erichlf erichlf commented Mar 22, 2025

This PR implements the conditional feature using a simple if keyword.

Fixes #5

Motivation and Context

To be able to include or exclude elements from a behavior tree in a programmatic way a conditional was necessary.

Changes

  • Add if keyword

Type of changes

  • New feature

Checklist

  • Update dependencies
  • Update version
  • Update README

Testing

  1. colcon build --packages-up-to py_trees_parser
  2. colcon test --event-handlers console_cohesion+ --packages-select py_trees_parser

@erichlf erichlf added the enhancement New feature or request label Mar 22, 2025
@erichlf erichlf requested a review from dave992 March 22, 2025 18:57
@erichlf
Copy link
Collaborator Author

erichlf commented Mar 22, 2025

@pakagronglb I think we may go with this implementation instead. If we decide to take this one I want to make sure you know that I really appreciate your contribution. Your PR forced me to think about this a bit more and spurred me to write the code.

@erichlf erichlf requested a review from SyZbidi March 22, 2025 19:02
@erichlf
Copy link
Collaborator Author

erichlf commented Mar 22, 2025

@SyZbidi this will be especially useful for simulation and excluding hardware specific behaviors.

@pakagronglb
Copy link
Contributor

@pakagronglb I think we may go with this implementation instead. If we decide to take this one I want to make sure you know that I really appreciate your contribution. Your PR forced me to think about this a bit more and spurred me to write the code.

Thank you so much for the opportunity! I really appreciate it. It was a fun and challenging one.

@erichlf erichlf force-pushed the conditional branch 2 times, most recently from 50b794c to 2a1c7a5 Compare March 24, 2025 17:26
@erichlf erichlf self-assigned this Mar 28, 2025
Copy link
Contributor

@pakagronglb pakagronglb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope my feedback or output will be useful somehow. Thank you again for the opportunity.

Looking at the specific implementation code, I have a few technical observations:

  1. Attribute Handling: The code removes the if attribute but doesn't appear to have attribute type validation. Consider adding type checking for condition values.

  2. Function Signature Analysis: The code has good handling of different function signatures (behaviour, subtrees, tasks). However, for consistency, the conditional processing could be abstracted into its own helper method to keep _create_node cleaner.

  3. Error Propagation: When raising BTParseError, it might be helpful to include more context about the failed condition in debug logs to aid troubleshooting.

  4. Advanced Pattern: You could consider implementing a lightweight condition parser instead of direct evaluation for better security and flexibility:

def _parse_condition(self, condition_str, args):
    # Safer condition parsing with explicit operator support
    # This would allow for more controlled evaluation
  1. Performance Enhancement: Since removing dictionary keys is an O(1) operation, you're handling things efficiently, but consider grouping the attribute processing:
# Extract and process special attributes in one block
special_attrs = {}
for attr in ["name", "if", "other_special_attr"]:
    if attr in node_attribs:
        special_attrs[attr] = node_attribs.pop(attr)

This implementation looks solid overall - the approach of handling conditions as attributes is clean and leverages the existing parsing architecture well. Nice work!!!

@erichlf
Copy link
Collaborator Author

erichlf commented Apr 3, 2025

I hope my feedback or output will be useful somehow. Thank you again for the opportunity.

Looking at the specific implementation code, I have a few technical observations:

  1. Attribute Handling: The code removes the if attribute but doesn't appear to have attribute type validation. Consider adding type checking for condition values.
  2. Function Signature Analysis: The code has good handling of different function signatures (behaviour, subtrees, tasks). However, for consistency, the conditional processing could be abstracted into its own helper method to keep _create_node cleaner.
  3. Error Propagation: When raising BTParseError, it might be helpful to include more context about the failed condition in debug logs to aid troubleshooting.
  4. Advanced Pattern: You could consider implementing a lightweight condition parser instead of direct evaluation for better security and flexibility:
def _parse_condition(self, condition_str, args):
    # Safer condition parsing with explicit operator support
    # This would allow for more controlled evaluation
  1. Performance Enhancement: Since removing dictionary keys is an O(1) operation, you're handling things efficiently, but consider grouping the attribute processing:
# Extract and process special attributes in one block
special_attrs = {}
for attr in ["name", "if", "other_special_attr"]:
    if attr in node_attribs:
        special_attrs[attr] = node_attribs.pop(attr)

This implementation looks solid overall - the approach of handling conditions as attributes is clean and leverages the existing parsing architecture well. Nice work!!!

  1. I am not sure this is possible since the conditional is a statement.
  2. Not sure how this applies.
  3. The BTParseError is propogated and should be logged by the user. It typically contains plenty of info to determine what happened.
  4. I may make a ticket for this as handling the python the way I do is not exactly secure, so this would be for an general python parsing overhaul to happen in the future.
  5. Not sure how this actually applies to the current code.

@erichlf erichlf requested a review from SyZbidi April 3, 2025 11:06
@erichlf erichlf merged commit 63a5a28 into humble Apr 3, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: The ability to handle conditional would be really helpful.

3 participants