Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c66fc23

Browse files
committed
RemappedSubTree added
1 parent 5c0c835 commit c66fc23

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

include/behaviortree_cpp_v3/decorators/subtree_node.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ class SubtreeNode : public DecoratorNode
2828
}
2929
};
3030

31+
/**
32+
* @brief Subtree that doesn't need any remapping.
33+
* Its blackboard is shared with the parent node
34+
*/
35+
class RemappedSubtreeNode : public DecoratorNode
36+
{
37+
public:
38+
RemappedSubtreeNode(const std::string& name);
39+
40+
virtual ~RemappedSubtreeNode() override = default;
41+
42+
private:
43+
virtual BT::NodeStatus tick() override;
44+
45+
virtual NodeType type() const override final
46+
{
47+
return NodeType::SUBTREE;
48+
}
49+
};
50+
51+
3152
/**
3253
* @brief The SubtreePlus is a new kind of subtree that gives you much more control over remapping:
3354
*

src/bt_factory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ BehaviorTreeFactory::BehaviorTreeFactory()
4444

4545
registerNodeType<SubtreeNode>("SubTree");
4646
registerNodeType<SubtreePlusNode>("SubTreePlus");
47+
registerNodeType<RemappedSubtreeNode>("RemappedSubTree");
4748

4849
registerNodeType<BlackboardPreconditionNode<int>>("BlackboardCheckInt");
4950
registerNodeType<BlackboardPreconditionNode<double>>("BlackboardCheckDouble");

src/decorators/subtree_node.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ BT::NodeStatus BT::SubtreeNode::tick()
1717
return child_node_->executeTick();
1818
}
1919

20+
//--------------------------------
21+
BT::RemappedSubtreeNode::RemappedSubtreeNode(const std::string &name) :
22+
DecoratorNode(name, {} )
23+
{
24+
setRegistrationID("RemappedSubtree");
25+
}
26+
27+
BT::NodeStatus BT::RemappedSubtreeNode::tick()
28+
{
29+
NodeStatus prev_status = status();
30+
if (prev_status == NodeStatus::IDLE)
31+
{
32+
setStatus(NodeStatus::RUNNING);
33+
}
34+
return child_node_->executeTick();
35+
}
36+
2037
//--------------------------------
2138
BT::SubtreePlusNode::SubtreePlusNode(const std::string &name) :
2239
DecoratorNode(name, {} )

src/xml_parsing.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
#include <list>
1515

1616
#if defined(__linux) || defined(__linux__)
17-
#pragma GCC diagnostic push
18-
#pragma GCC diagnostic ignored "-Wattributes"
19-
#endif
17+
#pragma GCC diagnostic push
18+
#pragma GCC diagnostic ignored "-Wattributes"
19+
#endif
2020

2121
#ifdef _MSC_VER
2222
#pragma warning(disable : 4996) // do not complain about sprintf
@@ -352,7 +352,7 @@ void VerifyXML(const std::string& xml_text,
352352
}
353353
//recursion
354354
if (StrEqual(name, "SubTree") == false)
355-
{
355+
{
356356
for (auto child = node->FirstChildElement(); child != nullptr;
357357
child = child->NextSiblingElement())
358358
{
@@ -462,23 +462,22 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement *element,
462462
instance_name = ID;
463463
}
464464

465+
PortsRemapping port_remap;
466+
465467
if (element_name == "SubTree" ||
466-
element_name == "SubTreePlus" )
468+
element_name == "SubTreePlus" ||
469+
element_name == "RemappedSubTree")
467470
{
468471
instance_name = element->Attribute("ID");
469472
}
470-
471-
PortsRemapping parameters_map;
472-
473-
// in Subtree attributes have different meaning...
474-
if (element_name != "SubTree" && element_name != "SubTreePlus")
475-
{
473+
else{
474+
// do this only if it NOT a Subtree
476475
for (const XMLAttribute* att = element->FirstAttribute(); att; att = att->Next())
477476
{
478477
const std::string attribute_name = att->Name();
479478
if (attribute_name != "ID" && attribute_name != "name")
480479
{
481-
parameters_map[attribute_name] = att->Value();
480+
port_remap[attribute_name] = att->Value();
482481
}
483482
}
484483
}
@@ -493,12 +492,12 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement *element,
493492
const auto& manifest = factory.manifests().at(ID);
494493

495494
//Check that name in remapping can be found in the manifest
496-
for(const auto& param_it: parameters_map)
495+
for(const auto& remap_it: port_remap)
497496
{
498-
if( manifest.ports.count( param_it.first ) == 0 )
497+
if( manifest.ports.count( remap_it.first ) == 0 )
499498
{
500499
throw RuntimeError("Possible typo? In the XML, you tried to remap port \"",
501-
param_it.first, "\" in node [", ID," / ", instance_name,
500+
remap_it.first, "\" in node [", ID," / ", instance_name,
502501
"], but the manifest of this node does not contain a port with this name.");
503502
}
504503
}
@@ -509,8 +508,8 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement *element,
509508
const std::string& port_name = port_it.first;
510509
const auto& port_info = port_it.second;
511510

512-
auto remap_it = parameters_map.find(port_name);
513-
if( remap_it == parameters_map.end())
511+
auto remap_it = port_remap.find(port_name);
512+
if( remap_it == port_remap.end())
514513
{
515514
continue;
516515
}
@@ -543,20 +542,20 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement *element,
543542
}
544543

545544
// use manifest to initialize NodeConfiguration
546-
for(const auto& param_it: parameters_map)
545+
for(const auto& remap_it: port_remap)
547546
{
548-
const auto& port_name = param_it.first;
547+
const auto& port_name = remap_it.first;
549548
auto port_it = manifest.ports.find( port_name );
550549
if( port_it != manifest.ports.end() )
551550
{
552551
auto direction = port_it->second.direction();
553552
if( direction != PortDirection::OUTPUT )
554553
{
555-
config.input_ports.insert( param_it );
554+
config.input_ports.insert( remap_it );
556555
}
557556
if( direction != PortDirection::INPUT )
558557
{
559-
config.output_ports.insert( param_it );
558+
config.output_ports.insert( remap_it );
560559
}
561560
}
562561
}
@@ -612,7 +611,11 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID,
612611

613612
if( node->type() == NodeType::SUBTREE )
614613
{
615-
if( dynamic_cast<const SubtreeNode*>(node.get()) )
614+
if( dynamic_cast<const RemappedSubtreeNode*>(node.get()) )
615+
{
616+
recursivelyCreateTree( node->name(), output_tree, blackboard, node );
617+
}
618+
else if( dynamic_cast<const SubtreeNode*>(node.get()) )
616619
{
617620
// This is the former SubTree with manual remapping
618621
auto new_bb = Blackboard::create(blackboard);
@@ -630,7 +633,7 @@ void BT::XMLParser::Pimpl::recursivelyCreateTree(const std::string& tree_ID,
630633
}
631634
else if( dynamic_cast<const SubtreePlusNode*>(node.get()) )
632635
{
633-
auto new_bb = Blackboard::create(blackboard);
636+
auto new_bb = Blackboard::create(blackboard);
634637
output_tree.blackboard_stack.emplace_back(new_bb);
635638
std::set<StringView> mapped_keys;
636639

0 commit comments

Comments
 (0)