[05a128]: doc / source / overview / plans.txt Maximize Restore History

Download this file

plans.txt    197 lines (158 with data), 6.8 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
.. $Id$
..
.. Copyright Š 2007 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: 3
/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 the
backward-chaining_ rules_. These functions are written at the end of each
rule_ in the `.krb file`_. They don't affect how the rules_ run, 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
Running the Example
========================
The plan is created as a byproduct of proving_ the goal:
>>> import pyke
>>> pyke.load('examples')
>>> pyke.activate('plan_example')
>>> no_vars, plan1 = pyke.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'.
>>> 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 another program:
>>> no_vars, plan2 = pyke.prove_1('plan_example', 'transfer',
... (('my_other_bank', 'bruce', 'checking'),
... ('bruce', 'savings')),
... 0)
``plan2`` is now a program to transfer X amount
from 'my_other_bank', 'bruce', 'checking' to 'bruce', 'savings'.
>>> plan2(150)
get 150 from bank my_other_bank acct bruce checking
withdraw 150 from central_accts ach_recv_acct
deposit 150 to bruce savings
And the final use case:
>>> no_vars, plan3 = pyke.prove_1('plan_example', 'transfer',
... (('bruce', 'checking'),
... ('my_other_bank', 'bruce', 'savings')),
... 0)
>>> plan3(200)
withdraw 200 from bruce checking
deposit 200 to central_accts ach_send_acct
send 200 to bank my_other_bank acct bruce savings
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.
.. _backward-chaining: rules/backward_chaining.html
.. _.krb file: ../krb_syntax/index.html
.. _pattern: ../krb_syntax/pattern.html
.. _patterns: pattern_
.. _pattern variable: patterns_
.. _pattern variables: `pattern variable`_
.. _prove: ../using_pyke.html#proving-goals
.. _proving: prove_
.. _rule: rules/index.html
.. _rules: rule_
.. _rule base: knowledge_bases/rule_bases.html