tree_node.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018 Davide Faconti - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * 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:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * 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,
11 * 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.
12 */
13 
14 #ifndef BEHAVIORTREECORE_TREENODE_H
15 #define BEHAVIORTREECORE_TREENODE_H
16 
17 #include <iostream>
18 #include <string>
19 #include <map>
20 #include <set>
21 
28 
29 namespace BT
30 {
31 // We call Parameters the set of Key/Values that can be read from file and are
32 // used to parametrize an object. It is up to the user's code to parse the string.
33 typedef std::unordered_map<std::string, std::string> NodeParameters;
34 
35 typedef std::chrono::high_resolution_clock::time_point TimePoint;
36 typedef std::chrono::high_resolution_clock::duration Duration;
37 
38 // Abstract base class for Behavior Tree Nodes
39 class TreeNode
40 {
41 
42  private:
43 
47  virtual void onInit() {}
48 
49  public:
60  TreeNode(const std::string& name, const NodeParameters& parameters);
61  virtual ~TreeNode() = default;
62 
63  typedef std::shared_ptr<TreeNode> Ptr;
64 
66  virtual BT::NodeStatus executeTick();
67 
69  virtual void halt() = 0;
70 
71  bool isHalted() const;
72 
73  NodeStatus status() const;
74 
75  void setStatus(NodeStatus new_status);
76 
77  void setBlackboard(const Blackboard::Ptr& bb);
78 
79  const Blackboard::Ptr& blackboard() const;
80 
81  const std::string& name() const;
82 
86 
87  virtual NodeType type() const = 0;
88 
92 
103 
104  // get an unique identifier of this instance of treeNode
105  uint16_t UID() const;
106 
108  const std::string& registrationName() const;
109 
112  const NodeParameters& initializationParameters() const;
113 
116  template <typename T>
117  BT::optional<T> getParam(const std::string& key) const
118  {
119  T out;
120  return getParam(key, out) ? std::move(out) : BT::nullopt;
121  }
122 
126  template <typename T>
127  bool getParam(const std::string& key, T& destination) const;
128 
129  static bool isBlackboardPattern(StringView str);
130 
131  protected:
133  virtual BT::NodeStatus tick() = 0;
134 
136  void setRegistrationName(const std::string& registration_name);
137 
138  friend class BehaviorTreeFactory;
139 
140  void initializeOnce();
141 
142  private:
143 
145 
146  const std::string name_;
147 
149 
150  std::condition_variable state_condition_variable_;
151 
153 
155 
156  const uint16_t uid_;
157 
158  std::string registration_name_;
159 
160  const NodeParameters parameters_;
161 
163 
164 };
165 
166 //-------------------------------------------------------
167 
168 
169 template <typename T> inline
170 bool TreeNode::getParam(const std::string& key, T& destination) const
171 {
172  auto it = parameters_.find(key);
173  if (it == parameters_.end())
174  {
175  return false;
176  }
177  const std::string& str = it->second;
178 
179  try
180  {
181  bool bb_pattern = isBlackboardPattern(str);
182  if( bb_pattern && not_initialized_)
183  {
184  std::cerr << "you are calling getParam inside a constructor, but this is not allowed "
185  "when the parameter contains a blackboard.\n"
186  "You should call getParam inside your tick() method"<< std::endl;
187  std::logic_error("Calling getParam inside a constructor");
188  }
189  // check if it follows this ${pattern}, if it does, search inside the blackboard
190  if ( bb_pattern && blackboard() )
191  {
192  const std::string stripped_key(&str[2], str.size() - 3);
193  const SafeAny::Any* val = blackboard()->getAny(stripped_key);
194  if( val )
195  {
196  if( std::is_same<T,std::string>::value == false &&
197  (val->type() == typeid (std::string) ||
198  val->type() == typeid (SafeAny::SimpleString)))
199  {
200  destination = convertFromString<T>(val->cast<std::string>());
201  }
202  else{
203  destination = val->cast<T>();
204  }
205  }
206  return val != nullptr;
207  }
208  else{
209  destination = convertFromString<T>(str.c_str());
210  return true;
211  }
212  }
213  catch (std::runtime_error& err)
214  {
215  std::cout << "Exception at getParam(" << key << "): " << err.what() << std::endl;
216  return false;
217  }
218 }
219 
220 
221 }
222 
223 #endif
BT::optional< T > getParam(const std::string &key) const
Definition: tree_node.h:117
std::condition_variable state_condition_variable_
Definition: tree_node.h:150
bool not_initialized_
Definition: tree_node.h:144
const uint16_t uid_
Definition: tree_node.h:156
bool isHalted() const
Definition: tree_node.cpp:97
StatusChangeSignal::Subscriber StatusChangeSubscriber
Definition: tree_node.h:90
virtual NodeType type() const =0
std::function< void(CallableArgs...)> CallableFunction
Definition: signal.h:18
const std::string & name() const
Definition: tree_node.cpp:92
virtual void halt()=0
The method used to interrupt the execution of a RUNNING node.
std::chrono::high_resolution_clock::time_point TimePoint
Definition: tree_node.h:35
const Blackboard::Ptr & blackboard() const
Definition: tree_node.cpp:64
void setRegistrationName(const std::string &registration_name)
registrationName() is set by the BehaviorTreeFactory
Definition: tree_node.cpp:113
const NodeParameters & initializationParameters() const
Definition: tree_node.cpp:137
const std::string name_
Definition: tree_node.h:146
StatusChangeSignal::CallableFunction StatusChangeCallback
Definition: tree_node.h:91
void initializeOnce()
Definition: tree_node.cpp:118
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
Blackboard::Ptr bb_
Definition: tree_node.h:162
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:40
StatusChangeSubscriber subscribeToStatusChange(StatusChangeCallback callback)
subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber ...
Definition: tree_node.cpp:103
TreeNode(const std::string &name, const NodeParameters &parameters)
TreeNode main constructor.
Definition: tree_node.cpp:25
std::mutex state_mutex_
Definition: tree_node.h:152
uint16_t UID() const
Definition: tree_node.cpp:108
virtual void onInit()
Definition: tree_node.h:47
const nullopt_t nullopt((nullopt_t::init()))
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
nonstd::string_view StringView
Definition: basic_types.h:58
NodeStatus status_
Definition: tree_node.h:148
virtual ~TreeNode()=default
StatusChangeSignal state_change_signal_
Definition: tree_node.h:154
const NodeParameters parameters_
Definition: tree_node.h:160
NodeStatus status() const
Definition: tree_node.cpp:75
NodeStatus
Definition: basic_types.h:28
std::shared_ptr< TreeNode > Ptr
Definition: tree_node.h:63
static bool isBlackboardPattern(StringView str)
Definition: tree_node.cpp:127
std::string registration_name_
Definition: tree_node.h:158
virtual BT::NodeStatus executeTick()
The method that will be executed to invoke tick(); and setStatus();.
Definition: tree_node.cpp:35
BT::NodeStatus waitValidStatus()
Definition: tree_node.cpp:81
const std::string & registrationName() const
registrationName is the ID used by BehaviorTreeFactory to create an instance.
Definition: tree_node.cpp:132
NodeType
Definition: basic_types.h:16
void setBlackboard(const Blackboard::Ptr &bb)
Definition: tree_node.cpp:59
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
std::chrono::high_resolution_clock::duration Duration
Definition: tree_node.h:36
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53