tree_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2020 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 
104 StringView TreeNode::getRawPortValue(const std::string& key) const
105 {
106  auto remap_it = config_.input_ports.find(key);
107  if (remap_it == config_.input_ports.end())
108  {
109  throw std::logic_error(StrCat("getInput() failed because "
110  "NodeConfiguration::input_ports "
111  "does not contain the key: [",
112  key, "]"));
113  }
114  return remap_it->second;
115 }
116 
118 {
119  const auto size = str.size();
120  if( size >= 3 && str.back() == '}')
121  {
122  if( str[0] == '{') {
123  return true;
124  }
125  if( size >= 4 && str[0] == '$' && str[1] == '{') {
126  return true;
127  }
128  }
129  return false;
130 }
131 
133 {
134  const auto size = str.size();
135  if( size >= 3 && str.back() == '}')
136  {
137  if( str[0] == '{') {
138  return str.substr(1, size-2);
139  }
140  if( str[0] == '$' && str[1] == '{') {
141  return str.substr(2, size-3);
142  }
143  }
144  return {};
145 }
146 
148 {
149  if( remapping_value == "=" )
150  {
151  return {port_name};
152  }
153  if( isBlackboardPointer( remapping_value ) )
154  {
155  return {stripBlackboardPointer(remapping_value)};
156  }
157  return nonstd::make_unexpected("Not a blackboard pointer");
158 }
159 
161 {
162  for (const auto& new_it: new_remapping)
163  {
164  auto it = config_.input_ports.find( new_it.first );
165  if( it != config_.input_ports.end() )
166  {
167  it->second = new_it.second;
168  }
169  it = config_.output_ports.find( new_it.first );
170  if( it != config_.output_ports.end() )
171  {
172  it->second = new_it.second;
173  }
174  }
175 }
176 
177 } // end namespace
std::string registration_ID_
Definition: tree_node.h:187
std::condition_variable state_condition_variable_
Definition: tree_node.h:177
NodeConfiguration config_
Definition: tree_node.h:185
const uint16_t uid_
Definition: tree_node.h:183
bool isHalted() const
Definition: tree_node.cpp:78
StatusChangeSignal::Subscriber StatusChangeSubscriber
Definition: tree_node.h:93
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:173
StringView getRawPortValue(const std::string &key) const
Definition: tree_node.cpp:104
StatusChangeSignal::CallableFunction StatusChangeCallback
Definition: tree_node.h:94
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:179
static uint16_t getUID()
Definition: tree_node.cpp:19
uint16_t UID() const
Definition: tree_node.cpp:89
std::string StrCat()
Definition: strcat.hpp:47
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:175
void notify(CallableArgs...args)
Definition: signal.h:21
void modifyPortsRemapping(const PortsRemapping &new_remapping)
Definition: tree_node.cpp:160
PortsRemapping output_ports
Definition: tree_node.h:49
StatusChangeSignal state_change_signal_
Definition: tree_node.h:181
static StringView stripBlackboardPointer(StringView str)
Definition: tree_node.cpp:132
nonstd::expected< T, std::string > Optional
Definition: basic_types.h:190
static bool isBlackboardPointer(StringView str)
Definition: tree_node.cpp:117
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:147
std::unordered_map< std::string, std::string > PortsRemapping
Definition: tree_node.h:39


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25