tree_node.cpp
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 
15 #include <cstring>
16 
17 namespace BT
18 {
19 static uint16_t getUID()
20 {
21  static uint16_t uid = 1;
22  return uid++;
23 }
24 
25 TreeNode::TreeNode(const std::string& name, const NodeParameters& parameters)
26  : not_initialized_(true),
27  name_(name),
28  status_(NodeStatus::IDLE),
29  uid_(getUID()),
30  parameters_(parameters)
31 
32 {
33 }
34 
36 {
38  const NodeStatus status = tick();
39  setStatus(status);
40  return status;
41 }
42 
44 {
45  NodeStatus prev_status;
46  {
47  std::unique_lock<std::mutex> UniqueLock(state_mutex_);
48  prev_status = status_;
49  status_ = new_status;
50  }
51  if (prev_status != new_status)
52  {
53  state_condition_variable_.notify_all();
54  state_change_signal_.notify(std::chrono::high_resolution_clock::now(), *this, prev_status,
55  new_status);
56  }
57 }
58 
60 {
61  bb_ = bb;
62 }
63 
65 {
66  if( not_initialized_ )
67  {
68  throw std::logic_error("You can NOT access the blackboard in the constructor."
69  " If you need to access the blackboard before the very first tick(), "
70  " you should override the virtual method TreeNode::onInit()");
71  }
72  return bb_;
73 }
74 
76 {
77  std::lock_guard<std::mutex> LockGuard(state_mutex_);
78  return status_;
79 }
80 
82 {
83  std::unique_lock<std::mutex> lk(state_mutex_);
84 
85  state_condition_variable_.wait(lk, [&]() {
88  });
89  return status_;
90 }
91 
92 const std::string& TreeNode::name() const
93 {
94  return name_;
95 }
96 
97 bool TreeNode::isHalted() const
98 {
99  return status() == NodeStatus::IDLE;
100 }
101 
104 {
105  return state_change_signal_.subscribe(std::move(callback));
106 }
107 
108 uint16_t TreeNode::UID() const
109 {
110  return uid_;
111 }
112 
113 void TreeNode::setRegistrationName(const std::string& registration_name)
114 {
115  registration_name_ = registration_name;
116 }
117 
119 {
120  if( not_initialized_ )
121  {
122  not_initialized_ = false;
123  onInit();
124  }
125 }
126 
128 {
129  return str.size() >= 4 && str[0] == '$' && str[1] == '{' && str.back() == '}';
130 }
131 
132 const std::string& TreeNode::registrationName() const
133 {
134  return registration_name_;
135 }
136 
138 {
139  return parameters_;
140 }
141 
142 } // end namespace
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
const std::string & name() const
Definition: tree_node.cpp:92
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
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
static uint16_t getUID()
Definition: tree_node.cpp:19
uint16_t UID() const
Definition: tree_node.cpp:108
virtual void onInit()
Definition: tree_node.h:47
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
void notify(CallableArgs...args)
Definition: signal.h:21
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
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
Subscriber subscribe(CallableFunction func)
Definition: signal.h:37
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
void setBlackboard(const Blackboard::Ptr &bb)
Definition: tree_node.cpp:59
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32