forked from AtsushiSakai/PythonRobotics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_behavior_tree.py
More file actions
207 lines (184 loc) · 6.35 KB
/
test_behavior_tree.py
File metadata and controls
207 lines (184 loc) · 6.35 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pytest
import conftest
from MissionPlanning.BehaviorTree.behavior_tree import (
BehaviorTreeFactory,
Status,
ActionNode,
)
def test_sequence_node1():
xml_string = """
<Sequence>
<Echo name="Echo 1" message="Hello, World1!" />
<Echo name="Echo 2" message="Hello, World2!" />
<ForceFailure name="Force Failure">
<Echo name="Echo 3" message="Hello, World3!" />
</ForceFailure>
</Sequence>
"""
bt_factory = BehaviorTreeFactory()
bt = bt_factory.build_tree(xml_string)
bt.tick()
assert bt.root.status == Status.RUNNING
assert bt.root.children[0].status == Status.SUCCESS
assert bt.root.children[1].status is None
assert bt.root.children[2].status is None
bt.tick()
bt.tick()
assert bt.root.status == Status.FAILURE
assert bt.root.children[0].status is None
assert bt.root.children[1].status is None
assert bt.root.children[2].status is None
def test_sequence_node2():
xml_string = """
<Sequence>
<Echo name="Echo 1" message="Hello, World1!" />
<Echo name="Echo 2" message="Hello, World2!" />
<ForceSuccess name="Force Success">
<Echo name="Echo 3" message="Hello, World3!" />
</ForceSuccess>
</Sequence>
"""
bt_factory = BehaviorTreeFactory()
bt = bt_factory.build_tree(xml_string)
bt.tick_while_running()
assert bt.root.status == Status.SUCCESS
assert bt.root.children[0].status is None
assert bt.root.children[1].status is None
assert bt.root.children[2].status is None
def test_selector_node1():
xml_string = """
<Selector>
<ForceFailure name="Force Failure">
<Echo name="Echo 1" message="Hello, World1!" />
</ForceFailure>
<Echo name="Echo 2" message="Hello, World2!" />
<Echo name="Echo 3" message="Hello, World3!" />
</Selector>
"""
bt_factory = BehaviorTreeFactory()
bt = bt_factory.build_tree(xml_string)
bt.tick()
assert bt.root.status == Status.RUNNING
assert bt.root.children[0].status == Status.FAILURE
assert bt.root.children[1].status is None
assert bt.root.children[2].status is None
bt.tick()
assert bt.root.status == Status.SUCCESS
assert bt.root.children[0].status is None
assert bt.root.children[1].status is None
assert bt.root.children[2].status is None
def test_selector_node2():
xml_string = """
<Selector>
<ForceFailure name="Force Success">
<Echo name="Echo 1" message="Hello, World1!" />
</ForceFailure>
<ForceFailure name="Force Failure">
<Echo name="Echo 2" message="Hello, World2!" />
</ForceFailure>
</Selector>
"""
bt_factory = BehaviorTreeFactory()
bt = bt_factory.build_tree(xml_string)
bt.tick_while_running()
assert bt.root.status == Status.FAILURE
assert bt.root.children[0].status is None
assert bt.root.children[1].status is None
def test_while_do_else_node():
xml_string = """
<WhileDoElse>
<Count name="Count" count_threshold="3" />
<Echo name="Echo 1" message="Hello, World1!" />
<Echo name="Echo 2" message="Hello, World2!" />
</WhileDoElse>
"""
class CountNode(ActionNode):
def __init__(self, name, count_threshold):
super().__init__(name)
self.count = 0
self.count_threshold = count_threshold
def tick(self):
self.count += 1
if self.count >= self.count_threshold:
return Status.FAILURE
else:
return Status.SUCCESS
bt_factory = BehaviorTreeFactory()
bt_factory.register_node_builder(
"Count",
lambda node: CountNode(
node.attrib.get("name", CountNode.__name__),
int(node.attrib["count_threshold"]),
),
)
bt = bt_factory.build_tree(xml_string)
bt.tick()
assert bt.root.status == Status.RUNNING
assert bt.root.children[0].status == Status.SUCCESS
assert bt.root.children[1].status is Status.SUCCESS
assert bt.root.children[2].status is None
bt.tick()
assert bt.root.status == Status.RUNNING
assert bt.root.children[0].status == Status.SUCCESS
assert bt.root.children[1].status is Status.SUCCESS
assert bt.root.children[2].status is None
bt.tick()
assert bt.root.status == Status.SUCCESS
assert bt.root.children[0].status is None
assert bt.root.children[1].status is None
assert bt.root.children[2].status is None
def test_node_children():
# ControlNode Must have children
xml_string = """
<Sequence>
</Sequence>
"""
bt_factory = BehaviorTreeFactory()
with pytest.raises(ValueError):
bt_factory.build_tree(xml_string)
# DecoratorNode Must have child
xml_string = """
<Inverter>
</Inverter>
"""
with pytest.raises(ValueError):
bt_factory.build_tree(xml_string)
# DecoratorNode Must have only one child
xml_string = """
<Inverter>
<Echo name="Echo 1" message="Hello, World1!" />
<Echo name="Echo 2" message="Hello, World2!" />
</Inverter>
"""
with pytest.raises(ValueError):
bt_factory.build_tree(xml_string)
# ActionNode Must have no children
xml_string = """
<Echo name="Echo 1" message="Hello, World1!">
<Echo name="Echo 2" message="Hello, World2!" />
</Echo>
"""
with pytest.raises(ValueError):
bt_factory.build_tree(xml_string)
# WhileDoElse Must have exactly 2 or 3 children
xml_string = """
<WhileDoElse>
<Echo name="Echo 1" message="Hello, World1!" />
</WhileDoElse>
"""
with pytest.raises(ValueError):
bt = bt_factory.build_tree(xml_string)
bt.tick()
xml_string = """
<WhileDoElse>
<Echo name="Echo 1" message="Hello, World1!" />
<Echo name="Echo 2" message="Hello, World2!" />
<Echo name="Echo 3" message="Hello, World3!" />
<Echo name="Echo 4" message="Hello, World4!" />
</WhileDoElse>
"""
with pytest.raises(ValueError):
bt = bt_factory.build_tree(xml_string)
bt.tick()
if __name__ == "__main__":
conftest.run_this_test(__file__)