choices may depend on some priority
order, as in standard BPJ execution; the
mechanism may use look-ahead subject to desired properties of the resulting event sequence, as in smart play-out19, 29 in LSC; it may vary over time,
based on learning; 13 or, as in Kugler et
al., 34 the entire execution may diverge
into multiple concurrent paths.
The programming idioms of
request, wait for, block thus express
multi-modality. Reminiscent of modal verbs
in a natural language (such as shall,
can or mustn’t), they state not only what
must be done (and how) as in standard
programming, but also what may be
done, and, more uniquely to behavioral programming, what is forbidden and
therefore must not be done.
Behavioral programming principles can be readily implemented
as part of different languages and
programming approaches, with possible variations of idioms. In addition
to Java with the BPJ package25 (
discussed later in more detail) we have
implemented them in the functional
language Erlang26, 43 and Shimony et
al. 42 applied them in the PicOS environment using C. Implementations
in visual contexts beyond the original
Play-Engine include PlayGo23 and SBT
by Kugler et al. 34
In behavioral programming, all
one must do in order to start developing and experimenting with scenarios
that will later constitute the final system, is to determine the common set
of events that are relevant to these scenarios. While this still requires contemplation, it is often easier to answer
the question “what are the events?”
than “which are the objects/functions,
etc.?” By default, events are opaque entities carrying nothing but their name,
but they may be extended with rich
data and functionality.
to specify its requested, waited-for and
blocked events as follows:
bSync(requested Events,
waitedForEvents,
blockedEvents);
By calling bSync the b-thread suspends itself until all other b-threads
are at a synchronization point and
is resumed when an event that it requested or waited for is selected, as
described below.
To enforce predictable and repeatable execution, we require that the
event selected at each synchronization
point be uniquely defined. To this end,
the programmer assigns a unique priority to each b-thread, and places the
requested events of each b-thread in an
ordered set. The event selection mechanism in BPJ then uses this ordering to
choose the first event that is requested
and not blocked.
The source code package of BPJ is
available online at http://www.b-prog.
org with examples and video demonstrations.
Example: Water flow control. To il-
lustrate how these constructs can be
used to allow new behaviors to non-
intrusively affect existing ones, con-
sider scenarios that are part of a system
that controls hot and cold water taps,
whose output flows are mixed.
Figure 3. Visualizing an execution of the water-tap application with traceVis. selected
events are marked with a green star; blocked events are marked with a red square; cells
marked R/W/B show requested, waited for, and blocked events.
AddHotThreeTimes
LeaderReset
Leader
AddColdThreeTimes
Active
Interleave
1 AddHot
R AddHot
W
B
R Add Hot
W
B
R
AddHot W
B AddCold
2 AddCold
R AddHot
W
B
R AddCold
W
B
R
AddCold W
B AddHot
Programming Behaviors in Java
Our implementation of behavioral programming in Java uses the BPJ package. 25 With BPJ, each behavior thread
is an instance of the class BThread.
Events are instances of the class
Event or classes that extend it (mainly
for adding data to events). The logic
of each behavior is coded as a method
supplied by the programmer, which
in turn invokes the method bSync to
synchronize with other behaviors, and
3 AddHot
R AddHot
W
B
R Add Hot
W
B
R
AddHot W
B AddCold
4 AddCold
R AddHot
W
B
R AddCold
W
B
R
AddCold W
B AddHot
juLy 2012 | voL. 55 | no. 7 | CommuniCations oF the aCm 93