[381f18]: doc / html / using_pyke / creating_engine.html Maximize Restore History

Download this file

creating_engine.html    242 lines (229 with data), 12.7 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
<!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>Creating an Inference Engine Object</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">Using Pyke</a></li>
<li>&gt;</li>
<li>Creating an Engine</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="title-nav"><a href="index.html">Using Pyke</a></div><div class="nav-branch">
<div class="normal-nav"><a href="creating_engine.html">Creating an Engine</a></div>
<div class="normal-nav"><a href="adding_facts.html">Asserting Facts</a></div>
<div class="normal-nav"><a href="proving_goals.html">Proving Goals</a></div>
<div class="normal-nav"><a href="other_functions.html">Other functions</a></div>
</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="creating-an-inference-engine-object">
<h1 class="title">Creating an Inference Engine Object</h1>
<p>The <tt class="docutils literal">engine</tt> object is your gateway into Pyke. Each engine object manages
multiple <a class="reference external" href="../knowledge_bases/index.html">knowledge bases</a> related to accomplishing some task.</p>
<p>You may create multiple Pyke engines, each with it's own knowledge bases to
accomplish different disconnected tasks.</p>
<p>When you create a Pyke engine object, Pyke scans for Pyke <a class="reference external" href="../pyke_syntax/kfb_syntax.html">.kfb</a>, <a class="reference external" href="../pyke_syntax/krb_syntax/index.html">.krb</a>
and <a class="reference external" href="../pyke_syntax/kqb_syntax.html">.kqb</a> source files and compiles these into .fbc pickle files,
Python .py source files and .qbc pickle files, respectively.</p>
<p>Each time a Pyke engine object is created it checks the file modification
times of the Pyke source files to see whether they need to be recompiled.
If you change a Pyke source file, you may create a new Pyke engine to compile
the changes and run with the new knowledge bases without having to restart
your application.</p>
<p>Pyke also lets you zip these compiled files into Python eggs and can load the
files from the egg. By including the compiled files in your application's
distribution, you don't need to include your Pyke source files if you don't
want to.</p>
<p>Once you have an <tt class="docutils literal">engine</tt> object; generally, all of the Pyke functions that
you need are provided directly by this object:</p>
<!-- this code is hidden and will set __file__ to the doc/examples directory.
>>> import os
>>> __file__ = \
... os.path.join(os.path.dirname(os.path.dirname(os.getcwd())),
... 'examples') -->
<p>knowledge_engine.engine(*paths, **kws)</p>
<blockquote>
<p>Pyke recursively searches for Pyke source files (<a class="reference external" href="../pyke_syntax/kfb_syntax.html">.kfb files</a>,
<a class="reference external" href="../pyke_syntax/krb_syntax/index.html">.krb files</a>, and <a class="reference external" href="../pyke_syntax/kqb_syntax.html">.kqb files</a>) starting at each source directory
indicated in <em>paths</em> and places all of the compiled files in the
associated <em>target packages</em>. This causes all of the <a class="reference external" href="../knowledge_bases/index.html">knowledge bases</a>
to be loaded and made ready to <a class="reference external" href="index.html#getting-started">activate</a>.</p>
<p>Pyke source files may be spread out over multiple directories and may be
compiled into one or more target packages. Multiple target packages
would be used when more than one application wants to share a set of
<a class="reference external" href="../knowledge_bases/index.html">knowledge bases</a>, perhaps adding some of its own knowledge that it
compiles into its own target package.</p>
<p>Each <tt class="docutils literal">path</tt> argument specifies a Pyke source directory and an optional
target package. The source directories do not have to be Python package
directories, but the target packages do.</p>
<p>The target package defaults to <tt class="docutils literal">.compiled_krb</tt>. The leading dot (.)
indicates that the compiled_krb directory will be subordinate to the
lowest Python package directory on the path to the Pyke source directory.
This uses Python's <a class="reference external" href="http://www.python.org/dev/peps/pep-0328/">relative import</a> notation, so multiple dots go up to
higher directories (one per dot). If the target package does not start
with a dot, it is taken to be an absolute package path and will be located
using Python's sys.path like a normal Python <tt class="docutils literal">import</tt>.</p>
<p>The Pyke source directory may be specified as a path (a string) or by
passing a Python module. If a module is passed, its __file__ attribute
is used. If the path points to a file, rather than a directory, the final
filename is discarded. In the simple case, when the Pyke source files are
in the same directory as the module creating the <tt class="docutils literal">engine</tt> object, you
can just pass <tt class="docutils literal">__file__</tt> as the sole argument.</p>
<pre class="doctest-block">
&gt;&gt;&gt; from pyke import knowledge_engine
&gt;&gt;&gt; my_engine = knowledge_engine.engine(__file__)
</pre>
<p>Passing a package instead, this example could also be written:</p>
<pre class="doctest-block">
&gt;&gt;&gt; import doc.examples
&gt;&gt;&gt; my_engine = knowledge_engine.engine(doc.examples)
</pre>
<p>or, you can pass a module within the desired package:</p>
<pre class="doctest-block">
&gt;&gt;&gt; from doc.examples import some_module
&gt;&gt;&gt; my_engine = knowledge_engine.engine(some_module)
</pre>
<p>In the all three cases, the final filename is stripped from the value of
the module's __file__ attribute to get the directory that the package
is in. This directory will then be recursively searched for Pyke source
files.</p>
<p>If you change some of your Pyke source files, you can create a new engine
object to compile and reload the generated Python modules without
restarting your program. But note that you'll need to rerun the
<tt class="docutils literal">add_universal_fact</tt> calls that you made (a reason to use <a class="reference external" href="../pyke_syntax/kfb_syntax.html">.kfb files</a>
instead).</p>
<p>All of the compiled Python .py source files and .fbc/.qbc pickle files
generated from each source directory are placed, by default, in a
<tt class="docutils literal">compiled_krb</tt> target package. You may specify a different target
package for any source directory by passing that source directory
along with the target package name as a 2-tuple. Thus, specifying the
default target package explicitly would look like:</p>
<pre class="doctest-block">
&gt;&gt;&gt; my_engine = knowledge_engine.engine((__file__, '.compiled_krb'))
</pre>
<p>You may specify the same target package for multiple source directories.</p>
<p>The last component of the target package will be created automatically
if it does not already exist.</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">You will probably want to add <tt class="docutils literal">compiled_krb</tt> (or whatever you've
chosen to call it) to your source code repository's list of files to
ignore.</p>
</div>
<p>If you want to distribute your application <em>without</em> the knowledge bases,
you can use the 2-tuple notation with <tt class="docutils literal">None</tt> as the source directory.
In this case, all of the Pyke source files must already be compiled,
and Pyke will simply load these files. Also, the target package must be
specified in absolute form (with no leading dots).</p>
<p>Finally, there are four optional keyword arguments that you may also pass
to the <tt class="docutils literal">engine</tt> constructor. These are all booleans that default to
<tt class="docutils literal">True</tt>:</p>
<ul class="simple">
<li><tt class="docutils literal">load_fb</tt> -- load fact bases</li>
<li><tt class="docutils literal">load_fc</tt> -- load forward-chaining rules</li>
<li><tt class="docutils literal">load_bc</tt> -- load backward-chaining rules and</li>
<li><tt class="docutils literal">load_qb</tt> -- load question bases</li>
</ul>
<p>These parameters must be passed as keyword parameters and let you
selectively load the various kinds of compiled files.</p>
</blockquote>
<!-- ADD_LINKS MARKER -->
</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="creating_engine.html">Creating an Inference Engine Object</a><p>How to create a Pyke <em>inference engine</em> object.</p>
</div>
<div class="right-item"><a href="adding_facts.html">Asserting New Facts</a><p>How to dynamically assert new <em>facts</em> from your Python program.</p>
</div>
<div class="right-item"><a href="proving_goals.html">Proving Goals</a><p>Using Pyke's API to prove goals from your Python program.</p>
</div>
<div class="right-item"><a href="other_functions.html">Other Functions</a><p>Other miscellaneous functions available that you might be interested
in, such as tracing rules and smart error tracebacks that show lines
from your .krb files.</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, Mar 29 2010.
</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>