Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

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

Download this file

plans.txt    263 lines (206 with data), 9.3 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
.. $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: Plans
page-description:
Explanation of *plans* and automatic program generation.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
initialheaderlevel: 2
/restindex
=============================================
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.
Example
===============
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::
1 transfer1
2 use transfer($from_acct, $to_acct) taking (amount)
3 when
4 withdraw($from_acct)
5 $$(amount)
6 deposit($to_acct)
7 $$(amount)
8 transfer2
9 use transfer($from_acct, $to_acct) taking (amount)
10 when
11 transfer_ach($from_acct, $to_acct)
12 $$(amount)
13 withdraw
14 use withdraw(($who, $acct_type)) taking (amount)
15 with
16 print "withdraw", amount, "from", $who, $acct_type
17 deposit
18 use deposit(($who, $acct_type)) taking (amount)
19 with
20 print "deposit", amount, "to", $who, $acct_type
21 transfer_ach1
22 use transfer_ach($from_acct, ($bank, $who, $acct_type)) taking (amount)
23 when
24 withdraw($from_acct)
25 $$(amount)
26 deposit((central_accts, ach_send_acct))
27 $$(amount)
28 with
29 print "send", amount, "to bank", $bank, "acct", $who, $acct_type
30 transfer_ach2
31 use transfer_ach($from_acct, $to_acct) taking (amount)
32 when
33 get_ach($from_acct)
34 $$(amount)
35 withdraw((central_accts, ach_recv_acct))
36 $$(amount)
37 deposit($to_acct)
38 $$(amount)
39 get_ach
40 use get_ach(($bank, $who, $acct_type)) taking (amount)
41 with
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
"transfer1".
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
========================
The plan is created as a byproduct of proving_ the goal:
>>> from pyke import knowledge_engine
>>> engine = knowledge_engine.engine('examples')
>>> engine.activate('plan_example')
>>> no_vars, plan1 = engine.prove_1('plan_example', 'transfer',
... (('bruce', 'checking'),
... ('bruce', 'savings')),
... 0)
``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
:width: 187
:height: 118
:scale: 100
:align: center
Plan1
And can be called like a standard function, passing the parameters
specified in the *taking* clause of the rules for the top-level goal
(transfer):
>>> plan1(100)
withdraw 100 from bruce checking
deposit 100 to bruce savings
The program may be used multiple times:
>>> plan1(50)
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 = engine.prove_1('plan_example', 'transfer',
... (('bruce', 'checking'),
... ('my_other_bank', 'bruce', 'savings')),
... 0)
``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
:width: 187
:height: 195
:scale: 100
:align: center
Plan2
And is run just like plan1, but produces different results:
>>> plan2(200)
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 = engine.prove_1('plan_example', 'transfer',
... (('my_other_bank', 'bruce', 'checking'),
... ('bruce', 'savings')),
... 0)
>>> plan3(150)
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
:width: 264
:height: 198
:scale: 100
:align: center
Plan3
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
*transfer2* function.
Conclusion
==============
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
specific situation.
If you would like to know more about how Pyke *cooks* (or customizes) your
Python functions, see `Cooking Functions`_.