KQB Syntax

# 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 ...
Asking `stupid_question` and answering "y" to it:
    >>> from pyke import knowledge_engine

    >>> engine = knowledge_engine.engine(__file__)

    >>> from StringIO 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.
Copyright © 2007-2009 Bruce Frederiksen

Page last modified Fri, Mar 05 2010.