Chapter 12

Event-Driven Actors and Action Rules

On this page

Many applications require event-driven computing. For example, an interactive GUI system needs to react to UI events such as mouse clicks on UI components; a Web service provider needs to respond to service requests; a constraint propagator for a constraint needs to react to updates to the domains of the variables in the constraint. Picat provides action rules for describing event-driven actors. An actor is a predicate call that can be delayed and can be activated later by events. Actors communicate with each other through event channels.

12.1 Channels, Ports, and Events

An event channel is an attributed variable to which actors can be attached, and through which events can be posted to actors. A channel has four ports, named ins, bound, dom, and any. Many built-ins in Picat post events. When an attributed variable is instantiated, an event is posted to the ins-port of the variable. When the lower bound or upper bound of a variable’s domain changes, an event is posted to the bound-port of the variable. When an inner element E, which is neither the lower nor the upper bound, is excluded from the domain of a variable, E is posted to the dom-port of the variable. When an arbitrary element E, which can be the lower or upper bound or an inner element, is excluded from the domain of a variable, E is posted to the any-port of the variable. The division of a channel into ports facilitates speedy handling of events. For better performance, the system posts an event to a port only when there are actors attached to the port. For example, if no actor is attached to a domain variable to handle exclusions of domain elements, then these events will never be posted.

The built-in post_event(X, T) posts the event term T to the dom-port of the channel variable X.

The following built-ins are used to post events to one of a channel’s four ports:

The call post_event(X, T) is the same as post_event_dom(X, T). This means that the dom-port of a finite domain variable has two uses: posting exclusions of inner elements from the domain, and posting general term events.

12.2 Action Rules

Picat provides action rules for describing the behaviors of actors. An action rule takes the following form:

  Head,Cond,{Event} = >Body

where Head is an actor pattern, Cond is an optional condition, Event is a non-empty set of event patterns separated by ’,’, and Body is an action. For an actor that is activated by an event, an action rule is said to be applicable if the actor matches Head and Cond is true. A predicate for actors is defined with action rules and non-backtrackable rules. It cannot contain backtrackable rules.

Unlike rules for a normal predicate or function, in which the conditions can contain any predicates, the conditions of the rules in a predicate for actors must be conjunctions of inline test predicates, such as type-checking built-ins (e.g., integer(X) and var(X)) and comparison built-ins (e.g., equality test X == Y , disequality test X !== Y , and arithmetic comparison X > Y ). This restriction ensures that no variables in an actor can be changed while the condition is executed.

For an actor that is activated by an event, the system searches the definition sequentially from the top for an applicable rule. If no applicable rule is found, then the actor fails. If an applicable rule is found, the system executes the body of the rule. If the body fails, then the actor also fails. The body cannot succeed more than once. The system enforces this by converting Body into ‘once Body’ if Body contains calls to nondeterministic predicates. If the applied rule is an action rule, then the actor is suspended after the body is executed, meaning that the actor is waiting to be activated again. If the applied rule is a normal non-backtrackable rule, then the actor vanishes after the body is executed. For each activation, only the first applicable rule is applied.

For a call and an action rule ‘Head,Cond,{Event} = >Body’, the call is registered as an actor if the call matches Head and Cond evaluates to true. The event pattern Event implicitly specifies the ports to which the actor is attached, and the events that the actor watches. The following event patterns are allowed in Event:

In an action rule, multiple event patterns can be specified. After a call is registered as an actor on the channels, it will be suspended, waiting for events, unless the atom generated occurs in Event, in which case the actor will be suspended after Body is executed.

The system has an event queue. After events are posted, they are added into the queue. Events are not handled until execution enters or exits a non-inline predicate or function. In other words, only non-inline predicates and functions can be interrupted, and inline predicates, such as X = Y , and inline functions, such as X + Y , are never interrupted by events.

Example

Consider the following action rule:

      p(X), {event(X,T)} => writeln(T).

The following gives a query and its output:

Picat> p(X), X.post_event(ping), X.post_event(pong)
ping
pong

The call p(X) is an actor. After X.post_event(ping), the actor is activated and the body of the action rule is executed, giving the output ping. After X.post_event(pong), the actor is activated again, outputting pong.

There is no primitive for killing actors or explicitly detaching actors from channels. As described above, an actor never disappears as long as action rules are applied to it. An actor vanishes only when a normal rule is applied to it. Consider the following example.

      p(X, Flag), 
          var(Flag), 
          {event(X,T)}
      => 
          writeln(T),
          Flag = 1.
      p(_, _) => true.

An actor defined here can only handle one event posting. After it handles an event, it binds the variable Flag. When a second event is posted, the action rule is no longer applicable, causing the second rule to be selected.

