Tutorial

English

Français

Expilcitly schedule tasks accross instants

This simple example illustrates the use of the stop primitive in reactive programs. This instruction allows one to explicitly mark the frontier of instants in a sequence of code. 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 the sequence of instructions.

Source: Stops.java
import inria.meije.rc.sugarcubes.*;

// 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.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.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();
        }
    }
}

The stop instruction indicates explicitly the end of an instant of execution in a sequence. The sequence stops at this point for the current instant of execution and the execution of the sequence will resume immediatly after the stop instruction at the next instant.

How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).
javac -classpath SugarCubesv3.1.0.jar Stops.java

How to execute?

java -classpath SugarCubesv3.1.0.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: 


Using the Junior API

Source: Junior_Stops.java
import junior.Machine;
import junior.Jr;
import junior.Program;

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

        // Then one makes a reactive execution machine with the program p.
        Machine machine = Jr.Machine(p);

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

How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).
javac -classpath SugarCubesv3.1.0.jar Junior_Stops.java

How to execute?

java -classpath SugarCubesv3.1.jar:. Junior_Stops

Multiple consecutive stops.

SugarCubes provides an instruction to stop a program for multiple consecutive instant.

Source: Pauses.java
import inria.meije.rc.sugarcubes.*;

// This program shows the use of stop.

class Pauses
{
    public static void main(String[] args){
// First one writes a reactive program.
        Program p = SC.seq(
                           SC.print("Hello World!\n")
                           ,SC.pause(5)
                           ,SC.print("Hello World! later\n")
                           );

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

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

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

The instruction pause is used to stop for a finite number of instants. It gets that number as parameter.

Program p = SC.pause(5);

The instruction halt is used to stop a sequence for ever. It is useful when one wants a program never terminates but it has nothing more to do.

Program p = SC.halt();

How to compile?

(Instructions provided for a UNIX system using JDK 1.1.x or higher version number).
javac -classpath SugarCubesv3.1.0.jar Pauses.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. Pauses

You should get the following result:

instant 1: 
Hello World!
instant 2: 
instant 3: 
instant 4: 
instant 5: 
instant 6: 
Hello World! later
instant 7: 
instant 8: 
instant 9: 
instant 10: