[c203b8]: doc / html / overview / knowledge_bases / special.html  Maximize  Restore  History

Download this file

189 lines (174 with data), 10.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
<!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>Special</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">Knowledge Bases</a></li>
<li>&gt;</li>
<li>Special</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="title-nav"><a href="index.html">Knowledge Bases</a></div><div class="nav-branch">
<div class="normal-nav"><a href="fact_bases.html">Fact Bases</a></div>
<div class="normal-nav"><a href="rule_bases.html">Rule Bases</a></div>
<div class="normal-nav"><a href="special.html">Special</a></div>
</div>
<div class="normal-nav"><a href="../rules/index.html">Rules</a></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 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="special">
<h1 class="title">Special</h1>
<p>Other kinds of <a class="reference" href="index.html">knowledge bases</a> may be created that act in different ways
when they are asked to lookup <a class="reference" href="fact_bases.html#facts">facts</a> or <a class="reference" href="../../using_pyke.html#proving-goals">prove</a> <a class="reference" href="../rules/backward_chaining.html">goals</a>. Pyke includes one of
these named <tt class="docutils literal"><span class="pre">special</span></tt>. Each entity in this <a class="reference" href="index.html">knowledge base</a> is a python
function that does something &quot;special&quot; when run.</p>
<p>Currently, there is only one special function:</p>
<pre class="literal-block">
``special.claim_goal()``
</pre>
<p>This acts like the <a class="reference" href="http://en.wikipedia.org/wiki/Prolog">prolog</a> <a class="reference" href="http://en.wikipedia.org/wiki/Cut_%28logic_programming%29">cut operator</a>.</p>
<p>In general there are multiple <a class="reference" href="../rules/index.html">rules</a> that might be used to try to <a class="reference" href="../../using_pyke.html#proving-goals">prove</a> any
<a class="reference" href="../rules/backward_chaining.html">goal</a>. They are each tried in the order that they appear in the <a class="reference" href="../../krb_syntax/index.html">.krb file</a>.
If one <a class="reference" href="../rules/index.html">rule</a> fails, the next <a class="reference" href="../rules/index.html">rule</a> is tried. The <a class="reference" href="../rules/backward_chaining.html">goal</a> itself doesn't fail
until all of the <a class="reference" href="../rules/index.html">rules</a> for it have failed.</p>
<div class="section">
<h1><a id="example" name="example">Example</a></h1>
<p>Suppose I want to translate a number, N, into the phrase &quot;N dogs&quot;. I could
use the following <a class="reference" href="../rules/index.html">rules</a>:</p>
<pre class="literal-block">
one_dog
use n_dogs(1, '1 dog')
n_dogs
use n_dogs($n, $phrase)
when
$phrase = &quot;%d dogs&quot; % $n
</pre>
<p>The problem here is that both <a class="reference" href="../rules/index.html">rules</a> might be used when <tt class="docutils literal"><span class="pre">n</span></tt> is 1, but the
second <a class="reference" href="../rules/index.html">rule</a> isn't appropriate in this case. <tt class="docutils literal"><span class="pre">Special.claim_goal()</span></tt> may
be used to fix this, as follows:</p>
<pre class="literal-block">
one_dog
use n_dogs(1, '1 dog')
when
special.claim_goal()
n_dogs
use n_dogs($n, $phrase)
when
$phrase = &quot;%d dogs&quot; % $n
</pre>
<p>The <tt class="docutils literal"><span class="pre">special.claim_goal()</span></tt> prevents the second <a class="reference" href="../rules/index.html">rule</a> from being used when
<tt class="docutils literal"><span class="pre">n</span></tt> is 1.</p>
</div>
<div class="section">
<h1><a id="explanation" name="explanation">Explanation</a></h1>
<p>When a <a class="reference" href="../rules/index.html">rule</a> executes <tt class="docutils literal"><span class="pre">special.claim_goal()</span></tt> in its <tt class="docutils literal"><span class="pre">when</span></tt>
clause, none of the rest of the <a class="reference" href="../rules/index.html">rules</a> will be tried for that <a class="reference" href="../rules/backward_chaining.html">goal</a>.
Thus, when <tt class="docutils literal"><span class="pre">special.claim_goal()</span></tt> is <a class="reference" href="../rules/backward_chaining.html#backtracking">backtracked</a> over, the <a class="reference" href="../rules/backward_chaining.html">goal</a> fails
immediately without trying any more <a class="reference" href="../rules/index.html">rules</a> for it.</p>
<p>This ends up acting like an &quot;else&quot;. You place it in the <tt class="docutils literal"><span class="pre">when</span></tt> clause
after the <a class="reference" href="../../krb_syntax/bc_rule.html#when-clause">premises</a> that show that this <a class="reference" href="../rules/index.html">rule</a> <strong>must</strong> be
the correct one to use. Then the subsequent <a class="reference" href="../rules/index.html">rules</a> will only be tried if
these <a class="reference" href="../../krb_syntax/bc_rule.html#when-clause">premises</a> fail, such that <tt class="docutils literal"><span class="pre">special.claim_goal()</span></tt> is never executed.</p>
<p>This means that you don't need to add extra <a class="reference" href="../../krb_syntax/bc_rule.html#when-clause">premises</a> in each subsequent rule
to make sure that these <a class="reference" href="../../krb_syntax/bc_rule.html#when-clause">premises</a> have <strong>not</strong> occurred.</p>
<p>Without the <tt class="docutils literal"><span class="pre">special.claim_goal()</span></tt> in the prior example, you would have to
write:</p>
<pre class="literal-block">
one_dog
use n_dogs(1, '1 dog')
n_dogs
use n_dogs($n, $phrase)
when
check $n != 1
$phrase = &quot;%d dogs&quot; % $n
</pre>
<p>This is a simple example where it is easy to add the check in the second
<a class="reference" href="../rules/index.html">rule</a>. But in general, it can be difficult to check for prior conditions,
especially when many <a class="reference" href="../rules/index.html">rules</a> are involved that each has its own condition.</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="fact_bases.html">Fact Bases</a><p>Explanation of facts and fact bases.</p>
</div>
<div class="right-item"><a href="rule_bases.html">Rule Bases</a><p>Explanation of rule bases, overview of .krb files and how these files
are compiled and loaded into your python program.</p>
</div>
<div class="right-item"><a href="special.html">Special</a><p>Explanation of the special knowledge base.</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 Sat, Jun 07 2008.
</div>
</body>
</html>

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

Sign up for the SourceForge newsletter:





No, thanks