Skip to content

Commit 81ef54e

Browse files
committed
add Tree::getNodesByPath
1 parent 03980cc commit 81ef54e

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

examples/t12_groot_howto.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main()
7373
BT::FileLogger2 logger2(tree, "t12_logger2.btlog");
7474
// SQLite logger can save multiple sessions into the same database
7575
bool append_to_database = true;
76-
BT::SqliteLogger sqlite_logger(tree, "t12_logger2.btsql", append_to_database);
76+
BT::SqliteLogger sqlite_logger(tree, "t12_sqlitelog.btsql", append_to_database);
7777

7878
while(1)
7979
{

include/behaviortree_cpp/bt_factory.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ inline TreeNodeManifest CreateManifest(const std::string& ID,
7979

8080
constexpr const char* PLUGIN_SYMBOL = "BT_RegisterNodesFromPlugin";
8181

82+
bool WildcardMatch(const std::string &str, StringView filter);
83+
8284
/**
8385
* @brief Struct used to store a tree.
8486
* If this object goes out of scope, the tree is destroyed.
@@ -175,6 +177,26 @@ class Tree
175177

176178
uint16_t getUID();
177179

180+
/// Get a list of nodes which fullPath() match a wildcard filter and
181+
/// a given path. Example:
182+
///
183+
/// move_nodes = tree.getNodesByPath<MoveBaseNode>("move_*");
184+
///
185+
template <typename NodeType = BT::TreeNode>
186+
std::vector<const TreeNode*> getNodesByPath(StringView wildcard_filter) {
187+
std::vector<const TreeNode*> nodes;
188+
for (auto const& subtree : subtrees) {
189+
for (auto const& node : subtree->nodes) {
190+
if(auto node_recast = dynamic_cast<const NodeType*>(node.get())) {
191+
if(WildcardMatch(node->fullPath(), wildcard_filter)) {
192+
nodes.push_back(node.get());
193+
}
194+
}
195+
}
196+
}
197+
return nodes;
198+
}
199+
178200

179201
private:
180202
std::shared_ptr<WakeUpSignal> wake_up_;

src/bt_factory.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424

2525
namespace BT
2626
{
27+
28+
bool WildcardMatch(std::string const& str, StringView filter)
29+
{
30+
return wildcards::match(str, filter);
31+
}
32+
2733
BehaviorTreeFactory::BehaviorTreeFactory()
2834
{
2935
parser_ = std::make_shared<XMLParser>(*this);

0 commit comments

Comments
 (0)