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

[381f18]: doc / html / logic_programming / rules / index.html Maximize Restore History

Download this file

index.html    230 lines (216 with data), 11.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
<!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>Rules</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">Logic Programming</a></li>
<li>&gt;</li>
<li>Rules</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="title-nav"><a href="../index.html">Logic Programming</a></div><div class="nav-branch">
<div class="normal-nav"><a href="../statements.html">Statements</a></div>
<div class="normal-nav"><a href="../pattern_matching/index.html">Pattern Matching</a></div>
<div class="title-nav"><a href="index.html">Rules</a></div><div class="nav-branch">
<div class="normal-nav"><a href="forward_chaining.html">Forward Chaining</a></div>
<div class="normal-nav"><a href="backward_chaining.html">Backward Chaining</a></div>
</div>
<div class="normal-nav"><a href="../plans.html">Plans</a></div>
</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/index.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="rules">
<h1 class="title">Rules</h1>
<p>Conceptually, a rule is very simple:</p>
<pre class="literal-block">
if
A
B
C
then
D
E
</pre>
<p>Meaning, &quot;if A, B <em>and</em> C are true, then D and E are also true&quot;. These are
often called <em>if-then</em> rules.</p>
<div class="admonition-and-what-are-a-b-c-d-and-e admonition">
<p class="first admonition-title">And what are A, B, C, D and E?</p>
<p class="last">They are simply <a class="reference external" href="../statements.html">statements</a>. Specifically, they are generalized statements
containing <a class="reference external" href="../pattern_matching/index.html">patterns</a> as arguments. You'll see more about this later.</p>
</div>
<div class="section" id="premises-and-conclusions">
<h2>Premises and Conclusions</h2>
<p>Rules have two parts to them: an <strong>if</strong> part (containing a list of statements
called the <em>premises</em>), and a <strong>then</strong> part (containing a list of statements
called the <em>conclusions</em>).</p>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Pyke doesn't use the words <strong>if</strong> and <strong>then</strong>, as you'll see shortly.</p>
</div>
<p>Each of these <strong>if</strong> and <strong>then</strong> parts contain one or more <a class="reference external" href="../../knowledge_bases/fact_bases.html#facts">facts</a> or <a class="reference external" href="backward_chaining.html">goals</a>
which are just generalized <a class="reference external" href="../statements.html">statements</a> (meaning statements with <a class="reference external" href="../pattern_matching/index.html">patterns</a> as
arguments).</p>
</div>
<div class="section" id="processing-the-premises-within-the-if-clause">
<h2>Processing the Premises Within the <em>If</em> Clause</h2>
<p>Because each premise with the <em>if</em> clause is a generalized statement, the
premise is pattern matched to known facts. This means that it may match more
than one fact.</p>
<p>Pyke tries all combinations of matching facts through a process called
<em>backtracking</em>. This will cause the same rule to potentially succeed multiple
times, once for each unique combination of matching facts.</p>
<div class="section" id="backtracking">
<h3>Backtracking</h3>
<p>Note that while processing the list of premises within a rule's <tt class="docutils literal">if</tt>
clause:</p>
<ul class="simple">
<li>If Pyke succeeds at proving a premise:<ul>
<li>Pyke will proceed <em>down</em> to the next premise in the list.</li>
<li>Trying to proceed <em>down</em> from the <em>last</em> premise in the list (when it
succeeds) causes the rule to <em>succeed</em>.</li>
</ul>
</li>
<li>If Pyke fails at proving a premise:<ul>
<li>Pyke will back <em>up</em> to the prior premise in the list and try to find
another solution for it. The process starts over at the prior premise,
going back down or further up the list depending on whether another
solution can be found to the prior premise or not.</li>
<li>Trying to back <em>up</em> from the <em>first</em> premise in the list (when it fails)
causes the rule to <em>fail</em>.</li>
</ul>
</li>
</ul>
<div class="figure align-center">
<img alt="Backtracking Flow Diagram" src="../../images/backtracking.png" />
<p class="caption">Backtracking Flow Diagram</p>
</div>
<p>Thus, execution within each rule's <tt class="docutils literal">if</tt> clause proceeds both backwards and
forwards, up and down the list of premises, depending on whether each
premise succeeds or fails. The process of backing up within the <tt class="docutils literal">if</tt>
clause to try alternate solutions is called <em>backtracking</em>.</p>
</div>
</div>
<div class="section" id="inferencing">
<h2>Inferencing</h2>
<p>Rules are specified individually within a <a class="reference external" href="../../knowledge_bases/rule_bases.html">rule base</a>. They are not nested
or explicitly linked to each other. So Pyke must automatically figure out
how to combine these rules to accomplish some larger task. This is called
<em>inferencing</em> and there are two different approaches that Pyke uses:</p>
<ul class="simple">
<li>All <a class="reference external" href="forward_chaining.html">forward-chaining</a> rules are processed when a <a class="reference external" href="../../knowledge_bases/rule_bases.html">rule base</a> is <a class="reference external" href="../../using_pyke/index.html#getting-started">activated</a>.<ul>
<li>Forward-chaining rules may <a class="reference external" href="../../using_pyke/adding_facts.html">assert</a> new facts, and <a class="reference external" href="../../using_pyke/index.html#getting-started">activate</a> more specific
<a class="reference external" href="../../knowledge_bases/rule_bases.html">rule bases</a>.</li>
</ul>
</li>
<li><a class="reference external" href="backward_chaining.html">Backward-chaining</a> rules are processed when your program asks Pyke to
<a class="reference external" href="../../using_pyke/proving_goals.html">prove</a> a specific <em>goal</em> (i.e., asks Pyke a <a class="reference external" href="../../knowledge_bases/question_bases.html">question</a>).<ul>
<li>These rules are designed to answer a question rather than assert new
facts or activate more specific rule bases.</li>
<li>They also have the ability to assemble Python functions into a customized
call graph or program, called a <a class="reference external" href="../plans.html">plan</a>, to meet a specific need.</li>
</ul>
</li>
</ul>
<div class="note">
<p class="first admonition-title">Note</p>
<p class="last">Forward-chaining and backward-chaining rules may both be included within
the same <a class="reference external" href="../../knowledge_bases/rule_bases.html">rule base</a>. Pyke knows the difference between forward-chaining
and backward-chaining rules because they have different <a class="reference external" href="../../pyke_syntax/krb_syntax/index.html">syntax</a>.</p>
</div>
<!-- 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="forward_chaining.html">Forward Chaining</a><p>Explanation of <em>forward-chaining rules</em> and how <em>forward-chaining</em>
works.</p>
</div>
<div class="right-item"><a href="backward_chaining.html">Backward Chaining</a><p>Explanation of <em>backward-chaining</em> rules, including how
<em>backward-chaining</em> and <em>backtracking</em> works.</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>