|
| 1 | + |
| 2 | +## Basics of the XML schema |
| 3 | + |
| 4 | +In the [first tutorial](tutorial_A_create_trees.md) this simple tree |
| 5 | +was presented. |
| 6 | + |
| 7 | +``` XML |
| 8 | + <root main_tree_to_execute = "MainTree" > |
| 9 | + <BehaviorTree ID="MainTree"> |
| 10 | + <Sequence name="root_sequence"> |
| 11 | + <SaySomething name="action_hello" message="Hello"/> |
| 12 | + <OpenGripper name="open_gripper"/> |
| 13 | + <ApproachObject name="approach_object"/> |
| 14 | + <CloseGripper name="close_gripper"/> |
| 15 | + </Sequence> |
| 16 | + </BehaviorTree> |
| 17 | + </root> |
| 18 | +``` |
| 19 | + |
| 20 | +You may notice that: |
| 21 | + |
| 22 | +- The first tag of the tree is `<root>`. It should contain 1 or more tags `<BehaviorTree>`. |
| 23 | + |
| 24 | +- The tag `<BehaviorTree>` should have the attribute `[ID]`. |
| 25 | + |
| 26 | + |
| 27 | +- The tag `<root>` should contain the attribute `[main_tree_to_execute]`,refering the ID of the main tree. |
| 28 | + |
| 29 | +- The attribute `[main_tree_to_execute]` is mandatory if the file contains multiple `<BehaviorTree>`, |
| 30 | + optional otherwise. |
| 31 | + |
| 32 | +- Each TreeNode is represented by a single tag. In particular: |
| 33 | + |
| 34 | + - The name of the tag is the __ID__ used to register the TreeNode in the factory. |
| 35 | + - The attribute `[name]` refers to the name of the instance and is __optional__. |
| 36 | + - Nodeparameters are passed as attribute as well. In the previous example, the action |
| 37 | + `SaySomething` requires the NodeParameter `message`. |
| 38 | + |
| 39 | +- In terms of number of children: |
| 40 | + |
| 41 | + - `ControlNodes` contain __1 to N children__. |
| 42 | + - `DecoratorNodes` and Subtrees contain __only 1 child__. |
| 43 | + - `ActionNodes` and `ConditionNodes` have __no child__. |
| 44 | + |
| 45 | + |
| 46 | +## Compact vs Explicit representation |
| 47 | + |
| 48 | +The following two syntaxes are both valid: |
| 49 | + |
| 50 | +``` XML |
| 51 | + <SaySomething name="action_hello" message="Hello World"/> |
| 52 | + <Action ID="SaySomething" name="action_hello" message="Hello World"/> |
| 53 | +``` |
| 54 | + |
| 55 | +We will call the former syntax "__compact__" and the latter "__explicit__". |
| 56 | +The first example represented with the explicit syntax would become: |
| 57 | + |
| 58 | +``` XML |
| 59 | + <root main_tree_to_execute = "MainTree" > |
| 60 | + <BehaviorTree ID="MainTree"> |
| 61 | + <Sequence name="root_sequence"> |
| 62 | + <Action ID="SaySomething" name="action_hello" message="Hello"/> |
| 63 | + <Action ID="OpenGripper" name="open_gripper"/> |
| 64 | + <Action ID="ApproachObject" name="approach_object"/> |
| 65 | + <Action ID="CloseGripper" name="close_gripper"/> |
| 66 | + </Sequence> |
| 67 | + </BehaviorTree> |
| 68 | + </root> |
| 69 | +``` |
| 70 | + |
| 71 | +Even if the compact syntax is more convenient and easier to write, it provides |
| 72 | +too little information about the model of the TreeNode. Tools like __Groot__ require either |
| 73 | +the _explicit_ syntax or additional information. |
| 74 | +This information can be added using the tag `<TreeNodeModel>`. |
| 75 | + |
| 76 | +To make the compact version of our tree compatible with Groot, the XML must be modified as follows: |
| 77 | + |
| 78 | + |
| 79 | +``` XML |
| 80 | + <root main_tree_to_execute = "MainTree" > |
| 81 | + <BehaviorTree ID="MainTree"> |
| 82 | + <Sequence name="root_sequence"> |
| 83 | + <SaySomething name="action_hello" message="Hello"/> |
| 84 | + <OpenGripper name="open_gripper"/> |
| 85 | + <ApproachObject name="approach_object"/> |
| 86 | + <CloseGripper name="close_gripper"/> |
| 87 | + </Sequence> |
| 88 | + </BehaviorTree> |
| 89 | + |
| 90 | + <!-- the BT executor don't require this, but Groot does --> |
| 91 | + <TreeNodeModel> |
| 92 | + <Action ID="SaySomething" message="default message"/> |
| 93 | + <Action ID="OpenGripper"/> |
| 94 | + <Action ID="ApproachObject"/> |
| 95 | + <Action ID="CloseGripper"/> |
| 96 | + </TreeNodeModel> |
| 97 | + </root> |
| 98 | +``` |
| 99 | + |
| 100 | +## Subtrees |
| 101 | + |
| 102 | +As we saw in [this tutorial](tutorial_D_subtrees.md), it is possible to include |
| 103 | +a Subtree inside another tree to avoid "copy and pasting" the same tree in |
| 104 | +multiple location and to reduce complexity. |
| 105 | + |
| 106 | +Let's say that we want to incapsulate few action into the behaviorTree "__GraspObject__" |
| 107 | +(being optional, attributes [name] are omitted for simplicity). |
| 108 | + |
| 109 | +``` XML hl_lines="6" |
| 110 | + <root main_tree_to_execute = "MainTree" > |
| 111 | + |
| 112 | + <BehaviorTree ID="MainTree"> |
| 113 | + <Sequence> |
| 114 | + <Action ID="SaySomething" message="Hello World"/> |
| 115 | + <Subtree ID="GraspObject"/> |
| 116 | + </Sequence> |
| 117 | + </BehaviorTree> |
| 118 | + |
| 119 | + <BehaviorTree ID="GraspObject"> |
| 120 | + <Sequence> |
| 121 | + <Action ID="OpenGripper"/> |
| 122 | + <Action ID="ApproachObject"/> |
| 123 | + <Action ID="CloseGripper"/> |
| 124 | + </Sequence> |
| 125 | + </BehaviorTree> |
| 126 | + </root> |
| 127 | +``` |
| 128 | + |
| 129 | +We may notice as the entire tree "GraspObject" is executed after "SaySomething". |
| 130 | + |
| 131 | +## Include external files |
| 132 | + |
| 133 | +__Since version 2.4__. |
| 134 | + |
| 135 | +You can include external files in a way that is similar to __#include <file>__ in C++. |
| 136 | +We can do this easily using the tag: |
| 137 | + |
| 138 | +``` XML |
| 139 | + <include path="relative_or_absolute_path_to_file"> |
| 140 | +``` |
| 141 | + |
| 142 | +using the previous example, we may split the two behavior trees into two files: |
| 143 | + |
| 144 | + |
| 145 | +``` XML hl_lines="5" |
| 146 | + <!-- file maintree.xml --> |
| 147 | + |
| 148 | + <root main_tree_to_execute = "MainTree" > |
| 149 | + |
| 150 | + <include path="grasp.xml"/> |
| 151 | + |
| 152 | + <BehaviorTree ID="MainTree"> |
| 153 | + <Sequence> |
| 154 | + <Action ID="SaySomething" message="Hello World"/> |
| 155 | + <Subtree ID="GraspObject"/> |
| 156 | + </Sequence> |
| 157 | + </BehaviorTree> |
| 158 | + </root> |
| 159 | +``` |
| 160 | + |
| 161 | +``` XML |
| 162 | + <!-- file grasp.xml --> |
| 163 | + |
| 164 | + <root main_tree_to_execute = "GraspObject" > |
| 165 | + <BehaviorTree ID="GraspObject"> |
| 166 | + <Sequence> |
| 167 | + <Action ID="OpenGripper"/> |
| 168 | + <Action ID="ApproachObject"/> |
| 169 | + <Action ID="CloseGripper"/> |
| 170 | + </Sequence> |
| 171 | + </BehaviorTree> |
| 172 | + </root> |
| 173 | +``` |
| 174 | + |
| 175 | +!!! Note "Note for ROS users" |
| 176 | + If you want to find a file inside a [ROS package](http://wiki.ros.org/Packages), |
| 177 | + you can use this syntax: |
| 178 | + |
| 179 | + `<include ros_pkg="name_package" path="path_relative_to_pkg/grasp.xml"/>` |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + |
| 186 | + |
| 187 | + |
| 188 | + |
| 189 | + |
| 190 | + |
0 commit comments