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

[f3d7de]: doc / source / examples.txt Maximize Restore History

Download this file

examples.txt    183 lines (138 with data), 7.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
.. $Id$
..
.. Copyright Š 2008 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: Examples
page-description:
An overview of the examples provided with Pyke.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
initialheaderlevel: 2
/restindex
uservalues
filedate: $Id$
/uservalues
=============================
Examples
=============================
Several examples are included to help you become familiar with Pyke. These
are all in an ``examples`` directory, which forms the top-level Python package
for importing::
>>> from examples.towers_of_hanoi import test
>>> test.test(2)
got 1: ((0, 1), (0, 2), (1, 2))
got 2: ((0, 2), (0, 1), (2, 0), (1, 2), (0, 2))
Thus the directory containing the ``examples`` directory must be on your
Python path.
If you did an hg_ clone, the entire source directory should already be on
your Python path in order to import pyke, so you're all set!
If you used easy_install for Pyke, you'll need to download the pyke_examples
tar file separately here__ and then expand the tar file in a directory on your
Python path.
Each example is in its own sub-directory and has a README file to get you
started. They all have `.krb files`_ and a Python module to run the example
that also demonstrates `how to call Pyke`_ from your Python program.
.. __: http://sourceforge.net/project/showfiles.php?group_id=207724
Family_relations
================
This is a very good basic example to start with.
The family_relations example takes an initial set of facts_ about people
(stated in a `.kfb file`_)::
son_of(david_r2, david_r, sarah_r)
daughter_of(shirley, david_r, sarah_r)
And figures out how any two people are related::
david_r2, shirley are ('brother', 'sister')
This same problem is solved in four different ways so that you can compare
them:
- Forward-chaining_ only
- Backward-chaining_ only
- Backward-chaining only with a few rule optimizations that make the rules
run 100 times faster!
- A mix of forward-chaining and backward-chaining with some use of plans_ added
too.
The test.py program also demonstrates how to use krb_traceback_ and the
print_stats_ function.
Knapsack
========
At the `PyCon 2008`_ conference, somebody asked about the `knapsack problem`_.
We found a solution in Prolog here__ (starting on page 19), and rewrote it in
Pyke. This is a quick simple little example.
.. __: http://www.ise.gmu.edu/~duminda/classes/fall03/set3.ppt
Sqlgen
======
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.
The top-level goal returns a plan_ 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 *not*, however, an Object
Relational Mapper (ORM).
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. The Pyke rules will
automatically figure out the table joins for this.
The program automatically introspects the schema information. For this
example, it assumes that ``id`` is the primary key for each table, and that
when one table links to another, it uses the target table name suffixed with
``_id`` as the column name.
This example was originally done using MySQL_ 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).
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.
This example also has much more elaborate rules than the prior two examples
and is a very real example of generating plans_.
Web_framework
=============
This example completes the Python web framework demo by adding rules to
automatically generate code to render HTML templates from the HTMLTemplates_
package (you can run ``easy_install HTMLTemplate`` to install this package).
This uses the sqlgen_ example, above, to generate the SQL statements.
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.
Note that the code generated here is fully cooked_ 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.
A test was done comparing this web framework example to the same example
done in `TurboGears 2`_ running against the same MySQL database. The results
of the siege_ benchmark tests show that Pyke is just over 10 times faster than
TurboGears 2::
- Pyke: 791 trans/sec
- TurboGears 2: 76 trans/sec
The demo is packaged as a WSGI_ application. It also demonstrates the use of
multiple `rule bases`_ by using the sqlgen example above, as well as the
caching and reuse of plans_ to achieve the order of magnitude improvement in
performance over current practice.