Valued Occurences of Event

This demo illustrates the use of the new communication mechanism between parallel components introduced by SugarCubes v3.0: Valued occurences of event. This mechanism is fully compatible with the notion of event introduced since SugarCubes v1.0, but it allows now to communicate more informations (numerical values or objects) between parallel components.

When an event is generated, for example using the Generate primitive (SC.generate), a list of numerical values or objects can be associated to this generation. This list is memorized by the event structure as a new occurence during the instant. At each end of instant occurences of events are discarded.

This doesn't interfere with the classical pure event communication, as valued occurences are only handled by one special primitive called Callback (SC.callback). A Callback can scan every occurences of an event during one instant. All Callback primitives will be activated once for each occurences of an event during the instant they appear, and this in the same order as they are generated in the environment. So, every parallel components that have a Callback on the same event will see exactly the same occurences in the same order during one instant.

When activated a Callback receives a set of values associated to the occurence that has triggered the activation of the Callback. The Callback reacts by executing an atomic action which can handle those values. A Callback only terminates at the end of the instant, when no more generation of the event, to which it is bound to, can occure.

This demo makes an intensive use this powerfull broadcast communication mechanism. All reactive objects in this demo (spiders and workspaces) are parallel components (cubes) communicating together using this broadcast event mechanism. No one of those objects has any reference to the other objects in the system. So, by that way,objects are designed without having to take care of any other objects. The only point to be careful with, in order to acheive a good communication, is the event protocol and types of values associated to an occurence of event. This provide a great modularity in the design of applications.

In this demo one uses valued occurences to acheive these communications:


Content

The jar file contains sources and compiled classes. So, you can directly execute the callback demo just by adding the SugarCubesv3.0.3.jar package and the callback.jar package directly in your CLASSPATH.

You access to source files just by typing jar xvf callback.jar


Execution

First, you have to extract the spider picture from the jar archive: just type jar xvf callback.jar Spider.gif

To execute the demo, just type java -cp callback.jar:SugarCubesv3.0.3.jar demos.callback2D.Main

Note about multithreading

If your JVM doesn't support preemptive multithreading, this demo will not allow you to interact with it, as it doesn't explecitly release the control to give a chance to the graphical thread of the Java AWT system to be executed. To allow execution on a cooperatively scheduled JVM edit the source of the class Main (Main.java) and uncomment the line enabling the time reserve system of the SugarCubes:

        Program p = new Test().buildProgram();
        //m.enableTimeReserve(true,30);
        m.addProgram(p);

This will enable other threads to be executed as the react method will explicitly release the control in order to fit the timming constraints. The enableTimeReserve will enforce the react method to sleep (if needed) in order to have a duration of at least 1/30 milliseconds.
Of course, you can also edit the main loop (in the Main class) to explicitly release the control at some points using Thread.yield() or Thread.sleep().

You should obtain something like this: