Skip to content

Commit 74cd9aa

Browse files
fixing issue #623: port type check backported from v4
1 parent 8c204c5 commit 74cd9aa

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

include/behaviortree_cpp_v3/basic_types.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ class PortInfo
253253

254254
const std::string& defaultValue() const;
255255

256+
bool isStronglyTyped() const
257+
{
258+
return _info != nullptr;
259+
}
260+
256261
private:
257262
PortDirection _type;
258263
const std::type_info* _info;

src/xml_parsing.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -565,23 +565,24 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
565565
continue;
566566
}
567567
StringView param_value = remap_it->second;
568-
auto param_res = TreeNode::getRemappedKey(port_name, param_value);
569-
if (param_res)
568+
569+
if (auto param_res = TreeNode::getRemappedKey(port_name, param_value))
570570
{
571+
// port_key will contain the key to find the entry in the blackboard
571572
const auto port_key = static_cast<std::string>(param_res.value());
572573

573-
auto prev_info = blackboard->portInfo(port_key);
574-
if (!prev_info)
574+
// if the entry already exists, check that the type is the same
575+
if (auto prev_info = blackboard->portInfo(port_key))
575576
{
576-
// not found, insert for the first time.
577-
blackboard->createEntry(port_key, port_info);
578-
}
579-
else
580-
{
581-
// found. check consistency
582-
if (prev_info->type() &&
583-
port_info.type() && // null type means that everything is valid
584-
*prev_info->type() != *port_info.type())
577+
bool const port_type_mismatch = (prev_info->isStronglyTyped() &&
578+
port_info.isStronglyTyped() &&
579+
*prev_info->type() != *port_info.type());
580+
581+
// special case related to convertFromString
582+
bool const string_input = ( prev_info->type() &&
583+
*prev_info->type() == typeid(std::string));
584+
585+
if (port_type_mismatch && !string_input)
585586
{
586587
blackboard->debugMessage();
587588

@@ -591,6 +592,11 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
591592
demangle(port_info.type()), "] was used somewhere else.");
592593
}
593594
}
595+
else
596+
{
597+
// not found, insert for the first time.
598+
blackboard->createEntry(port_key, port_info);
599+
}
594600
}
595601
}
596602

tests/gtest_subtree.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <gtest/gtest.h>
22
#include "behaviortree_cpp_v3/bt_factory.h"
33
#include "../sample_nodes/dummy_nodes.h"
4+
#include "../sample_nodes/movebase_node.h"
45

56
using namespace BT;
67

@@ -372,3 +373,30 @@ TEST(SubTree, SubtreeIssue563)
372373
auto ret = tree.tickRoot();
373374
ASSERT_EQ(ret, NodeStatus::SUCCESS);
374375
}
376+
377+
TEST(SubTree, String_to_Pose_Issue623)
378+
{
379+
// clang-format off
380+
381+
static const char* xml_text = R"(
382+
<root main_tree_to_execute="Test">
383+
<BehaviorTree ID="Test">
384+
<ReactiveSequence name="MainSequence">
385+
<SubTreePlus name="Visit2" ID="Visit2" tl1="1;2;3"/>
386+
</ReactiveSequence>
387+
</BehaviorTree>
388+
<BehaviorTree ID="Visit2">
389+
<Sequence name="Visit2MainSequence">
390+
<Action name="MoveBase" ID="MoveBase" goal="{tl1}"/>
391+
</Sequence>
392+
</BehaviorTree>
393+
</root>
394+
)";
395+
396+
// clang-format on
397+
398+
BehaviorTreeFactory factory;
399+
factory.registerNodeType<MoveBaseAction>("MoveBase");
400+
auto tree = factory.createTreeFromText(xml_text);
401+
tree.tickRootWhileRunning();
402+
}

0 commit comments

Comments
 (0)