Using Pyke
This describes how to call Pyke from your Python program.
Getting Started
The simplest use of Pyke involves three steps:
Create an engine object.
>>> from pyke import knowledge_engine>>> my_engine = knowledge_engine.engine(__file__)This step compiles the Pyke source files, if out of date, and loads the knowledge bases.
There are three kinds of Pyke source files:
- .kfb files define fact bases, which are compiled into .fbc pickle files.
- .krb files define rule bases, which are compiled into 1 to 3 .py Python source files.
- .kqb files define question bases, which are compiled into .qbc pickle files.
See Creating an Inference Engine to control where the compiled files are written, load knowledge bases from multiple directories, distribute your application without your knowledge base files, or distribute using egg files.
Activate rule bases.
>>> my_engine.activate('bc_related')You may activate one rule base for each rule base category. Simply pass multiple arguments to activate.
Note
Even if you only have one rule base, you must still activate it.
This is when the forward-chaining rules are run.
>>> my_engine.prove_1_goal('bc_related.father_son(bruce, $son, ())') ({'son': 'david'}, None)The goal might be met by simply matching an already known fact, or through the use of backward-chaining rules.
Then if you want to prove another goal, you can just repeat the last step. In this case, the forward-chaining rules are only run once and all goals operate against the same set of known facts.
>>> my_engine.prove_1_goal('bc_related.father_son(thomas, $grandson, (grand))') ({'grandson': 'david'}, None)See Proving Goals to pass different arguments into goals, compile the goal statements once in advance, and to retrieve multiple answers for a goal.
Dynamically Asserting Facts
To dynamically assert facts within your Python program, a new step is added:
Create the engine object:
>>> my_engine = knowledge_engine.engine(__file__)
>>> my_engine.assert_('family2', 'son_of', ('spike_the_dog', 'david'))
These facts must be asserted prior to activating the rule bases so that they are available to the forward-chaining rules. This example shows asserting case specific facts that are deleted before running the next case (as shown in the next section, below). But you can also assert universal facts that apply to all cases. See Asserting New Facts for more information.
After asserting your facts, activate your rule bases and prove your goal as before:
>>> my_engine.activate('bc_related') >>> my_engine.prove_1_goal('bc_related.father_son(bruce, $grandson, (grand))') ({'grandson': 'spike_the_dog'}, None)
Using Different Facts for Different Cases
But if you want to prove goals against different sets of facts or using different rule bases, you need to reset the Pyke engine:
Only need this once:
>>> my_engine = knowledge_engine.engine(__file__)First case, as before:
>>> my_engine.assert_('family2', 'son_of', ('spike_the_dog', 'david')) >>> my_engine.activate('bc_related') >>> my_engine.prove_1_goal('bc_related.father_son(bruce, $grandson, (grand))') ({'grandson': 'spike_the_dog'}, None)
Reset the Pyke engine.
>>> my_engine.reset()
This erases all of the case specific facts that you asserted in step 2, as well as all of the facts asserted by the forward-chaining rules.
It also deactivates all of the rule bases, so you'll need to call activate again after asserting your facts.
Second case:
>>> my_engine.assert_('family2', 'son_of', ('felix_the_cat', 'david')) >>> my_engine.activate('bc_related') >>> my_engine.prove_1_goal('bc_related.father_son(bruce, $grandson, (grand))') ({'grandson': 'felix_the_cat'}, None)