annotate python/tree.py @ 319:8d07a4254f04

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