Skip to content
snim2 edited this page Oct 3, 2012 · 1 revision

Builtins provide useful functionality they extend JavaCspProcess and provide an easy way to quickly develop CSP process networks, and include:

Blackhole

Is a consumer and continually reads from a channel and provides no feedback or output.

Clock

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.

Delta2

Is a connector, this reads from one channel and write the received object down two channels.

Fibonacci

Each successive read of its channel will produce the next number in the fibonacci sequence.

FixedDelay

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.

Generator

Write successive numbers down the channel, the number are integer sequenced in order of magnitude i.e. 0, 1, 2, 3, 4, 5 ... n

Id

Writes the object down a channel which has been read.

Mux2

Reads a number from the channel and multiplies by 2 i.e. x = channel.read(); channel.write(2x); .

Pred

Reads a number from the channel and subtracts 2 i.e. x = channel.read(); channel.write(x - 1); .

Prefix

Printer

Prints the object read from a channel to stdio (System.out)

Succ

Reads a number from the channel and subtracts 2 i.e. x = channel.read(); channel.write(x + 1); .

Zeroes

Continually write the value 0 down the channel.

Worked example

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.

Fib

In JavaCSP the network is constructed with the following code.

Java CSP

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();
	}
}

Python CSP

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()

Jython CSP

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()