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  SetBlackboardNode(const std::string& name, const NodeConfig& 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  StringView stripped_key;
64  BT::Any out_value;
65 
66  std::shared_ptr<Blackboard::Entry> dst_entry =
67  config().blackboard->getEntry(output_key);
68 
69  if(isBlackboardPointer(value_str, &stripped_key))
70  {
71  const auto input_key = std::string(stripped_key);
72  std::shared_ptr<Blackboard::Entry> src_entry =
73  config().blackboard->getEntry(input_key);
74 
75  if(!src_entry)
76  {
77  throw RuntimeError("Can't find the port referred by [value]");
78  }
79  if(!dst_entry)
80  {
81  config().blackboard->createEntry(output_key, src_entry->info);
82  dst_entry = config().blackboard->getEntry(output_key);
83  }
84 
85  out_value = src_entry->value;
86  }
87  else
88  {
89  out_value = BT::Any(value_str);
90  }
91 
92  if(out_value.empty())
93  return NodeStatus::FAILURE;
94 
95  // avoid type issues when port is remapped: current implementation of the set might be a little bit problematic for initialized on the fly values
96  // this still does not attack math issues
97  if(dst_entry && dst_entry->info.type() != typeid(std::string) && out_value.isString())
98  {
99  try
100  {
101  out_value = dst_entry->info.parseString(out_value.cast<std::string>());
102  }
103  catch(const std::exception& e)
104  {
105  throw LogicError("Can't convert string [", out_value.cast<std::string>(),
106  "] to type [", BT::demangle(dst_entry->info.type()),
107  "]: ", e.what());
108  }
109  }
110 
111  config().blackboard->set(output_key, out_value);
112 
113  return NodeStatus::SUCCESS;
114  }
115 };
116 } // namespace BT
117 
118 #endif
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:560
BT
Definition: ex01_wrap_legacy.cpp:29
BT::demangle
std::string demangle(char const *name)
Definition: demangle_util.h:74
BT::Any
Definition: safe_any.hpp:36
BT::Any::isString
bool isString() const
Definition: safe_any.hpp:124
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::StringView
std::string_view StringView
Definition: basic_types.h:59
BT::SetBlackboardNode::SetBlackboardNode
SetBlackboardNode(const std::string &name, const NodeConfig &config)
Definition: set_blackboard_node.h:39
BT::InputPort
std::pair< std::string, PortInfo > InputPort(StringView name, StringView description={})
Definition: basic_types.h:472
BT::NodeConfig::input_ports
PortsRemapping input_ports
Definition: tree_node.h:92
BT::SetBlackboardNode
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::BidirectionalPort
std::pair< std::string, PortInfo > BidirectionalPort(StringView name, StringView description={})
Definition: basic_types.h:496
BT::LogicError
Definition: exceptions.h:45
BT::NodeConfig::blackboard
Blackboard::Ptr blackboard
Definition: tree_node.h:88
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:587
BT::NodeStatus::FAILURE
@ FAILURE
BT::SetBlackboardNode::providedPorts
static PortsList providedPorts()
Definition: set_blackboard_node.h:45
BT::SetBlackboardNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: set_blackboard_node.h:53
BT::Any::empty
bool empty() const noexcept
Definition: safe_any.hpp:207
action_node.h
BT::RuntimeError
Definition: exceptions.h:58
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:439
BT::Any::cast
T cast() const
Definition: safe_any.hpp:146
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeConfig
Definition: tree_node.h:82
BT::TreeNode::isBlackboardPointer
static bool isBlackboardPointer(StringView str, StringView *stripped_pointer=nullptr)
Check a string and return true if it matches the pattern: {...}.
Definition: tree_node.cpp:375
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:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Wed Apr 16 2025 02:20:57