I’m a swiss Master student in Computer Science. I’m very interested in C++, open source projects, Linux, Intel Assembly and Agile. I'm currently working on Eddi, a new programming language that I created to improve my skills in C++. I've also worked a lot on Java technologies (Sprint, Osgi, Play!, ...), but I'm not currently working with Java anymore. Baptiste is a DZone MVB and is not an employee of DZone and has posted 51 posts at DZone. You can read more from them at their website. View Full User Profile

JR Operations and Capabilities

07.01.2010
| 3121 views |
  • submit to reddit

Now that we’ve seen how to use semaphores and monitors in JR, we must go to message passing. But first, we must learn how to use operations and capabilities in JR. These concepts are used in JR for message passing, so we must learn them first.

An operation (or op-method) has the same form as a method declaration but is declared with a op keyword. An operation can be invoked the same way as a normal method or can be invoked with a call statement (also with a send statement as we’ll see in next post about JR).

A little example :

public class Operations1 {
public static op int sum(int i1, int i2){
System.out.println("sum()");

return i1 + i2;
}

public static void main(String... args){
System.out.println(sum(2, 2));
call sum(3, 3);
}
}

You can also write the operation in two parts :

public static op int sum(int i1, int i2);
public static int sum(int i1, int i2){
System.out.println("sum()");

return i1 + i2;
}

This is exactly the same. As you will see in the next post, we’ll also use operations only declared with no method body.

When you’ve operations, you can refer to them using capabilities. The capabilities are a kind of function pointer. You can declare a capability like that :

cap operation_signature cap_name

You can invoke a capability like a normal method. Here is a little example :

public class Operations3 {
public static op int sum(int i1, int i2){
System.out.println("sum()");

return i1 + i2;
}

public static void main(String... args){
cap int (int, int) cap_sum; //Declare the capability

cap_sum = sum; //Make a reference to sum operations

System.out.println(cap_sum(2, 2));
}
}

And you can also pass them as arguments, that can be useful to generify a code. Here is a little example that uses this approach:

import java.util.Random;

public class Operations3 {
public static op int square(int x){
return x * x;
}

public static op int dec(int x){
return x - 3;
}

public static op int inc(int x){
return x + 11;
}

public static op int add(cap int (int) f1, cap int (int) f2, int result){
return f1(result) + f2(result);
}

public static op int sub(cap int (int) f1, cap int (int) f2, int result){
return f1(result) + f2(result);
}

public static void main(String... args){
cap int (int) functions[] = new cap int (int)[3];
functions[0] = square;
functions[1] = dec;
functions[2] = inc;

cap int (cap int (int), cap int (int), int)[] double_functions = new cap int (cap int (int), cap int (int), int)[2]
double_functions[0] = add;
double_functions[1] = sub;

int result = 100;
Random random = new Random();

for(int i = 0; i < 10; i++){
int index1 = random.nextInt(double_functions.length);
int index2 = random.nextInt(functions.length);

result = double_functions[index1](functions[index2], functions[index2], result);
}

System.out.println(result);
}
}

Now you know everything about operations and capabilities. In the next post, we’ll learn asynchronous message passing in JR.

From http://www.baptiste-wicht.com/2010/06/jr-operations-and-capabilities/

Published at DZone with permission of Baptiste Wicht, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)