.. 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.
Explanation of *plans* and automatic program generation.
Plans and Automatic Program Generation
Once you understand how backward-chaining_ works, it is relatively easy to
do automatic program generation.
Adding Plans to Backward-Chaining Rules
The way this is done is by attaching Python functions to
backward-chaining_ rules_. These functions are written in the *with*
clause at the end of each rule_ in the `.krb file`_. They don't affect how
the rules_ run to prove a goal, but are
gathered up to form a call graph that is returned along with the `pattern
variable`_ bindings that prove_ the top-level goal.
Consider a small `rule base`_ to construct programs to transfer money between
bank accounts. Each *from_acct* and *to_acct* takes one of two forms:
#. (name, account_type)
- This is a local account with this bank.
- Example: ('bruce', 'checking').
#. (bank, name, account_type)
- This is a foreign account with another bank.
- Example: ('my_other_bank', 'bruce', 'checking').
At least one of the bank accounts must be a local account.
Here's the example rule base::
2 use transfer($from_acct, $to_acct) taking (amount)
9 use transfer($from_acct, $to_acct) taking (amount)
11 transfer_ach($from_acct, $to_acct)
14 use withdraw(($who, $acct_type)) taking (amount)
16 print("withdraw", amount, "from", $who, $acct_type)
18 use deposit(($who, $acct_type)) taking (amount)
20 print("deposit", amount, "to", $who, $acct_type)
22 use transfer_ach($from_acct, ($bank, $who, $acct_type)) taking (amount)
26 deposit((central_accts, ach_send_acct))
29 print("send", amount, "to bank", $bank, "acct", $who, $acct_type)
31 use transfer_ach($from_acct, $to_acct) taking (amount)
35 withdraw((central_accts, ach_recv_acct))
40 use get_ach(($bank, $who, $acct_type)) taking (amount)
42 print("get", amount, "from bank", $bank, "acct", $who, $acct_type)
How the Plan Functions are Generated for This Example
Each of these rules_ will have a plan function generated for it. These plan
functions are generated with the same name as the rule_ name. Thus, the
name of the generated Python plan function for the first rule would be
The plan function generated for the first rule consists of two lines taken
from lines 5 and 7 of this example. The $$ in each of these lines will
be expanded to the subordinate plan function returned from the proof of
"withdraw($from_acct)" and "deposit($to_acct)" respectfully. The generated
plan function will be defined to take an "amount" parameter because of the
*taking* clause on line 2. This parameter is passed on to each of the
subordinate plan functions in lines 5 and 7.
The plan function generated for the "withdraw" rule on line 13 will have
the single line taken from line 16 in the *with* clause. The "$who" and
"$acct_type" `pattern variables`_ will be expanded to constant values taken
from the values bound to these `pattern variables`_ after the top-level
(transfer) goal has been proven.
Finally, the plan function generated for the "transfer_ach1" rule on line
21 will have three lines: two from the *when* clause (lines 25 and 27)
followed by one from the *with* clause (line 29). These lines will be
generated at the same indent level in the plan function even though they
are at different indent levels in the `.krb file`_.
For more detailed information about the options available for plans in the
`.krb file`_, see `Bc_rule Syntax`_.
Running the Example
.. This code is hidden. It will add '' to sys.path, change to the doc.examples
directory and store the directory path in __file__ for the code section
>>> import sys
>>> if '' not in sys.path: sys.path.insert(0, '')
>>> import os
>>> __file__ = os.getcwd()
The plan is created as a byproduct of proving_ the goal:
>>> from pyke import knowledge_engine
>>> engine = knowledge_engine.engine(__file__)
>>> no_vars, plan1 = \
... 'plan_example.transfer((bruce, checking), (bruce, savings))')
``plan1`` is now a program to transfer X amount
from 'bruce', 'checking' to 'bruce', 'savings'. Using the above rule names
as function names, plan1 looks like this:
.. figure:: ../images/plan1.png
And can be called like a standard function, passing the parameters
specified in the *taking* clause of the rules for the top-level goal
withdraw 100 from bruce checking
deposit 100 to bruce savings
The program may be used multiple times:
withdraw 50 from bruce checking
deposit 50 to bruce savings
Notice the strings: ``bruce``, ``checking`` and ``savings`` in the output.
These were specified as `pattern variables`_ in the code and are cooked_
into the plan along with the function call graph.
Let's create a second program:
>>> no_vars, plan2 = \
... 'plan_example.transfer((bruce, checking), '
... '(my_other_bank, bruce, savings))')
``plan2`` is now a program to transfer X amount
from 'my_other_bank', 'bruce', 'checking' to 'bruce', 'savings'.
Plan2 looks like this:
.. figure:: ../images/plan2.png
And is run just like plan1, but produces different results:
withdraw 200 from bruce checking
deposit 200 to central_accts ach_send_acct
send 200 to bank my_other_bank acct bruce savings
And the final use case:
>>> no_vars, plan3 = \
... 'plan_example.transfer((my_other_bank, bruce, checking), '
... '(bruce, savings))')
get 150 from bank my_other_bank acct bruce checking
withdraw 150 from central_accts ach_recv_acct
deposit 150 to bruce savings
Plan3 looks like this:
.. figure:: ../images/plan3.png
Note that the same *transfer2* function is calling two different functions
(*transfer_ach1* and *transfer_ach2*) in plan2 and plan3. This shows how
different functions may be chosen based on the rule_ inferencing. Also
note that after the generation of plan3, plan2 is still valid; both may
still be called successfully, resulting in different calls from the initial
So you can see that it quite easy to use Pyke to automatically combine
Python functions into programs!
It also allows data within each Python function to be specified using a
`pattern variable`_ so that Pyke can customize these values to match the
If you would like to know more about how Pyke *cooks* (or customizes) your
Python functions, see `Cooking Functions`_.