Tutorial

English

Français

Hello World !

This simple example illustrates the very basic structure of a reactive program using SugarCubes:

Source: HelloWorld.java
import inria.meije.rc.sugarcubes.Machine;
import inria.meije.rc.sugarcubes.SC;
import inria.meije.rc.sugarcubes.Program;

class HelloWorld
{
    public static void main(String[] args){
        // First one writes a reactive program.
        Program print = SC.print("Hello World!\n");

        // 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();
        }
    }
}
Reactive programs in SugarCubes are handled through the Program interface :
import inria.meije.rc.sugarcubes.Program;
The purpose of this interface is only to provide a type to handle programs it is not the task of the programmer to implement this interface. It provides the basic operations one can execute on a reactive program (make a copy of the program, transform it into a string representing the reactive program in Reactive Scripts syntax). Programs are created by calls to methods (static methods) of the class SC:
import inria.meije.rc.sugarcubes.SC;
The methods of the class SC are like constructors which allocate and initialize all objects (reactive instructions, event configurations, execution machine, ...) in SugarCubes. In this example a very simple reactive program is made of a simple atomic action which display the message "Hello World !" on the standard output.
Program print = SC.print("Hello World!\n");
To execute such a program one needs to initialize a reactive execution machine. In SugarCubes execution machines are handled through the Machine interface.
import inria.meije.rc.sugarcubes.Machine;
As for the Program interface it is not the task of the programmer to implement the Machine interface. The purpose of this interface is only to provide a type to handle reactive execution machines in SugarCubes. It provides a set of methods that one can invoke on a reactive machine. Reactive Machines are created using calls of static methods on the class SC:
Machine machine = SC.machine();
Then one adds a reactive program into a reactive execution machine by calling the method addProgram on the reactive machine:
machine.addProgram(print);
Finally one makes the reactive system running by cyclically calling the method react (ie. execute one instant) of the machine:
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 HelloWorld.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. HelloWorld

You should get the following result:

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

During the first reaction of the execution machine, the program is executed, that means the atomic action is activated displays the "HelloWorld" message and immediately terminates. The instant terminates and nothing is done during the subsequent reactions.

An alternative way is to build a reactive machine with an initial program.
Source: AnotherHelloWorld.java

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

class AnotherHelloWorld
{
    public static void main(String[] args){
        // First one writes a reactive program.
        Program print = SC.print("Hello World!\n");

        // Then one makes a reactive execution machine with the
        // program directly added as initial program.
        Machine machine = SC.machine(print);

        // One makes the machine reacts for ten consecutive instants.
	for(int i = 1; i <= 10; i++){
            System.out.println("instant "+i+": ");
            machine.react();
        }
    }
}
In this example, one create a machine by providing the program print as initial program. So it is no more necessary to add the program into the reactive machine using the addProgram method.
Machine machine = SC.machine(print);

How to compile?

javac -classpath SugarCubesv3.1.0.jar AnotherHelloWorld.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. AnotherHelloWorld

The Junior API

In this example one learns to program SugarCubes using the Junior API.

The same program using the Junior API leads to:

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

class Junior_HelloWorld
{
    public static void main(String[] args){
        // First one writes a reactive program.
        Program print = Jr.Print("Hello World!\n");

        // Then one makes a reactive execution machine.
        // In junior the machine needs an initial program which is set here to nothing
        Machine machine = Jr.Machine(Jr.Nothing());

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

        // 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?

javac -classpath SugarCubesv3.1.0.jar Junior_HelloWorld.java

How to execute?

java -classpath SugarCubesv3.1.0.jar:. Junior_HelloWorld