|
6 | 6 | static const char* xml_text = R"( |
7 | 7 | <root BTCPP_format="4"> |
8 | 8 |
|
9 | | - <BehaviorTree ID="MainTree"> |
10 | | - <Sequence> |
11 | | - <SaySomething name="talk" message="hello world"/> |
12 | | - <Fallback> |
13 | | - <AlwaysFailure name="failing_action"/> |
14 | | - <SubTree ID="MySub" name="mysub"/> |
15 | | - </Fallback> |
16 | | - <SaySomething message="before last_action"/> |
17 | | - <Script code="msg:='after last_action'"/> |
18 | | - <AlwaysSuccess name="last_action"/> |
19 | | - <SaySomething message="{msg}"/> |
20 | | - </Sequence> |
21 | | - </BehaviorTree> |
22 | | -
|
23 | | - <BehaviorTree ID="MySub"> |
24 | | - <Sequence> |
25 | | - <AlwaysSuccess name="action_subA"/> |
26 | | - <AlwaysSuccess name="action_subB"/> |
27 | | - </Sequence> |
28 | | - </BehaviorTree> |
| 9 | + <BehaviorTree ID="MainTree"> |
| 10 | + <Sequence> |
| 11 | + <SaySomething name="talk" message="hello world"/> |
| 12 | + <Fallback> |
| 13 | + <AlwaysFailure name="failing_action"/> |
| 14 | + <SubTree ID="MySub" name="mysub"/> |
| 15 | + </Fallback> |
| 16 | + <SaySomething message="before last_action"/> |
| 17 | + <Script code="msg:='after last_action'"/> |
| 18 | + <AlwaysSuccess name="last_action"/> |
| 19 | + <SaySomething message="{msg}"/> |
| 20 | + </Sequence> |
| 21 | + </BehaviorTree> |
| 22 | +
|
| 23 | + <BehaviorTree ID="MySub"> |
| 24 | + <Sequence> |
| 25 | + <AlwaysSuccess name="action_subA"/> |
| 26 | + <AlwaysSuccess name="action_subB"/> |
| 27 | + </Sequence> |
| 28 | + </BehaviorTree> |
29 | 29 |
|
30 | 30 | </root> |
31 | 31 | )"; |
32 | 32 |
|
| 33 | +static const char* json_text = R"( |
| 34 | +{ |
| 35 | + "TestNodeConfigs": { |
| 36 | + "MyTest": { |
| 37 | + "async_delay": 2000, |
| 38 | + "return_status": "SUCCESS", |
| 39 | + "post_script": "msg ='message SUBSTITUED'" |
| 40 | + } |
| 41 | + }, |
| 42 | +
|
| 43 | + "SubstitutionRules": { |
| 44 | + "mysub/action_*": "TestAction", |
| 45 | + "talk": "TestSaySomething", |
| 46 | + "last_action": "MyTest" |
| 47 | + } |
| 48 | +} |
| 49 | + )"; |
| 50 | + |
33 | 51 | // clang-format on |
34 | 52 |
|
35 | 53 | int main(int argc, char** argv) |
@@ -60,28 +78,38 @@ int main(int argc, char** argv) |
60 | 78 | return BT::NodeStatus::SUCCESS; |
61 | 79 | }); |
62 | 80 |
|
63 | | - // These configurations will be passed to a TestNode |
64 | | - BT::TestNodeConfig test_config; |
65 | | - // Convert the node in asynchronous and wait 2000 ms |
66 | | - test_config.async_delay = std::chrono::milliseconds(2000); |
67 | | - // Execute this postcondition, once completed |
68 | | - test_config.post_script = "msg ='message SUBSTITUED'"; |
69 | | - |
70 | 81 | //---------------------------- |
71 | 82 | // pass "no_sub" as first argument to avoid adding rules |
72 | 83 | bool skip_substitution = (argc == 2) && std::string(argv[1]) == "no_sub"; |
73 | 84 |
|
74 | 85 | if(!skip_substitution) |
75 | 86 | { |
76 | | - // Substitute nodes which match this wildcard pattern with TestAction |
77 | | - factory.addSubstitutionRule("mysub/action_*", "TestAction"); |
78 | | - |
79 | | - // Substitute the node with name [talk] with TestSaySomething |
80 | | - factory.addSubstitutionRule("talk", "TestSaySomething"); |
| 87 | + // we can use a JSOn file to configure the substitution rules |
| 88 | + // or do it manually |
| 89 | + bool const USE_JSON = true; |
81 | 90 |
|
82 | | - // Substitute the node with name [last_action] with a TestNode, |
83 | | - // configured using test_config |
84 | | - factory.addSubstitutionRule("last_action", test_config); |
| 91 | + if(USE_JSON) |
| 92 | + { |
| 93 | + factory.loadSubstitutionRuleFromJSON(json_text); |
| 94 | + } |
| 95 | + else { |
| 96 | + // Substitute nodes which match this wildcard pattern with TestAction |
| 97 | + factory.addSubstitutionRule("mysub/action_*", "TestAction"); |
| 98 | + |
| 99 | + // Substitute the node with name [talk] with TestSaySomething |
| 100 | + factory.addSubstitutionRule("talk", "TestSaySomething"); |
| 101 | + |
| 102 | + // This configuration will be passed to a TestNode |
| 103 | + BT::TestNodeConfig test_config; |
| 104 | + // Convert the node in asynchronous and wait 2000 ms |
| 105 | + test_config.async_delay = std::chrono::milliseconds(2000); |
| 106 | + // Execute this postcondition, once completed |
| 107 | + test_config.post_script = "msg ='message SUBSTITUED'"; |
| 108 | + |
| 109 | + // Substitute the node with name [last_action] with a TestNode, |
| 110 | + // configured using test_config |
| 111 | + factory.addSubstitutionRule("last_action", test_config); |
| 112 | + } |
85 | 113 | } |
86 | 114 |
|
87 | 115 | factory.registerBehaviorTreeFromText(xml_text); |
|
0 commit comments