next up previous index
Next: A More Advanced Up: Prolog as the Previous: System Event Handling

A Simple Example

Let us now rewrite the 'countries' example from Section 5.1 with Prolog as client. The following parts will be modified:

The modified Tcl procedures in the file country2.tcl. will look as follows:

# Query the Prolog database
proc query {w entries lb} {
    global var

    set list {}
    foreach e $entries {
        lappend list [$w.$e.e get]
    }
    prolog_event "find_country $list"
}

# Display the answers
proc answers {lb ans} {
    $lb delete 0 end
    foreach c $ans {
        $lb insert end $c
    }
}

There will be the following modifications in the Prolog file country2.pl:

:- lib(tk).
:- tk_source.

top :-
    tk([]),
    tk_source('country.tcl'),
    set_error_handler(333, handle_events/2),
    tcl make_display.

% The handler for the event 333. It will be invoked when we press
% the Query button.
handle_events(333, ["find_country", List]) :-
    !,                                                % no indexing on lists
    query_list(List, Args, Goals),
    Goal =.. [country|Args],
    findall(S, (Goal, Goals, term_string(Goal, S)), Countries),
    tcl_string(Countries, TclCountries),
    tcl('answers .l.l ##', TclCountries).
handle_events(333, ["exit"]) :-
    !,
    reset_error_handler(333).
...

The program works like the original one, but the Prolog top-level loop   is active all the time, it is possible both to type new queries and enter them through the visual interface.



Micha Meier
Tue Jul 2 09:49:39 MET DST 1996