An Example Next: Conclusion Up: Event Based Instructions Previous: Event Presence Test

An Example

In this example, one first adds an instruction which waits for an event named e and then prints ``e!'' to a machine . The machine is run and a copy of the previous instruction is also added to it. Then, the machine is run for the second time. Finally, an instruction which generates e is added, and the machine is run for the third time.

 

class Example1
{

  static int i = 1;

  static void run(Machine machine){
    System.out.print("instant "+(i++)+": ");
    machine.activ(machine); 
    System.out.println("");
  }

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

    Instruction inst = 
      new Seq(
        new Await(new PosConfig("e")),
        new PrintAtom("e! "));

    machine.add(inst);    
    run(machine);
    machine.add((Instruction)inst.clone());    
    run(machine);
    machine.add(new Generate("e"));
    run(machine);
  }
}

Execution gives:

 

instant 1: 
instant 2: 
instant 3: e! e!

Note that the two Await instructions are both fired during the same third instant where the event is generated (events are broadcast). Note also the use of the clone method to get a copy of the instruction.