Go to the documentation of this file. 1 #include <gtest/gtest.h>
3 #include "../sample_nodes/dummy_nodes.h"
4 #include "../sample_nodes/movebase_node.h"
9 TEST(SubTree, SiblingPorts_Issue_72)
13 <root BTCPP_format="4" main_tree_to_execute="MainTree" >
15 <BehaviorTree ID="MainTree">
17 <Script code = " myParam = 'hello' " />
18 <SubTree ID="mySubtree" param="{myParam}" />
19 <Script code = " myParam = 'world' " />
20 <SubTree ID="mySubtree" param="{myParam}" />
24 <BehaviorTree ID="mySubtree">
25 <SaySomething message="{param}" />
34 for(
auto& subtree : tree.subtrees)
36 subtree->blackboard->debugMessage();
37 std::cout <<
"-----" << std::endl;
42 ASSERT_EQ(tree.subtrees.size(), 3);
54 auto msg = getInput<std::string>(
"in");
59 setOutput(
"out", msg.value());
65 return { BT::InputPort<std::string>(
"in"), BT::OutputPort<std::string>(
"out") };
69 TEST(SubTree, GoodRemapping)
73 <root BTCPP_format="4" main_tree_to_execute="MainTree">
75 <BehaviorTree ID="MainTree">
77 <Script code = " thoughts = 'hello' " />
78 <SubTree ID="CopySubtree" in_arg="{thoughts}" out_arg="{greetings}"/>
79 <SaySomething message="{greetings}" />
83 <BehaviorTree ID="CopySubtree">
84 <CopyPorts in="{in_arg}" out="{out_arg}"/>
98 TEST(SubTree, BadRemapping)
104 static const char* xml_text_bad_in = R
"(
105 <root BTCPP_format="4" >
107 <BehaviorTree ID="MainTree">
109 <Script code = " thoughts='hello' " />
110 <SubTree ID="CopySubtree" out_arg="{greetings}"/>
111 <SaySomething message="{greetings}" />
115 <BehaviorTree ID="CopySubtree">
116 <CopyPorts in="{in_arg}" out="{out_arg}"/>
124 static const char* xml_text_bad_out = R
"(
125 <root BTCPP_format="4" >
127 <BehaviorTree ID="MainTree">
129 <Script code = " thoughts='hello' " />
130 <SubTree ID="CopySubtree" in_arg="{thoughts}"/>
131 <SaySomething message="{greetings}" />
135 <BehaviorTree ID="CopySubtree">
136 <CopyPorts in="{in_arg}" out="{out_arg}"/>
149 <root BTCPP_format="4" >
151 <BehaviorTree ID="MainTree">
153 <Script code = "myParam = 'Hello' " />
154 <SubTree ID="mySubtree" param="{myParam}" />
155 <SubTree ID="mySubtree" param="World" />
156 <Script code = "param = 'Auto remapped' " />
157 <SubTree ID="mySubtree" _autoremap="1" />
161 <BehaviorTree ID="mySubtree">
162 <SaySomething message="{param}" />
180 <root BTCPP_format="4" >
182 <BehaviorTree ID="MainTree">
184 <Script code = "myParam = 'Hello World'; param3='Auto remapped' " />
185 <SubTree ID="mySubtree" _autoremap="1" param1="{myParam}" param2="Straight Talking" />
189 <BehaviorTree ID="mySubtree">
191 <SaySomething message="{param1}" />
192 <SaySomething message="{param2}" />
193 <SaySomething message="{param3}" />
214 auto msg = getInput<std::string>(
"message");
227 return { BT::InputPort<std::string>(
"message") };
237 <root BTCPP_format="4" >
239 <BehaviorTree ID="MainTree">
241 <SubTree ID="mySubtree" _autoremap="1"/>
244 <BehaviorTree ID="mySubtree">
245 <ReadInConstructor message="{message}" />
265 <root BTCPP_format="4" >
267 <BehaviorTree ID="MainTree">
269 <Script code = "value:=0" />
270 <SubTree ID="mySubtree" value="{value}" />
274 <BehaviorTree ID="mySubtree">
275 <Script code = "value:=1" />
285 ASSERT_EQ(tree.
subtrees[1]->blackboard->get<
int>(
"value"), 1);
286 ASSERT_EQ(tree.
subtrees[0]->blackboard->get<
int>(
"value"), 1);
299 return { BT::BidirectionalPort<Pose2D>(
"pose") };
305 getInput(
"pose", pose);
307 setOutput(
"pose", pose);
312 TEST(SubTree, StringConversions_Issue530)
315 <root BTCPP_format="4" >
316 <BehaviorTree ID="MainTree">
318 <Script code=" pose:='1;2;3' "/>
319 <ModifyPose pose="{pose}"/>
320 <Script code=" pose:='1;2;3' "/>
339 std::cout <<
"CTOR:" << config.
blackboard->get<std::string>(
"ros_node") << std::endl;
344 std::cout <<
"tick:" << config().blackboard->get<std::string>(
"ros_node")
354 TEST(SubTree, SubtreeNav2_Issue563)
357 <root BTCPP_format="4" >
359 <BehaviorTree ID="Tree1">
361 <SetBlackboard output_key="the_message" value="hello world"/>
362 <SubTree ID="Tree2" _autoremap="true"/>
363 <SaySomething message="{reply}" />
367 <BehaviorTree ID="Tree2">
368 <SubTree ID="Tree3" _autoremap="true"/>
371 <BehaviorTree ID="Tree3">
372 <SubTree ID="Talker" _autoremap="true"/>
375 <BehaviorTree ID="Talker">
377 <SaySomething message="{the_message}" />
378 <Script code=" reply:='done' "/>
392 blackboard->set<std::string>(
"ros_node",
"nav2_shouldnt_do_this");
400 TEST(SubTree, SubtreeNav2_Issue724)
403 <root BTCPP_format="4" >
405 <BehaviorTree ID="Tree1">
407 <SubTree ID="Tree2" ros_node="{ros_node}"/>
411 <BehaviorTree ID="Tree2">
412 <SubTree ID="Tree3" ros_node="{ros_node}"/>
415 <BehaviorTree ID="Tree3">
416 <SubTree ID="Talker" ros_node="{ros_node}"/>
419 <BehaviorTree ID="Talker">
433 blackboard->set<std::string>(
"ros_node",
"nav2_shouldnt_do_this");
444 <root BTCPP_format="4" >
446 <BehaviorTree ID="Outer_Tree">
448 <Script code="variable := 'test'"/>
449 <Script code="var := 'test'"/>
450 <SubTree ID="Inner_Tree" _autoremap="false" variable="{var}" />
451 <SubTree ID="Inner_Tree" _autoremap="true"/>
455 <BehaviorTree ID="Inner_Tree">
457 <TestA _skipIf="variable != 'test'"/>
464 std::array<int, 1> counters;
472 ASSERT_EQ(counters[0], 2);
475 TEST(SubTree, Issue623_String_to_Pose2d)
480 <root main_tree_to_execute="Test" BTCPP_format="4">
482 <BehaviorTree ID="Test">
483 <ReactiveSequence name="MainSequence">
484 <SubTree name="Visit2" ID="Visit2" tl1="1;2;3"/>
488 <BehaviorTree ID="Visit2">
489 <Sequence name="Visit2MainSequence">
490 <Action name="MoveBase" ID="MoveBase" goal="{tl1}"/>
501 tree.tickWhileRunning();
513 return { BT::InputPort<bool>(
"condition") };
519 if(getInput<bool>(
"condition").value())
526 TEST(SubTree, Issue653_SetBlackboard)
531 <root main_tree_to_execute = "MainTree" BTCPP_format="4">
532 <BehaviorTree ID="MainTree">
534 <SubTree ID="Init" test="{test}" />
535 <Assert condition="{test}" />
539 <BehaviorTree ID="Init">
540 <SetBlackboard output_key="test" value="true"/>
550 tree.tickWhileRunning();
558 <root main_tree_to_execute = "MainTree" BTCPP_format="4">
561 <input_port name="in_value" default="42"/>
562 <input_port name="in_name"/>
563 <output_port name="out_result" default="{output}"/>
564 <output_port name="out_state"/>
568 <BehaviorTree ID="MainTree">
570 <Script code="my_name:= 'john' "/>
571 <SubTree ID="MySub" in_name="{my_name}" out_state="{my_state}"/>
572 <ScriptCondition code=" output==69 && my_state=='ACTIVE' " />
576 <BehaviorTree ID="MySub">
578 <ScriptCondition code="in_name=='john' && in_value==42" />
579 <Script code="out_result:=69; out_state:='ACTIVE'" />
596 std::vector<std::string>* console)
602 return { BT::InputPort<std::string>(
"message") };
608 if(
auto res = getInput<std::string>(
"message"))
610 console_->push_back(res.value());
619 TEST(SubTree, RemappingIssue696)
624 <root BTCPP_format="4">
625 <BehaviorTree ID="Subtree1">\n"
627 <PrintToConsole message="{msg1}"/>
628 <PrintToConsole message="{msg2}"/>
632 <BehaviorTree ID="Subtree2">
634 <SubTree ID="Subtree1" msg1="foo1" _autoremap="true"/>
635 <SubTree ID="Subtree1" msg1="foo2" _autoremap="true"/>
639 <BehaviorTree ID="MainTree">
640 <SubTree ID="Subtree2" msg2="bar"/>
648 std::vector<std::string> console;
655 ASSERT_EQ(console.size(), 4);
656 ASSERT_EQ(console[0],
"foo1");
657 ASSERT_EQ(console[1],
"bar");
658 ASSERT_EQ(console[2],
"foo2");
659 ASSERT_EQ(console[3],
"bar");
662 TEST(SubTree, PrivateAutoRemapping)
667 <root BTCPP_format="4">
668 <BehaviorTree ID="Subtree">\n"
670 <SetBlackboard output_key="public_value" value="hello"/>
671 <SetBlackboard output_key="_private_value" value="world"/>
675 <BehaviorTree ID="MainTree">
677 <SubTree ID="Subtree" _autoremap="true"/>
678 <PrintToConsole message="{public_value}"/>
679 <PrintToConsole message="{_private_value}"/>
687 std::vector<std::string> console;
696 ASSERT_EQ(console.size(), 1);
697 ASSERT_EQ(console[0],
"hello");
700 TEST(SubTree, SubtreeNameNotRegistered)
705 <root BTCPP_format="4">
706 <BehaviorTree ID="PrintToConsole">\n"
708 <PrintToConsole message="world"/>
712 <BehaviorTree ID="MainTree">
714 <PrintToConsole message="hello"/>
715 <SubTree ID="PrintToConsole"/>
723 std::vector<std::string> console;
static BT::PortsList providedPorts()
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
void RegisterTestTick(BT::BehaviorTreeFactory &factory, const std::string &name_prefix, std::array< int, N > &tick_counters)
Assert(const std::string &name, const BT::NodeConfiguration &config)
BT::NodeStatus tick() override
Method to be implemented by the user.
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
NodeStatus tickOnce()
by default, tickOnce() sends a single tick, BUT as long as there is at least one node of the tree inv...
ReadInConstructor(const std::string &name, const BT::NodeConfig &config)
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
TEST(SubTree, SiblingPorts_Issue_72)
static BT::PortsList providedPorts()
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Blackboard::Ptr blackboard
std::unordered_map< std::string, PortInfo > PortsList
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
static BT::PortsList providedPorts()
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
BT::NodeStatus tick() override
Method to be implemented by the user.
static BT::PortsList providedPorts()
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
static Blackboard::Ptr create(Blackboard::Ptr parent={})
ModifyPose(const std::string &name, const BT::NodeConfig &config)
std::vector< Subtree::Ptr > subtrees
CopyPorts(const std::string &name, const BT::NodeConfig &config)
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
BT::NodeStatus tick() override
Method to be implemented by the user.
NaughtyNav2Node(const std::string &name, const BT::NodeConfiguration &config)
void registerBehaviorTreeFromText(const std::string &xml_text)
static BT::PortsList providedPorts()
BT::NodeStatus tick() override
Method to be implemented by the user.
static BT::PortsList providedPorts()
static const char * xml_text
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
PrintToConsole(const std::string &name, const BT::NodeConfiguration &config, std::vector< std::string > *console)
std::vector< std::string > * console_