From 93357ae92df4418552dcc1471969585187189e0c Mon Sep 17 00:00:00 2001 From: corot Date: Wed, 24 Jan 2024 18:53:42 +0900 Subject: [PATCH 1/2] Add unset key method to blackboard and matching wrapper to TreeNode --- include/behaviortree_cpp_v3/blackboard.h | 20 +++++++++++++++- include/behaviortree_cpp_v3/tree_node.h | 29 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/include/behaviortree_cpp_v3/blackboard.h b/include/behaviortree_cpp_v3/blackboard.h index 12cd71502..ec8c7cff3 100644 --- a/include/behaviortree_cpp_v3/blackboard.h +++ b/include/behaviortree_cpp_v3/blackboard.h @@ -92,6 +92,9 @@ class Blackboard template void set(const std::string& key, const T& value) { + if (key.empty()) + throw LogicError("Blackboard::set with empty key is not allowed"); + std::unique_lock lock_entry(entry_mutex_); std::unique_lock lock(mutex_); @@ -142,7 +145,7 @@ class Blackboard { debugMessage(); - throw LogicError("Blackboard::set() failed: once declared, the type of a port " + throw LogicError("Blackboard::set('", key, "') failed: once declared, the type of a port " "shall not change. Declared type [", BT::demangle(previous_type), "] != current type [", BT::demangle(typeid(T)), "]"); @@ -151,6 +154,21 @@ class Blackboard previous_any = std::move(new_value); } + void unset(const std::string& key) + { + std::unique_lock lock(mutex_); + + // check local storage + auto it = storage_.find(key); + if (it == storage_.end()) + { + // No entry, nothing to do. + return; + } + + storage_.erase(it); + } + const PortInfo* portInfo(const std::string& key); void addSubtreeRemapping(StringView internal, StringView external); diff --git a/include/behaviortree_cpp_v3/tree_node.h b/include/behaviortree_cpp_v3/tree_node.h index d6d203125..8375b881e 100644 --- a/include/behaviortree_cpp_v3/tree_node.h +++ b/include/behaviortree_cpp_v3/tree_node.h @@ -166,6 +166,8 @@ class TreeNode template Result setOutput(const std::string& key, const T& value); + Result unsetOutput(const std::string& key); + // function provide mostrly for debugging purpose to see the raw value // in the port (no remapping and no conversion to a type) StringView getRawPortValue(const std::string& key) const; @@ -321,6 +323,33 @@ inline Result TreeNode::setOutput(const std::string& key, const T& value) return {}; } +inline Result TreeNode::unsetOutput(const std::string& key) +{ + if (!config_.blackboard) + { + return nonstd::make_unexpected("unsetOutput() failed: trying to access a " + "Blackboard(BB) entry, but BB is invalid"); + } + + auto remap_it = config_.output_ports.find(key); + if (remap_it == config_.output_ports.end()) + { + return {}; + } + StringView remapped_key = remap_it->second; + if (remapped_key == "=") + { + remapped_key = key; + } + if (isBlackboardPointer(remapped_key)) + { + remapped_key = stripBlackboardPointer(remapped_key); + } + config_.blackboard->unset(static_cast(remapped_key)); + + return {}; +} + // Utility function to fill the list of ports using T::providedPorts(); template inline void assignDefaultRemapping(NodeConfiguration& config) From 25a0f7762e70b8fc63c5d8e33fb7a3b573a8c2b1 Mon Sep 17 00:00:00 2001 From: corot Date: Thu, 25 Jan 2024 13:19:22 +0900 Subject: [PATCH 2/2] Failed experiments --- include/behaviortree_cpp_v3/blackboard.h | 6 ++++-- include/behaviortree_cpp_v3/tree_node.h | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/behaviortree_cpp_v3/blackboard.h b/include/behaviortree_cpp_v3/blackboard.h index ec8c7cff3..adede1b38 100644 --- a/include/behaviortree_cpp_v3/blackboard.h +++ b/include/behaviortree_cpp_v3/blackboard.h @@ -165,8 +165,10 @@ class Blackboard // No entry, nothing to do. return; } - - storage_.erase(it); +//it->second->value = Any(); +it->second.reset(); +//std::cout << "\n" << it->second->value.empty() << "\n"; + //storage_.erase(it); } const PortInfo* portInfo(const std::string& key); diff --git a/include/behaviortree_cpp_v3/tree_node.h b/include/behaviortree_cpp_v3/tree_node.h index 8375b881e..d33fd6786 100644 --- a/include/behaviortree_cpp_v3/tree_node.h +++ b/include/behaviortree_cpp_v3/tree_node.h @@ -334,7 +334,11 @@ inline Result TreeNode::unsetOutput(const std::string& key) auto remap_it = config_.output_ports.find(key); if (remap_it == config_.output_ports.end()) { - return {}; + return nonstd::make_unexpected(StrCat("unsetOutput() failed: " + "NodeConfiguration::output_ports " + "does not " + "contain the key: [", + key, "]")); } StringView remapped_key = remap_it->second; if (remapped_key == "=") @@ -345,7 +349,9 @@ inline Result TreeNode::unsetOutput(const std::string& key) { remapped_key = stripBlackboardPointer(remapped_key); } - config_.blackboard->unset(static_cast(remapped_key)); + //config_.blackboard->unset(static_cast(remapped_key)); + std::cerr << "\n\n" << key << "\t" << static_cast(remapped_key) << "\n\n"; +config_.blackboard->unset(key); return {}; }