t12_default_ports.cpp
Go to the documentation of this file.
3 
10 // Custom type. to make things more interesting
11 struct Point2D
12 {
13  int x = 0;
14  int y = 0;
15  bool operator==(const Point2D& p) const
16  {
17  return x == p.x && y == p.y;
18  }
19  bool operator!=(const Point2D& p) const
20  {
21  return !(*this == p);
22  }
23 };
24 
25 // Allow bi-directional convertion to JSON
27 {
28  add_field("x", &point.x);
29  add_field("y", &point.y);
30 }
31 
32 // We can extend the traditional BT::convertFromString<Point2D>()
33 // to support the JSON format too (see port with name "pointE")
34 template <>
35 [[nodiscard]] Point2D BT::convertFromString<Point2D>(StringView str)
36 {
37  if(StartWith(str, "json:"))
38  {
39  str.remove_prefix(5);
40  return convertFromJSON<Point2D>(str);
41  }
42  const auto parts = BT::splitString(str, ',');
43  if(parts.size() != 2)
44  {
45  throw BT::RuntimeError("invalid input)");
46  }
47  int x = convertFromString<int>(parts[0]);
48  int y = convertFromString<int>(parts[1]);
49  return { x, y };
50 }
51 
52 //-----------------------------------------------
53 using namespace BT;
54 
56 {
57 public:
58  NodeWithDefaultPoints(const std::string& name, const NodeConfig& config)
59  : SyncActionNode(name, config)
60  {}
61 
62  NodeStatus tick() override
63  {
64  // Let0s check if all the portas have the expected value
65  Point2D pointA, pointB, pointC, pointD, pointE, input;
66 
67  if(!getInput("pointA", pointA) || pointA != Point2D{ 1, 2 })
68  {
69  throw std::runtime_error("failed pointA");
70  }
71  if(!getInput("pointB", pointB) || pointB != Point2D{ 3, 4 })
72  {
73  throw std::runtime_error("failed pointB");
74  }
75  if(!getInput("pointC", pointC) || pointC != Point2D{ 5, 6 })
76  {
77  throw std::runtime_error("failed pointC");
78  }
79  if(!getInput("pointD", pointD) || pointD != Point2D{ 7, 8 })
80  {
81  throw std::runtime_error("failed pointD");
82  }
83  if(!getInput("pointE", pointE) || pointE != Point2D{ 9, 10 })
84  {
85  throw std::runtime_error("failed pointE");
86  }
87  if(!getInput("input", input) || input != Point2D{ -1, -2 })
88  {
89  throw std::runtime_error("failed input");
90  }
91  return NodeStatus::SUCCESS;
92  }
93 
95  {
96  return { BT::InputPort<Point2D>("input", "no default value"),
97  BT::InputPort<Point2D>("pointA", Point2D{ 1, 2 }, "default value is [1,2]"),
98  BT::InputPort<Point2D>("pointB", "{point}",
99  "default value inside blackboard {point}"),
100  BT::InputPort<Point2D>("pointC", "5,6", "default value is [5,6]"),
101  BT::InputPort<Point2D>("pointD", "{=}",
102  "default value inside blackboard {pointD}"),
103  BT::InputPort<Point2D>("pointE", R"(json:{"x":9,"y":10})",
104  "default value is [9,10]") };
105  }
106 };
107 
108 int main()
109 {
110  std::string xml_txt = R"(
111  <root BTCPP_format="4" >
112  <BehaviorTree>
113  <NodeWithDefaultPoints input="-1,-2"/>
114  </BehaviorTree>
115  </root>)";
116 
118 
119  BehaviorTreeFactory factory;
120  factory.registerNodeType<NodeWithDefaultPoints>("NodeWithDefaultPoints");
121  auto tree = factory.createTreeFromText(xml_txt);
122 
123  tree.subtrees.front()->blackboard->set<Point2D>("point", Point2D{ 3, 4 });
124  tree.subtrees.front()->blackboard->set<Point2D>("pointD", Point2D{ 7, 8 });
125 
126  BT::NodeStatus status = tree.tickOnce();
127  std::cout << "Result: " << toStr(status) << std::endl;
128 
129  return 0;
130 }
BT
Definition: ex01_wrap_legacy.cpp:29
NodeWithDefaultPoints::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t12_default_ports.cpp:62
Point2D::operator==
bool operator==(const Point2D &p) const
Definition: t12_default_ports.cpp:15
BT::StringView
std::string_view StringView
Definition: basic_types.h:59
Point2D
Definition: t12_default_ports.cpp:11
Point2D::operator!=
bool operator!=(const Point2D &p) const
Definition: t12_default_ports.cpp:19
bt_factory.h
NodeWithDefaultPoints::providedPorts
static PortsList providedPorts()
Definition: t12_default_ports.cpp:94
NodeWithDefaultPoints::NodeWithDefaultPoints
NodeWithDefaultPoints(const std::string &name, const NodeConfig &config)
Definition: t12_default_ports.cpp:58
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT_JSON_CONVERTER
BT_JSON_CONVERTER(Point2D, point)
Definition: t12_default_ports.cpp:26
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
BT::RuntimeError
Definition: exceptions.h:58
json_export.h
BT::splitString
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
Definition: basic_types.cpp:348
BT::JsonExporter::get
static JsonExporter & get()
Definition: json_export.cpp:6
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::StartWith
bool StartWith(StringView str, StringView prefix)
Definition: basic_types.cpp:464
NodeWithDefaultPoints
Definition: t12_default_ports.cpp:55
BT::JsonExporter::addConverter
void addConverter()
Definition: json_export.h:130
Point2D::x
int x
Definition: t12_default_ports.cpp:13
main
int main()
Definition: t12_default_ports.cpp:108
BT::NodeConfig
Definition: tree_node.h:73
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
Point2D::y
int y
Definition: t12_default_ports.cpp:14
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
lexyd::p
constexpr auto p
Parses the production.
Definition: production.hpp:127
BT::toStr
std::string toStr(const T &value)
toStr is the reverse operation of convertFromString.
Definition: basic_types.h:252


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08