set_blackboard_node.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 ACTION_SETBLACKBOARD_NODE_H
14 #define ACTION_SETBLACKBOARD_NODE_H
15 
17 
18 namespace BT
19 {
37 {
38 public:
39  SetBlackboard(const std::string& name, const NodeConfiguration& config) :
41  {
42  setRegistrationID("SetBlackboard");
43  }
44 
46  {
47  return {InputPort("value", "Value to be written int othe output_key"),
48  BidirectionalPort("output_key", "Name of the blackboard entry where the "
49  "value should be written")};
50  }
51 
52 private:
53  virtual BT::NodeStatus tick() override
54  {
55  std::string output_key;
56  if (!getInput("output_key", output_key))
57  {
58  throw RuntimeError("missing port [output_key]");
59  }
60 
61  const std::string value_str = config().input_ports.at("value");
62 
63  if(isBlackboardPointer(value_str))
64  {
65  StringView stripped_key = stripBlackboardPointer(value_str);
66  const auto input_key = std::string(stripped_key);
67  std::shared_ptr<Blackboard::Entry> src_entry = config().blackboard->getEntry(input_key);
68  std::shared_ptr<Blackboard::Entry> dst_entry = config().blackboard->getEntry(output_key);
69 
70  if(!src_entry)
71  {
72  throw RuntimeError("Can't find the port referred by [value]");
73  }
74  if(!dst_entry)
75  {
76  config().blackboard->createEntry(output_key, src_entry->port_info);
77  dst_entry = config().blackboard->getEntry(output_key);
78  }
79  dst_entry->value = src_entry->value;
80  }
81  else
82  {
83  config().blackboard->set(output_key, value_str);
84  }
85 
86  return NodeStatus::SUCCESS;
87  }
88 };
89 } // namespace BT
90 
91 #endif
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
BT
Definition: ex01_wrap_legacy.cpp:29
BT::InputPort
std::pair< std::string, PortInfo > InputPort(StringView name, StringView description={})
Definition: basic_types.h:302
BT::SetBlackboard::providedPorts
static PortsList providedPorts()
Definition: set_blackboard_node.h:45
BT::BidirectionalPort
std::pair< std::string, PortInfo > BidirectionalPort(StringView name, StringView description={})
Definition: basic_types.h:316
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:342
BT::SetBlackboard::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: set_blackboard_node.h:53
BT::NodeConfiguration::input_ports
PortsRemapping input_ports
Definition: tree_node.h:50
BT::TreeNode::config
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
BT::TreeNode::isBlackboardPointer
static bool isBlackboardPointer(StringView str)
Definition: tree_node.cpp:145
BT::NodeConfiguration
Definition: tree_node.h:44
action_node.h
BT::RuntimeError
Definition: exceptions.h:58
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:201
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeConfiguration::blackboard
Blackboard::Ptr blackboard
Definition: tree_node.h:49
BT::StringView
nonstd::string_view StringView
Definition: basic_types.h:55
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35
BT::SetBlackboard
The SetBlackboard is action used to store a string into an entry of the Blackboard specified in "outp...
Definition: set_blackboard_node.h:36
BT::TreeNode::stripBlackboardPointer
static StringView stripBlackboardPointer(StringView str)
Definition: tree_node.cpp:162
BT::SetBlackboard::SetBlackboard
SetBlackboard(const std::string &name, const NodeConfiguration &config)
Definition: set_blackboard_node.h:39


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19