|
|
|
This simple example illustrates the very basic structure of a reactive program using SugarCubes:
// One import important classes and interfaces of the SugarCubes API. // The Machine interface allows the program to handle the reference to a // reactive machine. import inria.meije.rc.sugarcubes.Machine; // The Program interface allows the program to handle the reference to a // reactive program. import inria.meije.rc.sugarcubes.Program; // The class SC allows the program to build SugarCubes objects and especially // to build Program by building a tree of instructions. import inria.meije.rc.sugarcubes.SC; public class HelloWorld { public static void main(final String[] args){ // First one makes a reactive execution machine. It is usually the first // operation to do. final Machine machine = SC.machine(); // Then one writes a reactive program. // This a very simple one made of a simple atomic // action displaying the text "Hello World!" on the // standard text output. Atomic actions are executed // without any interruption until its completion. final Program print = SC.print("Hello World!\n"); // 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(); } } } // As a result the message "Hello World!" is displayed on the standard output // only at the first instant, during which the atomic action is executed and // terminates forever.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;The purpose of this interface is only to provide a type to handle reactive machines; it is not the task of the programmer to implement this interface. It provides the basic operations one can execute on a reactive execution machines (make a machine react, add a program into the machine, generate events, ...). Reactive Machines are created using calls of static methods on the class SC:
final Machine machine = SC.machine();Reactive programs in SugarCubes are handled through the Program interface :
import inria.meije.rc.sugarcubes.Program;As for the Machine interface it is not the task of the programmer to implement the Program interface. The purpose of this interface is only to provide a type to handle reactive programs in SugarCubes. It provides a set of methods that one can invoke on a program (get a copy of a Program, transform it into a Java String using the reactive script 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.
final Program print = SC.print("Hello World!\n");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();
javac -classpath SugarCubesv4.0.0a4.jar HelloWorld.java
java -classpath SugarCubesv4.0.0a4.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 (first iteration of the loop) 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.*; public class AnotherHelloWorld { public static void main(final String[] args){ // First one writes a reactive program. final Program print = SC.print("Hello World!\n"); // Then one makes a reactive execution machine with the // program directly added. final 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 creates 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);
javac -classpath SugarCubesv4.0.0a4.jar AnotherHelloWorld.java
java -classpath SugarCubesv4.0.0a4.jar:. AnotherHelloWorld
In this example one learns to program SugarCubes using the Junior API.
The same program using the Junior API leads to:
Source: Junior_HelloWorld.javaimport junior.Machine; import junior.Jr; import junior.Program; class Junior_HelloWorld { public static void main(final String[] args){ // First one writes a reactive program. final 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 final 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(); } } }
javac -classpath SugarCubesv4.0.0a4.jar Junior_HelloWorld.java
java -classpath SugarCubesv4.0.0a4.jar:. Junior_HelloWorld