[d43b93]: doc / html / overview / plans.html  Maximize  Restore  History

Download this file

348 lines (331 with data), 15.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Plans and Automatic Program Generation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../stylesheets/pyke.css" type="text/css" />
</head>
<body>
<table id="page-table">
<thead class="head">
<tr id="header1"><th id="header" colspan="3">
&nbsp;
</th></tr>
<tr id="header2">
<th id="crumb-left"></th>
<th id="crumb-line">
<div id="nav">
<ul>
<li><a href="../index.html">Home</a></li>
<li>&gt;</li>
<li><a href="index.html">Overview</a></li>
<li>&gt;</li>
<li>Plans</li>
</ul>
</div>
</th>
<th id="crumb-right"></th>
</tr>
</thead>
<tbody id="body">
<tr id="body-tr">
<td id="left-nav">
<div id="left-nav-div">
<div class="title-nav"><a href="../index.html">Home</a></div><div class="nav-branch">
<div class="title-nav"><a href="index.html">Overview</a></div><div class="nav-branch">
<div class="normal-nav"><a href="knowledge_bases/index.html">Knowledge Bases</a></div>
<div class="normal-nav"><a href="rules/index.html">Rules</a></div>
<div class="normal-nav"><a href="plans.html">Plans</a></div>
</div>
<div class="normal-nav"><a href="../installing_pyke.html">Installing Pyke</a></div>
<div class="normal-nav"><a href="../using_pyke.html">Using Pyke</a></div>
<div class="normal-nav"><a href="../krb_syntax/index.html">KRB Syntax</a></div>
</div>
</div>
<div id="icons">
<div id="project-page">
<a href="http://sourceforge.net/projects/pyke/">Pyke Project Page</a>
</div>
Please Make a Donation:<br />
<a href="http://sourceforge.net/donate/index.php?group_id=207724">
<img src="http://images.sourceforge.net/images/project-support.jpg"
width="88" height="32" border="0"
alt="Support This Project" /> </a> <br /><br />
Hosted by: <br />
<!-- <a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=207724&amp;type=3" width="125" height="37" border="0" alt="SourceForge.net Logo" /></a> -->
<a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=207724&amp;type=4" width="125" height="37" border="0" alt="SourceForge.net Logo" /></a>
</div>
</td>
<td id="main-td">
<div id="main">
<a name="startcontent" id="startcontent"></a>
<div class="document" id="plans-and-automatic-program-generation">
<h1 class="title">Plans and Automatic Program Generation</h1>
<p>Once you understand how <a class="reference" href="rules/backward_chaining.html">backward-chaining</a> works, it is relatively easy to
do automatic program generation.</p>
<div class="section">
<h3><a id="adding-plans-to-backward-chaining-rules" name="adding-plans-to-backward-chaining-rules">Adding Plans to Backward-Chaining Rules</a></h3>
<p>The way this is done is by attaching python functions to
<a class="reference" href="rules/backward_chaining.html">backward-chaining</a> <a class="reference" href="rules/index.html">rules</a>. These functions are written in the <em>with</em>
clause at the end of each <a class="reference" href="rules/index.html">rule</a> in the <a class="reference" href="../krb_syntax/index.html">.krb file</a>. They don't affect how
the <a class="reference" href="rules/index.html">rules</a> run to prove a goal, but are
gathered up to form a call graph that is returned along with the <a class="reference" href="../krb_syntax/pattern.html#pattern-variable">pattern
variable</a> bindings that <a class="reference" href="../using_pyke.html#proving-goals">prove</a> the top-level goal.</p>
</div>
<div class="section">
<h3><a id="example" name="example">Example</a></h3>
<p>Consider a small <a class="reference" href="knowledge_bases/rule_bases.html">rule base</a> to construct programs to transfer money between
bank accounts. Each <em>from_acct</em> and <em>to_acct</em> takes one of two forms:</p>
<ol class="arabic simple">
<li>(name, account_type)<ul>
<li>This is a local account with this bank.</li>
<li>Example: ('bruce', 'checking').</li>
</ul>
</li>
<li>(bank, name, account_type)<ul>
<li>This is a foreign account with another bank.</li>
<li>Example: ('my_other_bank', 'bruce', 'checking').</li>
</ul>
</li>
</ol>
<p>At least one of the bank accounts must be a local account.</p>
<p>Here's the example rule base:</p>
<pre class="literal-block">
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 &quot;withdraw&quot;, amount, &quot;from&quot;, $who, $acct_type
17 deposit:
18 use deposit(($who, $acct_type)) taking (amount)
19 with
20 print &quot;deposit&quot;, amount, &quot;to&quot;, $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 &quot;send&quot;, amount, &quot;to bank&quot;, $bank, &quot;acct&quot;, $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 &quot;get&quot;, amount, &quot;from bank&quot;, $bank, &quot;acct&quot;, $who, $acct_type
</pre>
<div class="section">
<h4><a id="how-the-plan-functions-are-generated-for-this-example" name="how-the-plan-functions-are-generated-for-this-example">How the Plan Functions are Generated for This Example</a></h4>
<p>Each of these <a class="reference" href="rules/index.html">rules</a> will have a plan function generated for it. These plan
functions are generated with the same name as the <a class="reference" href="rules/index.html">rule</a> name. Thus, the
name of the generated python plan function for the first rule would be
&quot;transfer1&quot;.</p>
<p>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
&quot;withdraw($from_acct)&quot; and &quot;deposit($to_acct)&quot; respectfully. The generated
plan function will be defined to take an &quot;amount&quot; parameter because of the
<em>taking</em> clause on line 2. This parameter is passed on to each of the
subordinate plan functions in lines 5 and 7.</p>
<p>The plan function generated for the &quot;withdraw&quot; rule on line 13 will have
the single line taken from line 16 in the <em>with</em> clause. The &quot;$who&quot; and
&quot;$acct_type&quot; <a class="reference" href="../krb_syntax/pattern.html#pattern-variable">pattern variables</a> will be expanded to constant values taken
from the values bound to these <a class="reference" href="../krb_syntax/pattern.html#pattern-variable">pattern variables</a> after the top-level
(transfer) goal has been proven.</p>
<p>Finally, the plan function generated for the &quot;transfer_ach1&quot; rule on line
21 will have three lines: two from the <em>when</em> clause (lines 25 and 27)
followed by one from the <em>with</em> 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 <a class="reference" href="../krb_syntax/index.html">.krb file</a>.</p>
<p>For more detailed information about the options available for plans in the
<a class="reference" href="../krb_syntax/index.html">.krb file</a>, see <a class="reference" href="../krb_syntax/bc_rule.html">Bc_rule Syntax</a>.</p>
</div>
</div>
<div class="section">
<h3><a id="running-the-example" name="running-the-example">Running the Example</a></h3>
<p>The plan is created as a byproduct of <a class="reference" href="../using_pyke.html#proving-goals">proving</a> the goal:</p>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; import pyke
&gt;&gt;&gt; engine = pyke.engine('examples')
&gt;&gt;&gt; engine.activate('plan_example')
&gt;&gt;&gt; no_vars, plan1 = engine.prove_1('plan_example', 'transfer',
... (('bruce', 'checking'),
... ('bruce', 'savings')),
... 0)
</pre>
</blockquote>
<p><tt class="docutils literal"><span class="pre">plan1</span></tt> 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:</p>
<pre class="literal-block">
+-----------+
| transfer1 |
+-----+-----+
/ \
+----+ +----+
| |
V V
+----------+ +---------+
| withdraw | | deposit |
+----------+ +---------+
</pre>
<p>And can be called like a standard function, passing the parameters
specified in the <em>taking</em> clause of the rules for the top-level goal
(transfer):</p>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; plan1(100)
withdraw 100 from bruce checking
deposit 100 to bruce savings
</pre>
</blockquote>
<p>The program may be used multiple times:</p>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; plan1(50)
withdraw 50 from bruce checking
deposit 50 to bruce savings
</pre>
</blockquote>
<p>Notice the strings: <tt class="docutils literal"><span class="pre">bruce</span></tt>, <tt class="docutils literal"><span class="pre">checking</span></tt> and <tt class="docutils literal"><span class="pre">savings</span></tt> in the output.
These were specified as <a class="reference" href="../krb_syntax/pattern.html#pattern-variable">pattern variables</a> in the code and are cooked
into the plan along with the function call graph.</p>
<p>Let's create a second program:</p>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; no_vars, plan2 = engine.prove_1('plan_example', 'transfer',
... (('bruce', 'checking'),
... ('my_other_bank', 'bruce', 'savings')),
... 0)
</pre>
</blockquote>
<p><tt class="docutils literal"><span class="pre">plan2</span></tt> is now a program to transfer X amount
from 'my_other_bank', 'bruce', 'checking' to 'bruce', 'savings'.
Plan2 looks like this:</p>
<pre class="literal-block">
+-----------+
| transfer2 |
+-----+-----+
|
V
+---------------+
| transfer_ach1 |
+-------+-------+
/ \
+----+ +----+
| |
V V
+----------+ +---------+
| withdraw | | deposit |
+----------+ +---------+
</pre>
<p>And is run just like plan1, but produces different results:</p>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; 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
</pre>
</blockquote>
<p>And the final use case:</p>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; no_vars, plan3 = engine.prove_1('plan_example', 'transfer',
... (('my_other_bank', 'bruce', 'checking'),
... ('bruce', 'savings')),
... 0)
&gt;&gt;&gt; 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
</pre>
</blockquote>
<p>Plan3 looks like this:</p>
<pre class="literal-block">
+-----------+
| transfer2 |
+-----+-----+
|
V
+---------------+
| transfer_ach2 |
+-------+-------+
/|\
+-----------+ | +-----------+
| | |
V V V
+---------+ +----------+ +---------+
| get_ach | | withdraw | | deposit |
+---------+ +----------+ +---------+
</pre>
<p>Note that the same <em>transfer2</em> function is calling two different functions
(<em>transfer_ach1</em> and <em>transfer_ach2</em>) in plan2 and plan3. This shows how
different functions may be chosen based on the <a class="reference" href="rules/index.html">rule</a> 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
<em>transfer2</em> function.</p>
</div>
<div class="section">
<h3><a id="conclusion" name="conclusion">Conclusion</a></h3>
<p>So you can see that it quite easy to use pyke to automatically combine
python functions into programs!</p>
<p>It also allows data within each python function to
be specified using a <a class="reference" href="../krb_syntax/pattern.html#pattern-variable">pattern variable</a> so that pyke can customize these
values to match the specific situation.</p>
</div>
</div>
<!-- <div id="return-to-top">
<a href="#">Return to Top</a>
</div>
-->
</div>
</td>
<td id="right-nav">
<div id="right-nav-div">
<h3>More:</h3>
<div class="right-item"><a href="knowledge_bases/index.html">Knowledge Bases</a><p>Knowledge is made up of both <em>facts</em> and <em>rules</em>. These are gathered
into named repositories called <em>knowledge bases</em>.</p>
</div>
<div class="right-item"><a href="rules/index.html">Rules</a><p>Explanation of rules, forward-chaining and backward-chaining.</p>
</div>
<div class="right-item"><a href="plans.html">Plans and Automatic Program Generation</a><p>Explanation of plans and automatic program generation.</p>
</div>
</div>
</td>
</tr>
</tbody>
<tfoot id="foot">
<tr id="foot2">
<td id="copyright" colspan="3">
Copyright &copy; 2007 Bruce Frederiksen
</td>
</tr>
</tfoot>
</table>
<div id="last-modified">
Page last modified Tue, Feb 12 2008.
</div>
</body>
</html>

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks