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

Download this file

131 lines (106 with data), 4.6 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
.. $Id$
..
.. Copyright Š 2007 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: Special
page-description:
Explanation of the special knowledge base.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
/restindex
=============================================
Special
=============================================
Other kinds of `knowledge bases`_ may be created that act in different ways
when they are asked to lookup facts_ or prove_ goals_. Pyke includes one of
these named ``special``. Each entity in this `knowledge base`_ is a python
function that does something "special" when run.
Currently, there is only one special function::
``special.claim_goal()``
This acts like the prolog_ `cut operator`_.
In general there are multiple rules_ that might be used to try to prove_ any
goal_. They are each tried in the order that they appear in the `.krb file`_.
If one rule_ fails, the next rule_ is tried. The goal_ itself doesn't fail
until all of the rules_ for it have failed.
Example
===============
Suppose I want to translate a number, N, into the phrase "N dogs". I could
use the following rules_::
one_dog
use n_dogs(1, '1 dog')
n_dogs
use n_dogs($n, $phrase)
when
$phrase = "%d dogs" % $n
The problem here is that both rules_ might be used when ``n`` is 1, but the
second rule_ isn't appropriate in this case. ``Special.claim_goal()`` may
be used to fix this, as follows::
one_dog
use n_dogs(1, '1 dog')
when
special.claim_goal()
n_dogs
use n_dogs($n, $phrase)
when
$phrase = "%d dogs" % $n
The ``special.claim_goal()`` prevents the second rule_ from being used when
``n`` is 1.
Explanation
=================================
When a rule_ executes ``special.claim_goal()`` in its ``when``
clause, none of the rest of the rules_ will be tried for that goal_.
Thus, when ``special.claim_goal()`` is backtracked_ over, the goal_ fails
immediately without trying any more rules_ for it.
This ends up acting like an "else". You place it in the ``when`` clause
after the premises_ that show that this rule_ **must** be
the correct one to use. Then the subsequent rules_ will only be tried if
these premises_ fail, such that ``special.claim_goal()`` is never executed.
This means that you don't need to add extra premises_ in each subsequent rule
to make sure that these premises_ have **not** occurred.
Without the ``special.claim_goal()`` in the prior example, you would have to
write::
one_dog
use n_dogs(1, '1 dog')
n_dogs
use n_dogs($n, $phrase)
when
check $n != 1
$phrase = "%d dogs" % $n
This is a simple example where it is easy to add the check in the second
rule_. But in general, it can be difficult to check for prior conditions,
especially when many rules_ are involved that each has its own condition.
.. _backtracked: ../rules/backward_chaining.html#backtracking
.. _cut operator: http://en.wikipedia.org/wiki/Cut_%28logic_programming%29
.. _facts: fact_bases.html#facts
.. _goal: ../rules/backward_chaining.html
.. _goals: goal_
.. _knowledge base: index.html
.. _knowledge bases: `knowledge base`_
.. _.krb file: ../../krb_syntax/index.html
.. _premise: ../../krb_syntax/bc_rule.html#when-clause
.. _premises: premise_
.. _prolog: http://en.wikipedia.org/wiki/Prolog
.. _prove: ../../using_pyke.html#proving-goals
.. _rule: ../rules/index.html
.. _rules: rule_

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

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks