Repository navigation
Expand file tree
/
Copy patht14_subtree_model.cpp
More file actions
91 lines (75 loc) · 2.41 KB
/
Copy patht14_subtree_model.cpp
File metadata and controls
91 lines (75 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "behaviortree_cpp/bt_factory.h"
#include "behaviortree_cpp/loggers/bt_cout_logger.h"
using namespace BT;
/*
* Demonstrate how to use a SubTree Model (since version 4.4)
*/
/**
* You can optionally add a model to a SubTrees, in this case, "MySub".
* We are telling the factory that the callee should remap
* two mandatory inputs, and two outputs:
*
* - sub_in_value (that has the default value 42)
* - sub_in_name (no default)
* - sub_out_result (default remapping to port {output})
* - sub_out_state (no default)
*
* The callee (parent tree, including the subtree) MUST specify those
* remapping which have no default value.
*/
// clang-format off
namespace
{
const char* xml_subtree = R"(
<root BTCPP_format="4">
<TreeNodesModel>
<SubTree ID="MySub">
<input_port name="sub_in_value" default="42"/>
<input_port name="sub_in_name"/>
<output_port name="sub_out_result" default="{out_result}"/>
<output_port name="sub_out_state"/>
</SubTree>
</TreeNodesModel>
<BehaviorTree ID="MySub">
<Sequence>
<ScriptCondition code="sub_in_value==42 && sub_in_name=='john'" />
<Script code="sub_out_result:=69; sub_out_state:='ACTIVE'" />
</Sequence>
</BehaviorTree>
</root>
)";
/**
* Here, when calling "MySub", only `sub_in_name` and `sub_out_state` are explicitly
* remapped. We will use the default values for the other two.
*/
const char* xml_maintree = R"(
<root BTCPP_format="4">
<BehaviorTree ID="MainTree">
<Sequence>
<Script code="in_name:= 'john' "/>
<SubTree ID="MySub" sub_in_name="{in_name}"
sub_out_state="{out_state}"/>
<ScriptCondition code=" out_result==69 && out_state=='ACTIVE' " />
</Sequence>
</BehaviorTree>
</root>
)";
} // namespace
// clang-format on
int main()
{
BehaviorTreeFactory factory;
factory.registerBehaviorTreeFromText(xml_subtree);
factory.registerBehaviorTreeFromText(xml_maintree);
auto tree = factory.createTree("MainTree");
StdCoutLogger logger(tree);
tree.tickWhileRunning();
// We expect the sequence to be successful.
// The full remapping was:
//
// - sub_in_name <-> {in_name} // specified by callee 'MainTree'
// - sub_in_value <-> 42 // default remapping, see model
// - sub_out_result <-> {out_result} // default remapping, see model
// - sub_out_state <-> {out_state} // specified by callee 'MainTree'
return 0;
}