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

[853062]: doc / html / pyke_syntax / kqb_syntax.html Maximize Restore History

Download this file

kqb_syntax.html    549 lines (528 with data), 22.8 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
<!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>KQB Syntax</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">Pyke Syntax</a></li>
<li>&gt;</li>
<li>KQB Syntax</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="normal-nav"><a href="../logic_programming/index.html">Logic Programming</a></div>
<div class="normal-nav"><a href="../knowledge_bases/index.html">Knowledge Bases</a></div>
<div class="title-nav"><a href="index.html">Pyke Syntax</a></div><div class="nav-branch">
<div class="normal-nav"><a href="kfb_syntax.html">KFB Syntax</a></div>
<div class="normal-nav"><a href="krb_syntax/index.html">KRB Syntax</a></div>
<div class="normal-nav"><a href="kqb_syntax.html">KQB Syntax</a></div>
</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="kqb-syntax">
<h1 class="title">KQB Syntax</h1>
<p>This uses a different lexical structure than <a class="reference external" href="index.html#lexical-structure">KRB files</a>. Textual parameter
substitution is done with the standard Python <a class="reference external" href="http://docs.python.org/library/string.html#template-strings">string.Template</a> class, which
specifies parameters with a <tt class="docutils literal"><span class="pre">$</span></tt>, much like <a class="reference external" href="krb_syntax/pattern.html#pattern-variable">pattern variables</a>. In the
following descriptions, substitution parameters are acted upon in
<tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt>, but not in <tt class="docutils literal"><span class="pre">TEXT</span></tt>.</p>
<p>The name of the <a class="reference external" href="../knowledge_bases/question_bases.html">question base</a> is simple the filename with the <tt class="docutils literal"><span class="pre">.kqb</span></tt>
suffix stripped. This must be a valid Python identifier.</p>
<div class="section" id="parametrized-text">
<h2>PARAMETRIZED_TEXT</h2>
<p><tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt> may span multiple lines. Each subsequent line must
be indented at least as much as the first character of <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt>
on the first line. Line breaks and indentation are preserved. So syntax
like:</p>
<pre class="literal-block">
match '!' PARAMETRIZED_TEXT
</pre>
<p>Could be:</p>
<pre class="literal-block">
2-10 ! This is the start of the
parametrized text.
^
|
+------ all lines must be indented at least this far!
- what do you think?
</pre>
<p>The <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt> here would be:</p>
<pre class="literal-block">
This is the start of the
parametrized text.
^
|
+------ all lines must be indented at least this far!
- what do you think?
</pre>
<p>But this is not legal:</p>
<pre class="literal-block">
2-10 ! An example of PARAMETRIZED_TEXT
with a second line that's not indented enough!
</pre>
</div>
<div class="section" id="syntax-for-kqb-file">
<h2>Syntax for KQB File</h2>
<pre class="literal-block">
file ::= [NL] {question}
question ::= IDENTIFIER '(' [{parameter,}] ')' NL INDENT
{PARAMETRIZED_TEXT NL}
'---' NL
parameter '=' question_type
DEINDENT
parameter ::= '$' IDENTIFIER
</pre>
<p>Each question has a name and a fixed number of parameters. This is followed
by one or more lines of <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt> that will be presented as the
question to answer. These are terminated by a line containing only <tt class="docutils literal"><span class="pre">---</span></tt>.</p>
<p>One of the parameters is designated as the <em>answer</em> parameter on the line
immediately following the terminating <tt class="docutils literal"><span class="pre">---</span></tt>. This is the only parameter
that may be unbound when a rule uses this question.</p>
<p>For example, the file <tt class="docutils literal"><span class="pre">user_questions.kqb</span></tt> might contain:</p>
<pre class="literal-block">
ate($meal, $ans)
Did you eat $meal?
---
$ans = yn
</pre>
<p>This question could be referenced in the <a class="reference external" href="krb_syntax/bc_rule.html#when-clause">premise</a> of a <a class="reference external" href="../logic_programming/rules/index.html">rule</a> as:</p>
<pre class="literal-block">
user_questions.ate(lunch, $ans)
</pre>
<p>or:</p>
<pre class="literal-block">
user_questions.ate(lunch, False)
</pre>
<p>But not:</p>
<pre class="literal-block">
user_questions.ate($meal, False)
</pre>
<p>There are several different kinds of <tt class="docutils literal"><span class="pre">question_types</span></tt>, each corresponding to
a different way that the user might answer the question:</p>
<pre class="literal-block">
question_type ::= yn_type
| integer_type
| number_type
| float_type
| string_type
| select_1_type
| select_n_type
</pre>
<p>The <tt class="docutils literal"><span class="pre">integer_type</span></tt>, <tt class="docutils literal"><span class="pre">number_type</span></tt>, <tt class="docutils literal"><span class="pre">float_type</span></tt> and <tt class="docutils literal"><span class="pre">string_type</span></tt> may
include a <a class="reference internal" href="#match">match</a> to force the user to enter a sensible answer.</p>
<p>All of these may also include a <a class="reference internal" href="#review">review</a>, which is just <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt>
that will be displayed when the user's answer matches a certain <a class="reference internal" href="#match">match</a> value.</p>
<p>Question_type links:</p>
<ul class="simple">
<li><a class="reference internal" href="#yn-type">yn_type</a></li>
<li><a class="reference internal" href="#integer-type">integer_type</a></li>
<li><a class="reference internal" href="#number-type">number_type</a></li>
<li><a class="reference internal" href="#float-type">float_type</a></li>
<li><a class="reference internal" href="#string-type">string_type</a></li>
<li><a class="reference internal" href="#select-1-type">select_1_type</a></li>
<li><a class="reference internal" href="#select-n-type">select_n_type</a></li>
</ul>
</div>
<div class="section" id="yn-type">
<h2>YN_type</h2>
<pre class="literal-block">
yn_type ::= 'yn' NL [review]
</pre>
<p>The user answers &quot;yes&quot; or &quot;no&quot;. The answer returned is True or False. If the
<a class="reference external" href="../knowledge_bases/question_bases.html#presenting-questions-to-your-end-users">ask_tty</a> module is used, the user may type &quot;yes&quot;, &quot;y&quot;, &quot;true&quot; or &quot;t&quot; for
True and &quot;no&quot;, &quot;n&quot;, &quot;false&quot;, or &quot;f&quot; for False. These are case insensitive.</p>
<p>Example:</p>
<pre class="literal-block">
ate($meal, $ans)
Did you eat $meal?
---
$ans = yn
</pre>
<p>See <a class="reference internal" href="#review">review</a>, below.</p>
</div>
<div class="section" id="integer-type">
<h2>Integer_type</h2>
<pre class="literal-block">
integer_type ::= 'integer' ['(' match ')'] NL [review]
</pre>
<p>The user enters an integer. If the <a class="reference internal" href="#match">match</a> is specified, the integer must
match it or the user is asked to try again.</p>
<p>Example:</p>
<pre class="literal-block">
hours_since_last_meal($ans)
How many hours has it been since you last ate?
---
$ans = integer(0-48)
</pre>
<p>See <a class="reference internal" href="#review">review</a>, below.</p>
</div>
<div class="section" id="number-type">
<h2>Number_type</h2>
<pre class="literal-block">
number_type ::= 'number' ['(' match ')'] NL [review]
</pre>
<p>The user enters either an integer or a floating point number. If the user
enters an integer, a Python <tt class="docutils literal"><span class="pre">int</span></tt> is returned. Otherwise a Python <tt class="docutils literal"><span class="pre">float</span></tt>
is returned.</p>
<p>If the <a class="reference internal" href="#match">match</a> is specified, the number must match it or the user is asked
to try again.</p>
<p>Example:</p>
<pre class="literal-block">
miles_to($dest, $ans)
How many miles did you travel to get to $dest?
---
$ans = number(0.1-3000)
</pre>
<p>See <a class="reference internal" href="#review">review</a>, below.</p>
</div>
<div class="section" id="float-type">
<h2>Float_type</h2>
<pre class="literal-block">
float_type ::= 'float' ['(' match ')'] NL [review]
</pre>
<p>The user enters an integer or a floating point number. But the answer
returned is always a Python <tt class="docutils literal"><span class="pre">float</span></tt>.</p>
<p>If the <a class="reference internal" href="#match">match</a> is specified, the number must match it or the user is asked
to try again.</p>
<p>Example:</p>
<pre class="literal-block">
price($object, $price)
What did you pay for $object?
---
$price = float
</pre>
<p>See <a class="reference internal" href="#review">review</a>, below.</p>
</div>
<div class="section" id="string-type">
<h2>String_type</h2>
<pre class="literal-block">
string_type ::= 'string' ['(' match ')'] NL [review]
</pre>
<p>The user enters a string (text). If the <a class="reference internal" href="#match">match</a> is specified, the string must
match it or the user is asked to try again.</p>
<p>Example:</p>
<pre class="literal-block">
users_name($name)
What's your name?
- Please don't enter a fictitious (screen) name.
---
$name = string(2-40)
</pre>
<p>See <a class="reference internal" href="#review">review</a>, below.</p>
</div>
<div class="section" id="match">
<h2>Match</h2>
<p>There are several kinds of simple_matches that may be or-ed together with
<tt class="docutils literal"><span class="pre">|</span></tt>:</p>
<pre class="literal-block">
match ::= simple_match {'|' simple_match}
</pre>
<p>The match succeeds if any of the <tt class="docutils literal"><span class="pre">simple_matches</span></tt> succeed.</p>
<pre class="literal-block">
simple_match ::= '(' match ')'
| [ STRING ] [ '[' TEXT ']' ] '/' REGEXP_TEXT '/'
| [NUMBER] '-' NUMBER
| NUMBER '-'
| value '=' simple_match
| value
</pre>
<div class="section" id="regexp-match">
<h3>Regexp Match</h3>
<pre class="literal-block">
simple_match ::= [ STRING ] [ '[' TEXT ']' ] '/' REGEXP_TEXT '/'
</pre>
<p>A regexp match can only be used with <a class="reference internal" href="#string-type">string_type</a> questions. It matches if
the regexp matches.</p>
<p>If the regexp contains a single group, that group is returned as the
question's answer rather than the entire string.</p>
<p>If the regexp contains multiple groups, a tuple of the groups is returned as
the question's answer rather than entire string.</p>
<p>If STRING is specified on a regexp, it is used in the error message if the
regexp fails. The error message is &quot;Answer should be $error_msg, got $string&quot;.</p>
<p>If '[' TEXT ']' is specified on a regexp, it is used in the prompt for the
end user to inform him of what is expected. Generally, this prompt message
is enclosed in '[' and ']' when it is displayed to the user.</p>
<p>Example:</p>
<pre class="literal-block">
state_code($state)
Enter your two digit state code.
---
$state = string('uppercase'[uppercase]/[A-Z][A-Z]/)
</pre>
</div>
<div class="section" id="range-match">
<h3>Range Match</h3>
<pre class="literal-block">
simple_match ::= [NUMBER] '-' NUMBER
| NUMBER '-'
</pre>
<p>A range match has a '-' in it. It matches if the answer is between the two
values. If either value is omitted, that limit is not tested. If matched to
a string, it matches the length of the string.</p>
<p>Example:</p>
<pre class="literal-block">
age($years)
How old are you?
---
$years = integer(1-130)
</pre>
</div>
<div class="section" id="value-match">
<h3>Value '=' Match</h3>
<pre class="literal-block">
simple_match ::= value '=' simple_match
</pre>
<p>The '=' means &quot;substituted for&quot;. The <a class="reference internal" href="#match">match</a> fails if the match after the '='
fails. Otherwise it returns the value before the '=' rather than what the
user entered. Note that you can or (<tt class="docutils literal"><span class="pre">|</span></tt>) several of these together to
translate several different matched values.</p>
<p>Example:</p>
<pre class="literal-block">
age_category($period_of_life)
How old are you?
---
$period_of_life = integer(child=1-12 |
teenager=13-19 |
young_adult=20-35 |
middle_age=35-64 |
elder=65-130)
</pre>
</div>
<div class="section" id="id1">
<h3>Value Match</h3>
<pre class="literal-block">
simple_match ::= value
value ::= STRING | IDENTIFIER | NUMBER | 'None' | 'True' | 'False'
</pre>
<p>A value match, only matches that one value. An IDENTIFIER is treated as a
STRING. These are mostly used in reviews.</p>
</div>
</div>
<div class="section" id="review">
<h2>Review</h2>
<pre class="literal-block">
review ::= {match '!' PARAMETRIZED_TEXT NL}
</pre>
<p>All of the <tt class="docutils literal"><span class="pre">reviews</span></tt> must be at the same indent level.</p>
<p>The review is applied after the answer has been validated (validation possibly
changes the value).</p>
<p>Each <a class="reference internal" href="#match">match</a> is checked and all of the matching review's <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt>
messages are displayed to the user.</p>
<p>Examples:</p>
<pre class="literal-block">
stupid_question($ans)
Can you answer a question
that is several lines long?
---
$ans = yn
True ! Correct! This is true because the
sky is blue!
False ! Nope! Remember that the sky is blue!
wood($ans)
How much wood would a woodchuck chuck if a woodchuck could chuck wood?
---
$ans = integer(0-100)
-10 ! more than that!
10-20 ! bingo!
21- ! I guess they're not as strong as you think ...
</pre>
<!-- This code is hidden. It will add '' to sys.path, change to the doc.examples
directory and store the directory path in __file__ for the code section
following:
>>> import sys
>>> if '' not in sys.path: sys.path.insert(0, '')
>>> import os
>>> os.chdir("../../examples")
>>> __file__ = os.getcwd() -->
<p>Asking <tt class="docutils literal"><span class="pre">stupid_question</span></tt> and answering &quot;y&quot; to it:</p>
<pre class="literal-block">
&gt;&gt;&gt; from pyke import knowledge_engine
&gt;&gt;&gt; engine = knowledge_engine.engine(__file__)
&gt;&gt;&gt; from io import StringIO
&gt;&gt;&gt; import sys
&gt;&gt;&gt; class echo(object):
... def __init__(self, f): self.f = f
... def readline(self):
... ans = self.f.readline()
... sys.stdout.write(ans)
... return ans
&gt;&gt;&gt; sys.stdin = echo(StringIO('y\n'))
</pre>
<p>displays:</p>
<pre class="literal-block">
&gt;&gt;&gt; engine.prove_1_goal('user_questions.stupid_question($ans)')
______________________________________________________________________________
Can you answer a question
that is several lines long? (y/n) y
Correct! This is true because the
sky is blue!
({'ans': True}, None)
</pre>
</div>
<div class="section" id="select-1-type">
<h2>Select_1_type</h2>
<pre class="literal-block">
select_1_type ::= 'select_1' NL alternatives
</pre>
<p>This is a multiple choice question. The <a class="reference internal" href="#alternatives">alternatives</a> are displayed to the
user, and he picks one (and only one).</p>
<p>Example:</p>
<pre class="literal-block">
another_question($arg1, $arg2, $ans)
question text with $arg1 stuff in it.
on multiple lines
- possibly indented
- for who knows what reason...
- maybe for $arg2?
---
$ans = select_1
1: prompt for this selection with $arg2 in it too
which can span multiple lines
- and be indented ...
! Nope! Remember that the sky is blue!
2: next prompt
! =1 # same review as 1:
3: pick me! pick me!!!
! Correct! You certainly know about $arg1!
yep, multiple review lines too...
- and indented...
</pre>
</div>
<div class="section" id="select-n-type">
<h2>Select_n_type</h2>
<pre class="literal-block">
select_n_type ::= 'select_n' NL alternatives
</pre>
<p>This is a multiple choice question. The <a class="reference internal" href="#alternatives">alternatives</a> are displayed to the
user, and he picks as many as he likes.</p>
<p>Example:</p>
<pre class="literal-block">
problems($list)
Which of these problems are you experiencing?
- select all that apply
---
$list = select_n
boot: The system won't boot.
os: I hate Windows!
internet: I can't connect to the internet.
slow: The system is running too slow.
ouch: Help! I've fallen and I can't get up!
freeze: The system freezes or does not respond to input.
printer: The printer doesn't work.
senile: What's email?
</pre>
</div>
<div class="section" id="alternatives">
<h2>Alternatives</h2>
<pre class="literal-block">
alternatives ::= {value ':' PARAMETRIZED_TEXT NL [alt_review]}
</pre>
<p>All of the <tt class="docutils literal"><span class="pre">alternatives</span></tt> must be at the same indent level.</p>
<p>The user only sees the <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt> values. The <tt class="docutils literal"><span class="pre">value</span></tt>
associated with the selected <tt class="docutils literal"><span class="pre">PARAMETRIZED_TEXT</span></tt> is returned (but the user
never sees it). The <tt class="docutils literal"><span class="pre">value</span></tt> <em>tags</em> the alternative.</p>
<pre class="literal-block">
alt_review ::= '!' '=' value NL
| '!' PARAMETRIZED_TEXT NL
</pre>
<p>Each alternative may have it's own review associated with it.</p>
<p>The <tt class="docutils literal"><span class="pre">'!'</span> <span class="pre">'='</span> <span class="pre">value</span></tt> form uses the same review text as the previous
alternative with that <em>tag</em>. Note that this can not refer forward to a
following alternative.</p>
<p>The second form specifies the review text for this alternative directly.</p>
<!-- 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="kfb_syntax.html">KFB Syntax</a><p>The syntax of <em>Knowledge Fact Base</em> (KFB) files, which is where you
write your universal facts.</p>
</div>
<div class="right-item"><a href="krb_syntax/index.html">KRB Syntax</a><p>Syntax of the <em>Knowledge Rule Base</em> (KRB) files, which is where you
write your rules.</p>
</div>
<div class="right-item"><a href="kqb_syntax.html">KQB Syntax</a><p>The syntax of <em>Knowledge Question Base</em> (KQB) files, which is where you
spell out your end user questions.</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
Wed, Mar 10 2010.
</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>