tutorial_2_decisions   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 5

4 Functions

Rating   Name   Duplication   Size   Complexity  
A is_name_set() 0 2 1
A some_action() 0 3 1
A ask_for_name() 0 7 2
A say_hello() 0 3 1
1
"""Tutorial 2 - how to use decisions
2
3
4
The behavior tree looks like:
5
 --> sequence:
6
     succes_threshold: 2
7
     --(children)--> decision:
8
         --(condition)--> is_name_set:
9
         --(success_tree)--> say_hello:
10
         --(failure_tree)--> sequence:
11
                             succes_threshold: 2
12
             --(children)--> ask_for_name:
13
             --(children)--> say_hello:
14
     --(children)--> some_action:
15
16
"""
17
18
import curio
19
import async_btree as bt
20
import contextvars
21
22
23
async def some_action():
24
    print("continue here...")
25
    return bt.SUCCESS
26
27
28
async def say_hello():
29
    print(f"Hello {name.get()}")
30
    return bt.SUCCESS
31
32
33
async def is_name_set():
34
    return name.get() != ""
35
36
37
async def ask_for_name():
38
    new_name = input("Hello, whats your name? \n")
39
    if new_name != "":
40
        name.set(new_name)
41
        return bt.SUCCESS
42
    else:
43
        return bt.FAILURE
44
45
46
name = contextvars.ContextVar('name', default="")
47
48
greet_with_name = bt.decision(
49
    condition=is_name_set, success_tree=say_hello, failure_tree=bt.sequence([ask_for_name, say_hello])
50
)
51
52
b_tree = bt.sequence(children=[greet_with_name, some_action])
53
54
if __name__ == '__main__':
55
56
    name = contextvars.ContextVar('name', default="")
57
58
    curio.run(b_tree)
59
60
    # You can take a look at the final behavior tree
61
    abstract_tree_tree_1 = bt.analyze(b_tree)
62
    print(bt.stringify_analyze(abstract_tree_tree_1))
63