Binary Functions Next: Sum Process Up: Reactive ProcessesPrevious: The Funnel Process

Binary Functions

The abstract class BinaryFunction  is the class of processes that take an item on each of their two input channel, then apply a function fun to these idems and put the result on their output.

public abstract class BinaryFunction extends ReactiveProcess
{
  protected Channel left, right, out;
  protected boolean getLeft,getRight;

  public BinaryFunction(Channel left,Channel right,Channel out)
  {
    this.left  = left;
    this.right = right;
    this.out   = out;
    getLeft = getRight = false;
  }

  public BinaryFunction(NrpMachine machine,String left,
                                        String right,String out)
  {
    this.left  = machine.getChannel(left);
    this.right = machine.getChannel(right);
    this.out   = machine.getChannel(out);
    getLeft = getRight = false;
    machine.add(this);
  }


  protected abstract Object fun(Object arg1,Object arg2);

  protected byte activation(Machine machine)
  {
    if (fix(left,machine) == TERM ){ getLeft = true; }
    if (fix(right,machine) == TERM ){ getRight = true; }
    if (getLeft && getRight){
      getLeft = getRight = false;
      put(out,fun(left.get(),right.get()),machine);
      return STOP;
    }
    return SUSP;
  }
}