Skip to main content

JR Operations and Capabilities

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

An operation (or op-method) has the same form as a method declaration but are 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 part :

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 use

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.

Comments

Comments powered by Disqus