tree_node.h
Go to the documentation of this file.
00001 /* Copyright (C) 2015-2018 Michele Colledanchise -  All Rights Reserved
00002  * Copyright (C) 2018 Davide Faconti -  All Rights Reserved
00003 *
00004 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00005 *   to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00006 *   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:
00007 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00008 *
00009 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00010 *   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,
00011 *   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.
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 // We call Parameters the set of Key/Values that can be read from file and are
00032 // used to parametrize an object. It is up to the user's code to parse the string.
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 // Abstract base class for Behavior Tree Nodes
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     // get an unique identifier of this instance of treeNode
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         // check if it follows this ${pattern}, if it does, search inside the blackboard
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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10