Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions include/behaviortree_cpp_v3/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ template <typename T> using Optional = nonstd::expected<T, std::string>;
* */
using Result = Optional<void>;


const std::unordered_set<std::string> ReservedPortNames =
{
"ID", "name", "_description"
};

class PortInfo
{

Expand Down Expand Up @@ -261,15 +267,20 @@ std::pair<std::string,PortInfo> CreatePort(PortDirection direction,
StringView name,
StringView description = {})
{
auto sname = static_cast<std::string>(name);
if( ReservedPortNames.count(sname) != 0 )
{
throw std::runtime_error("A port can not use a reserved name. See ReservedPortNames");
}

std::pair<std::string,PortInfo> out;

if( std::is_same<T, void>::value)
{
out = {static_cast<std::string>(name), PortInfo(direction) };
out = {sname, PortInfo(direction) };
}
else{
out = {static_cast<std::string>(name), PortInfo(direction, typeid(T),
GetAnyFromStringFunctor<T>() ) };
out = {sname, PortInfo(direction, typeid(T), GetAnyFromStringFunctor<T>() ) };
}
if( !description.empty() )
{
Expand Down
4 changes: 4 additions & 0 deletions include/behaviortree_cpp_v3/bt_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ class BehaviorTreeFactory
Tree createTree(const std::string& tree_name,
Blackboard::Ptr blackboard = Blackboard::create());

/// Add a description to a specific manifest. This description will be added
/// to <TreeNodesModel> with the function writeTreeNodesModelXML()
void addDescriptionToManifest(const std::string& node_id, const std::string& description );

private:
std::unordered_map<std::string, NodeBuilder> builders_;
std::unordered_map<std::string, TreeNodeManifest> manifests_;
Expand Down
1 change: 1 addition & 0 deletions include/behaviortree_cpp_v3/tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct TreeNodeManifest
NodeType type;
std::string registration_ID;
PortsList ports;
std::string description;
};

typedef std::unordered_map<std::string, std::string> PortsRemapping;
Expand Down
10 changes: 10 additions & 0 deletions src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ Tree BehaviorTreeFactory::createTree(const std::string &tree_name, Blackboard::P
return tree;
}

void BehaviorTreeFactory::addDescriptionToManifest(const std::string &node_id, const std::string &description)
{
auto it = manifests_.find(node_id);
if( it == manifests_.end() )
{
throw std::runtime_error("addDescriptionToManifest: wrong ID");
}
it->second.description = description;
}

void Tree::sleep(std::chrono::system_clock::duration timeout)
{
wake_up_->waitFor(timeout);
Expand Down
26 changes: 14 additions & 12 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement *element,
for (const XMLAttribute* att = element->FirstAttribute(); att; att = att->Next())
{
const std::string attribute_name = att->Name();
if (attribute_name != "ID" && attribute_name != "name")
if ( ReservedPortNames.count(attribute_name) == 0 )
{
port_remap[attribute_name] = att->Value();
}
Expand Down Expand Up @@ -661,10 +661,10 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID,

for (const XMLAttribute* attr = element->FirstAttribute(); attr != nullptr; attr = attr->Next())
{
if( strcmp(attr->Name(), "__shared_blackboard") == 0 &&
convertFromString<bool>(attr->Value()) == true )
if( StrEqual(attr->Name(), "__shared_blackboard") )
{
is_isolated = false;
is_isolated = !convertFromString<bool>(attr->Value());
break;
}
}

Expand All @@ -678,11 +678,10 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID,

for (const XMLAttribute* attr = element->FirstAttribute(); attr != nullptr; attr = attr->Next())
{
if( strcmp(attr->Name(), "ID") == 0 )
if( ReservedPortNames.count(attr->Name()) == 0 )
{
continue;
new_bb->addSubtreeRemapping( attr->Name(), attr->Value() );
}
new_bb->addSubtreeRemapping( attr->Name(), attr->Value() );
}
output_tree.blackboard_stack.emplace_back(new_bb);
recursivelyCreateTree( node->name(), output_tree, new_bb, node );
Expand All @@ -701,7 +700,7 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID,
const char* attr_name = attr->Name();
const char* attr_value = attr->Value();

if( StrEqual(attr_name, "ID") )
if( ReservedPortNames.count(attr->Name()) != 0 )
{
continue;
}
Expand Down Expand Up @@ -772,9 +771,8 @@ void XMLParser::Pimpl::getPortsRecursively(const XMLElement *element,
{
const char* attr_name = attr->Name();
const char* attr_value = attr->Value();
if( !StrEqual(attr_name, "ID") &&
!StrEqual(attr_name, "name") &&
TreeNode::isBlackboardPointer(attr_value) )
if( ReservedPortNames.count(attr_name) == 0 &&
TreeNode::isBlackboardPointer(attr_value) )
{
auto port_name = TreeNode::stripBlackboardPointer(attr_value);
output_ports.push_back( static_cast<std::string>(port_name) );
Expand Down Expand Up @@ -845,10 +843,14 @@ std::string writeTreeNodesModelXML(const BehaviorTreeFactory& factory)
{
port_element->SetText( port_info.description().c_str() );
}

element->InsertEndChild(port_element);
}

if(!model.description.empty())
{
element->SetAttribute("description", model.registration_ID.c_str());
}

model_root->InsertEndChild(element);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ TEST(PortTest, DefaultPorts)
ASSERT_EQ( status, NodeStatus::SUCCESS );
}

TEST(PortTest, Descriptions)
{
std::string xml_txt = R"(
<root main_tree_to_execute = "MainTree" >
<BehaviorTree ID="MainTree" _description="this is my tree" >
<Sequence>
<NodeWithPorts name="first" in_port_B="66" _description="this is my action" />
<SubTree ID="SubTree" name="second" _description="this is a subtree"/>
</Sequence>
</BehaviorTree>

<BehaviorTree ID="SubTree" _description="this is a subtre" >
<NodeWithPorts name="third" in_port_B="99" />
</BehaviorTree>

</root>)";

BehaviorTreeFactory factory;
factory.registerNodeType<NodeWithPorts>("NodeWithPorts");

auto tree = factory.createTreeFromText(xml_txt);

NodeStatus status = tree.tickRoot();
ASSERT_EQ( status, NodeStatus::FAILURE ); // failure because in_port_B="99"
}

struct MyType
{
std::string value;
Expand Down Expand Up @@ -126,3 +152,25 @@ TEST(PortTest, EmptyPort)
ASSERT_EQ( status, NodeStatus::FAILURE );
}


class IllegalPorts: public SyncActionNode
{
public:
IllegalPorts(const std::string & name, const NodeConfiguration & config)
: SyncActionNode(name, config)
{ }

NodeStatus tick() { return NodeStatus::SUCCESS; }

static PortsList providedPorts()
{
return { BT::InputPort<std::string>("name") };
}
};

TEST(PortTest, IllegalPorts)
{
BehaviorTreeFactory factory;
ASSERT_ANY_THROW(factory.registerNodeType<IllegalPorts>("nope"));
}