[9f7068]: doc / source / using_pyke / proving_goals.txt Maximize Restore History

Download this file

proving_goals.txt    160 lines (127 with data), 6.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
.. $Id$
..
.. Copyright Š 2010 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: Proving Goals
page-description:
Using Pyke's API to prove goals from your Python program.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
initialheaderlevel: 2
/restindex
uservalues
filedate: $Id$
/uservalues
===================================
Proving Goals
===================================
.. 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')
>>> from pyke import knowledge_engine
>>> my_engine = knowledge_engine.engine(__file__)
>>> my_engine.add_universal_fact('family', 'son_of', ('bruce', 'thomas'))
>>> my_engine.add_universal_fact('family', 'son_of', ('david', 'bruce'))
>>> my_engine.activate('bc_father_son')
Though Pyke has the capability to return multiple answers to a single goal,
often you just want the first answer:
*some_engine*.prove_1_goal(goal, \*\*args)
``goal`` is a Pyke goal (as a string). This may include `pattern
variables`_ (which start with a '$').
>>> my_engine.prove_1_goal('bc_father_son.father_son(thomas, david, $depth)')
({'depth': ('grand',)}, None)
Returns the first proof found as a 2-tuple: a dict of bindings for the
pattern variables, and a plan_. The plan is ``None`` if no plan was
generated; otherwise, it is a Python function as described below__.
.. __: #running-and-pickling-plans
Args must be specified as keyword arguments and are set as the value of
the corresponding pattern variable.
>>> vars, plan = \
... my_engine.prove_1_goal('bc_father_son.father_son($father, $son, $depth)',
... father='thomas',
... son='david')
>>> sorted(vars.items(), key=lambda item: item[0])
[('depth', ('grand',)), ('father', 'thomas'), ('son', 'david')]
Prove_1_goal raises ``pyke.knowledge_engine.CanNotProve`` if no proof is
found:
>>> my_engine.prove_1_goal('bc_father_son.father_son(thomas, bogus, $depth)')
Traceback (most recent call last):
...
CanNotProve: Can not prove bc_father_son.father_son(thomas, bogus, $depth)
*some_engine*.prove_goal(goal, \*\*args)
This returns a context manager for a generator yielding 2-tuples, as
above. Unlike ``prove_1_goal`` it does not raise an exception if no
proof is found:
>>> from __future__ import with_statement
>>> with my_engine.prove_goal(
... 'bc_father_son.father_son(thomas, $son, $depth)') as gen:
... for vars, plan in gen:
... print vars['son'], vars['depth']
bruce ()
david ('grand',)
Like ``prove_1_goal``, above, `pattern variables`_ in the goal_ may be
specified with keyword arguments:
>>> with my_engine.prove_goal(
... 'bc_father_son.father_son($father, $son, $depth)',
... father='thomas') as gen:
... for vars, plan in gen:
... print vars['son'], vars['depth']
bruce ()
david ('grand',)
Compiling Goals at Program Startup
----------------------------------
Similar to Python's regular expression library, ``re``, you may compile your
goal statements once at program startup:
>>> from pyke import goal
>>> my_goal = goal.compile('bc_father_son.father_son($father, $son, $depth)')
Then use ``my_goal.prove_1`` and ``my_goal.prove`` as many times as you'd
like:
>>> vars, plan = my_goal.prove_1(my_engine, father='thomas', son='david')
>>> sorted(vars.items(), key=lambda item: item[0])
[('depth', ('grand',)), ('father', 'thomas'), ('son', 'david')]
>>> with my_goal.prove(my_engine, father='thomas') as gen:
... for vars, plan in gen:
... print vars['son'], vars['depth']
bruce ()
david ('grand',)
Running and Pickling Plans
----------------------------
Once you've obtained a plan_ from ``prove_1_goal`` or ``prove_goal``, you just
call it like a normal Python function. The arguments required are simply those
specified, if any, in the `taking clause`_ of the rule__ proving the top-level
goal.
You may call the plan function any number of times. You may even pickle
the plan for later use. But the plans are constructed out of
`functools.partial`_ functions that need to be registered with copy_reg if
you are running Python 2.x:
>>> import copy_reg
>>> import functools
>>> copy_reg.pickle(functools.partial,
... lambda p: (functools.partial, (p.func,) + p.args))
No special code is required to unpickle a plan. Just unpickle and call it.
(Unpickling the plan only imports one small Pyke module to be able to run
the plan).
.. __: ../pyke_syntax/krb_syntax/bc_rule.html