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

[4dca5a]: doc / source / pyke_syntax / kqb_syntax.txt Maximize Restore History

Download this file

kqb_syntax.txt    530 lines (369 with data), 14.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
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
.. $Id$
..
.. Copyright Š 2008 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: KQB Syntax
page-description:
The syntax of *Knowledge Question Base* (KQB) files, which is where you
spell out your end user questions.
/description
format: rest
encoding: utf8
output-encoding: utf8
include: yes
initialheaderlevel: 2
/restindex
uservalues
filedate: $Id$
/uservalues
==========
KQB Syntax
==========
This uses a different lexical structure than `KRB files`_. Textual parameter
substitution is done with the standard Python `string.Template`_ class, which
specifies parameters with a ``$``, much like `pattern variables`_. In the
following descriptions, substitution parameters are acted upon in
``PARAMETRIZED_TEXT``, but not in ``TEXT``.
The name of the `question base`_ is simple the filename with the ``.kqb``
suffix stripped. This must be a valid Python identifier.
PARAMETRIZED_TEXT
==================
``PARAMETRIZED_TEXT`` may span multiple lines. Each subsequent line must
be indented at least as much as the first character of ``PARAMETRIZED_TEXT``
on the first line. Line breaks and indentation are preserved. So syntax
like::
match '!' PARAMETRIZED_TEXT
Could be::
2-10 ! This is the start of the
parametrized text.
^
|
+------ all lines must be indented at least this far!
- what do you think?
The ``PARAMETRIZED_TEXT`` here would be::
This is the start of the
parametrized text.
^
|
+------ all lines must be indented at least this far!
- what do you think?
But this is not legal::
2-10 ! An example of PARAMETRIZED_TEXT
with a second line that's not indented enough!
Syntax for KQB File
===================
::
file ::= [NL] {question}
question ::= IDENTIFIER '(' [{parameter,}] ')' NL INDENT
{PARAMETRIZED_TEXT NL}
'---' NL
parameter '=' question_type
DEINDENT
parameter ::= '$' IDENTIFIER
Each question has a name and a fixed number of parameters. This is followed
by one or more lines of ``PARAMETRIZED_TEXT`` that will be presented as the
question to answer. These are terminated by a line containing only ``---``.
One of the parameters is designated as the *answer* parameter on the line
immediately following the terminating ``---``. This is the only parameter
that may be unbound when a rule uses this question.
For example, the file ``user_questions.kqb`` might contain::
ate($meal, $ans)
Did you eat $meal?
---
$ans = yn
This question could be referenced in the premise_ of a rule_ as::
user_questions.ate(lunch, $ans)
or::
user_questions.ate(lunch, False)
But not::
user_questions.ate($meal, False)
There are several different kinds of ``question_types``, each corresponding to
a different way that the user might answer the question::
question_type ::= yn_type
| integer_type
| number_type
| float_type
| string_type
| select_1_type
| select_n_type
The ``integer_type``, ``number_type``, ``float_type`` and ``string_type`` may
include a match_ to force the user to enter a sensible answer.
All of these may also include a review_, which is just ``PARAMETRIZED_TEXT``
that will be displayed when the user's answer matches a certain match_ value.
Question_type links:
- yn_type_
- integer_type_
- number_type_
- float_type_
- string_type_
- select_1_type_
- select_n_type_
YN_type
=======
::
yn_type ::= 'yn' NL [review]
The user answers "yes" or "no". The answer returned is True or False. If the
`ask_tty`_ module is used, the user may type "yes", "y", "true" or "t" for
True and "no", "n", "false", or "f" for False. These are case insensitive.
Example::
ate($meal, $ans)
Did you eat $meal?
---
$ans = yn
See review_, below.
Integer_type
============
::
integer_type ::= 'integer' ['(' match ')'] NL [review]
The user enters an integer. If the match_ is specified, the integer must
match it or the user is asked to try again.
Example::
hours_since_last_meal($ans)
How many hours has it been since you last ate?
---
$ans = integer(0-48)
See review_, below.
Number_type
============
::
number_type ::= 'number' ['(' match ')'] NL [review]
The user enters either an integer or a floating point number. If the user
enters an integer, a Python ``int`` is returned. Otherwise a Python ``float``
is returned.
If the match_ is specified, the number must match it or the user is asked
to try again.
Example::
miles_to($dest, $ans)
How many miles did you travel to get to $dest?
---
$ans = number(0.1-3000)
See review_, below.
Float_type
============
::
float_type ::= 'float' ['(' match ')'] NL [review]
The user enters an integer or a floating point number. But the answer
returned is always a Python ``float``.
If the match_ is specified, the number must match it or the user is asked
to try again.
Example::
price($object, $price)
What did you pay for $object?
---
$price = float
See review_, below.
String_type
============
::
string_type ::= 'string' ['(' match ')'] NL [review]
The user enters a string (text). If the match_ is specified, the string must
match it or the user is asked to try again.
Example::
users_name($name)
What's your name?
- Please don't enter a fictitious (screen) name.
---
$name = string(2-40)
See review_, below.
Match
=====
There are several kinds of simple_matches that may be or-ed together with
``|``::
match ::= simple_match {'|' simple_match}
The match succeeds if any of the ``simple_matches`` succeed.
::
simple_match ::= '(' match ')'
| [ STRING ] [ '[' TEXT ']' ] '/' REGEXP_TEXT '/'
| [NUMBER] '-' NUMBER
| NUMBER '-'
| value '=' simple_match
| value
Regexp Match
------------
::
simple_match ::= [ STRING ] [ '[' TEXT ']' ] '/' REGEXP_TEXT '/'
A regexp match can only be used with string_type_ questions. It matches if
the regexp matches.
If the regexp contains a single group, that group is returned as the
question's answer rather than the entire string.
If the regexp contains multiple groups, a tuple of the groups is returned as
the question's answer rather than entire string.
If STRING is specified on a regexp, it is used in the error message if the
regexp fails. The error message is "Answer should be $error_msg, got $string".
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.
Example::
state_code($state)
Enter your two digit state code.
---
$state = string('uppercase'[uppercase]/[A-Z][A-Z]/)
Range Match
-----------
::
simple_match ::= [NUMBER] '-' NUMBER
| NUMBER '-'
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.
Example::
age($years)
How old are you?
---
$years = integer(1-130)
Value '=' Match
---------------
::
simple_match ::= value '=' simple_match
The '=' means "substituted for". The match_ 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 (``|``) several of these together to
translate several different matched values.
Example::
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)
Value Match
-----------
::
simple_match ::= value
value ::= STRING | IDENTIFIER | NUMBER | 'None' | 'True' | 'False'
A value match, only matches that one value. An IDENTIFIER is treated as a
STRING. These are mostly used in reviews.
Review
======
::
review ::= {match '!' PARAMETRIZED_TEXT NL}
All of the ``reviews`` must be at the same indent level.
The review is applied after the answer has been validated (validation possibly
changes the value).
Each match_ is checked and all of the matching review's ``PARAMETRIZED_TEXT``
messages are displayed to the user.
Examples::
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 ...
.. 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()
Asking ``stupid_question`` and answering "y" to it::
>>> from pyke import knowledge_engine
>>> engine = knowledge_engine.engine(__file__)
>>> from io import StringIO
>>> import sys
>>> class echo(object):
... def __init__(self, f): self.f = f
... def readline(self):
... ans = self.f.readline()
... sys.stdout.write(ans)
... return ans
>>> sys.stdin = echo(StringIO('y\n'))
displays::
>>> 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)
Select_1_type
=============
::
select_1_type ::= 'select_1' NL alternatives
This is a multiple choice question. The alternatives_ are displayed to the
user, and he picks one (and only one).
Example::
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...
Select_n_type
=============
::
select_n_type ::= 'select_n' NL alternatives
This is a multiple choice question. The alternatives_ are displayed to the
user, and he picks as many as he likes.
Example::
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?
Alternatives
============
::
alternatives ::= {value ':' PARAMETRIZED_TEXT NL [alt_review]}
All of the ``alternatives`` must be at the same indent level.
The user only sees the ``PARAMETRIZED_TEXT`` values. The ``value``
associated with the selected ``PARAMETRIZED_TEXT`` is returned (but the user
never sees it). The ``value`` *tags* the alternative.
::
alt_review ::= '!' '=' value NL
| '!' PARAMETRIZED_TEXT NL
Each alternative may have it's own review associated with it.
The ``'!' '=' value`` form uses the same review text as the previous
alternative with that *tag*. Note that this can not refer forward to a
following alternative.
The second form specifies the review text for this alternative directly.