[2bb500]: doc / source / using_pyke.txt Maximize Restore History

Download this file

using_pyke.txt    395 lines (305 with data), 16.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
.. $Id$
..
.. 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.
restindex
crumb: Using Pyke
page-description:
How your Python program uses Pyke. I.e., Pyke's API to Python.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
initialheaderlevel: 2
/restindex
===================================
Using Pyke
===================================
This describes how to use Pyke from within your Python program.
Initializing Pyke
===============================
There are two steps to initializing a Pyke knowledge engine:
knowledge_engine.engine(paths = ('.',), generated_root_dir = 'compiled_krb', load_fc = True, load_bc = True, load_fb = True, load_qb = True)
The Pyke inference engine is offered
as a class so that you can instantiate multiple copies of it with different
rule bases to accomplish different tasks.
Once you have a ``knowledge_engine.engine`` object; generally, all of
the functions that you need are provided directly by this object:
>>> from pyke import knowledge_engine
>>> my_engine = knowledge_engine.engine('examples')
This expects either a single directory or a sequence of directories as
the ``paths`` argument. It recursively walks each directory looking
for Pyke source files (`.kfb files`_, `.krb files`_, and `.kqb files`_).
Each source file that it finds is compiled, if out of date, and then
imported (depending on ``load_fc``, ``load_bc``, ``load_fb`` and
``load_qb``). This causes all of the `rule bases`_ to be loaded and made
ready to activate_.
All generated Python source files and pickle files are placed in the
``generated_root_dir`` directory. By default, this directory is
"compiled_krb". You probably want to add ``compiled_krb`` to your
subversion ``global-ignores`` option. The ``generated_root_dir`` will be
created automatically if it does not already exist.
If you change some of your Pyke source files, you can create a new engine
object to compile and reload the generated Python modules without
restarting your program. But note that you'll need to rerun your
``add_universal_fact`` calls.
*some_engine*.add_universal_fact(kb_name, fact_name, arguments)
The ``add_universal_fact`` function is called once per fact_. These
facts_ are never deleted and apply to all *cases*.
Alternatively, you can place universal facts in a `.kfb file`_ so that
they are loaded automatically.
>>> my_engine.add_universal_fact('family', 'son_of', ('bruce', 'thomas'))
Multiple facts with the same name are allowed.
>>> my_engine.add_universal_fact('family', 'son_of', ('david', 'bruce'))
But duplicate facts (with the same arguments) are silently ignored.
>>> my_engine.add_universal_fact('family', 'son_of', ('david', 'bruce'))
>>> my_engine.get_kb('family').dump_universal_facts()
son_of('bruce', 'thomas')
son_of('david', 'bruce')
These facts are accessed as ``kb_name.fact_name(arguments)`` within the
`.krb files`_.
Setting up Each Case
===========================
Pyke is designed to be run multiple times for multiple *cases*. In
general each case has its own set of starting facts_ and may use different
`rule bases`_, depending upon the situation.
Three functions initialize each case:
*some_engine*.reset()
The ``reset`` function is called once to delete all of the `case specific
facts`_ from the last run. It also deactivates all `rule bases`_.
*some_engine*.assert_(kb_name, fact_name, arguments)
Call ``assert_`` (or the equivalent, ``add_case_specific_fact``,
see `Other Functions`_, below) for each starting fact_ for this case.
Like universal facts, you may have multiple facts with the same name so
long as they have different arguments.
>>> my_engine.assert_('family', 'son_of', ('michael', 'bruce'))
>>> my_engine.assert_('family', 'son_of', ('fred', 'thomas'))
>>> my_engine.assert_('family', 'son_of', ('fred', 'thomas'))
Duplicates with universal facts are also ignored.
>>> my_engine.assert_('family', 'son_of', ('bruce', 'thomas'))
>>> my_engine.get_kb('family').dump_specific_facts()
son_of('michael', 'bruce')
son_of('fred', 'thomas')
>>> my_engine.get_kb('family').dump_universal_facts()
son_of('bruce', 'thomas')
son_of('david', 'bruce')
There is no difference within the `.krb files`_ of how universal facts
verses specific facts are used. The only difference between the two
types of facts is that the specific facts are deleted when a ``reset`` is
done.
>>> my_engine.reset()
>>> my_engine.get_kb('family').dump_specific_facts()
>>> my_engine.get_kb('family').dump_universal_facts()
son_of('bruce', 'thomas')
son_of('david', 'bruce')
*some_engine*.activate(\*rb_names)
Then call ``activate`` to activate the appropriate `rule bases`_. This
may be called more than once, if desired, or it can simply take multiple
arguments.
>>> my_engine.activate('bc_example')
Your Pyke engine is now ready to prove goals for this case!
Proving Goals
======================
Two functions are provided that cover the easy cases. More general
functions are provided in `Other Functions`_, below.
*some_engine*.prove_1(kb_name, entity_name, fixed_args, num_returns)
``Kb_name`` may name either a `fact base`_, `question base`_ or
`rule base category`_.
The ``entity_name`` is the fact name for fact bases, question name for
question bases or the name of the `backward chaining`_ goal for
`rule bases`_.
The ``fixed_args`` are a tuple of Python values. These form the first
set of arguments to the proof. ``Num_returns`` specifies the number of
additional `pattern variables`_ to be appended to these arguments for the
proof. The bindings of these pattern variables will be returned as a
tuple in the answer for the proof. For example:
*some_engine*.prove_1(*some_rule_base_category*, *some_goal*, (1, 2, 3), 2)
Proves the goal:
*some_rule_base_category.some_goal* (1, 2, 3, $ans_0, $ans_1)
And will return the bindings produced by the proof as (``$ans_0``,
``$ans_1``).
Returns the first proof found as a 2-tuple: a tuple of the bindings for
the ``num_returns`` pattern variables, and a plan_. The plan is ``None``
if no plan was generated; otherwise, it is a Python function as
described below__.
.. __: #running-and-pickling-plans
>>> my_engine.prove_1('bc_example', 'father_son', ('thomas', 'david'), 1)
((('grand',),), None)
Raises ``pyke.knowledge_engine.CanNotProve`` if no proof is found.
>>> my_engine.prove_1('bc_example', 'father_son', ('thomas', 'bogus'), 1)
Traceback (most recent call last):
...
CanNotProve: Can not prove bc_example.father_son(thomas, bogus, $ans_0)
*some_engine*.prove_n(kb_name, entity_name, fixed_args, num_returns)
This returns a context manager for a generator yielding 2-tuples, a tuple
whose length == ``num_returns`` and a plan_, for each possible proof.
Like ``prove_1``, the plan is None if no plan was generated. Unlike
``prove_1`` it does not raise an exception if no proof is found.
>>> from __future__ import with_statement
>>> with my_engine.prove_n('bc_example', 'father_son', ('thomas',), 2) as gen:
... for ans in gen:
... print ans
(('bruce', ()), None)
(('david', ('grand',)), None)
Running and Pickling Plans
----------------------------
Once you've obtained a plan_ from ``prove_1`` or ``prove_n``, you just call
it like a normal Python function. The arguments required are simply those
specified, if any, in the `taking clause`_ of the rule__ proving the top-level
goal.
You may call the plan function any number of times. You may even pickle
the plan for later use. But the plans are constructed out of
`functools.partial`_ functions, so you need to register this with copy_reg_
before pickling the plan:
>>> import copy_reg
>>> import functools
>>> copy_reg.pickle(functools.partial,
... lambda p: (functools.partial, (p.func,) + p.args))
No special code is required to unpickle a plan. Just unpickle and call it.
(Unpickling the plan only imports one small Pyke module to be able to run
the plan).
.. __: pyke_syntax/krb_syntax/bc_rule.html
Tracing Rules
-------------
Individual rules may be traced to aid in debugging. The ``trace`` function
takes two arguments: the rule base name, and the name of the rule to trace:
>>> my_engine.trace('bc_example', 'grand_father_son')
>>> my_engine.prove_1('bc_example', 'father_son', ('thomas', 'david'), 1)
bc_example.grand_father_son('thomas', 'david', '$ans_0')
bc_example.grand_father_son succeeded with ('thomas', 'david', ('grand',))
((('grand',),), None)
This can be done either before or after rule base activation and will remain
in effect until you call ``untrace``:
>>> my_engine.untrace('bc_example', 'grand_father_son')
>>> my_engine.prove_1('bc_example', 'father_son', ('thomas', 'david'), 1)
((('grand',),), None)
Krb_traceback
---------------
A handy traceback module is provided to convert Python functions, lines and
line numbers to the `.krb file`_ rule names, lines and line numbers in a
Python traceback. This makes it much easier to read the tracebacks that occur
during proofs.
The ``krb_traceback`` module has exactly the same functions as the standard
Python traceback_ module, but they convert the generated Python function
information into .krb file information. They also delete the intervening
Python functions between subgoal proofs.
>>> import sys
>>> from pyke import knowledge_engine
>>> from pyke import krb_traceback
>>>
>>> my_engine = knowledge_engine.engine('examples')
>>> my_engine.activate('error_test')
>>> try: # doctest: +ELLIPSIS
... my_engine.prove_1('error_test', 'goal', (), 0)
... except:
... krb_traceback.print_exc(None, sys.stdout) # sys.stdout needed for doctest
Traceback (most recent call last):
File "<doctest using_pyke.txt[32]>", line 2, in <module>
my_engine.prove_1('error_test', 'goal', (), 0)
File "...knowledge_engine.py", line 207, in prove_1
return iter(it).next()
File "...knowledge_engine.py", line 191, in gen
for plan in it:
File "...rule_base.py", line 46, in next
return self.iterator.next()
File "...knowledge_engine.py", line 41, in from_iterable
for x in iterable: yield x
File "...knowledge_engine.py", line 41, in from_iterable
for x in iterable: yield x
File "...error_test.krb", line 26, in rule1
goal2()
File "...error_test.krb", line 31, in rule2
goal3()
File "...error_test.krb", line 36, in rule3
goal4()
File "...error_test.krb", line 41, in rule4
check $bar > 0
File "...contexts.py", line 227, in lookup_data
raise KeyError("$%s not bound" % var_name)
KeyError: '$bar not bound'
Other Functions
========================
There are a few more functions that may be useful in special situations.
The first two of these provide more general access to the fact_ lookup and
goal proof mechanisms. The catch is that you must first convert **all**
arguments into patterns__ and create a *context* for these patterns. This is
discussed below_.
.. __: #creating-your-own-patterns
*some_engine*.lookup(kb_name, entity_name, pattern_context, patterns)
This returns a context manager for a generator that binds patterns__ to
successive facts_. Yields ``None`` for each successful match.
*some_engine*.prove(kb_name, entity_name, pattern_context, patterns)
Returns a context manager for a generator that binds patterns__ to
successive proofs. Yields a *prototype_plan* or ``None`` for each
successful match. To turn the prototype_plan into a Python function,
use *prototype_plan*.create_plan(). This returns the plan_ function.
.. __: #creating-your-own-patterns
.. __: #creating-your-own-patterns
The remaining functions are:
*some_engine*.add_case_specific_fact(kb_name, fact_name, args)
This is an alternate to the ``assert_`` function.
*some_engine*.get_kb(kb_name)
Finds and returns the `knowledge base`_ by the name ``kb_name``. Raises
``KeyError`` if not found. Note that for `rule bases`_, this returns the
active `rule base`_ where ``kb_name`` is the `rule base category`_ name.
Thus, not all `rule bases`_ are accessible through this call.
*some_engine*.get_rb(rb_name)
Finds and returns the `rule base`_ by the name ``rb_name``. Raises
``KeyError`` if not found. This works for any `rule base`_, whether it
is active_ or not.
*some_engine*.print_stats([f = sys.stdout])
Prints a brief set of statistics for each knowledge base to file ``f``.
These are reset by the ``reset`` function. This will show how many facts
were asserted, and counts of how many forward-chaining rules were fired
and rerun, as well as counts of how many backward-chaining goals were
tried, and how many backward-chaining rules matched, succeeded and failed.
Note that one backward-chaining rule may succeed many times through
backtracking.
Creating Your Own Patterns
----------------------------------
You'll need two more Pyke modules to create your own patterns__ and contexts:
.. __: logic_programming/pattern_matching/index.html
>>> from pyke import pattern, contexts
There are four kinds of patterns:
pattern.pattern_literal(data)
This matches the ``data`` provided.
pattern.pattern_tuple((elements), rest_var = None)
This matches a tuple. ``Elements`` must each be a pattern and must
match the first *n* elements of the tuple. ``Rest_var`` must be a
variable (or anonymous). It will match the rest of the tuple and is
always bound to a (possibly empty) tuple.
contexts.variable(name)
This will match anything the first time it is encountered and becomes
bound to that value. After that, it only matches this bound value each
additional time it is encountered. Calling the constructor twice with
the same name produces the same variable and must match the same value
in all of the places that it is used.
contexts.anonymous(name)
This will match anything each time it is encountered. Calling the
constructor many times with the same name is not a problem. The name
must start with an underscore.
Finally, to create a *pattern context*, you need:
contexts.simple_context()
You'll need to save this context to lookup your variable values after each
proof is yielded. This is done by either:
| *some_context*.lookup_data(*variable_name*)
| *some_variable*.as_data(*some_context*)
.. _below: `Creating Your Own Patterns`_