Skip to content

Commit 4794fb4

Browse files
committed
more readable API
1 parent 0f780ec commit 4794fb4

File tree

6 files changed

+32
-33
lines changed

6 files changed

+32
-33
lines changed

examples/crossdoor_example.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ int main()
5858

5959
// Important: when the object tree goes out of scope, all the TreeNodes are destroyed
6060
auto tree = buildTreeFromText(factory, xml_text, blackboard);
61-
TreeNode* root_node = tree.first.get();
6261

63-
StdCoutLogger logger_cout(root_node);
64-
MinitraceLogger logger_minitrace(root_node, "bt_trace.json");
65-
FileLogger logger_file(root_node, "bt_trace.fbl", 32);
62+
StdCoutLogger logger_cout(tree.root_node);
63+
MinitraceLogger logger_minitrace(tree.root_node, "bt_trace.json");
64+
FileLogger logger_file(tree.root_node, "bt_trace.fbl", 32);
6665
#ifdef ZMQ_FOUND
67-
PublisherZMQ publisher_zmq(root_node);
66+
PublisherZMQ publisher_zmq(tree.root_node);
6867
#endif
6968

70-
std::cout << writeXML( factory, root_node, false ) << std::endl;
69+
std::cout << writeXML( factory, tree.root_node, false ) << std::endl;
7170
std::cout << "---------------" << std::endl;
7271

7372
// Keep on ticking until you get either a SUCCESS or FAILURE state
74-
while( root_node->executeTick() == BT::NodeStatus::RUNNING)
73+
NodeStatus status = NodeStatus::RUNNING;
74+
while( status == NodeStatus::RUNNING )
7575
{
76-
// continue;
76+
status = tree.root_node->executeTick();
7777
}
7878
return 0;
7979
}

examples/tutorial_blackboard.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,12 @@ int main()
9696
auto blackboard = Blackboard::create<BlackboardLocal>();
9797

9898
// Important: when the object tree goes out of scope, all the TreeNodes are destroyed
99-
auto res = buildTreeFromText(factory, xml_text, blackboard);
100-
const TreeNode::Ptr& root_node = res.first;
99+
auto tree = buildTreeFromText(factory, xml_text, blackboard);
101100

102101
NodeStatus status = NodeStatus::RUNNING;
103102
while( status == NodeStatus::RUNNING )
104103
{
105-
status = root_node->executeTick();
104+
status = tree.root_node->executeTick();
106105
}
107106

108107
return 0;

examples/tutorial_factory_tree.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ int main()
3535

3636
// IMPORTANT: when the object tree goes out of scope, all the TreeNodes are destroyed
3737
auto tree = buildTreeFromText(factory, xml_text);
38-
const TreeNode::Ptr& root_node = tree.first;
3938

4039
// The tick is propagated to all the children.
4140
// until one of the returns FAILURE or RUNNING.
4241
// In this case all of the return SUCCESS
43-
root_node->executeTick();
42+
tree.root_node->executeTick();
4443

4544
return 0;
4645
}

examples/tutorial_sequence_star.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,32 @@ int main()
6666
std::cout << "\n------------ BUILDING A NEW TREE ------------\n\n" << std::endl;
6767

6868
auto tree = buildTreeFromText(factory, xml_text);
69-
TreeNode::Ptr root_node = tree.first;
7069

7170
// This logger will show all the state transitions on console
72-
StdCoutLogger logger_cout(root_node.get());
71+
StdCoutLogger logger_cout(tree.root_node);
7372

7473
// This other logger will save the state transition in a custom file format
7574
// simple_trace.fbl can be visualized using the command line tool [bt_log_cat]
76-
FileLogger file_file(root_node.get(), "simple_trace.fbl", 32);
75+
FileLogger file_file(tree.root_node, "simple_trace.fbl", 32);
7776

7877
NodeStatus status;
7978

8079
std::cout << "\n------- First executeTick() --------" << std::endl;
81-
status = root_node->executeTick();
80+
status = tree.root_node->executeTick();
8281
Assert( status == NodeStatus::RUNNING);
8382

8483
std::cout << "\n------- sleep --------" << std::endl;
8584
std::this_thread::sleep_for(std::chrono::milliseconds(100));
8685

