Skip to content

Commit

Permalink
fixing issue #623: port type check backported from v4
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Aug 14, 2023
1 parent 4894916 commit dac3ed3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
5 changes: 5 additions & 0 deletions include/behaviortree_cpp_v3/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ class PortInfo

const std::string& defaultValue() const;

bool isStronglyTyped() const
{
return _info != nullptr;
}

private:
PortDirection _type;
const std::type_info* _info;
Expand Down
32 changes: 19 additions & 13 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,23 +565,24 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
continue;
}
StringView param_value = remap_it->second;
auto param_res = TreeNode::getRemappedKey(port_name, param_value);
if (param_res)

if (auto param_res = TreeNode::getRemappedKey(port_name, param_value))
{
// port_key will contain the key to find the entry in the blackboard
const auto port_key = static_cast<std::string>(param_res.value());

auto prev_info = blackboard->portInfo(port_key);
if (!prev_info)
// if the entry already exists, check that the type is the same
if (auto prev_info = blackboard->portInfo(port_key))
{
// not found, insert for the first time.
blackboard->createEntry(port_key, port_info);
}
else
{
// found. check consistency
if (prev_info->type() &&
port_info.type() && // null type means that everything is valid
*prev_info->type() != *port_info.type())
bool const port_type_mismatch = (prev_info->isStronglyTyped() &&
port_info.isStronglyTyped() &&
*prev_info->type() != *port_info.type());

// special case related to convertFromString
bool const string_input = ( prev_info->type() &&
*prev_info->type() == typeid(std::string));

if (port_type_mismatch && !string_input)
{
blackboard->debugMessage();

Expand All @@ -591,6 +592,11 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
demangle(port_info.type()), "] was used somewhere else.");
}
}
else
{
// not found, insert for the first time.
blackboard->createEntry(port_key, port_info);
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions tests/gtest_subtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,20 @@ TEST(SubTree, SubtreeIssue563)
ASSERT_EQ(ret, NodeStatus::SUCCESS);
}

TEST(SubTree, SubtreeIssue623)
TEST(SubTree, String_to_Pose_Issue623)
{
// clang-format off

static const char* xml_text = R"(
<root main_tree_to_execute="Test">
<BehaviorTree ID="Test">
<ReactiveSequence name="MainSequence">
<SubTreePlus name="Visit2" ID="Visit2" tl1="1;2;3" tl2="4;5;6"/>
<SubTreePlus name="Visit2" ID="Visit2" tl1="1;2;3"/>
</ReactiveSequence>
</BehaviorTree>
<BehaviorTree ID="Visit2">
<Sequence name="Visit2MainSequence">
<Action name="MoveBase" ID="MoveBase" goal="{tl1}"/>
<Action name="MoveBase" ID="MoveBase" goal="{tl2}"/>
</Sequence>
</BehaviorTree>
</root>
Expand Down

0 comments on commit dac3ed3

Please sign in to comment.