blackboard_precondition.h
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #ifndef DECORATOR_BLACKBOARD_PRECONDITION_NODE_H
14 #define DECORATOR_BLACKBOARD_PRECONDITION_NODE_H
15 
17 #include <type_traits>
18 
19 namespace BT
20 {
33 template <typename T>
35 {
36 public:
38  DecoratorNode(name, config)
39  {
40  if (std::is_same<T, int>::value)
41  setRegistrationID("BlackboardCheckInt");
42  else if (std::is_same<T, double>::value)
43  setRegistrationID("BlackboardCheckDouble");
44  else if (std::is_same<T, std::string>::value)
45  setRegistrationID("BlackboardCheckString");
46  else if (std::is_same<T, bool>::value)
47  setRegistrationID("BlackboardCheckBool");
48  }
49 
50  virtual ~BlackboardPreconditionNode() override = default;
51 
53  {
54  return {InputPort("value_A"), InputPort("value_B"),
55  InputPort<NodeStatus>("return_on_mismatch")};
56  }
57 
58 private:
59  virtual BT::NodeStatus tick() override;
60 };
61 
62 //----------------------------------------------------
63 
64 template <typename T>
65 inline bool IsSame(const T& a, const T& b)
66 {
67  return a == b;
68 }
69 
70 inline bool IsSame(const double& a, const double& b)
71 {
72  constexpr double EPS = static_cast<double>(std::numeric_limits<float>::epsilon());
73  return std::abs(a - b) <= EPS;
74 }
75 
76 template <typename T>
78 {
79  T value_A;
80  T value_B;
81  NodeStatus default_return_status = NodeStatus::FAILURE;
82 
84 
85  if (getInput("value_A", value_A) && getInput("value_B", value_B) &&
86  IsSame(value_A, value_B))
87  {
88  return child_node_->executeTick();
89  }
90 
91  if (child()->status() == NodeStatus::RUNNING)
92  {
93  haltChild();
94  }
95  getInput("return_on_mismatch", default_return_status);
96  return default_return_status;
97 }
98 
99 } // namespace BT
100 
101 #endif
virtual ~BlackboardPreconditionNode() override=default
std::pair< std::string, PortInfo > InputPort(StringView name, StringView description={})
Definition: basic_types.h:293
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
NodeStatus status() const
Definition: tree_node.cpp:84
bool IsSame(const T &a, const T &b)
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
TreeNode * child_node_
void haltChild()
Same as resetChild()
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
const TreeNode * child() const
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:201
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:63
BlackboardPreconditionNode(const std::string &name, const NodeConfiguration &config)


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14