tree_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2019 Davide Faconti, Eurecat - 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(std::string name, NodeConfiguration config)
26  : name_(std::move(name)),
27  status_(NodeStatus::IDLE),
28  uid_(getUID()),
29  config_(std::move(config))
30 {
31 }
32 
34 {
35  const NodeStatus status = tick();
36  setStatus(status);
37  return status;
38 }
39 
41 {
42  NodeStatus prev_status;
43  {
44  std::unique_lock<std::mutex> UniqueLock(state_mutex_);
45  prev_status = status_;
46  status_ = new_status;
47  }
48  if (prev_status != new_status)
49  {
50  state_condition_variable_.notify_all();
51  state_change_signal_.notify(std::chrono::high_resolution_clock::now(), *this, prev_status,
52  new_status);
53  }
54 }
55 
57 {
58  std::lock_guard<std::mutex> lock(state_mutex_);
59  return status_;
60 }
61 
63 {
64  std::unique_lock<std::mutex> lock(state_mutex_);
65 
66  while( isHalted() )
67  {
68  state_condition_variable_.wait(lock);
69  }
70  return status_;
71 }
72 
73 const std::string& TreeNode::name() const
74 {
75  return name_;
76 }
77 
78 bool TreeNode::isHalted() const
79 {
80  return status_ == NodeStatus::IDLE;
81 }
82 
85 {
86  return state_change_signal_.subscribe(std::move(callback));
87 }
88 
89 uint16_t TreeNode::UID() const
90 {
91  return uid_;
92 }
93 
94 const std::string& TreeNode::registrationName() const
95 {
96  return registration_ID_;
97 }
98 
100 {
101  return config_;
102 }
103 
105 {
106  const auto size = str.size();
107  if( size >= 3 && str.back() == '}')
108  {
109  if( str[0] == '{') {
110  return true;
111  }
112  if( size >= 4 && str[0] == '$' && str[1] == '{') {
113  return true;
114  }
115  }
116  return false;
117 }
118 
120 {
121  const auto size = str.size();
122  if( size >= 3 && str.back() == '}')
123  {
124  if( str[0] == '{') {
125  return str.substr(1, size-2);
126  }
127  if( str[0] == '$' && str[1] == '{') {
128  return str.substr(2, size-3);
129  }
130  }
131  return {};
132 }
133 
135 {
136  if( remapping_value == "=" )
137  {
138  return {port_name};
139  }
140  if( isBlackboardPointer( remapping_value ) )
141  {
142  return {stripBlackboardPointer(remapping_value)};
143  }
144  return nonstd::make_unexpected("Not a blackboard pointer");
145 }
146 
148 {
149  for (const auto& new_it: new_remapping)
150  {
151  auto it = config_.input_ports.find( new_it.first );
152  if( it != config_.input_ports.end() )
153  {
154  it->second = new_it.second;
155  }
156  it = config_.output_ports.find( new_it.first );
157  if( it != config_.output_ports.end() )
158  {
159  it->second = new_it.second;
160  }
161  }
162 }
163 
164 } // end namespace
std::string registration_ID_
Definition: tree_node.h:180
std::condition_variable state_condition_variable_
Definition: tree_node.h:170
NodeConfiguration config_
Definition: tree_node.h:178
const uint16_t uid_
Definition: tree_node.h:176
bool isHalted() const
Definition: tree_node.cpp:78
StatusChangeSignal::Subscriber StatusChangeSubscriber
Definition: tree_node.h:95
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
const std::string name_
Definition: tree_node.h:166
StatusChangeSignal::CallableFunction StatusChangeCallback
Definition: tree_node.h:96
Definition: any.hpp:455
StatusChangeSubscriber subscribeToStatusChange(StatusChangeCallback callback)
subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber ...
Definition: tree_node.cpp:84
std::mutex state_mutex_
Definition: tree_node.h:172
static uint16_t getUID()
Definition: tree_node.cpp:19
uint16_t UID() const
Definition: tree_node.cpp:89
TreeNode(std::string name, NodeConfiguration config)
TreeNode main constructor.
Definition: tree_node.cpp:25
nonstd::string_view StringView
Definition: basic_types.h:50
NodeStatus status_
Definition: tree_node.h:168
void notify(CallableArgs...args)
Definition: signal.h:21
void modifyPortsRemapping(const PortsRemapping &new_remapping)
Definition: tree_node.cpp:147
const char * str
Definition: util.h:257
PortsRemapping output_ports
Definition: tree_node.h:49
StatusChangeSignal state_change_signal_
Definition: tree_node.h:174
static StringView stripBlackboardPointer(StringView str)
Definition: tree_node.cpp:119
nonstd::expected< T, std::string > Optional
Definition: basic_types.h:181
static bool isBlackboardPointer(StringView str)
Definition: tree_node.cpp:104
NodeStatus status() const
Definition: tree_node.cpp:56
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:33
Subscriber subscribe(CallableFunction func)
Definition: signal.h:37
BT::NodeStatus waitValidStatus()
Definition: tree_node.cpp:62
const std::string & registrationName() const
registrationName is the ID used by BehaviorTreeFactory to create an instance.
Definition: tree_node.cpp:94
virtual BT::NodeStatus tick()=0
Method to be implemented by the user.
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:40
PortsRemapping input_ports
Definition: tree_node.h:48
static Optional< StringView > getRemappedKey(StringView port_name, StringView remapping_value)
Definition: tree_node.cpp:134
std::unordered_map< std::string, std::string > PortsRemapping
Definition: tree_node.h:39


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05