Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Apr 24, 2024
1 parent 9c644e1 commit d72b319
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
31 changes: 31 additions & 0 deletions examples/t11_replace_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,34 @@ int main(int argc, char** argv)

return 0;
}

/* Expecte output:
----- Nodes fullPath() -------
Sequence::1
talk
mysub
mysub/Sequence::4
mysub/action_subA
mysub/action_subB
set_message
SaySomething::8
counting
SaySomething::10
SaySomething::11
SaySomething::12
------ Output (original) ------
Robot says: hello world
Robot says: the original message
Robot says: 1
Robot says: 2
Robot says: 3
------ Output (substituted) ------
DummySaySomething: hello world
DummyAction substituting node with fullPath(): mysub/action_subA
DummyAction substituting node with fullPath(): mysub/action_subB
Robot says: message SUBSTITUTED
*/
11 changes: 2 additions & 9 deletions include/behaviortree_cpp/actions/test_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,14 @@ struct TestNodeConfig
class TestNode : public BT::StatefulActionNode
{
public:
TestNode(const std::string& name, const NodeConfig& config,
TestNodeConfig test_config = {})
: StatefulActionNode(name, config), _test_config(std::move(test_config))
{
setRegistrationID("TestNode");
}
TestNode(const std::string& name, const NodeConfig& config, TestNodeConfig test_config);

static PortsList providedPorts()
{
return {};
}

void setConfig(const TestNodeConfig& config);

private:
protected:
virtual NodeStatus onStart() override;

virtual NodeStatus onRunning() override;
Expand Down
15 changes: 9 additions & 6 deletions src/actions/test_node.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "behaviortree_cpp/actions/test_node.h"

void BT::TestNode::setConfig(const TestNodeConfig& config)
BT::TestNode::TestNode(const std::string& name, const NodeConfig& config,
TestNodeConfig test_config)
: StatefulActionNode(name, config), _test_config(std::move(test_config))
{
if(config.return_status == NodeStatus::IDLE)
setRegistrationID("TestNode");

if(_test_config.return_status == NodeStatus::IDLE)
{
throw RuntimeError("TestNode can not return IDLE");
}
_test_config = config;

auto prepareScript = [](const std::string& script, auto& executor) {
if(!script.empty())
Expand All @@ -19,9 +22,9 @@ void BT::TestNode::setConfig(const TestNodeConfig& config)
executor = result.value();
}
};
prepareScript(config.success_script, _success_executor);
prepareScript(config.failure_script, _failure_executor);
prepareScript(config.post_script, _post_executor);
prepareScript(_test_config.success_script, _success_executor);
prepareScript(_test_config.failure_script, _failure_executor);
prepareScript(_test_config.post_script, _post_executor);
}

BT::NodeStatus BT::TestNode::onStart()
Expand Down
4 changes: 1 addition & 3 deletions src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ std::unique_ptr<TreeNode> BehaviorTreeFactory::instantiateTreeNode(
else if(const auto test_config = std::get_if<TestNodeConfig>(&rule))
{
// second case, the varian is a TestNodeConfig
auto test_node = new TestNode(name, config);
test_node->setConfig(*test_config);

auto test_node = new TestNode(name, config, *test_config);
node.reset(test_node);
substituted = true;
break;
Expand Down

0 comments on commit d72b319

Please sign in to comment.