[d43b93]: doc / html / overview / rules / backward_chaining.html Maximize Restore History

Download this file

backward_chaining.html    254 lines (237 with data), 12.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
<!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>Backward Chaining</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">Overview</a></li>
<li>&gt;</li>
<li><a href="index.html">Rules</a></li>
<li>&gt;</li>
<li>Backward Chaining</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="title-nav"><a href="../index.html">Overview</a></div><div class="nav-branch">
<div class="normal-nav"><a href="../knowledge_bases/index.html">Knowledge Bases</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="../../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>
</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="backward-chaining">
<h1 class="title">Backward Chaining</h1>
<p>Backward chaining <a class="reference" href="index.html">rules</a> are processed when your program asks pyke to <a class="reference" href="../../using_pyke.html#proving-goals">prove</a>
a specific <em>goal</em>. Pyke will only use <a class="reference" href="../../using_pyke.html#setting-up-each-case">activated</a> <a class="reference" href="../knowledge_bases/rule_bases.html">rule bases</a> to do the
proof.</p>
<div class="section">
<h3><a id="example" name="example">Example</a></h3>
<p>Consider this example:</p>
<pre class="literal-block">
1 son_father:
2 use child_parent($son, $father, (), son, father)
3 when
4 family.son_of($son, $father, $_)
5 son_mother:
6 use child_parent($son, $mother, (), son, mother)
7 when
8 family.son_of($son, $_, $mother)
9 daughter_father:
10 use child_parent($daughter, $father, (), daughter, father)
11 when
12 family.daughter_of($daughter, $father, $_)
13 daughter_mother:
14 use child_parent($daughter, $mother, (), daughter, mother)
15 when
16 family.daughter_of($daughter, $_, $mother)
17 grand_parent_and_child:
18 use child_parent($grand_child, $grand_parent, (grand),
19 $grand_child_type, $grand_parent_type)
20 when
21 child_parent($grand_child, $parent, (), $grand_child_type, $_)
22 child_parent($parent, $grand_parent, (), $_, $grand_parent_type)
23 great_grand_parent_and_child:
24 use child_parent($gg_child, $gg_parent, (great, $prefix1, *$rest_prefixes),
25 $gg_child_type, $gg_parent_type)
26 when
27 child_parent($gg_child, $parent, (), $gg_child_type, $_)
28 child_parent($parent, $gg_parent, ($prefix1, *$rest_prefixes),
29 $_, $gg_parent_type)
</pre>
</div>
<div class="section">
<h3><a id="identifying-backward-chaining-rules" name="identifying-backward-chaining-rules">Identifying Backward-Chaining Rules</a></h3>
<p>These <a class="reference" href="index.html">rules</a> draw the same conclusions as the <a class="reference" href="forward_chaining.html">forward-chaining</a> <a class="reference" href="forward_chaining.html#example">example</a>;
but you'll notice three differences:</p>
<ol class="arabic simple">
<li>The <a class="reference" href="index.html">rule's</a> <strong>if</strong> and <strong>then</strong> parts are reversed.</li>
<li>The <a class="reference" href="index.html">rules</a> use different keywords: <tt class="docutils literal"><span class="pre">use</span></tt> for the <strong>then</strong> clause
and <tt class="docutils literal"><span class="pre">when</span></tt> for the <strong>if</strong> clause.</li>
<li>The <a class="reference" href="../knowledge_bases/fact_bases.html#facts">facts</a> established by backward-chaining do not have a knowledge
base name prefix. In this case, the knowledge base name defaults to
the <a class="reference" href="../knowledge_bases/rule_bases.html#rule-base-categories">rule base category</a> of this <a class="reference" href="../knowledge_bases/rule_bases.html">rule base</a> (it's root rule base name).</li>
</ol>
</div>
<div class="section">
<h3><a id="how-backward-chaining-rules-are-run" name="how-backward-chaining-rules-are-run">How Backward-Chaining Rules are Run</a></h3>
<p>These <a class="reference" href="index.html">rules</a> are not used until you ask pyke to <a class="reference" href="../../using_pyke.html#proving-goals">prove</a> a goal.</p>
<p>The easiest way to do this is with
<em>some_engine</em>.prove_1_ or <em>some_engine</em>.prove_n_. <a class="reference" href="../../using_pyke.html#proving-goals">Prove_1</a> only
returns the first proof found and then stops (or raises pyke.CanNotProve).
<a class="reference" href="../../using_pyke.html#proving-goals">Prove_n</a> is a generator that generates all possible proofs (which, in
some cases, might be infinite). In both cases, you pass a tuple of
arguments and the number of variable arguments as the last two parameters.
The total number of arguments for the goal is the sum of the length of the
actual arguments that you pass plus the number of variable arguments that you
specify.</p>
<p>Both functions return the variable bindings for the number of variable
arguments you specified as a tuple, along with the <a class="reference" href="../plans.html">plan</a>.</p>
</div>
<div class="section">
<h3><a id="running-the-example" name="running-the-example">Running the Example</a></h3>
<blockquote>
<pre class="doctest-block">
&gt;&gt;&gt; import pyke
&gt;&gt;&gt; engine = pyke.engine('examples')
&gt;&gt;&gt; engine.assert_('family', 'son_of', ('michael', 'bruce', 'marilyn'))
&gt;&gt;&gt; engine.assert_('family', 'son_of', ('bruce', 'thomas', 'norma'))
&gt;&gt;&gt; engine.assert_('family', 'daughter_of', ('norma', 'allen', 'ismay'))
&gt;&gt;&gt; engine.activate('bc_example')
&gt;&gt;&gt; for vars, no_plan in engine.prove_n('bc_example', 'child_parent',
... ('michael',), 4):
... print vars
('bruce', (), 'son', 'father')
('marilyn', (), 'son', 'mother')
('thomas', ('grand',), 'son', 'father')
('norma', ('grand',), 'son', 'mother')
('allen', ('great', 'grand'), 'son', 'father')
('ismay', ('great', 'grand'), 'son', 'mother')
</pre>
</blockquote>
<p>Pyke performs the proof by:</p>
<ol class="arabic simple">
<li>Checking to see if the goal is simply a known <a class="reference" href="../knowledge_bases/fact_bases.html#facts">fact</a>.
If so, it has a proof!</li>
<li>Look for the first backward-chaining <a class="reference" href="index.html">rule</a> that concludes the goal
in its <tt class="docutils literal"><span class="pre">use</span></tt> clause.<ul>
<li>If not found, the goal fails.</li>
<li>If found, try to recursively prove all of the subgoals in the
<a class="reference" href="index.html">rule's</a> <tt class="docutils literal"><span class="pre">when</span></tt> clause.<ul>
<li>If this succeeds, the goal is proven.</li>
<li>If this fails, go back to step 2 and look for the next rule
that concludes the goal in its <tt class="docutils literal"><span class="pre">use</span></tt> clause.</li>
</ul>
</li>
</ul>
</li>
</ol>
</div>
<div class="section">
<h3><a id="backward-chaining-defined" name="backward-chaining-defined">Backward-Chaining Defined</a></h3>
<p>Note how the <a class="reference" href="index.html">rules</a> are combined in the opposite direction from
<a class="reference" href="forward_chaining.html">forward-chaining</a> <a class="reference" href="index.html">rules</a>. Here the first <a class="reference" href="index.html">rule's</a> <strong>if</strong> (<tt class="docutils literal"><span class="pre">when</span></tt>) clause is
linked backwards to the next rule's <strong>then</strong> (<tt class="docutils literal"><span class="pre">use</span></tt>) clause.</p>
<p>This is why these rules are called <em>backward-chaining</em> rules. This is also
referred to as <em>goal directed</em>, since the inferencing is driven by the final
goal.</p>
</div>
<div class="section">
<h3><a id="backtracking" name="backtracking">Backtracking</a></h3>
<p>Also note that while processing each subgoal within a <a class="reference" href="index.html">rule's</a> <tt class="docutils literal"><span class="pre">when</span></tt> clause:</p>
<ul class="simple">
<li>If pyke succeeds at proving the subgoal:<ul>
<li>Pyke will proceed to the next subgoal within the <tt class="docutils literal"><span class="pre">when</span></tt> clause.</li>
<li>If pyke reaches the end of the <tt class="docutils literal"><span class="pre">when</span></tt> clause, the <a class="reference" href="index.html">rule</a> succeeds.</li>
</ul>
</li>
<li>If pyke fails at proving the subgoal:<ul>
<li>Pyke will back up to the prior subgoal within the <tt class="docutils literal"><span class="pre">when</span></tt> clause
and try to find another proof for it. The process starts over
from this prior subgoal, going forward or backing up depending on
whether another proof can be found.</li>
<li>If pyke reaches the beginning of the <tt class="docutils literal"><span class="pre">when</span></tt> clause, the <a class="reference" href="index.html">rule</a>
fails.</li>
</ul>
</li>
</ul>
<p>Thus, execution within each <a class="reference" href="index.html">rule's</a> <tt class="docutils literal"><span class="pre">when</span></tt> clause proceeds both forwards
and backwards up and down the list of subgoals, depending on whether each
subgoal succeeds or fails. The process of backing up in the <tt class="docutils literal"><span class="pre">when</span></tt>
clause to try alternate subproofs is called <em>backtracking</em>.</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="forward_chaining.html">Forward Chaining</a><p>Explanation of forward-chaining rules.</p>
</div>
<div class="right-item"><a href="backward_chaining.html">Backward Chaining</a><p>Explanation of backward-chaining rules.</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 Mon, Feb 11 2008.
</div>
</body>
</html>