-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygroot.py
162 lines (135 loc) · 4.12 KB
/
pygroot.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
//EXAMPLE
auto tree = Builder<Context>{}
.leaf([](Context &context) {
auto success = DoSomething();
return success ? Status::SUCCESS : Status::FAILURE;
})
.build();
"""
import xml.etree.ElementTree as ET
import sys
TREE_HEADER = """
#ifndef TREE__H
#define TREE__H
#include <beehive.hpp>
#include "context.h"
#undef min
#undef max
using namespace beehive;
"""
TREE_FOOTER = """
#endif //TREE_H
"""
CONTEXT_HEADER = """
#ifndef CONTEXT_H
#define CONTEXT_H
#include <beehive.hpp>
#include "Data.h"
using namespace beehive;
struct Context
{
Data &data;
Context(Data &_data):data(_data){}
"""
CONTEXT_FOOTER = """};
#endif //CONTEXT_H
"""
fname = 'meta_tree.xml'
ddest = "lib/behavior_tree/"
if len(sys.argv) > 1:
fname = sys.argv[1]
if len(sys.argv) > 2:
ddest = sys.argv[2]
tree = ET.parse(fname)
root = tree.getroot()
TREE_PATH = ddest+"TREE.h"
CONTEXT_PATH = ddest+"context.h"
print("Available trees:\n")
trees = {}
for ind, child in enumerate(root):
print(child.tag, child.attrib)
try:
trees[child.attrib['ID']] = ind
except KeyError:
print(child.tag)
actions = {}
def process_node(child, to_write, depends):
requires_end = 0
subtree = None
print("enter:")
print(child.tag, child.attrib)
if child.tag == "Sequence":
to_write += (".sequence()\n")
requires_end = 1
elif child.tag == "Fallback":
to_write += (".selector()\n")
requires_end = 1
elif child.tag == "Action" or child.tag == "Condition":
to_write += (".leaf(&Context::{})\n".format(child.attrib['ID']))
if child.attrib['ID'] not in actions:
f_context.write(" Status {}();\n".format(child.attrib['ID']))
actions[child.attrib['ID']] = 1
elif child.tag == "ForceFailure":
to_write += (".inverter().succeeder()\n")
requires_end = 2
elif child.tag == "ForceSuccess":
to_write += (".succeeder()\n")
requires_end = 1
elif child.tag == "Inverter":
to_write += (".inverter()\n")
requires_end = 1
elif child.tag == "AlwaysFailure":
to_write += (".leaf([](Context _) { return false; })\n")
elif child.tag == "AlwaysSuccess":
to_write += (".leaf([](Context _) { return true; })\n")
elif child.tag == "SubTree":
#subtree = root[trees[child.attrib['ID']]]
depends.append(child.attrib['ID'])
to_write += (".tree({})\n".format(child.attrib['ID']))
else:
print("UNKNOWN")
print(child.tag, child.attrib)
exit()
return requires_end, subtree, depends, to_write
print("START:")
def traverse(root, level=0, to_write="", depends=[]):
for child in root:
for i in range(level):
to_write += (" ")
N_ENDS, subtree, depends, to_write = process_node(
child, to_write, depends)
if subtree != None:
to_write, depends = traverse(subtree, level, to_write, depends)
if N_ENDS > 0:
to_write, depends = traverse(child, level + 1, to_write, depends)
for i in range(level):
to_write += (" ")
for i in range(N_ENDS):
to_write += (".end()")
to_write += ("\n")
return to_write, depends
f_tree = open(TREE_PATH, 'w')
f_tree.write(TREE_HEADER)
f_context = open(CONTEXT_PATH, 'w')
f_context.write(CONTEXT_HEADER)
for tree in reversed(root):
try:
f_tree.write(
"\ntemplate <typename T = Context> //template to avoid multiple declaration, default type to fix deduction error\nTree<T> build_tree_{}(){{\n"
.format(tree.attrib['ID']))
to_write, depends = traverse(tree, 0, "", [])
for depend in depends:
f_tree.write(" Tree<T> {} = build_tree_{}();\n".format(
depend, depend))
f_tree.write("\n return Builder<T>{{}}".format(tree.attrib['ID']))
f_tree.write(to_write)
f_tree.write(".build();}\n\n")
except KeyError:
pass
f_tree.write(TREE_FOOTER)
f_context.write(CONTEXT_FOOTER)
f_tree.close()
f_context.close()