gtest_enums.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
3 
4 using namespace BT;
5 
6 enum class Color
7 {
8  Red = 0,
9  Blue = 1,
10  Green = 2,
11  Undefined
12 };
13 
14 static const char* ToStr(const Color& c)
15 {
16  switch(c)
17  {
18  case Color::Red:
19  return "Red";
20  case Color::Blue:
21  return "Blue";
22  case Color::Green:
23  return "Green";
24  case Color::Undefined:
25  return "Undefined";
26  }
27  return nullptr;
28 }
29 
30 class ActionEnum : public SyncActionNode
31 {
32 public:
33  ActionEnum(const std::string& name, const NodeConfig& config)
34  : SyncActionNode(name, config)
35  {}
36 
37  NodeStatus tick() override
38  {
39  getInput("color", color);
40  std::cout << "Node: " << name() << " has color : " << ToStr(color) << std::endl;
41  return NodeStatus::SUCCESS;
42  }
43 
45  {
46  return { BT::InputPort<Color>("color") };
47  }
48 
50 };
51 
52 TEST(Enums, StrintToEnum)
53 {
54  std::string xml_txt = R"(
55  <root BTCPP_format="4" >
56  <BehaviorTree ID="Main">
57  <Sequence>
58  <Script code=" my_color := Red "/>
59  <ActionEnum name="maybe_blue" color="Blue"/>
60  <ActionEnum name="maybe_green" color="2"/>
61  <ActionEnum name="maybe_red" color="{my_color}"/>
62  </Sequence>
63  </BehaviorTree>
64  </root>)";
65 
66  BehaviorTreeFactory factory;
67  factory.registerNodeType<ActionEnum>("ActionEnum");
68  factory.registerScriptingEnums<Color>();
69 
70  auto tree = factory.createTreeFromText(xml_txt);
71 
72  NodeStatus status = tree.tickWhileRunning();
73 
74  ASSERT_EQ(status, NodeStatus::SUCCESS);
75 
76  for(const auto& node : tree.subtrees.front()->nodes)
77  {
78  if(auto enum_node = dynamic_cast<ActionEnum*>(node.get()))
79  {
80  if(enum_node->name() == "maybe_red")
81  {
82  ASSERT_EQ(Color::Red, enum_node->color);
83  }
84  else if(enum_node->name() == "maybe_green")
85  {
86  ASSERT_EQ(Color::Green, enum_node->color);
87  }
88  else if(enum_node->name() == "maybe_blue")
89  {
90  ASSERT_EQ(Color::Blue, enum_node->color);
91  }
92  }
93  }
94 }
95 
96 TEST(Enums, SwitchNodeWithEnum)
97 {
98  const std::string xml_txt = R"(
99  <root BTCPP_format="4" >
100  <BehaviorTree ID="Main">
101  <Sequence>
102  <Script code=" my_color := Blue "/>
103  <Switch4 variable="{my_color}"
104  case_1="Red"
105  case_2="Blue"
106  case_3="Green"
107  case_4="Undefined">
108  <AlwaysFailure name="case_red" />
109  <AlwaysSuccess name="case_blue" />
110  <AlwaysFailure name="case_green" />
111  <AlwaysFailure name="case_undefined" />
112  <AlwaysFailure name="default_case" />
113  </Switch4>
114  </Sequence>
115  </BehaviorTree>
116  </root>)";
117 
118  BehaviorTreeFactory factory;
119  factory.registerScriptingEnums<Color>();
120 
121  auto tree = factory.createTreeFromText(xml_txt);
122 
123  NodeStatus status = tree.tickWhileRunning();
124 
125  ASSERT_EQ(status, NodeStatus::SUCCESS);
126 }
127 
129 {
130  NO_FAULT = 0,
132 };
133 
135 {
136 public:
137  explicit PrintEnum(const std::string& name, const BT::NodeConfig& config)
138  : ConditionNode(name, config)
139  {}
140 
141  ~PrintEnum() override = default;
142 
144  {
145  return {
146  BT::InputPort<BatteryStatus>("enum", "Name of the check"),
147  };
148  }
149 
150 private:
151  BT::NodeStatus tick() override
152  {
153  auto enum_value = getInput<BatteryStatus>("enum");
154  if(!enum_value)
155  {
156  std::cout << "missing required input [enum]" << std::endl;
158  }
159  std::cout << "Enum value: " << (enum_value == NO_FAULT ? "NO_FAULT" : "LOW_BATTERY")
160  << std::endl;
162  }
163 };
164 
166 {
167 public:
168  explicit IsHealthOk(const std::string& name, const BT::NodeConfig& config)
169  : BT::ConditionNode(name, config)
170  {}
171 
172  ~IsHealthOk() override = default;
173 
175  {
176  return { BT::InputPort<std::string>("check_name"), BT::InputPort<bool>("health") };
177  }
178 
179 private:
180  BT::NodeStatus tick() override
181  {
182  auto health = getInput<bool>("health");
183  if(!health)
184  {
185  std::cout << "missing required input [health]" << std::endl;
187  }
188 
189  if(health.value())
190  {
192  }
193  else
194  {
195  std::cerr << "IsHealthOk FAILED " << std::endl;
197  }
199  }
200 };
201 
202 TEST(Enums, SubtreeRemapping)
203 {
204  const std::string xml_txt = R"(
205  <root BTCPP_format="4" >
206  <BehaviorTree ID="MainTree">
207  <Sequence>
208  <Script code=" fault_status := NO_FAULT " />
209  <PrintEnum enum="{fault_status}"/>
210 
211  <SubTree ID="FailsafeCheck"
212  health="false"
213  trigger_fault_status="LOW_BATTERY"
214  fault_status="{=}" />
215 
216  <PrintEnum enum="{fault_status}"/>
217  </Sequence>
218  </BehaviorTree>
219 
220  <BehaviorTree ID="FailsafeCheck">
221  <ForceSuccess>
222  <IsHealthOk
223  health="{health}"
224  _onFailure="fault_status = trigger_fault_status"/>
225  </ForceSuccess>
226  </BehaviorTree>
227  </root>)";
228 
229  BehaviorTreeFactory factory;
231  factory.registerNodeType<PrintEnum>("PrintEnum");
232  factory.registerNodeType<IsHealthOk>("IsHealthOk");
233 
234  factory.registerBehaviorTreeFromText(xml_txt);
235 
236  auto tree = factory.createTree("MainTree");
237  NodeStatus status = tree.tickWhileRunning();
238 
239  ASSERT_EQ(status, NodeStatus::SUCCESS);
240  ASSERT_EQ(tree.rootBlackboard()->get<BatteryStatus>("fault_status"), LOW_BATTERY);
241 }
lexy::_detail::color
color
Definition: visualize.hpp:120
BT
Definition: ex01_wrap_legacy.cpp:29
PrintEnum::providedPorts
static BT::PortsList providedPorts()
Definition: gtest_enums.cpp:143
BT::ConditionNode
Definition: condition_node.h:21
Color::Undefined
@ Undefined
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
NO_FAULT
@ NO_FAULT
Definition: gtest_enums.cpp:130
ToStr
static const char * ToStr(const Color &c)
Definition: gtest_enums.cpp:14
Color::Blue
@ Blue
bt_factory.h
ActionEnum::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: gtest_enums.cpp:37
TEST
TEST(Enums, StrintToEnum)
Definition: gtest_enums.cpp:52
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:610
IsHealthOk::IsHealthOk
IsHealthOk(const std::string &name, const BT::NodeConfig &config)
Definition: gtest_enums.cpp:168
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
PrintEnum::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: gtest_enums.cpp:151
ActionEnum
Definition: gtest_enums.cpp:30
BatteryStatus
BatteryStatus
Definition: gtest_enums.cpp:128
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
BT::BehaviorTreeFactory::createTreeFromText
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 ...
Definition: bt_factory.cpp:395
Color
Color
Definition: gtest_enums.cpp:6
magic_enum::enum_value
constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_t< E, std::decay_t< E >>
Definition: magic_enum.hpp:1176
PrintEnum::PrintEnum
PrintEnum(const std::string &name, const BT::NodeConfig &config)
Definition: gtest_enums.cpp:137
Color::Green
@ Green
IsHealthOk
Definition: gtest_enums.cpp:165
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
Color::Red
@ Red
BT::NodeStatus::SUCCESS
@ SUCCESS
ActionEnum::ActionEnum
ActionEnum(const std::string &name, const NodeConfig &config)
Definition: gtest_enums.cpp:33
PrintEnum
Definition: gtest_enums.cpp:134
BT::BehaviorTreeFactory::registerBehaviorTreeFromText
void registerBehaviorTreeFromText(const std::string &xml_text)
Definition: bt_factory.cpp:277
IsHealthOk::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: gtest_enums.cpp:180
IsHealthOk::providedPorts
static BT::PortsList providedPorts()
Definition: gtest_enums.cpp:174
LOW_BATTERY
@ LOW_BATTERY
Definition: gtest_enums.cpp:131
ActionEnum::providedPorts
static PortsList providedPorts()
Definition: gtest_enums.cpp:44
BT::NodeConfig
Definition: tree_node.h:73
BT::BehaviorTreeFactory::registerScriptingEnums
void registerScriptingEnums()
registerScriptingEnums is syntactic sugar to automatically register multiple enums....
Definition: bt_factory.h:462
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16