One question arises here: what happens if a second event is never posted to X? In this case, the actor will stay forever. If users want to immediately kill the actor after it is activated once, then users have to define it as follows:

      p(X, Flag), 
          var(Flag), 
          {event(X,O),ins(Flag)}
      => 
          write(O),
          Flag = 1.
      p(_, _) => true.

In this way, the actor will be activated again after Flag is bound to 1, and will be killed after the second rule is applied to it.

12.3 Lazy Evaluation

The built-in predicate freeze(X, Goal) is equivalent to ‘once Goal’, but its evaluation is delayed until X is bound to a non-variable term. The predicate is defined as follows:

     freeze(X, Goal), var(X), {ins(X)} => true.
     freeze(X, Goal) => call(Goal).

For the call freeze(X, Goal), if X is a variable, then the call is registered as an actor on the ins-port of X, and the call is then suspended. Whenever X is bound, the event ins is posted to the ins-port of X, which activates the actor freeze(X, Goal). The condition var(X) is checked. If true, the actor is suspended again; otherwise, the second rule is executed, causing the actor to vanish after it is rewritten into once Goal.

The built-in predicate different_terms(T1, T2 ) is a disequality constraint on terms T1 and T2 . The constraint fails if the two terms are identical; it succeeds whenever the two terms are found to be different; it is delayed if no decision can be made because the terms are not sufficiently instantiated. The predicate is defined as follows:

import cp.

different_terms(X, Y) =>
    different_terms(X, Y, 1).

different_terms(X, Y, B), var(X), {ins(X),ins(Y)} => true.
different_terms(X, Y, B), var(Y), {ins(Y)} => true.
different_terms([X|Xs], [Y|Ys], B) =>
    different_terms(X, Y, B1),
    different_terms(Xs, Ys, B2),
    B #= (B1 #\/ B2).
different_terms(X, Y, B), struct(X), struct(Y) =>
    if (X.name !== Y.name; X.length !== Y.length)
       B = 1
    else
       Bs = new_list(X.length),
       foreach (I in 1 .. X.length)
           different_terms(X[I], Y[I], Bs[I])
       end,
       max(Bs) #= B
    end.
different_terms(X, Y, B), X == Y => B = 0.
different_terms(X, Y, B) => B = 1.

The call different_terms(X, Y, B) is delayed if either X or Y is a variable. The delayed call watches ins(X) and ins(Y) events. Once both X and Y become non-variable, the action rule becomes inapplicable, and one of the subsequent rules will be applied. If X and Y are lists, then they are different if the heads are different (B1), or if the tails are different (B2). This relationship is represented as the Boolean constraint  B #= (B1 #' / B2). If X and Y are both structures, then they are different if the functor is different, or if any pair of arguments of the structures is different.

12.4 Constraint Propagators

A constraint propagator is an actor that reacts to updates of the domains of the variables in a constraint. The following predicate defines a propagator for maintaining arc consistency on X for the constraint X+Y #= C:

   import cp.

   x_in_c_y_ac(X, Y, C), var(X), var(Y),
      {dom(Y,Ey)}
       =>         
      fd_set_false(X, C-Ey).
   x_in_c_y_ac(X, Y, C) => true.

Whenever an inner element Ey is excluded from the domain of Y, this propagator is triggered to exclude C-Ey, which is the support of Ey, from the domain of X. For the constraint X+Y #= C, users need to generate two propagators, namely,

  x_in_c_y_ac(X, Y, C) and x_in_c_y_ac(Y, X, C)

to maintain the arc consistency. Note that in addition to these two propagators, users also need to generate propagators for maintaining interval consistency, because dom(Y, Ey) only captures exclusions of inner elements, and does not capture bounds. The following propagator maintains interval consistency for the constraint:

   import cp.

   x_add_y_eq_c_ic(X, Y, C), var(X), var(Y),
       {generated,ins(X),ins(Y),bound(X),bound(Y)}
   =>         
       X :: C-fd_max(Y) .. C-fd_min(Y),
       Y :: C-fd_max(X) .. C-fd_min(X).
   x_add_y_eq_c_ic(X, Y, C), var(X) =>
       X = C-Y.
   x_add_y_eq_c_ic(X, Y, C) =>
       Y = C-X.

When both X and Y are variables, the propagator x_add_y_eq_c_ic(X, Y, C) is activated whenever either X or Y is instantiated, or whenever the bounds of their domains are updated. The body maintains the interval consistency of the constraint X+Y #= C. The body is also executed when the propagator is generated. When either X or Y becomes non-variable, the propagator becomes a normal call, and vanishes after the variable X or Y is solved.