Skip to content

Commit 7a9b153

Browse files
committed
Fix #942: getLockedPortContent creates entry for default-remapped ports
1 parent b3dbe30 commit 7a9b153

2 files changed

Lines changed: 76 additions & 1 deletion

File tree

‎src/tree_node.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,21 @@ AnyPtrLocked BT::TreeNode::getLockedPortContent(const std::string& key)
479479
{
480480
if(auto remapped_key = getRemappedKey(key, getRawPortValue(key)))
481481
{
482-
return _p->config.blackboard->getAnyLocked(std::string(*remapped_key));
482+
const auto bb_key = std::string(*remapped_key);
483+
auto result = _p->config.blackboard->getAnyLocked(bb_key);
484+
if(!result && _p->config.manifest)
485+
{
486+
// Entry doesn't exist yet. Create it using the port's type info
487+
// from the manifest so that getLockedPortContent works even when
488+
// the port is not explicitly declared in XML. Issue #942.
489+
auto port_it = _p->config.manifest->ports.find(key);
490+
if(port_it != _p->config.manifest->ports.end())
491+
{
492+
_p->config.blackboard->createEntry(bb_key, port_it->second);
493+
result = _p->config.blackboard->getAnyLocked(bb_key);
494+
}
495+
}
496+
return result;
483497
}
484498
return {};
485499
}

‎tests/gtest_blackboard.cpp‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,64 @@ TEST(BlackboardTest, SetBlackboard_WithPortRemapping)
745745
// Tick till the end with no crashes
746746
ASSERT_NO_THROW(tree.tickWhileRunning(););
747747
}
748+
749+
// Issue #942: getLockedPortContent should return a valid locked reference
750+
// even when the port is not explicitly declared in XML, as long as there is
751+
// a default blackboard remapping (e.g., "{=}").
752+
class ActionWithLockedPort : public SyncActionNode
753+
{
754+
public:
755+
ActionWithLockedPort(const std::string& name, const NodeConfig& config)
756+
: SyncActionNode(name, config)
757+
{}
758+
759+
NodeStatus tick() override
760+
{
761+
auto any_locked = getLockedPortContent("value");
762+
if(!any_locked)
763+
{
764+
locked_valid = false;
765+
return NodeStatus::FAILURE;
766+
}
767+
locked_valid = true;
768+
// Assign a value through the locked reference
769+
any_locked.assign(42);
770+
return NodeStatus::SUCCESS;
771+
}
772+
773+
static PortsList providedPorts()
774+
{
775+
return { BidirectionalPort<int>("value", "{=}", "A value with default BB remap") };
776+
}
777+
778+
bool locked_valid = false;
779+
};
780+
781+
TEST(BlackboardTest, GetLockedPortContentWithDefault_Issue942)
782+
{
783+
// XML does NOT specify the "value" port — relies on default "{=}"
784+
std::string xml_txt = R"(
785+
<root BTCPP_format="4" >
786+
<BehaviorTree ID="Main">
787+
<ActionWithLockedPort/>
788+
</BehaviorTree>
789+
</root>)";
790+
791+
BehaviorTreeFactory factory;
792+
factory.registerNodeType<ActionWithLockedPort>("ActionWithLockedPort");
793+
auto tree = factory.createTreeFromText(xml_txt);
794+
auto status = tree.tickWhileRunning();
795+
796+
ASSERT_EQ(status, NodeStatus::SUCCESS);
797+
798+
for(const auto& node : tree.subtrees.front()->nodes)
799+
{
800+
if(auto action = dynamic_cast<ActionWithLockedPort*>(node.get()))
801+
{
802+
ASSERT_TRUE(action->locked_valid);
803+
}
804+
}
805+
806+
// The value should be accessible from the blackboard
807+
ASSERT_EQ(tree.rootBlackboard()->get<int>("value"), 42);
808+
}

0 commit comments

Comments
 (0)