Mercurial > lcfOS
diff python/tree.py @ 319:8d07a4254f04
Work on burg
author | Windel Bouwman |
---|---|
date | Sat, 18 Jan 2014 18:58:43 +0100 |
parents | e84047f29c78 |
children | 84d67cce67b7 |
line wrap: on
line diff
--- a/python/tree.py Tue Dec 31 12:38:15 2013 +0100 +++ b/python/tree.py Sat Jan 18 18:58:43 2014 +0100 @@ -1,8 +1,49 @@ class Tree: + """ Tree node with a name and possibly some child nodes """ def __init__(self, name, *args): self.name = name self.children = args def __repr__(self): - return 'Tree({}, {})'.format(self.name, self.children) + if self.children: + ch = ', '.join(str(c) for c in self.children) + return '{}({})'.format(self.name, ch) + else: + return '{}'.format(self.name) + + +class State: + def __init__(self): + self.labels = {} + def has_goal(self, goal): + return goal in self.labels + def get_cost(self, goal): + if not self.has_goal(goal): return 999999 + return self.labels[goal][0] + def get_rule(self, goal): + return self.labels[goal][1] + def set_cost(self, goal, cost, rule): + if self.has_goal(goal): + if self.get_cost(goal) > cost: + self.labels[goal] = (cost, rule) + else: + self.labels[goal] = (cost, rule) + +class BaseMatcher: + def kids(self, tree, rule): + return self.kid_functions[rule](tree) + + def nts(self, rule): + return self.nts_map[rule] + + def burm_label(self, tree): + for c in tree.children: + self.burm_label(c) + self.burm_state(tree) + + def apply_rules(self, tree, goal): + print(tree.state.get_rule(goal)) + rule = tree.state.get_rule(goal) + for kid_tree, kid_goal in zip(self.kids(tree, rule), self.nts(rule)): + self.apply_rules(kid_tree, kid_goal)