Skip to content

Refactor ActionNode to isolate side effects via pure virtual methods #1612

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

Merged
merged 3 commits into from
May 30, 2025

Conversation

takanotaiga
Copy link
Member

@takanotaiga takanotaiga commented May 28, 2025

Abstract

This pull request is a refactoring of the BehaviorTreePlugin proposed by ROBOTECH.

Background

N/A

Details

Extract common pre-/post-processing into the base ActionNode and introduce a pure virtual doAction() method.
Delegate all action-specific side effects (state updates, outputs) to derived classes for improved traceability and testability.

// Base class (defines pure virtual function)
class ActionNode : public BT::ActionNodeBase {
public:
  ActionNode(const std::string &name, const BT::NodeConfiguration &config)
  : BT::ActionNodeBase(name, config) {}

  BT::NodeStatus tick() override {
    // ① Common: Retrieve values from the blackboard
    getBlackBoardValues();
    // ② Common: Perform precondition checks
    if (!checkPreconditions()) {
      return BT::NodeStatus::FAILURE;
    }
    // ③ Delegate logic to derived class (pure virtual)
    return doAction();
  }

protected:
  virtual void getBlackBoardValues() {
    // Common blackboard reading logic
  }
  virtual bool checkPreconditions() { return true; }

  // Must be implemented by derived classes
  virtual BT::NodeStatus doAction() = 0;
};

// Example of a derived class
class ExampleAction : public ActionNode {
public:

protected:
  void getBlackBoardValues() override {
    ActionNode::getBlackBoardValues();
    // Retrieve additional vehicle-specific parameters
  }

  BT::NodeStatus doAction() override {
    // Consolidate logic such as calculateUpdatedEntityStatus,
    // setOutput(), and other side effects here
    return BT::NodeStatus::SUCCESS;  // or RUNNING/FAILURE
  }
};

References

Destructive Changes

N/A

Known Limitations

N/A

@takanotaiga takanotaiga self-assigned this May 28, 2025
@takanotaiga takanotaiga added the bump patch If this pull request merged, bump patch version of the scenario_simulator_v2 label May 28, 2025
Copy link

Checklist for reviewers ☑️

All references to "You" in the following text refer to the code reviewer.

  • Is this pull request written in a way that is easy to read from a third-party perspective?
  • Is there sufficient information (background, purpose, specification, algorithm description, list of disruptive changes, and migration guide) in the description of this pull request?
  • If this pull request contains a destructive change, does this pull request contain the migration guide?
  • Labels of this pull request are valid?
  • All unit tests/integration tests are included in this pull request? If you think adding test cases is unnecessary, please describe why and cross out this line.
  • The documentation for this pull request is enough? If you think adding documents for this pull request is unnecessary, please describe why and cross out this line.

@takanotaiga takanotaiga marked this pull request as ready for review May 29, 2025 01:52
@takanotaiga takanotaiga requested a review from hakuturu583 May 29, 2025 01:53
Copy link

@hakuturu583 hakuturu583 merged commit 3b708d2 into master May 30, 2025
21 of 29 checks passed
@github-actions github-actions bot deleted the refactor/behavior_tree_5 branch May 30, 2025 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch If this pull request merged, bump patch version of the scenario_simulator_v2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants