Event Presence Test Next: An Example Up: Event Based InstructionsPrevious: Preemption

Event Presence Test

When extends BinaryInstruction and gives the control to the left or right instruction according to an event configuration. Evaluation of the configuration only takes place at the instant thecontrol reaches the When  instruction and the choice of the branch to be executed is final.

 
public class When extends BinaryInstruction
{
  private Config config;
  private boolean confEvaluated = false;
  private boolean value;

  public When(Config config,Instruction th,Instruction el) 
  { 
    this.config = config;
    left = th;
    right = el;
  }

  public void reset(){ super.reset(); confEvaluated = false; }

  final public boolean equals(Instruction inst){
    return  super.equals(inst) 
          && config.equals(((When)inst).config);
  }

  final public String toString(){ 
    return "when "+config+" then "+left+" else "+right+" end"; 
  }

  final protected byte activation(Machine machine) 
  { 
      if (!confEvaluated){
          if (!config.fixed(machine)) return SUSP;
          confEvaluated = true;
          value = config.evaluate(machine);
          if(machine.isEndOfInstant()) return STOP;
      }
      return value ? left.activ(machine) : right.activ(machine);
  }
}