The Sift reactive process is recursively defined. First, it outputs the first value v it receives, then it adds in front of itself a process which filters all multiple of v.
public class Sift extends ReactiveProcess
{
private Channel in, out, intern;
private int prime;
private boolean first = true;
public Sift(Channel in, Channel out)
{
this.in = in;
this.out = out;
}
public Sift(NrpMachine machine,String in, String out)
{
this.in = machine.getChannel(in);
this.out= machine.getChannel(out);
machine.add(this);
}
final public String toString(){
return "sift(" + prime + "," + out + "," + in + ")";
}
protected byte activation(Machine machine)
{
if (first){
byte b = fix(in,machine);
if (b == TERM){
prime = ((Integer)in.get()).intValue();
put(out,new Integer(prime),machine);
first = false;
return STOP;
}
return b;
}else{
intern = ((NrpMachine)machine).newChannel();
machine.add(new Filter(in,intern,prime));
machine.add(new Sift(intern,out));
return TERM;
}
}
}