annotate python/tree.py @ 320:84d67cce67b7

Working burg
author Windel Bouwman
date Sun, 19 Jan 2014 16:09:44 +0100
parents 8d07a4254f04
children 44f336460c2a
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:
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
17 """ State used to label tree nodes """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
18 def __init__(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
19 self.labels = {}
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
20
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
21 def has_goal(self, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
22 return goal in self.labels
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
23
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
24 def get_cost(self, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
25 return self.labels[goal][0]
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
26
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
27 def get_rule(self, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
28 return self.labels[goal][1]
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
29
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
30 def set_cost(self, goal, cost, rule):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
31 if self.has_goal(goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
32 if self.get_cost(goal) > cost:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
33 self.labels[goal] = (cost, rule)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
34 else:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
35 self.labels[goal] = (cost, rule)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
36
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
37
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
38 class BaseMatcher:
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
39 """ Base class for matcher objects. """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
40 def kids(self, tree, rule):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
41 return self.kid_functions[rule](tree)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
42
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
43 def nts(self, rule):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
44 return self.nts_map[rule]
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
45
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
46 def burm_label(self, tree):
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
47 """ Label all nodes in the tree bottom up """
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
48 for c in tree.children:
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
49 self.burm_label(c)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
50 self.burm_state(tree)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
51
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
52 def apply_rules(self, tree, goal):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
53 rule = tree.state.get_rule(goal)
320
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
54 results = [self.apply_rules(kid_tree, kid_goal)
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
55 for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule))]
84d67cce67b7 Working burg
Windel Bouwman
parents: 319
diff changeset
56 self.pat_f[rule](*results)