[4ca57f]: doc / html / examples.html  Maximize  Restore  History

Download this file

191 lines (181 with data), 9.9 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
<!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="overview/index.html">Overview</a></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 class="normal-nav"><a href="examples.html">Examples</a></div>
<div class="normal-nav"><a href="logic_tricks.html">Logic Tricks</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"><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="examples">
<h1 class="title">Examples</h1>
<p>Several examples are included to help you become familiar with pyke. If you
did an svn checkout, these should all be included in the <tt class="docutils literal"><span class="pre">examples</span></tt> directory.
If you used easy_install for pyke, you'll need to download the pyke_examples
tar file separately <a class="reference" href="http://sourceforge.net/project/showfiles.php?group_id=207724">here</a>.</p>
<p>Each example is in its own sub-directory and has a README file to get you
started. They all have <a class="reference" href="krb_syntax/index.html">.krb files</a> and a python module to run the example
that also demonstrates <a class="reference" href="using_pyke.html">how to call pyke</a> from your python program.</p>
<div class="section">
<h2><a id="family-relations" name="family-relations">family_relations</a></h2>
<p>This is a good basic example to start with.</p>
<p>The family_relations example takes an initial set of <a class="reference" href="overview/knowledge_bases/fact_bases.html#facts">facts</a> about people:</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" href="overview/rules/forward_chaining.html">Forward-chaining</a> only</li>
<li><a class="reference" href="overview/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" href="overview/plans.html">plans</a> added
too.</li>
</ul>
<p>The test.py program also demonstrates how to use <a class="reference" href="using_pyke.html#krb-traceback">krb_traceback</a> and the
<a class="reference" href="using_pyke.html#other-functions">print_stats</a> function.</p>
</div>
<div class="section">
<h2><a id="knapsack" name="knapsack">knapsack</a></h2>
<p>At the <a class="reference" href="http://us.pycon.org/2008/about/">PyCon 2008</a> conference, somebody asked about the <a class="reference" href="http://en.wikipedia.org/wiki/Knapsack_problem">knapsack problem</a>.
We found a solution in prolog <a class="reference" 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">
<h2><a id="sqlgen" name="sqlgen">sqlgen</a></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 in the top-level tuple are
expected to produce 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 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 table A through it's link to table A. Thus, the pyke rules will
automatically figure out the table joins.</p>
<p>The program automatically introspects the schema information. For this
example, it assumes that <cite>id</cite> is the primary key for each table, and that
when one table links to another, it uses the target table name suffixed with
<cite>_id</cite> as the column name.</p>
<p>This example uses <a class="reference" href="http://www.mysql.com/">MySQL</a> and includes the .sql files to create the database,
tables, and example data. It lacks more general capabilities that would be
required for real use, but may serve as a starting point for something more
complete.</p>
<p>This example also has much more elaborate rules than the prior two examples.</p>
</div>
<div class="section">
<h2><a id="web-framework" name="web-framework">web_framework</a></h2>
<p>This example completes the web framework demo by adding rules to automatically
generate code to render <a class="reference" href="http://py-templates.sourceforge.net/htmltemplate/index.html">HTMLTemplates</a>. This uses the sqlgen example, above,
to generate the SQL statements.</p>
<p>The HTMLTemplates do not include anything resembling program code in them, 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 &quot;cooked&quot; code, custom built for that
schema and HTML template. This should run 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>The demo is packaged as a <a class="reference" href="http://www.python.org/dev/peps/pep-0333/">WSGI</a> application and also demonstrates the use of
multiple <a class="reference" href="overview/knowledge_bases/rule_bases.html">rule bases</a> by using the sqlgen example above.</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="overview/index.html">Overview</a><p>The big picture of
what all this knowledge-base, inferencing and automatic program
generation stuff means and how it works.</p>
</div>
<div class="right-item"><a href="installing_pyke.html">Installing Pyke</a><p>System Requirements and installing pyke.</p>
</div>
<div class="right-item"><a href="using_pyke.html">Using Pyke</a><p>How your python program uses pyke.</p>
</div>
<div class="right-item"><a href="krb_syntax/index.html">KRB Syntax</a><p>Syntax of the knowledge rule base (KRB) files, which is where you
write your rules.</p>
</div>
<div class="right-item"><a href="examples.html">Examples</a><p>This gives an overview of the examples provided with pyke.</p>
</div>
<div class="right-item"><a href="logic_tricks.html">Logic Tricks</a><p>How to accomplish different logic operations with rules.</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 Bruce Frederiksen
</td>
</tr>
</tfoot>
</table>
<div id="last-modified">
Page last modified Sun, Jun 08 2008.
</div>
</body>
</html>

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

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks