[e6e990]: doc / html / PyCon2008-paper.html Maximize Restore History

Download this file

PyCon2008-paper.html    679 lines (664 with data), 35.5 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
<!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>Applying Expert System Technology to Code Reuse with Pyke</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>PyCon 2008 Paper</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="normal-nav"><a href="about_pyke/index.html">About Pyke</a></div>
<div class="normal-nav"><a href="logic_programming/index.html">Logic Programming</a></div>
<div class="normal-nav"><a href="knowledge_bases/index.html">Knowledge Bases</a></div>
<div class="normal-nav"><a href="pyke_syntax/index.html">Pyke Syntax</a></div>
<div class="normal-nav"><a href="using_pyke.html">Using Pyke</a></div>
<div class="normal-nav"><a href="examples.html">Examples</a></div>
<div class="normal-nav"><a href="PyCon2008-paper.html">PyCon 2008 Paper</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/projects/pyke">
<img src="http://sflogo.sourceforge.net/sflogo.php?group_id=207724&amp;type=14"
width="150" height="40"
alt="Get Python Knowledge Engine (PyKE) at SourceForge.net. Fast, secure and Free Open Source software downloads" /></a>
</div>
</td>
<td id="main-td">
<div id="main">
<a name="startcontent" id="startcontent"></a>
<div class="document" id="applying-expert-system-technology-to-code-reuse-with-pyke">
<h1 class="title">Applying Expert System Technology to Code Reuse with Pyke</h1>
<h2 class="subtitle" id="pycon-2008-chicago">PyCon 2008, Chicago</h2>
<table class="docinfo" frame="void" rules="none">
<col class="docinfo-name" />
<col class="docinfo-content" />
<tbody valign="top">
<tr><th class="docinfo-name">Author:</th>
<td>Bruce Frederiksen</td></tr>
<tr><th class="docinfo-name">Date:</th>
<td>Fri, 14 Mar 2008</td></tr>
<tr class="field"><th class="docinfo-name">Web:</th><td class="field-body">pyke.sourceforge.net</td>
</tr>
<tr><th class="docinfo-name">Copyright:</th>
<td>�� 2008, Bruce Frederiksen</td></tr>
</tbody>
</table>
<div class="section" id="abstract">
<h1><a class="toc-backref" href="#id2">Abstract</a></h1>
<p>This paper explores a new approach to code reuse using a backward-chaining
rule-based system, similar to prolog, to generate a function call graph <em>before</em>
the functions are called. This is compared with current solutions which build
the call graph <em>as</em> the functions are called.</p>
<p>This approach is introduced through an open source project called Pyke (Python
Knowledge Engine).</p>
<p>Finally, the initial results show that the utility of this approach far
exceeds expectations; leading to something more akin to automatic
programming rather than adaptable libraries. A call for help is given to
explore the capabilities of this approach across different domains.</p>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#abstract" id="id2">Abstract</a></li>
<li><a class="reference internal" href="#the-thinking-that-led-to-pyke" id="id3">The Thinking that Led to Pyke</a><ul>
<li><a class="reference internal" href="#the-need-for-code-reuse" id="id4">The Need for Code Reuse</a></li>
<li><a class="reference internal" href="#what-is-code-reuse" id="id5">What is Code Reuse?</a><ul>
<li><a class="reference internal" href="#example-1" id="id6">Example 1</a></li>
<li><a class="reference internal" href="#current-solutions" id="id7">Current Solutions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#current-solution-limitations" id="id8">Current Solution Limitations</a><ul>
<li><a class="reference internal" href="#example-2" id="id9">Example 2</a></li>
<li><a class="reference internal" href="#what-s-needed" id="id10">What's Needed</a></li>
</ul>
</li>
<li><a class="reference internal" href="#applying-backward-chaining-to-code-reuse" id="id11">Applying Backward-Chaining to Code Reuse</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pyke" id="id12">Pyke</a><ul>
<li><a class="reference internal" href="#pyke-krb-syntax" id="id13">Pyke KRB Syntax</a></li>
<li><a class="reference internal" href="#attaching-python-functions-to-backward-chaining-rules" id="id14">Attaching Python Functions to Backward-Chaining Rules</a><ul>
<li><a class="reference internal" href="#calling-subordinate-plans" id="id15">Calling Subordinate Plans</a></li>
<li><a class="reference internal" href="#some-final-points-about-plans" id="id16">Some Final Points about Plans</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-capabilities" id="id17">Other Capabilities</a></li>
</ul>
</li>
<li><a class="reference internal" href="#initial-results" id="id18">Initial Results</a><ul>
<li><a class="reference internal" href="#code-reuse-through-automatic-programming" id="id19">Code Reuse through Automatic Programming</a></li>
</ul>
</li>
<li><a class="reference internal" href="#going-forward" id="id20">Going Forward</a></li>
</ul>
</div>
</div>
<div class="section" id="the-thinking-that-led-to-pyke">
<h1><a class="toc-backref" href="#id3">The Thinking that Led to Pyke</a></h1>
<div class="section" id="the-need-for-code-reuse">
<h2><a class="toc-backref" href="#id4">The Need for Code Reuse</a></h2>
<p>At one of my contracting jobs, they had many clients running essentially the
same program, but each client needed minor code modifications. Their objective
was to maximize code reuse.</p>
</div>
<div class="section" id="what-is-code-reuse">
<h2><a class="toc-backref" href="#id5">What is Code Reuse?</a></h2>
<p>The first question is what does &quot;code reuse&quot; mean? And the answer that seems
most logical is <em>function</em> reuse. Where code modifications are required, a
new function can be created incorporating those modifications.</p>
<p>Then the remaining task is to bring the proper collection of functions
together for each client.</p>
<p>This gets more complicated as several versions of many functions will be
produced for various clients that are all available for reuse by the next
client. So it's not simply the case that there will be one standard default
version of each function, and then several one-off customized versions that
each only apply to a single client.</p>
<p>The result of this function combination exercise is a function call graph.</p>
<div class="section" id="example-1">
<h3><a class="toc-backref" href="#id6">Example 1</a></h3>
<p>Let us imagine that we start out with two functions for client1:</p>
<blockquote>
<img alt="images/PyCon2008/client1.png" src="images/PyCon2008/client1.png" style="width: 200.4px; height: 251.4px;" />
</blockquote>
<p>And then client2 comes along.</p>
<p>Let us first suppose that we need a new version of function A, but can reuse
function B<sub>1</sub>:</p>
<blockquote>
<img alt="images/PyCon2008/client2b.png" src="images/PyCon2008/client2b.png" style="width: 232.2px; height: 251.4px;" />
</blockquote>
<p>This is easy in any programming language and leads naturally to the idea that
the functions to reuse are the lower-level ones, which can be placed into
libraries.</p>
<p>But now let us suppose the opposite; that we need a new version of function B,
but can reuse function A<sub>1</sub>:</p>
<blockquote>
<img alt="images/PyCon2008/client2d.png" src="images/PyCon2008/client2d.png" style="width: 232.2px; height: 250.2px;" />
</blockquote>
<p>This is where we need help.</p>
</div>
<div class="section" id="current-solutions">
<h3><a class="toc-backref" href="#id7">Current Solutions</a></h3>
<p>The current solutions are all run-time solutions that trap the call from
function A<sub>1</sub> to some function B and figure out which function B to use
when the call is made. For example:</p>
<ul class="simple">
<li>O-O Dynamic Binding</li>
<li>Zope Adapters</li>
<li>Generic Functions</li>
</ul>
</div>
</div>
<div class="section" id="current-solution-limitations">
<h2><a class="toc-backref" href="#id8">Current Solution Limitations</a></h2>
<p>These solutions are all limited for the same reason. Let's look at another
example to see why.</p>
<div class="section" id="example-2">
<h3><a class="toc-backref" href="#id9">Example 2</a></h3>
<p>Real world programs have many more than two functions, but we can start to see
the limitations of the current solutions by looking at a three function
example.</p>
<p>We start with one client and three functions.</p>
<p>When client2 was added, it could only share function A<sub>1</sub> and had to
have a new B (B<sub>2</sub>) that needs a new function with a different call
interface than C, so we'll call it D<sub>1</sub>.</p>
<p>Then along comes client3. This time things are looking up, because all we
need is a new version of function D:</p>
<blockquote>
<img alt="images/PyCon2008/client3d.png" src="images/PyCon2008/client3d.png" style="width: 363.6px; height: 369.0px;" />
</blockquote>
<p>Now let's see what happens when we want to call the program for client3. We
know we need to start with function A<sub>1</sub>, since there is only version of
function A:</p>
<blockquote>
<img alt="images/PyCon2008/client3e.png" src="images/PyCon2008/client3e.png" style="width: 363.6px; height: 368.4px;" />
</blockquote>
<p>But at this point we have two choices for function B. All we know for client3
is that we're supposed to use function D<sub>2</sub>, so we're left to guess about
function B. So we try the first one, function B<sub>1</sub>:</p>
<blockquote>
<img alt="images/PyCon2008/client3f2.png" src="images/PyCon2008/client3f2.png" style="width: 363.6px; height: 368.4px;" />
</blockquote>
<p>It's not until function B<sub>1</sub> tries to call some function C that we
discover a problem.</p>
<p>This is where the current solutions break down.</p>
<p>Certainly for this example, it is easy to imagine a developer telling the
binding system: oh yea and client3 is going to have to use function B<sub>2</sub>
as well. But more realistic call graphs are much more complicated than this;
so the developer would have to specify which functions to use going back many
levels.</p>
<p>And then when there is a change in these upper level shared functions later on,
it will affect the call graphs for many clients.</p>
<p>So the current solutions don't scale well.</p>
<p>Continuing on with our example; what we need to do at this point is back up
and try the other B function:</p>
<blockquote>
<img alt="images/PyCon2008/client3g.png" src="images/PyCon2008/client3g.png" style="width: 363.6px; height: 368.4px;" />
</blockquote>
<p>After doing this, we discover the solution for the final call graph:</p>
<blockquote>
<img alt="images/PyCon2008/client3h.png" src="images/PyCon2008/client3h.png" style="width: 363.6px; height: 368.4px;" />
</blockquote>
</div>
<div class="section" id="what-s-needed">
<h3><a class="toc-backref" href="#id10">What's Needed</a></h3>
<p>By looking at this example, we discover two things about how to solve
this problem:</p>
<ol class="arabic">
<li><p class="first">Do function selection <strong>prior</strong> to calling any of the functions.</p>
<p>We can't wait until one function calls another to figure out what to do,
because we may change our minds!</p>
</li>
<li><p class="first">Use a standard backward-chaining rule-based algorithm.</p>
<p>The process of first trying function B<sub>1</sub>, then backing up and trying
function B<sub>2</sub> is exactly the process used in backward-chaining
rule-based systems like prolog. They call it <em>backtracking</em>.</p>
</li>
</ol>
</div>
</div>
<div class="section" id="applying-backward-chaining-to-code-reuse">
<h2><a class="toc-backref" href="#id11">Applying Backward-Chaining to Code Reuse</a></h2>
<p>The next question is how do we use a backward-chaining system to produce
function call graphs?</p>
<p>Let's examine, conceptually, what a set of backward-chaining rules would look
like to find a solution to this problem. Then we can determine how to turn
this into a function call graph.</p>
<p>The following diagram shows <em>goals</em> as dotted line boxes around the <em>rules</em>
that prove that goal. In this example, some goals only have one rule and some
have two.</p>
<p>We also see how <em>rules</em> link to other <em>goals</em>. For example, rule <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B1</span></tt> and
rule <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B2</span></tt> both prove the same goal: <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">B</span></tt>. But <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B1</span></tt> links to
the <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">C</span></tt> goal, while <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B2</span></tt> links to <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">D</span></tt>.</p>
<blockquote>
<img alt="images/PyCon2008/bc_rules2.png" src="images/PyCon2008/bc_rules2.png" style="width: 381.0px; height: 278.4px;" />
</blockquote>
<p>Now we can follow how these rules would be run by the knowledge engine:</p>
<ul class="simple">
<li>The whole process is kicked off by asking the knowledge engine for a
solution to <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">A</span></tt>.</li>
<li>There is only one rule for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">A</span></tt>: <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">A1</span></tt>, so the knowledge engine
tries this rule.</li>
<li><tt class="docutils literal"><span class="pre">Use</span> <span class="pre">A1</span></tt> needs a solution to <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">B</span></tt>.</li>
<li>The knowledge engine tries the first rule for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">B</span></tt>: <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B1</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B1</span></tt> needs a solution to <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">C</span></tt>.</li>
<li>The knowledge engine tries the only rule for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">C</span></tt>:
<tt class="docutils literal"><span class="pre">Use</span> <span class="pre">C1</span></tt>, which fails for client3!</li>
</ul>
<p>The situation now looks like:</p>
<blockquote>
<img alt="images/PyCon2008/bc_rules5.png" src="images/PyCon2008/bc_rules5.png" style="width: 381.0px; height: 278.4px;" />
</blockquote>
<p>Continuing on:</p>
<ul class="simple">
<li>Since there are no other rules for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">C</span></tt>, the <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">C</span></tt> goal fails.</li>
<li>Which means that the <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B1</span></tt> rule fails.</li>
<li>So the knowledge engine tries the next rule for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">B</span></tt>: <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B2</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">Use</span> <span class="pre">B2</span></tt> needs a solution for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">D</span></tt>.</li>
<li>The knowledge engine tries the first rule for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">D</span></tt>: <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">D1</span></tt>,
which fails for client3.</li>
<li>The knowledge engine tries the next rule for <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">D</span></tt>: <tt class="docutils literal"><span class="pre">Use</span> <span class="pre">D2</span></tt>,
which succeeds for client3!</li>
<li>The <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">D</span></tt> goal succeeds.</li>
<li>The <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">B</span></tt> goal succeeds.</li>
<li>And the <tt class="docutils literal"><span class="pre">Find</span> <span class="pre">A</span></tt> goal succeeds.</li>
</ul>
<p>When we achieve final success, we have the following situation:</p>
<blockquote>
<img alt="images/PyCon2008/bc_rules8.png" src="images/PyCon2008/bc_rules8.png" style="width: 381.0px; height: 278.4px;" />
</blockquote>
<p>What remains is to translate this into a function call graph.</p>
<p>It becomes obvious that we want to attach our python functions directly to the
backward-chaining rules:</p>
<blockquote>
<img alt="images/PyCon2008/bc_rules9.png" src="images/PyCon2008/bc_rules9.png" style="width: 426.0px; height: 304.2px;" />
</blockquote>
</div>
</div>
<div class="section" id="pyke">
<h1><a class="toc-backref" href="#id12">Pyke</a></h1>
<div class="section" id="pyke-krb-syntax">
<h2><a class="toc-backref" href="#id13">Pyke KRB Syntax</a></h2>
<p>How does all of this look in Pyke?</p>
<p>Pyke has its own language for rules, which it compiles into python source
modules and then imports. This gives a performance boost by circumventing
nearly all of the inference engine interpretation logic. It also makes it
very easy to embed short python code snippets directly within the rules to
help out with the inferencing. This keeps the inference mechanism simpler as
it does not have to deal with things that are already easy in a procedural
language (like arithmetic and simple list manipulation).</p>
<p>The Pyke rule source files are called <em>knowledge rule bases</em> and have a
<tt class="docutils literal"><span class="pre">.krb</span></tt> suffix.</p>
<p>We'll continue with the previous example here.</p>
<p>First, let's look at the rules before we attach the python functions.
Here's three of the rules:</p>
<pre class="literal-block">
use_B2
use find_B($client)
when
check_function($client, B, 2)
find_D($client)
use_D1
use find_D($client)
when
check_function($client, D, 1)
use_D2
use find_D($client)
when
check_function($client, D, 2)
</pre>
<p>Note that Pyke uses a <tt class="docutils literal"><span class="pre">$</span></tt> to indicate pattern variables (anonymous pattern
variables start with <tt class="docutils literal"><span class="pre">$_</span></tt>).</p>
<p>The <tt class="docutils literal"><span class="pre">check_function</span></tt> goal checks to see what version of the indicated
function should be used for this client. If this is the incorrect version,
it fails. If there is no indication for this function, it succeeds to
allow guessing.</p>
</div>
<div class="section" id="attaching-python-functions-to-backward-chaining-rules">
<h2><a class="toc-backref" href="#id14">Attaching Python Functions to Backward-Chaining Rules</a></h2>
<p>Here are the last two rules with the python code added. The rules have the
python function attached to them so that the function can be returned from
the goal as an additional parameter. Because this parameter does not affect
the inferencing process, it is a hidden parameter.</p>
<p>These examples just show one line of python code, but you may have as many
lines as you want:</p>
<pre class="literal-block">
use_D1
use find_D($client)
when
check_function($client, D, 1)
with
print &quot;D1&quot;
use_D2
use find_D($client)
when
check_function($client, D, 2)
with
print &quot;D2&quot;
</pre>
<p>Pyke calls the function call graphs <em>plans</em>. This terms applies to both the
final top-level call graph, as well as intermediate call graphs.</p>
<div class="section" id="calling-subordinate-plans">
<h3><a class="toc-backref" href="#id15">Calling Subordinate Plans</a></h3>
<p>Now we do the same thing to add python code to the <tt class="docutils literal"><span class="pre">use_B2</span></tt> rule:</p>
<pre class="literal-block">
use_B2
use find_B($client)
when
check_function($client, B, 2)
find_D($client)
with
print &quot;B2&quot;
</pre>
<p>We have code for the B<sub>2</sub> function, but how does it call the plan
returned from the <tt class="docutils literal"><span class="pre">find_D</span></tt> goal?</p>
<p>The most common way is:</p>
<pre class="literal-block">
use_B2
use find_B($client)
when
check_function($client, B, 2)
find_D($client)
$$()
with
print &quot;B2&quot;
</pre>
<p>In general, there may be many goals in the <tt class="docutils literal"><span class="pre">when</span></tt> clause that produce plans.
Each would have an indented line of python code under it with <tt class="docutils literal"><span class="pre">$$</span></tt>
indicating the subordinate function. These indented lines are combined with
the lines in the <tt class="docutils literal"><span class="pre">with</span></tt> clause to form the complete python function for this
rule (with the differences in indenting levels corrected).</p>
<p>But in this case, this would mean that <tt class="docutils literal"><span class="pre">print</span> <span class="pre">&quot;Dx&quot;</span></tt> would be executed before
<tt class="docutils literal"><span class="pre">print</span> <span class="pre">&quot;B2&quot;</span></tt>, which seems backwards.</p>
<p>To call the subordinate plan within the <tt class="docutils literal"><span class="pre">with</span></tt> clause, there is an alternate
mechanism:</p>
<pre class="literal-block">
use_B2
use find_B($client)
when
check_function($client, B, 2)
find_D($client) as $d
with
print &quot;B2&quot;
$d()
</pre>
<p>The <tt class="docutils literal"><span class="pre">as</span> <span class="pre">$d</span></tt> clause stores the plan function in pattern variable <tt class="docutils literal"><span class="pre">$d</span></tt> rather
than adding a call to it to the <tt class="docutils literal"><span class="pre">with</span></tt> clause. Then you can decide in the
<tt class="docutils literal"><span class="pre">with</span></tt> clause whether to call it, when to call it, how many times to call it,
etc.</p>
<p>Note that pattern variables in general can be used within the python code.
These are replaced by their final bound values (as constants) after the
top-level goal has been proven. Thus, the rules can also be used to determine
and set constant values within the plan functions to further customize the
code. This is the reason that the code for the attached python functions is
placed directly in the .krb file rather than in a separate python module.</p>
</div>
<div class="section" id="some-final-points-about-plans">
<h3><a class="toc-backref" href="#id16">Some Final Points about Plans</a></h3>
<ul>
<li><p class="first">Function parameters are specified at the end of the <tt class="docutils literal"><span class="pre">use</span></tt> clause with an
optional <tt class="docutils literal"><span class="pre">taking</span></tt> clause:</p>
<pre class="literal-block">
use_B2
use find_B($client) taking (a, b = None)
...
</pre>
</li>
<li><p class="first">A completed plan appears as a normal python function.</p>
</li>
<li><p class="first">Plans may be pickled and reused.</p>
<ul class="simple">
<li>If you add functools.partial to copy_reg.</li>
</ul>
</li>
<li><p class="first">You don't need to import all of Pyke to unpickle and run a plan.</p>
<ul class="simple">
<li>Only one small Pyke module is needed.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="other-capabilities">
<h2><a class="toc-backref" href="#id17">Other Capabilities</a></h2>
<ul>
<li><p class="first">Pyke also supports forward-chaining rules:</p>
<pre class="literal-block">
fc_rule_name
foreach
fact_base_name.fact_name(pattern...)
...
assert
fact_base_name.fact_name(pattern...)
...
</pre>
<ul class="simple">
<li>Pyke runs all of the forward-chaining rules whose <tt class="docutils literal"><span class="pre">foreach</span></tt> clause
succeeds prior to running any backward-chaining rules. Thus,
forward-chaining rules can not call backward-chaining rules and vice versa.
But backward-chaining rules <em>can</em> examine facts asserted by
forward-chaining rules.</li>
</ul>
</li>
<li><p class="first">There are different kinds of knowledge bases:</p>
<ul class="simple">
<li>Fact Bases:<ul>
<li>simply store facts.</li>
</ul>
</li>
<li>Rule Bases:<ul>
<li>store both forward-chaining and backward-chaining rules.</li>
<li>can use rule base inheritance to inherit, and build upon, the rules from
another rule base.<ul>
<li>But only single inheritance.</li>
<li>Thus each rule base has a unique root rule base.</li>
<li>All rule bases that share the same root form a <em>rule base category</em>.</li>
</ul>
</li>
<li>allow selection of which rule base(s) to use through rule base
<em>activation</em>.<ul>
<li>But only one rule base per rule base category may be active at one time.</li>
</ul>
</li>
</ul>
</li>
<li>Extensibility. You can write your own knowledge bases. These might:<ul>
<li>look up facts in a database</li>
<li>ask users questions</li>
<li>probe hardware/software settings</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="section" id="initial-results">
<h1><a class="toc-backref" href="#id18">Initial Results</a></h1>
<p>After writing Pyke's younger brother, it occurred to me that backward-chaining
could be used to automatically figure out how to join database tables together
and generate SQL statements.</p>
<p>And if the backward-chaining rules could see which substitution variables are
needed by an HTML templating system, it could automatically generate the SQL
to get these data and build the code to update the template.</p>
<p>It seemed that it would no longer be necessary to include anything that
looks like code in the HTML templates. The graphic designers could just add
simple attributes to their tags and the backward-chaining system would figure
out the rest. This would mean that the programmers don't need to modify the
HTML templates, and the graphic designers could maintain full ownership of
the HTML.</p>
<p>I had a WSGI front-end that would simply assert the data passed to it as facts.</p>
<p>The forward-chaining rules took these starting facts, parsed the cookie
information, form information, browser information, and url, determined whether
the user was logged in, figured out which client the request was for,
established all of this as additional facts and activated the appropriate rule
base for this client.</p>
<p>Then the WSGI front-end simply asked for a proof of the <tt class="docutils literal"><span class="pre">process()</span></tt> goal and
executed the resulting plan function which returned the final HTTP status codes
and HTML document.</p>
<p>For a page retrieval (vs. form action), the <tt class="docutils literal"><span class="pre">process</span></tt> goal used two sub-goals:</p>
<ol class="arabic simple">
<li>A <tt class="docutils literal"><span class="pre">format_retrieval</span></tt> goal that read the HTML template, and built a plan
to render the template, given the needed data. This goal also returned a
simple descriptor of this needed data as part of its inferencing.</li>
<li>A <tt class="docutils literal"><span class="pre">retrieve</span></tt> goal then took that descriptor of the needed data, built
the necessary SQL statements, and cooked them into a plan to execute those
statements and return the needed data as a simple dictionary.</li>
</ol>
<p>Then the two sub plans were combined in the reverse order, to first retrieve
the data and then populate the template, for the final plan that went back to
the WSGI front-end.</p>
<p>The Pyke examples/sqlgen and examples/web_framework are simplified examples
that you can look at.</p>
<p>Now, as it turned out, the company had been running without a president for
quite awhile, and had finally hired a new president.</p>
<p>So just as I finished the SQL generation logic to handle unique data (vs.
multi-row data) and was preparing to show some demonstrations; our new
president, coming from a java background and apparently never having heard of
python, decided to cancel the project.</p>
<p>End of contract!</p>
<div class="section" id="code-reuse-through-automatic-programming">
<h2><a class="toc-backref" href="#id19">Code Reuse through Automatic Programming</a></h2>
<p>The fundamental lesson learned was that this technique ends up being far more
capable than what I had first imagined.</p>
<p>More than producing adaptable libraries capable of using B<sub>1</sub> or
B<sub>2</sub> at some point in their call graphs, this approach leads to
something more akin to the back-end of a compiler -- except that the compiler
front-end does not target a textual language that needs to be written and
parsed; but is rather a simple observer of already known facts:</p>
<blockquote>
<p>Show me your schema, and I'll build your SQL statements.</p>
<p>Show me your HTML templates, and I'll build the code to populate them for you.</p>
</blockquote>
<p>This seems to change the whole concept of <em>code reuse</em>; elevating it from the
realm of static <em>libraries</em>, to the realm of dynamic <em>automatic programming</em>.</p>
</div>
</div>
<div class="section" id="going-forward">
<h1><a class="toc-backref" href="#id20">Going Forward</a></h1>
<p>Thinking that others might find this useful, I've re-implemented the underlying
knowledge engine from scratch, with numerous improvements gained from the
experience of the first attempt, and made it open source.</p>
<p>With the backward-chaining rule base system, many applications are possible:</p>
<ul class="simple">
<li>Complicated decision making applications.</li>
<li>Compiler back-ends.<ul>
<li>The .krb compiler uses Pyke.</li>
</ul>
</li>
<li>Automatic SQL statement generation.</li>
<li>Automatic HTML generation/template processing.</li>
<li>The control module for a web framework tool.</li>
<li>Incorporate new custom functions into a large set
of standard functions, which may change the
selection or configuration of standard functions
in other parts of the program.</li>
<li>Automatically re-distribute the modules of a system
over different programs and computers to meet a
wide range of performance and capacity goals.</li>
<li>Diagnosis systems.<ul>
<li>E.g., Automated customer service systems.</li>
</ul>
</li>
<li>Program or library customization for specific uses.</li>
<li>Instantiate, configure, and interconnect networks of objects to meet a
specific need or situation.</li>
</ul>
<p>Up to this point, I've been flying solo. For this project to move forward
to fully explore its capabilities, I'm going to need help!</p>
<p>I'd like to see several early adopters run with this and try it out in different
domains. Pyke is in alpha status now and is ready to start to lean on.</p>
<br />
<a rel="license" href="http://creativecommons.org/licenses/by/3.0/">
<img alt="Creative Commons License" style="border-width:0"
src="http://i.creativecommons.org/l/by/3.0/88x31.png" />
</a><p>This paper is licensed under a <a class="reference external" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported
License</a>.</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="about_pyke/index.html">About Pyke</a><p>What pyke does for you, its features, steps to using pyke and
installation.</p>
</div>
<div class="right-item"><a href="logic_programming/index.html">Logic Programming Tutorial</a><p>A tutorial on logic programming in Pyke, including <em>statements</em>,
<em>pattern matching</em> and <em>rules</em>.</p>
</div>
<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="pyke_syntax/index.html">Pyke Syntax</a><p>The syntax of Pyke's three different kinds of source files.</p>
</div>
<div class="right-item"><a href="using_pyke.html">Using Pyke</a><p>How your Python program uses Pyke. I.e., Pyke's API to Python.</p>
</div>
<div class="right-item"><a href="examples.html">Examples</a><p>An overview of the examples provided with Pyke.</p>
</div>
<div class="right-item"><a href="PyCon2008-paper.html">Applying Expert System Technology to Code Reuse with Pyke</a><p>Paper presented at the PyCon 2008 conference in Chicago.</p>
</div>
</div>
</td>
</tr>
</tbody>
<tfoot id="foot">
<tr id="foot2">
<td id="copyright" colspan="3">
Copyright &copy; 2007-2009 Bruce Frederiksen
</td>
</tr>
</tfoot>
</table>
<div id="last-modified">
Page last modified
Mon, Oct 27 2008.
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ?
"https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost +
"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-6310805-1");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>