Machines Next: Basic Instructions Up: The Main SUGARCUBES Classes Previous: BinaryInstruction Class

Machines

A reactive machine of the Machine class runs a program which is an instruction. Initially programs are the Nothing instruction (defined in section 3.1) which does nothing and terminates instantaneously.

Basically, Machine detects the end of the current instant, that is when all parallel instructions of the program are terminated or stopped. There is another case where the end of the current instant is detected: when there is no new move in the program after two activations of it; in this case, there is no hope that new activations will change anything to the situation and the end of the instant can be safely set. In this case the machine activates the program once more, after having set the end of instant flag, to let suspended instructions stop. Code for this is (see the activation method of Machine below):

while ((res = program.activ(this)) == SUSP){
   if (move) move = false; else endOfInstant = true;
}

Two methods are used to manage the two flags move and endOfInstant: newMove which sets the move flag to indicate that something new happens in the program (thus, end of instant has to be postponed), and isEndOfInstant which tests the endOfInstant flag.

The add method adds a new instruction to the program; this new instruction is run in parallel with the previous program, using the Merge parallel instruction defined in section 3.3:

public void add(Instruction inst){ 
  program = new Merge(program,inst); 
  newMove();
}

Note that newMove is called to let the new instruction execute during the current instant.

Machine contains an environment named eventEnv to deal with events (events are described in section 5).

Machine extends Instruction and thus, to execute the program for one instant simply means to activate the machine. The class Machine  has the following structure:

 
public class Machine extends Instruction
{
  public Instruction program = new Nothing();
  protected EventEnv eventEnv = new EventEnv();

  protected int instant = 1;
  protected boolean endOfInstant = false, move = false;

  public void newMove()           { move = true; }
  public int currentInstant()     { return instant; }
  public boolean isEndOfInstant() { return endOfInstant; }

  public Object clone(){ ... }

  public void add(Instruction inst){
     program = new Merge(program,inst); 
     newMove();
  }

  /** Methods to use the event environment. */
  public Event getEvent(String name){ ... }
  public boolean isGenerated(String name){ ... }
  public void generate(String name){ ... } 
  public void putEvent(String name,Event event){ ... }

  protected byte activation(Machine machine){ 
     ... 
    endOfInstant = move = false;
    while ((res = program.activ(this)) == SUSP){
       if (move) move = false; else endOfInstant = true;
    }
    instant++;
    ...
  }
}