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

[9ac085]: doc / html / examples.html Maximize Restore History

Download this file

examples.html    240 lines (228 with data), 13.0 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
<!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>Examples</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>Examples</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="examples">
<h1 class="title">Examples</h1>
<p>Several examples are included to help you become familiar with Pyke. These
are all in an <tt class="docutils literal"><span class="pre">examples</span></tt> directory; which forms the top-level Python package
for importing:</p>
<pre class="literal-block">
&gt;&gt;&gt; from examples.towers_of_hanoi import test
&gt;&gt;&gt; test.test(2)
got 1: ((0, 1), (0, 2), (1, 2))
got 2: ((0, 2), (0, 1), (2, 0), (1, 2), (0, 2))
</pre>
<p>Thus the source directory (containing the <tt class="docutils literal"><span class="pre">examples</span></tt> directory) must be on
your Python path. This is most easily accomplished by running the examples
from the source directory.</p>
<p>Each example is in its own sub-directory and has a README.txt file to get you
started. They all have <a class="reference external" href="pyke_syntax/krb_syntax/index.html">.krb files</a> and a Python module to run the example
that also demonstrates <a class="reference external" href="using_pyke.html">how to call Pyke</a> from your Python program.</p>
<div class="section" id="family-relations">
<h2>Family_relations</h2>
<p>This is a very good basic example to start with.</p>
<p>The family_relations example takes an initial set of <a class="reference external" href="knowledge_bases/fact_bases.html#facts">facts</a> about people
(stated in a <a class="reference external" href="pyke_syntax/kfb_syntax.html">.kfb file</a>):</p>
<pre class="literal-block">
son_of(david_r2, david_r, sarah_r)
daughter_of(shirley, david_r, sarah_r)
</pre>
<p>And figures out how any two people are related:</p>
<pre class="literal-block">
david_r2, shirley are ('brother', 'sister')
</pre>
<p>This same problem is solved in four different ways so that you can compare
them:</p>
<ul class="simple">
<li><a class="reference external" href="logic_programming/rules/forward_chaining.html">Forward-chaining</a> only</li>
<li><a class="reference external" href="logic_programming/rules/backward_chaining.html">Backward-chaining</a> only</li>
<li>Backward-chaining only with a few rule optimizations that make the rules
run 100 times faster!</li>
<li>A mix of forward-chaining and backward-chaining with some use of <a class="reference external" href="logic_programming/plans.html">plans</a> added
too.</li>
</ul>
<p>The test.py program also demonstrates how to use <a class="reference external" href="using_pyke.html#krb-traceback">krb_traceback</a> and the
<a class="reference external" href="using_pyke.html#other-functions">print_stats</a> function.</p>
</div>
<div class="section" id="knapsack">
<h2>Knapsack</h2>
<p>At the <a class="reference external" href="http://us.pycon.org/2008/about/">PyCon 2008</a> conference, somebody asked about the <a class="reference external" href="http://en.wikipedia.org/wiki/Knapsack_problem">knapsack problem</a>.
We found a solution in Prolog <a class="reference external" href="http://www.ise.gmu.edu/~duminda/classes/fall03/set3.ppt">here</a> (starting on page 19), and rewrote it in
Pyke. This is a quick simple little example.</p>
</div>
<div class="section" id="sqlgen">
<h2>Sqlgen</h2>
<p>Pyke was originally developed as the control component for a web framework.
This example shows how Pyke can automatically generate SQL SELECT statements,
given a set of tables that the calling program has keys to and a tuple of the
desired column names. Column names specified at the top-level in this tuple
are expected to have a single value each. Nested tuples are used when
multiple rows are expected. The column names in nested tuples make up the
columns in the result rows.</p>
<p>The top-level goal returns a <a class="reference external" href="logic_programming/plans.html">plan</a> that takes the key values for the initial
set of tables given to the goal and returns an immutable dictionary mapping
the column names to the values retrieved from the database. The plan may be
used repeatedly without re-running the rules each time to figure out the
SELECT statements. Thus, this acts like a SELECT statement compiler resulting
in queries with virtually no extra overhead. It is <em>not</em>, however, an Object
Relational Mapper (ORM).</p>
<p>The data model used for the requested columns is that tables inherit the
columns from tables they link to. So if there is a 1-many relationship
between tables A and B (1 A row has many B rows), the B table inherits the
columns from the A table through it's link to table A. The Pyke rules will
automatically figure out the table joins for this.</p>
<p>The program automatically introspects the schema information. For this
example, it assumes that <tt class="docutils literal"><span class="pre">id</span></tt> is the primary key for each table, and that
when one table links to another, it uses the target table name suffixed with
<tt class="docutils literal"><span class="pre">_id</span></tt> as the column name.</p>
<p>This example was originally done using <a class="reference external" href="http://www.mysql.com/">MySQL</a> and includes the .sql files to
create the database, tables, and example data. The example has since been
converted to use the Sqlite3 database to make it easier to run, as Sqlite3
does not require any setup (the Sqlite3 database file is included in the
example).</p>
<p>Sqlgen lacks more general capabilities that would be required for real use,
but may serve as a starting point for another project that's more complete.</p>
<p>This example also has much more elaborate rules than the prior two examples
and is a very real example of generating <a class="reference external" href="logic_programming/plans.html">plans</a>.</p>
</div>
<div class="section" id="web-framework">
<h2>Web_framework</h2>
<p>This example completes the Python web framework demo by adding rules to
automatically generate code to render HTML templates from the <a class="reference external" href="http://py-templates.sourceforge.net/htmltemplate/index.html">HTMLTemplate</a>
package (you can run <tt class="docutils literal"><span class="pre">easy_install</span> <span class="pre">HTMLTemplate</span></tt> to install the HTMLTemplate
package). This uses the <a class="reference external" href="examples.html#sqlgen">sqlgen</a> example, above, to generate the SQL statements.</p>
<p>An HTMLTemplate does not include anything resembling program code in it, so
that your graphics designers can completely own the html files without the
developers having to modify them in any way.</p>
<p>Note that the code generated here is fully <a class="reference external" href="about_pyke/cooking_functions.html">cooked</a> code, custom built for
that specific schema and HTML template. This runs extremely fast because
there is nothing left at run-time concerning parsing and figuring out the
HTML template, or constructing the SQL statements.</p>
<p>A test was done comparing this web framework example to the same example
done in <a class="reference external" href="http://turbogears.org/2.0/">TurboGears 2</a> running against the same MySQL database. The results
of the <a class="reference external" href="http://www.joedog.org/index/siege-home">siege</a> benchmark tests show that Pyke is just over 10 times faster than
TurboGears 2:</p>
<pre class="literal-block">
- Pyke: 791 trans/sec
- TurboGears 2: 76 trans/sec
</pre>
<p>The demo is packaged as a <a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">WSGI</a> application. It also demonstrates the use of
multiple <a class="reference external" href="knowledge_bases/rule_bases.html">rule bases</a> by using the sqlgen example above, as well as the
caching and reuse of <a class="reference external" href="logic_programming/plans.html">plans</a> to achieve the order of magnitude improvement in
performance over current practice.</p>
<!-- ADD_LINKS MARKER -->
</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
Tue, Nov 03 2009.
</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>