[2bb500]: / pyke / rule_base.py  Maximize  Restore  History

Download this file

228 lines (217 with data), 9.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# $Id$
# coding=utf-8
#
# Copyright Š 2007-2008 Bruce Frederiksen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import itertools
from pyke import knowledge_base
class StopProof(Exception): pass
class stopIteratorContext(object):
def __init__(self, rule_base, iterator_context):
self.rule_base = rule_base
self.context = iterator_context
def __enter__(self):
return stopIterator(self.rule_base, self.context.__enter__())
def __exit__(self, type, value, tb):
self.context.__exit__(type, value, tb)
class stopIterator(object):
def __init__(self, rule_base, iterator):
self.rule_base = rule_base
self.iterator = iter(iterator)
def __iter__(self): return self
def next(self):
if self.iterator:
try:
return self.iterator.next()
except StopProof:
self.iterator = None
self.rule_base.num_bc_rule_failures += 1
raise StopIteration
class chain_context(object):
def __init__(self, outer_it):
self.outer_it = outer_iterable(outer_it)
def __enter__(self):
return itertools.chain.from_iterable(self.outer_it)
def __exit__(self, type, value, tb): self.outer_it.close()
class outer_iterable(object):
def __init__(self, outer_it):
self.outer_it = iter(outer_it)
self.inner_it = None
def __iter__(self): return self
def close(self):
if hasattr(self.inner_it, '__exit__'):
self.inner_it.__exit__(None, None, None)
elif hasattr(self.inner_it, 'close'): self.inner_it.close()
if hasattr(self.outer_it, 'close'): self.outer_it.close()
def next(self):
ans = self.outer_it.next()
if hasattr(ans, '__enter__'):
self.inner_it = ans
return ans.__enter__()
ans = iter(ans)
self.inner_it = ans
return ans
class rule_base(knowledge_base.knowledge_base):
def __init__(self, engine, name, parent = None, exclude_list = ()):
super(rule_base, self).__init__(engine, name, rule_list, False)
if name in engine.rule_bases:
raise AssertionError("rule_base %s already exists" % name)
if name in engine.knowledge_bases:
raise AssertionError("name clash between rule_base '%s' and "
"fact_base '%s'" % (name, name))
engine.rule_bases[name] = self
self.fc_rules = []
self.parent = parent
self.exclude_set = frozenset(exclude_list)
self.rules = {} # {name: rule}
def add_fc_rule(self, fc_rule):
if fc_rule.name in self.rules:
raise AssertionError("%s rule_base: duplicate rule name: %s" %
(self.name, fc_rule.name))
self.rules[fc_rule.name] = fc_rule
self.fc_rules.append(fc_rule)
def add_bc_rule(self, bc_rule):
if bc_rule.name in self.rules:
raise AssertionError("%s rule_base: duplicate rule name: %s" %
(self.name, bc_rule.name))
self.rules[bc_rule.name] = bc_rule
self.get_entity_list(bc_rule.goal_name).add_bc_rule(bc_rule)
def init2(self):
if not self.initialized:
self.initialized = True
if self.parent:
parent = self.engine.rule_bases.get(self.parent)
if parent is None:
raise KeyError("rule_base %s: parent %s not found" % \
(self.name, self.parent))
self.parent = parent
self.parent.init2()
self.root_name = self.parent.root_name
else:
self.root_name = self.name
self.reset()
def derived_from(self, rb):
parent = self.parent
while parent:
if parent == rb: return True
parent = parent.parent
return False
def register_fc_rules(self, stop_at_rb):
rb = self
while rb is not stop_at_rb:
for fc_rule in rb.fc_rules: fc_rule.register_rule()
if not rb.parent: break
rb = rb.parent
def run_fc_rules(self, stop_at_rb):
rb = self
while rb is not stop_at_rb:
for fc_rule in rb.fc_rules: fc_rule.run()
if not rb.parent: break
rb = rb.parent
def activate(self):
current_rb = self.engine.knowledge_bases.get(self.root_name)
if current_rb:
assert self.derived_from(current_rb), \
"%s.activate(): not derived from current rule_base, %s" % \
(self.name, current_rb.name)
self.engine.knowledge_bases[self.root_name] = self
self.register_fc_rules(current_rb)
self.run_fc_rules(current_rb)
def reset(self):
if self.root_name in self.engine.knowledge_bases:
del self.engine.knowledge_bases[self.root_name]
for fc_rule in self.fc_rules: fc_rule.reset()
self.num_fc_rules_triggered = 0
self.num_fc_rules_rerun = 0
self.num_prove_calls = 0
self.num_bc_rules_matched = 0
self.num_bc_rule_successes = 0
self.num_bc_rule_failures = 0
def gen_rule_lists_for(self, goal_name):
rule_base = self
while True:
rl = rule_base.entity_lists.get(goal_name)
if rl: yield rl
if rule_base.parent and goal_name not in rule_base.exclude_set:
rule_base = rule_base.parent
else:
break
def prove(self, bindings, pat_context, goal_name, patterns):
self.num_prove_calls += 1
return stopIteratorContext(self,
chain_context(
rl.prove(bindings, pat_context, patterns)
for rl in self.gen_rule_lists_for(goal_name)))
def print_stats(self, f):
f.write("%s: %d fc_rules, %d triggered, %d rerun\n" %
(self.name, len(self.fc_rules), self.num_fc_rules_triggered,
self.num_fc_rules_rerun))
num_bc_rules = sum(rule_list.num_bc_rules()
for rule_list in self.entity_lists.itervalues())
f.write("%s: %d bc_rules, %d goals, %d rules matched\n" %
(self.name, num_bc_rules, self.num_prove_calls,
self.num_bc_rules_matched))
f.write("%s %d successes, %d failures\n" %
(' ' * len(self.name), self.num_bc_rule_successes,
self.num_bc_rule_failures))
if self.parent: self.parent.print_stats(f)
def trace(self, rule_name):
for rule_list in self.entity_lists.itervalues():
if rule_list.trace(rule_name): return
raise KeyError("trace: rule %s not found" % rule_name)
def untrace(self, rule_name):
for rule_list in self.entity_lists.itervalues():
if rule_list.untrace(rule_name): return
raise KeyError("untrace: rule %s not found" % rule_name)
class rule_list(knowledge_base.knowledge_entity_list):
def __init__(self, name):
self.name = name
self.bc_rules = []
def add_bc_rule(self, bc_rule):
self.bc_rules.append(bc_rule)
def prove(self, bindings, pat_context, patterns):
""" Returns a context manager for a generator that binds patterns to
successively proven goals, yielding the plan (or None, if no plan)
for each successful match. Undoes bindings upon continuation,
so that no bindings remain at StopIteration.
"""
return chain_context(
bc_rule.bc_fn(bc_rule, patterns, pat_context)
for bc_rule in self.bc_rules)
def num_bc_rules(self):
return len(self.bc_rules)
def trace(self, rule_name):
for bc_rule in self.bc_rules:
if bc_rule.name == rule_name:
bc_rule.trace()
return True
return False
def untrace(self, rule_name):
for bc_rule in self.bc_rules:
if bc_rule.name == rule_name:
bc_rule.untrace()
return True
return False
def test():
import doctest
import sys
sys.exit(doctest.testmod()[0])
if __name__ == "__main__":
test()