[f16399]: examples / family_relations / test.py Maximize Restore History

Download this file

test.py    219 lines (192 with data), 8.2 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
# $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.
'''
This example shows how people are related. The primary data (facts) that
are used to figure everything out are in family.kfb.
There are four independent rule bases that all do the same thing. The
fc_example rule base only uses forward-chaining rules. The bc_example
rule base only uses backward-chaining rules. The bc2_example rule base
also only uses backward-chaining rules, but with a few optimizations that
make it run 100 times faster than bc_example. And the example rule base
uses all three (though it's a poor use of plans).
Once the pyke engine is created, all the rule bases loaded and all the
primary data established as universal facts; there are five functions
that can be used to run each of the three rule bases: fc_test, bc_test,
bc2_test, test and general.
'''
from __future__ import with_statement
import contextlib
import sys
import time
from pyke import knowledge_engine, krb_traceback, goal
# Compile and load .krb files in same directory that I'm in (recursively).
engine = knowledge_engine.engine('examples.family_relations')
fc_goal = goal.compile('family.how_related($person1, $person2, $relationship)')
def fc_test(person1 = 'bruce'):
'''
This function runs the forward-chaining example (fc_example.krb).
'''
engine.reset() # Allows us to run tests multiple times.
start_time = time.time()
engine.activate('fc_example') # Runs all applicable forward-chaining rules.
fc_end_time = time.time()
fc_time = fc_end_time - start_time
print "doing proof"
with fc_goal.prove(engine, person1=person1) as gen:
for vars, plan in gen:
print "%s, %s are %s" % \
(person1, vars['person2'], vars['relationship'])
prove_time = time.time() - fc_end_time
print
print "done"
engine.print_stats()
print "fc time %.2f, %.0f asserts/sec" % \
(fc_time, engine.get_kb('family').get_stats()[2] / fc_time)
def bc_test(person1 = 'bruce'):
engine.reset() # Allows us to run tests multiple times.
start_time = time.time()
engine.activate('bc_example')
fc_end_time = time.time()
fc_time = fc_end_time - start_time
print "doing proof"
try:
with engine.prove_goal(
'bc_example.how_related($person1, $person2, $relationship)',
person1=person1) \
as gen:
for vars, plan in gen:
print "%s, %s are %s" % \
(person1, vars['person2'], vars['relationship'])
except StandardError:
# This converts stack frames of generated python functions back to the
# .krb file.
krb_traceback.print_exc()
sys.exit(1)
prove_time = time.time() - fc_end_time
print
print "done"
engine.print_stats()
print "bc time %.2f, %.0f goals/sec" % \
(prove_time, engine.get_kb('bc_example').num_prove_calls / prove_time)
def bc2_test(person1 = 'bruce'):
engine.reset() # Allows us to run tests multiple times.
start_time = time.time()
engine.activate('bc2_example')
fc_end_time = time.time()
fc_time = fc_end_time - start_time
print "doing proof"
try:
with engine.prove_goal(
'bc2_example.how_related($person1, $person2, $relationship)',
person1=person1) \
as gen:
for vars, plan in gen:
print "%s, %s are %s" % \
(person1, vars['person2'], vars['relationship'])
except StandardError:
# This converts stack frames of generated python functions back to the
# .krb file.
krb_traceback.print_exc()
sys.exit(1)
prove_time = time.time() - fc_end_time
print
print "done"
engine.print_stats()
print "bc time %.2f, %.0f goals/sec" % \
(prove_time,
engine.get_kb('bc2_example').num_prove_calls / prove_time)
def test(person1 = 'bruce'):
engine.reset() # Allows us to run tests multiple times.
# Also runs all applicable forward-chaining rules.
start_time = time.time()
engine.activate('example')
fc_end_time = time.time()
fc_time = fc_end_time - start_time
print "doing proof"
try:
# In this case, the relationship is returned when you run the plan.
with engine.prove_goal(
'example.how_related($person1, $person2)',
person1=person1) \
as gen:
for vars, plan in gen:
print "%s, %s are %s" % (person1, vars['person2'], plan())
except StandardError:
# This converts stack frames of generated python functions back to the
# .krb file.
krb_traceback.print_exc()
sys.exit(1)
prove_time = time.time() - fc_end_time
print
print "done"
engine.print_stats()
print "fc time %.2f, %.0f asserts/sec" % \
(fc_time, engine.get_kb('family').get_stats()[2] / fc_time)
print "bc time %.2f, %.0f goals/sec" % \
(prove_time, engine.get_kb('example').num_prove_calls / prove_time)
print "total time %.2f" % (fc_time + prove_time)
# Need some extra goodies for general()...
from pyke import contexts, pattern
def general(person1 = None, person2 = None, relationship = None):
engine.reset() # Allows us to run tests multiple times.
start_time = time.time()
engine.activate('bc2_example') # same rule base as bc2_test()
fc_end_time = time.time()
fc_time = fc_end_time - start_time
print "doing proof"
top_context = contexts.simple_context()
if person1: arg1 = pattern.pattern_literal(person1)
else: arg1 = contexts.variable('person1')
if person2: arg2 = pattern.pattern_literal(person2)
else: arg2 = contexts.variable('person2')
if relationship: arg3 = make_pattern(relationship)
else: arg3 = contexts.variable('relationship')
try:
with engine.prove('bc2_example', 'how_related', top_context,
(arg1, arg2, arg3)) \
as gen:
for prototype_plan in gen:
print "%s, %s are %s" % (arg1.as_data(top_context),
arg2.as_data(top_context),
arg3.as_data(top_context))
except StandardError:
# This converts stack frames of generated python functions back to the
# .krb file.
krb_traceback.print_exc()
sys.exit(1)
prove_time = time.time() - fc_end_time
print
print "done"
engine.print_stats()
print "bc time %.2f, %.0f goals/sec" % \
(prove_time,
engine.get_kb('bc2_example').num_prove_calls / prove_time)
import types
def make_pattern(x):
if isinstance(x, types.StringTypes):
if x[0] == '$': return contexts.variable(x[1:])
return pattern.pattern_literal(x)
if isinstance(x, (tuple, list)):
return pattern.pattern_tuple(tuple(make_pattern(element)
for element in x))
return pattern.pattern_literal(x)