Skip to content

Commit

Permalink
fix issue 489
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Jan 2, 2023
1 parent e147009 commit 5e16d72
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
12 changes: 2 additions & 10 deletions include/behaviortree_cpp/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,7 @@ using StringView = std::string_view;
template <typename T>
inline T convertFromString(StringView /*str*/)
{
auto type_name = BT::demangle(typeid(T));

std::cerr << "You (maybe indirectly) called BT::convertFromString() for type ["
<< type_name << "], but I can't find the template specialization.\n"
<< std::endl;

throw LogicError(std::string("You didn't implement the template specialization of "
"convertFromString for this type: ") +
type_name);
static_assert(true, "This template specialization of convertFromString doesn't exist");
}

template <>
Expand Down Expand Up @@ -269,7 +261,7 @@ class PortInfo

bool isStronglyTyped() const
{
return _type_info == typeid(AnyTypeAllowed);
return _type_info != typeid(AnyTypeAllowed);
}

const StringConverter& converter() const
Expand Down
13 changes: 9 additions & 4 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,10 +578,15 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
// if the entry already exists, check that the type is the same
if (auto prev_info = blackboard->portInfo(port_key))
{
// found. check consistency
// null type means that everything is valid
if (!prev_info->isStronglyTyped() && !port_info.isStronglyTyped() &&
prev_info->type() != port_info.type())
// Check consistency of types.
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() == typeid(std::string));

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

Expand Down
54 changes: 54 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,57 @@ TEST(PortTest, IllegalPorts)
BehaviorTreeFactory factory;
ASSERT_ANY_THROW(factory.registerNodeType<IllegalPorts>("nope"));
}


class ActionVectorIn : public SyncActionNode
{
public:
ActionVectorIn(const std::string& name, const NodeConfig& config,
std::vector<double>* states) :
SyncActionNode(name, config),
states_(states)
{}

NodeStatus tick() override
{
getInput("states", *states_);
return NodeStatus::SUCCESS;
}

static PortsList providedPorts()
{
return {BT::InputPort<std::vector<double>>("states")};
}
std::vector<double>* states_;
};


TEST(PortTest, SubtreeStringInput_Issue489)
{
std::string xml_txt = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="Main">
<SubTree ID="Subtree_A" states="3;7"/>
</BehaviorTree>
<BehaviorTree ID="Subtree_A">
<ActionVectorIn states="{states}"/>
</BehaviorTree>
</root>)";

std::vector<double> states;

BehaviorTreeFactory factory;
factory.registerNodeType<ActionVectorIn>("ActionVectorIn", &states);

factory.registerBehaviorTreeFromText(xml_txt);
auto tree = factory.createTree("Main");

NodeStatus status = tree.tickWhileRunning();

ASSERT_EQ(status, NodeStatus::SUCCESS);
ASSERT_EQ(2, states.size());
ASSERT_EQ(3, states[0]);
ASSERT_EQ(7, states[1]);
}

0 comments on commit 5e16d72

Please sign in to comment.