|
|
|
This example shows how to build cyclic programs using inifinite loops.
Source: InfiniteLoops.javaimport inria.meije.rc.sugarcubes.*; class InfiniteLoops { public static void main(String[] args){ Program p1 = SC.loop(SC.seq(SC.print("Hello World! 1\n"),SC.stop())); Program p2 = SC.loop(SC.seq(SC.print("Hello World! 2\n"),SC.stop())); Program p3 = SC.loop(SC.seq(SC.print("Hello World! 3\n"),SC.stop())); Program p4 = SC.loop(SC.seq(SC.print("Hello World! 4\n"),SC.stop())); Machine machine = SC.machine(p1); for(int i = 1; i <= 10; i++){ System.out.println("instant "+i+": "); if(3 == i){ machine.addProgram(SC.merge(p2,SC.merge(p3,SC.seq(SC.stop(),p4)))); } machine.react(); } } }
Infinite loops are made of an instruction loop and the body of the loop is a program which is executed at each iteration.
Program p1 = SC.loop(SC.seq(SC.print("Hello World! 1\n"),SC.stop()));
The execution of the body can last multiple instants. When the body terminates its execution it is restarted immediately during the same instant. The loop instruction can only iterate when its body has finished its execution. But this can lead to pathological situations called Instantaneous Loops (when the body terminates its execution at the very same instant when it starts its execution) go to this section to learn more about how instantaneous loops are handled in SugarCubes.
javac -classpath SugarCubesv3.1.0.jar InfiniteLoops.java
java -classpath SugarCubesv3.1.0.jar:. InfiniteLoops
You should get the following result:
instant 1: Hello World! 1 instant 2: Hello World! 1 instant 3: Hello World! 1 Hello World! 2 Hello World! 3 instant 4: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4 instant 5: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4 instant 6: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4 instant 7: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4 instant 8: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4 instant 9: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4 instant 10: Hello World! 1 Hello World! 2 Hello World! 3 Hello World! 4
import junior.*; class Junior_InfiniteLoops { public static void main(String[] args){ Program p1 = Jr.Loop(Jr.Seq(Jr.Print("Hello World! 1\n"),Jr.Stop())); Program p2 = Jr.Loop(Jr.Seq(Jr.Print("Hello World! 2\n"),Jr.Stop())); Program p3 = Jr.Loop(Jr.Seq(Jr.Print("Hello World! 3\n"),Jr.Stop())); Program p4 = Jr.Loop(Jr.Seq(Jr.Print("Hello World! 4\n"),Jr.Stop())); Machine machine = Jr.Machine(p1); for(int i = 1; i <= 10; i++){ System.out.println("instant "+i+": "); if(3 == i){ machine.add(Jr.Par(p2,Jr.Par(p3,Jr.Seq(Jr.Stop(),p4)))); } machine.react(); } } }
javac -classpath SugarCubesv3.1.0.jar Junior_InfiniteLoops.java
java -classpath SugarCubesv3.1.0.jar:. Junior_InfiniteLoops
Instantaneous Loops are pathological situations in which the body of an infinite loop always terminates its execution at the very instant when it starts its execution (the execution of the body always last less than an instant). In those cases, the loop can never converge to the end of the instant and the whole reactive system loop for ever in the same instant and cannot progress.
Source: InstantaneousLoops.javaimport inria.meije.rc.sugarcubes.*; //One shows pathological infinite loops. class InstantaneousLoops { public static void main(String[] args){ Program print = SC.loop(SC.print("Hello World!\n")); Machine machine = SC.machine(); // One adds the looping program into the machine. machine.addProgram(print); for(int i = 1; i <= 10; i++){ System.out.println("instant "+i+": "); machine.react(); } } }
javac -classpath SugarCubesv3.1.0.jar InstantaneousLoops.java
java -classpath SugarCubesv3.1.0.jar:. InstantaneousLoops
You should get the following result:
instant 1: Hello World! Warning: instantaneous loop detected instant 2: Hello World! Warning: instantaneous loop detected instant 3: Hello World! Warning: instantaneous loop detected instant 4: Hello World! Warning: instantaneous loop detected instant 5: Hello World! Warning: instantaneous loop detected instant 6: Hello World! Warning: instantaneous loop detected instant 7: Hello World! Warning: instantaneous loop detected instant 8: Hello World! Warning: instantaneous loop detected instant 9: Hello World! Warning: instantaneous loop detected instant 10: Hello World! Warning: instantaneous loop detected
In SugarCubes instantaneous loop are broken by enforcing the execution of the body in parallel with a stop instruction. That way execution of the body last at least one instant, which prevents instantaneous loops to occur. So the whole system doesn't stale because of a bad designed component. In addition a warning message is dumped on the standard error stream to inform of the pathological situation.
Junior doesn't prevent instantaneous loops to be executed in a system. But if one uses the Junior-API of the SugarCubes, infinite loops inherit from SugarCubes the mechanism which prevent instantaneous loops to occur, which is a difference with the original Junior semantics.
The repeat instruction express finite loops in SugarCubes.
Source: FiniteLoops.javaimport inria.meije.rc.sugarcubes.*; //One shows use of finite loops. class FiniteLoops { public static void main(String[] args){ Program print = SC.repeat(5,SC.seq(SC.print("Hello World!\n"),SC.stop())); Machine machine = SC.machine(); machine.addProgram(print); for(int i = 1; i <= 10; i++){ System.out.println("instant "+i+": "); machine.react(); } } }
The repeat instruction specify a finite number of execution. So, instantaneous loops cannot occur because because loops are known to terminate in a finite time, even in the case of instantaneous bodies (body that terminate their execution in the very same instant of when they have start their execution). So there is no such mechanism in the repeat instruction as the one found in infinite loops.
javac -classpath SugarCubesv3.1.0.jar FiniteLoops.java
java -classpath SugarCubesv3.1.0.jar:. FiniteLoops
You should get the following result:
instant 1: Hello World! instant 2: Hello World! instant 3: Hello World! instant 4: Hello World! instant 5: Hello World! instant 6: instant 7: instant 8: instant 9: instant 10: