Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef BEHAVIORTREECORE_TREENODE_H
00015 #define BEHAVIORTREECORE_TREENODE_H
00016
00017 #include <iostream>
00018 #include <string>
00019 #include <map>
00020 #include <set>
00021
00022 #include "behaviortree_cpp/optional.hpp"
00023 #include "behaviortree_cpp/tick_engine.h"
00024 #include "behaviortree_cpp/exceptions.h"
00025 #include "behaviortree_cpp/signal.h"
00026 #include "behaviortree_cpp/basic_types.h"
00027 #include "behaviortree_cpp/blackboard/blackboard.h"
00028
00029 namespace BT
00030 {
00031
00032
00033 typedef std::unordered_map<std::string, std::string> NodeParameters;
00034
00035 typedef std::chrono::high_resolution_clock::time_point TimePoint;
00036 typedef std::chrono::high_resolution_clock::duration Duration;
00037
00038
00039 class TreeNode
00040 {
00041
00042 private:
00043
00047 virtual void onInit() {}
00048
00049 public:
00060 TreeNode(const std::string& name, const NodeParameters& parameters);
00061 virtual ~TreeNode() = default;
00062
00063 typedef std::shared_ptr<TreeNode> Ptr;
00064
00066 virtual BT::NodeStatus executeTick();
00067
00069 virtual void halt() = 0;
00070
00071 bool isHalted() const;
00072
00073 NodeStatus status() const;
00074
00075 void setStatus(NodeStatus new_status);
00076
00077 void setBlackboard(const Blackboard::Ptr& bb);
00078
00079 const Blackboard::Ptr& blackboard() const;
00080
00081 const std::string& name() const;
00082
00085 BT::NodeStatus waitValidStatus();
00086
00087 virtual NodeType type() const = 0;
00088
00089 using StatusChangeSignal = Signal<TimePoint, const TreeNode&, NodeStatus, NodeStatus>;
00090 using StatusChangeSubscriber = StatusChangeSignal::Subscriber;
00091 using StatusChangeCallback = StatusChangeSignal::CallableFunction;
00092
00102 StatusChangeSubscriber subscribeToStatusChange(StatusChangeCallback callback);
00103
00104
00105 uint16_t UID() const;
00106
00108 const std::string& registrationName() const;
00109
00112 const NodeParameters& initializationParameters() const;
00113
00116 template <typename T>
00117 BT::optional<T> getParam(const std::string& key) const
00118 {
00119 T out;
00120 return getParam(key, out) ? std::move(out) : BT::nullopt;
00121 }
00122
00126 template <typename T>
00127 bool getParam(const std::string& key, T& destination) const;
00128
00129 static bool isBlackboardPattern(StringView str);
00130
00131 protected:
00133 virtual BT::NodeStatus tick() = 0;
00134
00136 void setRegistrationName(const std::string& registration_name);
00137
00138 friend class BehaviorTreeFactory;
00139
00140 void initializeOnce();
00141
00142 private:
00143
00144 bool not_initialized_;
00145
00146 const std::string name_;
00147
00148 NodeStatus status_;
00149
00150 std::condition_variable state_condition_variable_;
00151
00152 mutable std::mutex state_mutex_;
00153
00154 StatusChangeSignal state_change_signal_;
00155
00156 const uint16_t uid_;
00157
00158 std::string registration_name_;
00159
00160 const NodeParameters parameters_;
00161
00162 Blackboard::Ptr bb_;
00163
00164 };
00165
00166
00167
00168
00169 template <typename T> inline
00170 bool TreeNode::getParam(const std::string& key, T& destination) const
00171 {
00172 auto it = parameters_.find(key);
00173 if (it == parameters_.end())
00174 {
00175 return false;
00176 }
00177 const std::string& str = it->second;
00178
00179 try
00180 {
00181 bool bb_pattern = isBlackboardPattern(str);
00182 if( bb_pattern && not_initialized_)
00183 {
00184 std::cerr << "you are calling getParam inside a constructor, but this is not allowed "
00185 "when the parameter contains a blackboard.\n"
00186 "You should call getParam inside your tick() method"<< std::endl;
00187 std::logic_error("Calling getParam inside a constructor");
00188 }
00189
00190 if ( bb_pattern && blackboard() )
00191 {
00192 const std::string stripped_key(&str[2], str.size() - 3);
00193 const SafeAny::Any* val = blackboard()->getAny(stripped_key);
00194 if( val )
00195 {
00196 if( std::is_same<T,std::string>::value == false &&
00197 (val->type() == typeid (std::string) ||
00198 val->type() == typeid (SafeAny::SimpleString)))
00199 {
00200 destination = convertFromString<T>(val->cast<std::string>());
00201 }
00202 else{
00203 destination = val->cast<T>();
00204 }
00205 }
00206 return val != nullptr;
00207 }
00208 else{
00209 destination = convertFromString<T>(str.c_str());
00210 return true;
00211 }
00212 }
00213 catch (std::runtime_error& err)
00214 {
00215 std::cout << "Exception at getParam(" << key << "): " << err.what() << std::endl;
00216 return false;
00217 }
00218 }
00219
00220
00221 }
00222
00223 #endif