27 parser_ = std::make_shared<XMLParser>(*this);
28 registerNodeType<FallbackNode>(
"Fallback");
29 registerNodeType<SequenceNode>(
"Sequence");
30 registerNodeType<SequenceStarNode>(
"SequenceStar");
31 registerNodeType<ParallelNode>(
"Parallel");
32 registerNodeType<ReactiveSequence>(
"ReactiveSequence");
33 registerNodeType<ReactiveFallback>(
"ReactiveFallback");
34 registerNodeType<IfThenElseNode>(
"IfThenElse");
35 registerNodeType<WhileDoElseNode>(
"WhileDoElse");
37 registerNodeType<InverterNode>(
"Inverter");
39 registerNodeType<RetryNode>(
"RetryUntilSuccessful");
40 registerNodeType<KeepRunningUntilFailureNode>(
"KeepRunningUntilFailure");
41 registerNodeType<RepeatNode>(
"Repeat");
42 registerNodeType<TimeoutNode<>>(
"Timeout");
43 registerNodeType<DelayNode>(
"Delay");
45 registerNodeType<ForceSuccessNode>(
"ForceSuccess");
46 registerNodeType<ForceFailureNode>(
"ForceFailure");
48 registerNodeType<AlwaysSuccessNode>(
"AlwaysSuccess");
49 registerNodeType<AlwaysFailureNode>(
"AlwaysFailure");
50 registerNodeType<SetBlackboard>(
"SetBlackboard");
52 registerNodeType<SubtreeNode>(
"SubTree");
53 registerNodeType<SubtreePlusNode>(
"SubTreePlus");
55 registerNodeType<BlackboardPreconditionNode<int>>(
"BlackboardCheckInt");
56 registerNodeType<BlackboardPreconditionNode<double>>(
"BlackboardCheckDouble");
57 registerNodeType<BlackboardPreconditionNode<std::string>>(
"BlackboardCheckString");
58 registerNodeType<BlackboardPreconditionNode<bool>>(
"BlackboardCheckBool");
60 registerNodeType<SwitchNode<2>>(
"Switch2");
61 registerNodeType<SwitchNode<3>>(
"Switch3");
62 registerNodeType<SwitchNode<4>>(
"Switch4");
63 registerNodeType<SwitchNode<5>>(
"Switch5");
64 registerNodeType<SwitchNode<6>>(
"Switch6");
67 registerNodeType<ManualSelectorNode>(
"ManualSelector");
79 throw LogicError(
"You can not remove the builtin registration ID [", ID,
"]");
108 NodeBuilder builder = [tick_functor, ID](
const std::string& name,
110 return std::make_unique<SimpleConditionNode>(name, tick_functor, config);
121 NodeBuilder builder = [tick_functor, ID](
const std::string& name,
123 return std::make_unique<SimpleActionNode>(name, tick_functor, config);
134 NodeBuilder builder = [tick_functor, ID](
const std::string& name,
136 return std::make_unique<SimpleDecoratorNode>(name, tick_functor, config);
146 loader.
load(file_path);
156 std::cout <<
"ERROR loading library [" << file_path <<
"]: can't find symbol [" 164 const char os_pathsep(
';');
166 const char os_pathsep(
':');
172 std::vector<std::string> getCatkinLibraryPaths()
174 std::vector<std::string> lib_paths;
175 const char* env = std::getenv(
"CMAKE_PREFIX_PATH");
178 const std::string env_catkin_prefix_paths(env);
179 std::vector<BT::StringView> catkin_prefix_paths =
183 filesystem::path
path(static_cast<std::string>(catkin_prefix_path));
184 filesystem::path lib(
"lib");
185 lib_paths.push_back((path / lib).str());
193 std::vector<std::string> plugins;
195 std::vector<std::string> catkin_lib_paths = getCatkinLibraryPaths();
197 for (
const auto& plugin : plugins)
200 for (
const auto& lib_path : catkin_lib_paths)
202 const auto full_path = filesystem::path(lib_path) / filename;
203 if (full_path.exists())
205 std::cout <<
"Registering ROS plugins from " << full_path.str() << std::endl;
216 throw RuntimeError(
"Using attribute [ros_pkg] in <include>, but this library was " 218 "without ROS support. Recompile the BehaviorTree.CPP using " 225 parser_->loadFromFile(filename);
230 parser_->loadFromText(xml_text,
true );
235 return parser_->registeredBehaviorTrees();
244 const std::string& name,
const std::string& ID,
const NodeConfiguration& config)
const 249 std::cerr << ID <<
" not included in this list:" << std::endl;
252 std::cerr << builder_it.first << std::endl;
254 throw RuntimeError(
"BehaviorTreeFactory: ID [", ID,
"] not registered");
257 std::unique_ptr<TreeNode> node = it->second(name, config);
258 node->setRegistrationID(ID);
267 const std::unordered_map<std::string, TreeNodeManifest>&
281 if(!
parser_->registeredBehaviorTrees().empty()) {
282 std::cout <<
"WARNING: You executed BehaviorTreeFactory::createTreeFromText " 283 "after registerBehaviorTreeFrom[File/Text].\n" 284 "This is NOTm probably, what you want to do.\n" 285 "You should probably use BehaviorTreeFactory::createTree, instead" 298 if(!
parser_->registeredBehaviorTrees().empty()) {
299 std::cout <<
"WARNING: You executed BehaviorTreeFactory::createTreeFromFile " 300 "after registerBehaviorTreeFrom[File/Text].\n" 301 "This is NOTm probably, what you want to do.\n" 302 "You should probably use BehaviorTreeFactory::createTree, instead" 316 auto tree =
parser_->instantiateTree(blackboard, tree_name);
322 const std::string& description)
327 throw std::runtime_error(
"addDescriptionToManifest: wrong ID");
329 it->second.description = description;
334 wake_up_->waitFor(timeout);
344 if (blackboard_stack.size() > 0)
346 return blackboard_stack.front();
constexpr const char * PLUGIN_SYMBOL
void clearRegisteredBehaviorTrees()
Clear previously-registered behavior trees.
void sleep(std::chrono::system_clock::duration timeout)
std::function< NodeStatus(NodeStatus, TreeNode &)> TickFunctor
std::unique_ptr< TreeNode > instantiateTreeNode(const std::string &name, const std::string &ID, const NodeConfiguration &config) const
instantiateTreeNode creates an instance of a previously registered TreeNode.
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeConfiguration &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
const std::set< std::string > & builtinNodes() const
List of builtin IDs.
std::unordered_map< std::string, NodeBuilder > builders_
void loadFromFile(const std::string &filename, bool add_includes=true) override
std::set< std::string > builtin_IDs_
std::shared_ptr< Blackboard > Ptr
This information is used mostly by the XMLParser.
std::unordered_map< std::string, TreeNodeManifest > manifests_
Blackboard::Ptr rootBlackboard()
void addDescriptionToManifest(const std::string &node_id, const std::string &description)
bool hasSymbol(const std::string &name)
static const char * xml_text
Tree instantiateTree(const Blackboard::Ptr &root_blackboard, std::string main_tree_to_execute={}) override
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
void registerBehaviorTreeFromText(const std::string &xml_text)
static std::string suffix()
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
std::vector< std::string > registeredBehaviorTrees() const
void * getSymbol(const std::string &name)
nonstd::string_view StringView
std::string registration_ID
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
void registerFromPlugin(const std::string &file_path)
registerFromPlugin load a shared library and execute the function BT_REGISTER_NODES (see macro)...
void loadFromText(const std::string &xml_text, bool add_includes=true) override
const std::unordered_map< std::string, NodeBuilder > & builders() const
All the builders. Made available mostly for debug purposes.
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Simple class for manipulating paths on Linux/Windows/Mac OS.
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleAction help you register nodes of type SimpleActionNode.
ROSLIB_DECL void getPlugins(const std::string &package, const std::string &attribute, V_string &plugins, bool force_recrawl=false)
std::function< NodeStatus(TreeNode &)> TickFunctor
void load(const std::string &path, int flags=0)
std::unordered_map< std::string, PortInfo > PortsList
static volatile int count
The XMLParser is a class used to read the model of a BehaviorTree from file or text and instantiate t...
const std::unordered_map< std::string, TreeNodeManifest > & manifests() const
Manifests of all the registered TreeNodes.
std::unordered_map< std::string, TreeNodeManifest > manifests
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
void registerFromROSPlugins()
registerFromROSPlugins finds all shared libraries that export ROS plugins for behaviortree_cpp, and calls registerFromPlugin for each library.
bool unregisterBuilder(const std::string &ID)
Remove a registered ID.
void registerBehaviorTreeFromFile(const std::string &filename)
registerBehaviorTreeFromFile. Load the definition of an entire behavior tree, but don't instantiate i...
Tree createTreeFromFile(const std::string &file_path, Blackboard::Ptr blackboard=Blackboard::create())
void registerSimpleDecorator(const std::string &ID, const SimpleDecoratorNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleDecorator help you register nodes of type SimpleDecoratorNode.
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleCondition help you register nodes of type SimpleConditionNode.
std::shared_ptr< BT::Parser > parser_
std::function< NodeStatus(TreeNode &)> TickFunctor