8786
std::cout << "\n------- Second executeTick() --------" << std::endl;
88-
status = root_node->executeTick();
87+
status = tree.root_node->executeTick();
8988
Assert( status == NodeStatus::RUNNING);
9089

9190
std::cout << "\n------- sleep --------" << std::endl;
9291
std::this_thread::sleep_for(std::chrono::milliseconds(100));
9392

9493
std::cout << "\n------- Third executeTick() --------" << std::endl;
95-
status = root_node->executeTick();
94+
status = tree.root_node->executeTick();
9695
Assert( status == NodeStatus::SUCCESS);
9796

9897
std::cout << std::endl;

include/behavior_tree_core/xml_parsing.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,22 @@ class XMLParser
3535
const BehaviorTreeFactory& factory_;
3636
};
3737

38+
struct Tree
39+
{
40+
TreeNode* root_node;
41+
std::vector<TreeNode::Ptr> nodes;
42+
};
43+
3844
/** Helper function to do the most common steps all at once:
3945
* 1) Create an instance of XMLParse and call loadFromText.
4046
* 2) Instantiate the entire tree.
4147
* 3) Assign the given Blackboard
4248
*
4349
* return: a pair containing the root node (first) and a vector with all the instantiated nodes (second).
4450
*/
45-
std::pair<TreeNode::Ptr, std::vector<TreeNode::Ptr>>
46-
buildTreeFromText(const BehaviorTreeFactory& factory,
47-
const std::string& text,
48-
const Blackboard::Ptr& blackboard = Blackboard::Ptr() );
51+
Tree buildTreeFromText(const BehaviorTreeFactory& factory,
52+
const std::string& text,
53+
const Blackboard::Ptr& blackboard = Blackboard::Ptr() );
4954

5055
/** Helper function to do the most common steps all at once:
5156
* 1) Create an instance of XMLParse and call loadFromFile.
@@ -54,10 +59,9 @@ buildTreeFromText(const BehaviorTreeFactory& factory,
5459
*
5560
* return: a pair containing the root node (first) and a vector with all the instantiated nodes (second).
5661
*/
57-
std::pair<TreeNode::Ptr, std::vector<TreeNode::Ptr>>
58-
buildTreeFromFile(const BehaviorTreeFactory& factory,
59-
const std::string& filename,
60-
const Blackboard::Ptr& blackboard = Blackboard::Ptr() );
62+
Tree buildTreeFromFile(const BehaviorTreeFactory& factory,
63+
const std::string& filename,
64+
const Blackboard::Ptr& blackboard = Blackboard::Ptr() );
6165

6266
std::string writeXML(const BehaviorTreeFactory& factory,
6367
const TreeNode* root_node,

src/xml_parsing.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,7 @@ std::string writeXML(const BehaviorTreeFactory& factory,
483483
return std::string( printer.CStr(), printer.CStrSize()-1 );
484484
}
485485

486-
std::pair<TreeNode::Ptr, std::vector<TreeNode::Ptr> >
487-
buildTreeFromText(const BehaviorTreeFactory &factory,
486+
Tree buildTreeFromText(const BehaviorTreeFactory &factory,
488487
const std::string &text,
489488
const Blackboard::Ptr &blackboard)
490489
{
@@ -494,11 +493,10 @@ buildTreeFromText(const BehaviorTreeFactory &factory,
494493
std::vector<TreeNode::Ptr> nodes;
495494
auto root = parser.instantiateTree(nodes);
496495
assignBlackboardToEntireTree(root.get(), blackboard );
497-
return {root, nodes};
496+
return {root.get(), nodes};
498497
}
499498

500-
std::pair<TreeNode::Ptr, std::vector<TreeNode::Ptr> >
501-
buildTreeFromFile(const BehaviorTreeFactory &factory,
499+
Tree buildTreeFromFile(const BehaviorTreeFactory &factory,
502500
const std::string &filename,
503501
const Blackboard::Ptr &blackboard)
504502
{
@@ -508,7 +506,7 @@ buildTreeFromFile(const BehaviorTreeFactory &factory,
508506
std::vector<TreeNode::Ptr> nodes;
509507
auto root = parser.instantiateTree(nodes);
510508
assignBlackboardToEntireTree(root.get(), blackboard );
511-
return {root, nodes};
509+
return {root.get(), nodes};
512510
}
513511

514512
}

0 commit comments

Comments
 (0)