/usr/share/doc/swi-prolog-doc/UserGuide/control.html is in swi-prolog-doc 5.6.59-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Programming in XPCE/Prolog: Section 10.1</TITLE><LINK REL=home HREF="index.html">
<LINK REL=contents HREF="Contents.html">
<LINK REL=index HREF="DocIndex.html">
<LINK REL=summary HREF="summary.html">
<LINK REL=previous HREF="techniques.html">
<LINK REL=next HREF="exeobjects.html">
<STYLE type="text/css">
/* Style sheet for SWI-Prolog latex2html
*/
dd.defbody
{ margin-bottom: 1em;
}
dt.pubdef
{ background-color: #c5e1ff;
}
pre.code
{ margin-left: 1.5em;
margin-right: 1.5em;
border: 1px dotted;
padding-top: 5px;
padding-left: 5px;
padding-bottom: 5px;
background-color: #f8f8f8;
}
div.navigate
{ text-align: center;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
}
div.title
{ text-align: center;
padding-bottom: 1em;
font-size: 200%;
font-weight: bold;
}
div.author
{ text-align: center;
font-style: italic;
}
div.abstract
{ margin-top: 2em;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
margin-left: 10%; margin-right:10%;
}
div.abstract-title
{ text-align: center;
padding: 5px;
font-size: 120%;
font-weight: bold;
}
div.toc-h1
{ font-size: 200%;
font-weight: bold;
}
div.toc-h2
{ font-size: 120%;
font-weight: bold;
margin-left: 2em;
}
div.toc-h3
{ font-size: 100%;
font-weight: bold;
margin-left: 4em;
}
div.toc-h4
{ font-size: 100%;
margin-left: 6em;
}
span.sec-nr
{
}
span.sec-title
{
}
span.pred-ext
{ font-weight: bold;
}
/* Footnotes */
sup.fn { color: blue; text-decoration: underline; }
span.fn-text: { display: none; }
sup.fn span {display: none;}
sup:hover span
{ display: block !important;
position: absolute; top: auto; left: auto; width: 80%;
color: #000; background: white;
border: 2px solid;
padding: 5px; margin: 10px; z-index: 100;
font-size: smaller;
}
</STYLE>
</HEAD>
<BODY BGCOLOR="white">
<DIV class="navigate"><A class="nav" href="index.html"><IMG SRC="home.gif" BORDER=0 ALT="Home"></A>
<A class="nav" href="Contents.html"><IMG SRC="index.gif" BORDER=0 ALT="Contents"></A>
<A class="nav" href="DocIndex.html"><IMG SRC="yellow_pages.gif" BORDER=0 ALT="Index"></A>
<A class="nav" href="summary.html"><IMG SRC="info.gif" BORDER=0 ALT="Summary"></A>
<A class="nav" href="techniques.html"><IMG SRC="prev.gif" BORDER=0 ALT="Previous"></A>
<A class="nav" href="exeobjects.html"><IMG SRC="next.gif" BORDER=0 ALT="Next"></A>
</DIV>
<H2><A NAME="sec:10.1"><SPAN class="sec-nr">10.1</SPAN> <SPAN class="sec-title">Control-structure
of <font size=-1>XPCE/P</font>rolog applications</SPAN></A></H2>
<A NAME="sec:control"></A>
<P><A NAME="idx:controlstructure:281"></A>This section deals with the
control-structure of interactive applications written in <font size=-1>XPCE/P</font>rolog
. Interactive graphical applications are very different from terminal
oriented applications. Terminal oriented applications often have a top
level control structure of the form:
<PRE class="code">
go :-
initialise,
main_loop.
main_loop :-
present_question,
read_answer(Answer),
process_answer(Answer),
main_loop.
</PRE>
<P>This schema is often refined with sub-loops dealing with
question/answers in a specific context.
<P>Many interactive graphical applications present various UI components
simultaneously: the user is free on which component s/he wants to
operate next. The users actions (keyboard-typing, mouse movement, and
mouse-clicking) must be related to the correct UI component and
interpreted accordingly in the application. This interpretation is much
more complex than the interpretation of a stream of ASCII characters
typed by the user.
<H3><A NAME="sec:10.1.1"><SPAN class="sec-nr">10.1.1</SPAN> <SPAN class="sec-title">Event-driven
applications</SPAN></A></H3>
<P><A NAME="idx:eventdriven:282"></A>One approach is to write a
main-loop that reads events, locates the UI-component referred to and
executes the appropriate actions. This loop, which must take care of
repaint-requests, various local feedback procedures (changing the
mouse-cursor, inverting objects, etc.), is complicated. The approach
taken by most graphical programming systems including XPCE , is to move
this loop into the infra-structure (i.e. into the XPCE kernel). The
application programmer creates the desired UI-components and supplies
code fragments that will be called by the main-loop when a certain event
happens. This control-structure is called <EM>event-driven</EM> control.
Consider a button:
<PRE class="code">
1 ?- new(B, button(hello,
message(@pce, write_ln, hello))),
send(B, open).
</PRE>
<P>In this example the application creates and displays a button UI
component and associates a code fragment (the message) to be executed
when the button is pressed. The XPCE kernel will loop through the main
event-loop. For each event it will locate the UI component that should
handle the event. When the button has recognised a `click' it will
execute the code fragment attached to it. This behaviour is part of the
definition of class <A class="" href="summary.html#class:button">button</A>.
<P>It is clear that this approach relieves the application programmer of
many of the complications associated with event-processing. As a
consequence, the `main-loop' of a XPCE application is no longer in the
application itself, but in the XPCE kernel. Below is an outline of the
control structure of a <font size=-1>XPCE/P</font>rolog application:
<PRE class="code">
go :-
initialise_database,
create_ui_components.
handle_help_pressed :-
create_help_window.
handle_solve :-
solve_the_problem,
create_solution_window.
...
</PRE>
<P>The predicate go will exit after it has initialised the application
and created the UI components. Assuming the application window has a
button invoking the predicate handle_help_pressed, XPCE will call this
predicate when the user presses the help button.
<H4><A NAME="sec:10.1.1.1"><SPAN class="sec-nr">10.1.1.1</SPAN> <SPAN class="sec-title">Keeping
control</SPAN></A></H4>
<P>The application code often wants to wait for the user to finish an
interaction. In <A class="sec" href="modal.html">section 4.4</A>, we
have seen a simple way of programming this using <A NAME="idx:framegetconfirm:283"></A>`<B>frame<CODE><-</CODE>confirm</B>'.
In this section, we will provide some other options.
<P><B>Message Queue</B> One possibility is to fall back to the <font size=-1>XPCE</font>
1 and 2 compatibility, where <A NAME="idx:prolog:284"></A><B>@prolog</B>
implements a queue of messages.
<A NAME="idx:prolog:285"></A><B>@prolog</B> is an instance of class <A class="" href="summary.html#class:host">host</A>.
The relevant methods are:
<DL>
<DT><STRONG>host ->call_back:</STRONG> <VAR>bool</VAR></DT>
<DD class="defbody">
The default is <A NAME="idx:on:286"></A><B>@on</B>. In this case, a
message to <A NAME="idx:prolog:287"></A><B>@prolog</B> is translated
into a predicate call on the Prolog engine. If <A NAME="idx:off:288"></A><B>@off</B>,
a message is appended to the <A NAME="idx:hostgetmessages:289"></A>`<B>host<CODE><-</CODE>messages</B>'
queue.
</DD>
<DT><STRONG>host ->catch_all:</STRONG> <VAR>Selector:name, Arg:any...</VAR></DT>
<DD class="defbody">
If <B><CODE><-</CODE>call_back</B> equals <A NAME="idx:on:290"></A><B>@on</B>,
use the <VAR>Selector</VAR> to determine the predicate to call, and the
arguments to construct the argument vector for the predicate. Call the
predicate and succeed or fail according to success or failure of the
Prolog predicate.
<P>If <B><CODE><-</CODE>call_back</B> equals <A NAME="idx:off:291"></A><B>@off</B>,
create a <A class="" href="summary.html#class:message">message</A> of
the form
<BLOCKQUOTE> message(<A NAME="idx:prolog:292"></A><B>@prolog</B>, <VAR>Selector</VAR>, <VAR>Arg</VAR>
...)
</BLOCKQUOTE>
<P>and append this message to the <B><CODE><-</CODE>messages</B>
queue.
</DD>
<DT><STRONG>host <-message:</STRONG> <VAR></VAR></DT>
<DD class="defbody">
message Return the <B><CODE><-</CODE>head</B> of the <B><CODE><-</CODE>messages</B>
queue. If the queue is empty, ensure
<B><CODE><-</CODE>call_back</B> is (temporary) set to <A NAME="idx:off:293"></A><B>@off</B>,
and dispatch events using
<A NAME="idx:objectnamedisplaysenddispatch:294"></A>`<B><B>@display</B><CODE>-></CODE>dispatch</B>'
as long as the <B><CODE><-</CODE>messages</B> queue is empty.
</DD>
</DL>
<P>Note that it is possible to create multiple instances of class
<A class="" href="summary.html#class:host">host</A>, to realise multiple
message queues. It is not desirable to modify the <A NAME="idx:prolog:295"></A><B>@prolog</B>
host object, as other code may rely on the <B><CODE><-</CODE>call_back</B>
properties of <A NAME="idx:prolog:296"></A><B>@prolog</B>.
<BLOCKQUOTE>
<B>Warning</B> During normal operation, event processing guards the
objects created that are not assigned to any other object and destroys
all such objects after the event has completely been processed (see <A class="sec" href="memory.html">section
E</A>. Using the host message queue mechanism, the Prolog programmer
becomes responsible for such objects. For example, the message object
returned should be discarded using <A NAME="idx:objectsenddone:297"></A>`<B>object<CODE>-></CODE>done</B>'
after processing.
</BLOCKQUOTE>
<P><B>Explicit dispatching</B> An alternative to the above, and the
<A NAME="idx:framegetconfirm:298"></A>`<B>frame<CODE><-</CODE>confirm</B>'
mechanism is to dispatch the events yourself. This is realised using <CODE>send(<A NAME="idx:display:299"></A><B>@display</B>,
dispatch)</CODE>, described below. This mechanism is the base of all the
others. It should be used to realise different interaction schemas than
the default callback schema.
<DL>
<DT><STRONG>display ->dispatch:</STRONG> <VAR></VAR></DT>
<DD class="defbody">
Process events and return on any of the following conditions
<UL>
<LI><I>event has been processed</I><BR>
Either a normal event, a timer or an input stream has been processed.
The method fails in this case.
<LI><I>timeout</I><BR>
The timeout (see <A NAME="idx:displaymanagersenddispatch:300"></A>`<B>display_manager<CODE>-></CODE>dispatch</B>')
has expired. The method fails in this case.
<LI><I>Input on the console</I><BR>
There is input from the Prolog window. The message succeeds in this
case.
</UL>
<P>For example, the following processes events in call-back style until
the fact <A NAME="idx:quit0:301"></A><SPAN class="pred-ext">quit/0</SPAN>
is in the Prolog database:
<PRE class="code">
:- dynamic
quit/0.
process_to_quit :-
repeat,
send(@display, dispatch),
quit, !.</PRE>
<P></DD>
</DL>
<H3><A NAME="sec:10.1.2"><SPAN class="sec-nr">10.1.2</SPAN> <SPAN class="sec-title">XPCE
and existing applications</SPAN></A></H3>
<P>Due to the different control-regime described in the previous
section, traditional terminal oriented applications are not easily
transformed into <font size=-1>XPCE/P</font>rolog graphical
applications. Depending on the application, there are two ways to
proceed.
<P>The first is to keep the existing control-regime. This implies that
the questions asked on the terminal will be replaced by <A NAME="idx:modal:302">modal</A>
dialog windows. The main loop will be:
<PRE class="code">
go :-
initialise_database,
create_dialog(Dialog).
main_loop(Dialog) :-
fill_dialog_with_next_question(Dialog),
send(Dialog, fit),
get(Dialog, confirm, Answer),
process_answer(Answer),
main_loop(Dialog).
</PRE>
<P>This example reuses the same dialog window for all questions. It is
trivial to change this loop to use a new dialog window for each
question. Output from the program may be presented in other windows. The
approach does not exploit the potentially larger freedom for the user
that is possible in graphical user interfaces.
<P>If the application could be viewed as a number of commands operating
on some data-structure and this data-structure is stored on the Prolog
heap using <A NAME="idx:assert1:303"></A><SPAN class="pred-ext">assert/1</SPAN>
or <A NAME="idx:recorda2:304"></A><SPAN class="pred-ext">recorda/2</SPAN>
one could consider rewriting the toplevel control and provide a more
flexible interface.
<P></BODY></HTML>
|