-
Notifications
You must be signed in to change notification settings - Fork 10
Builtins
Builtins provide useful functionality they extend JavaCspProcess
and provide an easy way to quickly develop CSP process networks, and include:
Is a consumer and continually reads from a channel and provides no feedback or output.
Is a clock which writes an Object down the channel every tick of the clock, the duration is configurable using the parameters in the constructor.
Is a connector, this reads from one channel and write the received object down two channels.
Each successive read of its channel will produce the next number in the fibonacci sequence.
Is used to delay the transportation of an object down a channel, An object is read and written down the output channel after a certain duration of time has passed. The Duration of time can be configured using the parameters in the constructor.
Write successive numbers down the channel, the number are integer sequenced in order of magnitude i.e. 0, 1, 2, 3, 4, 5 ... n
Writes the object down a channel which has been read.
Reads a number from the channel and multiplies by 2 i.e. x = channel.read(); channel.write(2x);
.
Reads a number from the channel and subtracts 2 i.e. x = channel.read(); channel.write(x - 1);
.
Prints the object read from a channel to stdio (System.out)
Reads a number from the channel and subtracts 2 i.e. x = channel.read(); channel.write(x + 1);
.
Continually write the value 0
down the channel.
With builtins, it is possible to build process networks similar to the one in this worked example. New builtins can be made to provide any function you require.
The image below shows the process network that will be constructed in this tutorial, it has no specific use but it serves as a good example of how builtins can be chained together to perform complex tasks.
In JavaCSP the network is constructed with the following code.
package examples.builtins;
import JyCSP.JavaCspChannel;
import JyCSP.Par;
import JyCSP.builtins.*;
public class example4 {
public static void main(String[] args) {
JavaCspChannel[] chnls = new JavaCspChannel[] { new JavaCspChannel(),
new JavaCspChannel(), new JavaCspChannel()};
Fibonacci f = new Fibonacci(chnls[0]);
Generator g = new Generator(chnls[1]);
Multi m = new Multi(chnls[0],chnls[1],chnls[2]);
Printer p = new Printer(chnls[2]);
Par par = new Par(f,g,m,p);
par.start();
}
}
In python the process network can be constructed with the following code
#!/usr/bin/env python2.6
from csp.cspthread import *
if __name__ == '__main__':
c = []
c.append(Channel())
c.append(Channel())
c.append(Channel())
f = Fibonacci(c[0])
g = Generate(c[1])
m = Multiply(c[0],c[1],c[2])
p = Printer(c[2])
par = Par(f,g,m,p)
par.start()
In Jython the code to construct the process network is
from Jycspthread import *
if __name__ == '__main__':
c = []
c.append(Channel())
c.append(Channel())
c.append(Channel())
f = Fibonacci(c[0])
g = Generate(c[1])
m = Multiply(c[0],c[1],c[2])
p = Printer(c[2])
par = Par(f,g,m,p)
par.start()