1 #include <gtest/gtest.h>
7 #include "../sample_nodes/crossdoor_nodes.h"
8 #include "../sample_nodes/dummy_nodes.h"
16 <root BTCPP_format="4" >
18 <BehaviorTree ID="MainTree">
19 <Fallback name="root_selector">
21 <Sequence name="door_open_sequence">
22 <Action ID="IsDoorOpen" />
23 <Action ID="PassThroughDoor" />
26 <Sequence name="door_closed_sequence">
27 <Decorator ID="Inverter">
28 <Action ID="IsDoorOpen" />
30 <Action ID="OpenDoor" />
31 <Action ID="PassThroughDoor" />
32 <Action ID="CloseDoor" />
35 <Action ID="PassThroughWindow" />
40 <!-- TreeNodesModel is used only by the Graphic interface -->
42 <Action ID="IsDoorOpen" />
43 <Action ID="PassThroughDoor" />
44 <Action ID="CloseDoor" />
45 <Action ID="OpenDoor" />
46 <Action ID="PassThroughWindow" />
53 <root BTCPP_format="4" main_tree_to_execute="MainTree" >
55 <BehaviorTree ID="MainTree">
61 <SubTree ID="DoorClosedSubtree"/>
67 <BehaviorTree ID="DoorClosedSubtree">
70 <RetryUntilSuccessful num_attempts="5">
72 </RetryUntilSuccessful>
81 <root BTCPP_format="4">
82 <BehaviorTree ID="MainTree">
83 <Fallback name="root_selector">
84 <SubTree ID="DoorClosedSubtree" />
85 <Action ID="PassThroughWindow" />
92 <root BTCPP_format="4">
93 <BehaviorTree ID="DoorClosedSubtree">
94 <Sequence name="door_sequence">
95 <Decorator ID="Inverter">
96 <Action ID="IsDoorLocked" />
98 <Action ID="OpenDoor" />
99 <Action ID="PassThroughDoor" />
100 <Action ID="CloseDoor" />
124 ASSERT_EQ(trees[0],
"DoorClosedSubtree");
125 ASSERT_EQ(trees[1],
"MainTree");
132 ASSERT_EQ(trees[0],
"DoorClosedSubtree");
133 ASSERT_EQ(trees[1],
"MainTree");
140 ASSERT_EQ(trees[0],
"DoorClosedSubtree");
141 ASSERT_EQ(trees[1],
"MainTree");
157 auto const& main_tree = tree.
subtrees[0];
158 auto const& subtree = tree.
subtrees[1];
160 ASSERT_EQ(main_tree->nodes.size(), 6);
161 ASSERT_EQ(main_tree->nodes[0]->name(),
"Sequence");
162 ASSERT_EQ(main_tree->nodes[1]->name(),
"Fallback");
163 ASSERT_EQ(main_tree->nodes[2]->name(),
"Inverter");
164 ASSERT_EQ(main_tree->nodes[3]->name(),
"IsDoorClosed");
166 ASSERT_EQ(main_tree->nodes[5]->name(),
"PassThroughDoor");
168 ASSERT_EQ(subtree->nodes.size(), 5);
169 ASSERT_EQ(subtree->nodes[0]->name(),
"Fallback");
170 ASSERT_EQ(subtree->nodes[1]->name(),
"OpenDoor");
171 ASSERT_EQ(subtree->nodes[2]->name(),
"RetryUntilSuccessful");
172 ASSERT_EQ(subtree->nodes[3]->name(),
"PickLock");
173 ASSERT_EQ(subtree->nodes[4]->name(),
"SmashDoor");
178 const std::string xml_text_issue = R
"(
179 <root BTCPP_format="4">
180 <BehaviorTree ID="ReceiveGuest">
194 <root BTCPP_format="4" main_tree_to_execute="MainTree">
196 <BehaviorTree ID="TalkToMe">
198 <SaySomething message="{hello_msg}" />
199 <SaySomething message="{bye_msg}" />
200 <Script code=" output:='done!' " />
204 <BehaviorTree ID="MainTree">
206 <Script code = " talk_hello:='hello' " />
207 <Script code = " talk_bye:='bye bye' " />
208 <SubTree ID="TalkToMe" hello_msg="{talk_hello}"
210 output="{talk_out}" />
211 <SaySomething message="{talk_out}" />
226 auto main_bb = tree.
subtrees.at(0)->blackboard;
227 auto talk_bb = tree.subtrees.at(1)->blackboard;
229 std::cout <<
"\n --------------------------------- \n" << std::endl;
230 main_bb->debugMessage();
231 std::cout <<
"\n ----- \n" << std::endl;
232 talk_bb->debugMessage();
233 std::cout <<
"\n --------------------------------- \n" << std::endl;
236 tree.tickWhileRunning();
238 ASSERT_EQ(main_bb->entryInfo(
"talk_hello")->type(),
typeid(std::string));
239 ASSERT_EQ(main_bb->entryInfo(
"talk_bye")->type(),
typeid(std::string));
240 ASSERT_EQ(main_bb->entryInfo(
"talk_out")->type(),
typeid(std::string));
242 ASSERT_EQ(talk_bb->entryInfo(
"bye_msg")->type(),
typeid(std::string));
243 ASSERT_EQ(talk_bb->entryInfo(
"hello_msg")->type(),
typeid(std::string));
245 std::cout <<
"\n --------------------------------- \n" << std::endl;
246 main_bb->debugMessage();
247 std::cout <<
"\n ----- \n" << std::endl;
248 talk_bb->debugMessage();
249 std::cout <<
"\n --------------------------------- \n" << std::endl;
251 ASSERT_EQ(main_bb->entryInfo(
"talk_hello")->type(),
typeid(std::string));
252 ASSERT_EQ(main_bb->entryInfo(
"talk_bye")->type(),
typeid(std::string));
253 ASSERT_EQ(main_bb->entryInfo(
"talk_out")->type(),
typeid(std::string));
255 ASSERT_EQ(talk_bb->entryInfo(
"bye_msg")->type(),
typeid(std::string));
256 ASSERT_EQ(talk_bb->entryInfo(
"hello_msg")->type(),
typeid(std::string));
257 ASSERT_EQ(talk_bb->entryInfo(
"output")->type(),
typeid(std::string));
259 ASSERT_EQ(main_bb->get<std::string>(
"talk_hello"),
"hello");
260 ASSERT_EQ(main_bb->get<std::string>(
"talk_bye"),
"bye bye");
261 ASSERT_EQ(main_bb->get<std::string>(
"talk_out"),
"done!");
264 ASSERT_FALSE(talk_bb->getAnyLocked(
"talk_hello"));
265 ASSERT_FALSE(talk_bb->getAnyLocked(
"talk_bye"));
266 ASSERT_FALSE(talk_bb->getAnyLocked(
"talk_out"));
269 std::string
FilePath(
const std::filesystem::path& relative_path)
272 static const std::filesystem::path search_paths[] = {
274 std::filesystem::current_path() /
"tests"};
277 for(
auto const& path : search_paths)
279 if(std::filesystem::exists(path / relative_path))
281 return (path / relative_path).string();
292 std::string path =
FilePath(
"trees/parent_no_include.xml");
302 std::string path =
FilePath(
"trees/child/child_include_sibling.xml");
312 std::string path =
FilePath(
"trees/parent_include_child.xml");
319 CreateTreeFromFileWhichIncludesFileFromChildDirectoryWhichIncludesFileFromSameDirectory)
324 std::string path =
FilePath(
"trees/parent_include_child_include_sibling.xml");
331 CreateTreeFromFileWhichIncludesFileFromChildDirectoryWhichIncludesFileFromChildDirectory)
336 std::string path =
FilePath(
"trees/parent_include_child_include_child.xml");
343 CreateTreeFromFileWhichIncludesFileFromChildDirectoryWhichIncludesFileFromParentDirectory)
348 std::string path =
FilePath(
"trees/parent_include_child_include_parent.xml");
355 const char* xmlA = R
"(
356 <root BTCPP_format="4" >
357 <BehaviorTree ID="MainTree">
365 EXPECT_ANY_THROW(auto tree = factory.
createTree(
"Wrong Name"));
368 TEST(BehaviorTreeReload, ReloadSameTree)
370 const char* xmlA = R
"(
371 <root BTCPP_format="4" >
372 <BehaviorTree ID="MainTree">
377 const char* xmlB = R
"(
378 <root BTCPP_format="4" >
379 <BehaviorTree ID="MainTree">
402 std::make_pair<std::string, std::string>(
"foo",
"hello"),
403 std::make_pair<std::string, std::string>(
"bar",
"42"),
432 const char* expectedXML = R
"(
433 <Action ID="ActionWithMetadata">
435 <Metadata foo="hello"/>
446 std::cout << xml << std::endl;
448 EXPECT_NE(xml.find(expectedXML), std::string::npos);
455 const auto& initial_manifest = factory.
manifests().at(
"SaySomething");
456 EXPECT_TRUE(initial_manifest.metadata.empty());
458 const auto& modified_manifest = factory.
manifests().at(
"SaySomething");