Subject: Re: [SWIPL] Newbie question for XPCE

From: João Mariz <[email-masked]>
Date: Mon, 21 May 2001 17:52:59 +0100

Hello Jan and SwiProlog users,

Now I am trying to build city/2 predicate (above); the difference is
that I am
trying to pass two attribute values, one from the choosen city, and the
other
from the button selected (Rules or Search); to other predicates.
I could assert something in meu_prolog/2 predicate (which I would
retract later
on), however I am more interested on passing those values through
attribute
values to the city/2 predicate.

thanks in advance for any help you can provide,

João

city(City, X):-
    new(C, dialog('My Expert System' ) ) ,
    send(C, append, new(Gr1, dialog_group('Choose a City'))),
    send(Gr1, append, new(LB, list_browser)),
    add_cities(LB),
    send(C, append, new(Bts, dialog_group('Options')), right),
    send(Bts, append,
               button('Rules' ,
                      and(message(@prolog, meu_prolog, LB?selection?key,
'Rules'),
                          message(C, destroy) ) ) ) ,
    send(Bts, append,
               button('Search' ,
                       and(message(@prolog, meu_prolog,
LB?selection?key,
'Search'),
                           message(C, destroy) ) ) ,  below ),
    send(Bts, append, button('Cancel', message(C, destroy)), below ) ,
/* How to get City and X values??? */
send(C, open)  .

meu_prolog(City, 'Rules'):-
     nl,nl, write('The City is ' ), write(City), nl,nl,
     write('My Expert System / Good Pratice Rules'), nl, nl, !.

meu_prolog( City, 'Search'):-
     nl,nl, write('The city is ' ), write(City), nl,nl,
     write('My Expert System / Search of Minimal Solution'), nl, nl, !.

add_cities(LB):-
  send_list(LB , append,
   ['Abrantes',
    'Açores',
    'Águeda',
    'Aguiar da Beira',
    'Alandroal', 'Coimbra', 'Lisboa', 'Porto', 'Faro'] ).

Jan Wielemaker wrote:

> > Hello SwiProlog users;
> >
> > I have done some work with SWI PROLOG. Now I am trying to do something
> > with XPCE so that my application becames more pretty.
> > I have developped a simple predicate [called city/0] that selectes text
> > from a list. I write it above:
> >
> > city:-
> >        new(C, dialog('Select a City')),
> >        send(C, append, new(LB, list_browser)),
> >        send(LB, width,  28),
> >             add_cities(LB),
> >        send(C, append,
> >                    new(Bts, dialog_group('Options')), below),
> >        send(Bts, append, button(quit, message(C, destroy))),
> >        send(Bts, append, button(ok, message(@prolog, listar,
> > LB?selection?key, C), right )),
> >        send(C, open) .
> >
> > listar(L, C):-
> >        get(L, name, Text_item),
> >        nl,  nl,  write('The city is '), write(Text_item), nl, nl,
> >        send(C, destroy) .
> >
> > add_cities(LB):-
> >         send(LB, append, new(Ti1, text_item('Abrantes', 'Abrantes'))),
> >         send(LB, append, new(Ti2, text_item('Coimbra', 'Coimbra'))),
> >         send(LB, append, new(Ti3, text_item('Lisboa', 'Lisboa'))),
> >         send(LB, append, new(X1, text_item('Golegã', 'golega'))),
> >         send(LB, append, new(X2, text_item('Salva Terra de Magos',
> > 'salva terra de magos'))),
> >         send(LB, append, new(Xl3, text_item('Chamusca',  'chamusca'))),
> >         send(LB, append, new(X4, text_item('Santarém',  'santarem'))),
> >         send(LB, append, new(S1, text_item('Porto', 'porto'))),
> >         send(LB, append, new(B2, text_item('Faro', 'faro'))),
> >         send(LB, append, new(N3, text_item('Évora',  'evora'))),
> >         send(LB, append, new(C4, text_item('Beja',  'beja'))), ! .
> >
> > However, after querying with city/0 predicate, when I select both, the
> > text from the list_browser and the ok button, I do not get a "yes"
> > answer on my XPCE ?- prompt. Perhaps I did not finnish city/0 predicate.
>
> City/0 simply returns after opening the window.  If you hit the ok button,
> it will print the selection and remove the window in a `call-back'.  The
> Prolog toplevel isn't aware of the call-back and doesn't repeat the prompt
> (this has been improved in 4.0.6 on Unix when using the readline library).
> If you hit return it will show the continuation prompt `|'.
>
> > If I have a city/0 predicate inside another predicate I get the answer
> > "no" at the XPCE '- prompt and I still have my XPCE window open.
> > How can I have a Yes answer? or pursuit with the city/0 predicate until
> > it finnishes?
>
> What you want is a blocking dialog.  There is a section on modal dialogs
> in the User Guide.  For short:
>
> city(City):-
>        new(C, dialog('Select a City')),
>        send(C, append, new(LB, list_browser)),
>        send(LB, width,  28),
>             add_cities(LB),
>        send(C, append,
>                    new(Bts, dialog_group('Options')), below),
>        send(Bts, append, button(quit, message(C, destroy))),
>        send(Bts, append, button(ok, message(C, return,
>                                 LB?selection?key), right )),
>        get(C, confirm, Answer),
>        send(C, destroy),
>         get(Answer, name, City).
>
> Next, a list_browser contains dict_items that are normally created from
> atoms.  So your add_cities can look like:
>
> add_cities(LB) :-
>         send_list(LB, append,
>                   [ 'Abrantes',
>                     'Coimbra',
>                     ....
>                   ]).
>
> And you won't need the get(Answer, name, City) anymore.  A text_item is
> a controller that allows the user to enter a value.  Due to type-conversion
> and automatic generation of printable labels, your code works, but doesn't
> really reflect the purpose.
>
>         Cheers --- Jan
>
>         --- Jan
>
>