Tutorial

English

Français

Control over instants

Goals:

Explicitly split a sequence into consecutive instants

This simple example illustrates the use of the stop primitive in reactive programs. This instruction allows one to explicitly set the frontier of instants in a sequential 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.Machine;
import inria.meije.rc.sugarcubes.SC;
import inria.meije.rc.sugarcubes.Program;

// This program shows the use of stop.

public class Stops
{
    public static void main(final String[] args){
        final Machine machine = SC.emptyMachine();        
        final Program print = SC.seq(
                                     SC.println("Hello World!")
                                     , SC.stop()
                                     , SC.println("This program")
                                     , SC.stop()
                                     , SC.println("displays a message")
                                     , SC.stop()
                                     , SC.seq(
                                              SC.println("which differs")
                                              , SC.stop()
                                              , SC.println("at each reactions")
                                              , SC.stop()
                                              , SC.println("It shows how to use the 'stop' and the 'seq' operators.")
                                              , SC.stop()
                                              , SC.println("---------------------")
                                              , SC.stop())
                                     , SC.seq(
                                              SC.print("Without stop, ")
                                              , SC.print("messages are displayed ")
                                              , SC.println("during the same reaction")
                                              , SC.stop()
                                              , SC.stop()
                                              , SC.print("Consecutive stops is like to have a nothing instruction in between.")
                                              , SC.println(" Nothing is done during this instant."))
                                     );
        machine.addProgram(print);
        machine.run();
    }
}

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 SugarCubesv4.0.0a4.jar Stops.java

How to execute?

java -classpath SugarCubesv4.0.0a4.jar:. Stops

You should get the following result:

1-: Hello World!
2-: This program
3-: displays a message
4-: which differs
5-: at each reactions
6-: It shows how to use the 'stop' and the 'seq' operators.
7-: ---------------------
8-: Without stop, messages are displayed during the same reaction
9-: 10-: Consecutive stops is like to have a nothing instruction in between. Nothing is done during this instant.

Using the Junior API

As one shows already in the tutorial about sequence. The junior interface only support binary sequence instruction, so to make large sequence, one has to compose it with elementary sequence primitive.

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 SugarCubesv4.0.0a4.jar Junior_Stops.java

How to execute?

java -classpath SugarCubesv4.0.0a4.jar:. Junior_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: 

Multiple consecutive stops.

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

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

// This program shows the use of the pause instruction.

public class Pause
{
    public static void main(final String[] args){
//this program makes a pause for 5 consecutive instants
        final Program p = SC.seq(
                           SC.println("Hello World!")
                           ,SC.pause(5)
                           ,SC.println("Hello World! ... later")
                           );
        SC.emptyMachine(p).run();
    }
}

The instruction pause is used to stop for a finite number of instants. It gets that number as parameter. For example, the following program pause for 10 instants

Program p = SC.pause(10);

The instruction pause can also use a JavaItegerExpression to parameter dynamically the number of instant to stop. For example, one defines a JavaItegerExpression as the following one which pick randomly an integer between 2 and 10 and returns it as an integer parameter:

class RandomIntegerExp implements JavaIntegerExpression
{
    public int evaluate(final Object self
                        , final LocalVariables vars
                        , final ReactiveEngine engine){
        new (int)(Math.random()*8)+2;
    }
}

Then one uses this atomic integer expression in a program like this one:

Program p = SC.seq(
                   SC.println("Hello World!")
                   , SC.pause(new RandomIntegerExp())
                   , SC.println("Hello World! ... later")
                   );

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 SugarCubesv4.0.0a4.jar Pause.java

How to execute?

java -classpath SugarCubesv4.0.0a4.jar:. Pause

You should get the following result:

1-: Hello World!
2-: 3-: 4-: 5-: 6-: Hello World! ... later

See also Source: RandomPause.java