Tutorial

English

Français

Dynamicity

This example illustrates the ability to extend dynamically a running reactive system, by adding a new reactive component in parallel between two instants of execution.

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

//In this example one dynamicaly adds a new copy of a program while
//the machine is running.

class AddProgram
{
    public static void main(String[] args){
        Program p = SC.seq(
                           SC.print("Hello World! 1\n")
                           ,SC.stop()
                           ,SC.print("Hello World! 2\n")
                           ,SC.stop()
                           ,SC.print("Hello World! 3\n")
                           ,SC.stop()
                           ,SC.print("Hello World! 4\n")
                           ,SC.stop()
                           );

        Machine machine = SC.machine(p);
        for(int i = 1; i <= 10; i++){
            System.out.println("instant "+i+": ");
            //Before reaction for the 3th instant one adds a copy of print into
            //the machine.
            if(3 == i){ machine.addProgram(p.getCopy()); }
            //Before reaction for the 5th instant one adds a copy of print into
            //the machine.
            if(5 == i){ machine.addProgram(p.getCopy()); }
            machine.react();
        }
    }
}

The method getCopy of the Program interface allows one to get an exact copy of a reactive program in order to add it in a reactive machine.

p.getCopy()

This is required beacause reactive programs are not reentrant. That means taht two concurent thread of execution cannot run concurently through the same program. When one adds a program into a machine, while the program gets executed, the internal state of the program (the state of the instructions that are node of the program tree) is modified. So to avoid possible interferences between the execution of twice the same program, it is strongly recommended to make a copy of the program (even if actual implementation of the method addProgram in SugarCubes does a copy of each program added).

How to compile?

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

How to execute?

java -classpath SugarCubesv3.1.0.jar:. AddProgram

You should get the following result:

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


The Junior API

Source: Junior_AddProgram.java
import junior.*;

class Junior_AddProgram
{
    public static void main(String[] args){
        // First one writes a reactive program.
        Program p = Jr.Seq(Jr.Print("Hello World! 1\n"),Jr.Stop());
        p = Jr.Seq(p,Jr.Seq(Jr.Print("Hello World! 2\n"),Jr.Stop()));
        p = Jr.Seq(p,Jr.Seq(Jr.Print("Hello World! 3\n"),Jr.Stop()));
        p = Jr.Seq(p,Jr.Seq(Jr.Print("Hello World! 4\n"),Jr.Stop()));

        Machine machine = Jr.Machine(p);
        for(int i = 1; i <= 10; i++){
            System.out.println("instant "+i+": ");
            //Before reaction for the 3th instant one adds a copy of print into
            //the machine.
            if(3 == i){ machine.add(p.copy()); }
            //Before reaction for the 5th instant one adds a copy of print into
            //the machine.
            if(5 == i){ machine.add(p.copy()); }
            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_AddProgram.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. Junior_AddProgram