comparison python/tree.py @ 319:8d07a4254f04

Work on burg
author Windel Bouwman
date Sat, 18 Jan 2014 18:58:43 +0100
parents e84047f29c78
children 84d67cce67b7
comparison
equal deleted inserted replaced
318:e84047f29c78 319:8d07a4254f04
1 1
2 class Tree: 2 class Tree:
3 """ Tree node with a name and possibly some child nodes """
3 def __init__(self, name, *args): 4 def __init__(self, name, *args):
4 self.name = name 5 self.name = name
5 self.children = args 6 self.children = args
6 7
7 def __repr__(self): 8 def __repr__(self):
8 return 'Tree({}, {})'.format(self.name, self.children) 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)