Node:Query Processing, Next:, Previous:Message Handling Predicates, Up:Messages and Queries



Query Processing

All user input in the Prolog system is handled by a single predicate:

     ask_query(+QueryClass, +Query, +Help, -Answer)
     

QueryClass, described below, specifies the form of the query interaction. Query is an abstract message term specifying the query text, Help is an abstract message term used as a help message in certain cases, and Answer is the (abstract) result of the query.

ask_query/4 is a built-in predicate, so that users can invoke it to have their own queries processed in the same way as the system queries.

The processing of queries is highly customizable. For example, this allows changing the language of the input expected from the user, or to make queries appear in dialog windows rather than on the terminal.

Query Classes

Queries posed by the system can be classified according to the kind of input they expect, the way the input is processed, etc. Queries of the same kind form a query class.

For example, queries requiring a yes/no answer form a query class with the following characteristics:

There are built-in query classes for reading in yes/no answers, toplevel queries, debugger commands, etc.

A query class is characterized by a ground Prolog term, which is supplied as the first argument to the query processing predicate ask_query/4. The characteristics of a query class are normally described by the extendible predicate

     'SU_messages':query_class(+QueryClass, -Prompt, -InputMethod,
                               -MapMethod, -FailureMode).
     

The arguments of the query_class predicate have the following meaning:

Phases of Query Processing

Query processing is done in several phases, described below. We will illustrate what is done in each phase through a simple example: the question put to the user when the solution to the toplevel query X is 1+1 is displayed, requesting a decision whether to find alternative answers or not:

     | ?- X is 1+1.
     
     X = 2 ?  no
     Please enter ";" for more choices; otherwise, <return>
      ? ;
     

We focus on the query X = 2 ? in the above script.

The example query belongs to the class next_solution, its text is described by the message term solutions([binding("X",2)]), and its help text by the message term bindings_help. Accordingly, such a query is executed by calling:

     ask_query(next_solution,               /* QueryClass */
               solutions([binding("X",2)]), /* Query */
               bindings_help,               /* Help */
               Answer)
     

In general, execution of ask_query(+QueryClass, +Query, +Help, -Answer) consists of the following phases:

Hooks in Query Processing

As explained above, the major parts of query processing are implemented in the 'SU_messages' module in the file library('SU_messages') through the following extendible predicates:

This is to enable the user to change the language used, the processing done, etc., simply by changing or replacing the library('SU_messages') file.

To give more control to the user and to make the system more robust (for example if the 'SU_messages' module is corrupt) the so-called four step procedure is used in the above three cases--obtaining the query class parameters, performing the query input and performing the mapping. The four steps of this procedure, described below, are tried in the given order until the first one that succeeds. Note that if an exception is raised within the first three steps, then a warning is printed and the step is considered to have failed.

Default Input Methods

The following InputMethod types are implemented by the default 'SU_messages':query_input(+InputMethod, +Prompt, -RawInput) (and these are the input methods known to the third, fall-back step):

line
The Prompt is printed, a line of input is read using read_line/2 and the list of character codes is returned as RawInput.
term(Options)
Prompt is set to be the prompt (cf. prompt/2), and a Prolog term is read by read_term/2 using the given Options, and is returned as RawInput.
FinalTerm^term(Term,Options)
A Prolog term is read as above, and is unified with Term. FinalTerm is returned as RawInput. For example, the T-Vs^term(T,[variable_names(Vs)]) input method will return the term read, paired with the list of variable names.
Default Map Methods

The following MapMethod types are known to 'SU_messages':query_map(+MapMethod, +RawInput, -Result, -Answer) and to the built-in fall-back mapping:

char(Pairs)
In this map method RawInput is assumed to be a string (a list of character codes).

Pairs is a list of Name-Abbreviations pairs, where Name is a ground term, and Abbreviations is a list of character codes. The first non-layout character of RawInput is used for finding the corresponding name as the answer, by looking it up in the abbreviation lists. If the character is found, then Result is success, and Answer is set to the Name found; otherwise, Result is failure.

=
No conversion is done, Answer is equal to RawInput and Result is success.
debugger
This map method is used when reading a single line debugger command. It parses the debugger command and returns the corresponding abstract command term. If the parse is unsuccessful, the answer unknown(Line,Warning) is returned. This is to allow the user to extend the debugger command language via debugger_command_hook/2, see Debug Commands.

The details of this mapping can be obtained from the library('SU_messages') file.

Note that the fall-back version of this mapping is simplified, it only accepts parameterless debugger commands.

Default Query Classes

Most of the default query classes are designed to support some specific interaction with the user within the Prolog development environment. The full list of query classes can be inspected in the file library('SU_messages'). Here, we only describe the two classes defined by 'SU_messages':query_class/5 that may be of general use:

QueryClass yes_or_no yes_no_proceed
Prompt ' (y or n) ' ' (y, n, p, s, a, or ?) '
InputMethod line line
MapMethod char([yes-"yY", no-"nN"]) char([yes-"yY", no-"nN", proceed-"pP", suppress-"sS", abort-"aA"])
FailureMode help_query help_query