Pyke: Python Knowledge Engine
Both forward-chaining and backward-chaining rules (which may include python
code) are compiled into python. Can also automatically assemble python
programs out of python functions which are attached to backward-chaining
COPYRIGHT AND LICENSE:
This is published under the MIT License. The copyright and license are in
the file "LICENSE" in the source directory.
The complete documentation is at:
A copy of the html documentation is also included in the "doc/html" directory
within the source distribution.
See the end of this file for how to regenerate the html files.
Pyke requires python 2.5 or later. Check with:
$ python --version
You can download python at:
1. Download and unzip the source distribution for the version of Python
that you want to use.
If you want to use Python 2.5 or 2.6, you need to use the pyke-1.0.3.zip
If you want to use Python 3.x, you need to use the pyke3.1-1.0.3.zip
2. Open a command line window in the directory above.
3. Run "python setup.py build"
4. As administrator, run: "python setup.py install"
The source distribution contains the pyke source code, documentation (both
source and html), unit tests, and examples.
Each example is in a separate subdirectory under the "examples" directory.
Each example has a README file that explains how to run it.
The family_relations example is a good place to start. It shows several
solutions to the same problem. It also has an example of a few rule
optimizations that result in a 100 times performance improvement on this
The sqlgen example uses Sqlite3 (or MySQL) and the python sqlite3 (or MySQLdb)
modules. It has a function that reads the schema information into pyke facts.
Then the rules in database.krb automatically figure out how to join tables
together to retrieve a list of column names, generate the SQL select
statements and return a plan to execute this SQL statement and return the
results as a dictionary.
The web_framework example uses the sqlgen example. This demonstrates the use
of multiple rule bases. The web_framework is a WSGI application that uses the
HTMLTemplate package (install this with "sudo easy_install HTMLTemplate", be
sure to get version 1.5 or later). It gets the column names from the
HTMLTemplate and feeds those to the sqlgen example to generate a plan to
retrieve the data. It then builds a plan to populate the template and return
the finished HTML document. It also caches the plans so that they don't have
to be re-generated for each request. This makes it run a full 10 times faster
than the same example done in TurboGears 2! The example includes a wsgiref
simple_server setup to run it as an http server so that you can access it
through your browser.
The learn_pyke example is an incomplete attempt at a computer based training
program. It only deals with the topic of pattern matching. It is left here
as an example of using question bases.
The findall, forall, knapsack, notany and towers_of_hanoi examples are each
Pyke uses the doctest-tools package to run its doctests. You can run the
"testall" program from doctest-tools in any subdirectory, or in the top-level
directory. You can install doctest-tools as administrator with:
# easy-install doctest-tools
The top-level directory also has it's own "testpyke" script that removes all
compiled_krb directories, then runs the testall script (from doctest-tools)
twice. The first time forces pyke to recompile everything, and the second
time runs the same tests again having pyke re-use the compiled results from
the previous run. If the "testpyke" program is not on your path, run it as:
WORKING WITH THE SOURCE CODE REPOSITORY:
If you want to run Python3.1, read this:
COMPILING kfbparser_tables.py, krbparser_tables.py and scanner_tables.py:
>>> from pyke.krb_compiler import kfbparser, krbparser, scanner
>>> scanner.init(scanner, 0, True)
>>> krbparser.init(krbparser, True)
>>> kfbparser.init(kfbparser, True)
or just run the "testall" program from the doctest-tools package:
$ cd pyke/krb_compiler
COMPILING THE COMPILER.KRB FILE:
$ PYTHONPATH=../.. python
>>> from pyke import krb_compiler
>>> krb_compiler.compile_krb('compiler', 'compiled_krb', 'compiled_krb', 'compiler.krb')
$ mv compiled_krb/compiler_bc.py .
COMPILING THE DOCUMENTATION:
To compile the documentation into html, you'll need docutils and rest2web.
If you're running linux, your distro may have packages for these already.
Or you can:
$ sudo easy_install rest2web
I used version 0.4.1 of docutils and 0.5.0 of rest2web. I don't know if
earlier versions work OK or not.
To check your versions:
>>> import docutils
>>> import rest2web
If your linux distro doesn't have packages for these, and you don't want to
use easy_install, you can get them at:
To regenerate the html:
$ cd doc/source
This regenerates everything from doc/source into doc/html. It takes about
9 seconds to run.
If you want to run doctest on all of the *.txt files in doc/source, use the
"testall" program from the doctest-tools package:
$ cd doc