Comments:

This simple example illustrates the use of reactive instants. During five instants, the program progress (one step at each reaction of the execution machine) and the stop primitive is used to set the boundaries of an instant in a sequence of instructions.


How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).

javac -classpath SugarCubesv3.0.3.jar Stops.java


How to execute?

java -cp SugarCubesv3.0.3.jar:. Stops

You should get the following result:

        instant 1: 
        Hello World!
        instant 2: 
        This program
        instant 3: 
        displays a message
        instant 4: 
        which differs
        instant 5: 
        at each reactions
        instant 6: 
        It shows how to use the 'stop' and the 'seq' operators.
        instant 7: 
        ---------------------
        instant 8: 
        Without stop
        messages are displayed
        during the same reaction
        instant 9: 
        instant 10: 

import inria.meije.rc.sugarcubes.Machine;
import inria.meije.rc.sugarcubes.SC;
import inria.meije.rc.sugarcubes.Program;

// This program shows the use of stop.

class Stops
{
    public static void main(String[] args){
        // First one writes a reactive program.
        Program print = SC.seq(
            SC.print("Hello World!\n"),
            SC.stop(),
            SC.seq(
                SC.seq(SC.print("This program\n"),SC.stop(),
                        SC.print("displays a message\n"),SC.stop()),
                SC.seq(SC.print("which differs\n"),SC.stop(),
                        SC.print("at each reactions\n"),SC.stop()),
                SC.seq(SC.print("It shows how to use the 'stop' and the 'seq' operators.\n"),
                        SC.stop(),
                        SC.print("---------------------\n"),SC.stop()),
                SC.seq(SC.print("Without stop\n"),
                        SC.print("messages are displayed\n"),
                        SC.print("during the same reaction\n"),
                        SC.stop())
                )
            );

        // Then one makes a reactive execution machine.
        Machine machine = SC.machine();

        // One adds the program into the machine.
        machine.addProgram(print);

        // One makes the machine reacts for ten consecutive instants.
        for(int i = 1; i<=10; i++){
            System.out.println("instant "+i+": ");
            machine.react();
        }
    }
}