An Example Next: Infinite Loop Up: Basic InstructionsPrevious: Atoms

An Example

The little example we consider consists of running three instants of a machine whose program could be represented by:

 
    (
       stop;{System.out.print("left ")} 
    || 
       {System.out.print("right ")}
    ); 
    {System.out.print("end ")}

This code fragment is actually a reactive script.

First, one defines a new machine and an instruction using Stop, Seq, Merge, and PrintAtom; then, the instruction is added to the machine program; finally, three machine activations are provoked. The code is:

 
class Example
{
  public static void main (String argv[])
  {
    Machine machine = new Machine();

    Instruction inst = 
      new Seq(
        new Merge(
          new Seq(new Stop(),new PrintAtom("left ")),
          new PrintAtom("right ")),
        new PrintAtom("end "));  

    machine.add(inst);        


    for (int i = 1; i<4; i++){ 
       System.out.print("instant "+i+": ");
       machine.activ(machine); 
       System.out.println("");
    }
  }
}

Execution of this class gives:

 

instant 1: right 
instant 2: left end 
instant 3:

Note that termination of the Merge only occurs at the second instant because of the Stop instruction in the first branch. Note also that printing of end occurs in the second instant. Sequencing is ``instantaneous'', that is control goes to the second component of the sequence as soon as the first one terminates.