318
|
1
|
|
2 class Tree:
|
319
|
3 """ Tree node with a name and possibly some child nodes """
|
318
|
4 def __init__(self, name, *args):
|
|
5 self.name = name
|
|
6 self.children = args
|
|
7
|
|
8 def __repr__(self):
|
319
|
9 if self.children:
|
|
10 ch = ', '.join(str(c) for c in self.children)
|
|
11 return '{}({})'.format(self.name, ch)
|
|
12 else:
|
|
13 return '{}'.format(self.name)
|
|
14
|
|
15
|
|
16 class State:
|
|
17 def __init__(self):
|
|
18 self.labels = {}
|
|
19 def has_goal(self, goal):
|
|
20 return goal in self.labels
|
|
21 def get_cost(self, goal):
|
|
22 if not self.has_goal(goal): return 999999
|
|
23 return self.labels[goal][0]
|
|
24 def get_rule(self, goal):
|
|
25 return self.labels[goal][1]
|
|
26 def set_cost(self, goal, cost, rule):
|
|
27 if self.has_goal(goal):
|
|
28 if self.get_cost(goal) > cost:
|
|
29 self.labels[goal] = (cost, rule)
|
|
30 else:
|
|
31 self.labels[goal] = (cost, rule)
|
|
32
|
|
33 class BaseMatcher:
|
|
34 def kids(self, tree, rule):
|
|
35 return self.kid_functions[rule](tree)
|
|
36
|
|
37 def nts(self, rule):
|
|
38 return self.nts_map[rule]
|
|
39
|
|
40 def burm_label(self, tree):
|
|
41 for c in tree.children:
|
|
42 self.burm_label(c)
|
|
43 self.burm_state(tree)
|
|
44
|
|
45 def apply_rules(self, tree, goal):
|
|
46 print(tree.state.get_rule(goal))
|
|
47 rule = tree.state.get_rule(goal)
|
|
48 for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule)):
|
|
49 self.apply_rules(kid_tree, kid_goal)
|