Skip to content

Commit

Permalink
add private ports to exclude from autoremapping #706
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Dec 6, 2023
1 parent 65504b0 commit b3c3c25
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions include/behaviortree_cpp/decorators/subtree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace BT
* If you want to have data flow through ports, you need to explicitly
* remap the ports.
*
* NOTE: _autoremap will exclude all the ports which name start with underscore '_'
*
* Consider this example:
<root main_tree_to_execute = "MainTree" >
Expand Down
11 changes: 8 additions & 3 deletions src/blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
namespace BT
{

bool IsPrivateKey(StringView str)
{
return str.size() >= 1 && str.data()[0] == '_';
}

void Blackboard::enableAutoRemapping(bool remapping)
{
autoremapping_ = remapping;
Expand Down Expand Up @@ -52,7 +57,7 @@ const std::shared_ptr<Blackboard::Entry> Blackboard::getEntry(const std::string
auto const& new_key = remap_it->second;
return parent->getEntry(new_key);
}
if(autoremapping_)
if(autoremapping_ && !IsPrivateKey(key))
{
return parent->getEntry(key);
}
Expand Down Expand Up @@ -83,7 +88,7 @@ std::shared_ptr<Blackboard::Entry> Blackboard::getEntry(const std::string &key)
}
return entry;
}
if(autoremapping_)
if(autoremapping_ && !IsPrivateKey(key))
{
auto entry = parent->getEntry(key);
if(entry)
Expand Down Expand Up @@ -202,7 +207,7 @@ Blackboard::createEntryImpl(const std::string& key, const TypeInfo& info)
entry = parent->createEntryImpl(remapped_key, info);
}
}
else if(autoremapping_)
else if(autoremapping_ && !IsPrivateKey(key))
{
if (auto parent = parent_bb_.lock())
{
Expand Down
38 changes: 38 additions & 0 deletions tests/gtest_subtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,41 @@ TEST(SubTree, RemappingIssue696)
ASSERT_EQ(console[3], "bar");
}

TEST(SubTree, PrivateAutoRemapping)
{
// clang-format off

static const char* xml_text = R"(
<root BTCPP_format="4">
<BehaviorTree ID="Subtree">\n"
<Sequence>
<SetBlackboard output_key="public_value" value="hello"/>
<SetBlackboard output_key="_private_value" value="world"/>
</Sequence>
</BehaviorTree>
<BehaviorTree ID="MainTree">
<Sequence>
<SubTree ID="Subtree" _autoremap="true"/>
<PrintToConsole message="{public_value}"/>
<PrintToConsole message="{_private_value}"/>
</Sequence>
</BehaviorTree>
</root>
)";

// clang-format on
BehaviorTreeFactory factory;
std::vector<std::string> console;
factory.registerNodeType<PrintToConsole>("PrintToConsole", &console);

factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MainTree");
const auto res = tree.tickWhileRunning();

// should fail because _private_value is not autoremapped
ASSERT_EQ(res, BT::NodeStatus::FAILURE);
ASSERT_EQ(console.size(), 1);
ASSERT_EQ(console[0], "hello");
}

0 comments on commit b3c3c25

Please sign in to comment.