A more complex plug-in in Java
The complex plug-in sample is comparable to the corresponding C and C++ complex plug-in samples.
The Java code for the ComplexPlugin class contains the public static methods: test1, test2, test3, and test4. It also contains an object, ComplexChunk that represents a complex type.
class ComplexChunk
{
long size;
double[] data;
public ComplexChunk(int size)
{
this.size = size;
this.data = new double[size];
for (int l = 0; l < size; ++l) {
this.data[l] = l;
}
print();
}
public void print()
{
System.out.println("Chunk size = "+size);
for (int l = 0; l < size; ++l) {
System.out.println("Chunk element ["+l+"] = "+data[l]);
}
}
}
/**
A more complex correlator plugin written in Java with multiple methods, arrays and chunks.
*/
public class ComplexPlugin
{
/** Prints the arguments and returns a string */
public static String test1(long l, double f, boolean b, String s)
{
System.out.println("integer value = "+l);
System.out.println("float value = "+f);
System.out.println("boolean value = "+b);
System.out.println("string value = "+s);
return "Hello, World";
}
/** Prints the (array) arguments and returns a string */
public static double test2(long[] ls, double[] fs,
boolean[] bs, String[] ss)
{
System.out.println("sequence size = "+ls.length);
for (int i = 0; i < ls.length; ++i) {
System.out.print("sequence element["+i+"]: ");
System.out.println("integer value = "+ls[i]);
}
System.out.println("sequence size = "+fs.length);
for (int i = 0; i < fs.length; ++i) {
System.out.print("sequence element["+i+"]: ");
System.out.println("float value = "+fs[i]);
}
System.out.println("sequence size = "+bs.length);
for (int i = 0; i < bs.length; ++i) {
System.out.print("sequence element["+i+"]: ");
System.out.println("boolean value = "+bs[i]);
}
System.out.println("sequence size = "+ss.length);
for (int i = 0; i < ss.length; ++i) {
System.out.print("sequence element["+i+"]: ");
System.out.println("string value = "+ss[i]);
}
return 2.71828;
}
/** Returns a chunk containing an array of 'l' doubles */
public static Object test3(int l)
{
return new ComplexChunk(l);
}
/** Takes a chunk from test3, mutates it and prints it */
public static void test4(Object o)
{
ComplexChunk cc = (ComplexChunk) o;
for (int i = 0; i < cc.size; ++i) {
cc.data[i] = Math.sqrt(cc.data[i]);
}
cc.print();
}
}
The complex_plugin.xml file is the plug-in's deployment descriptor and contains the following <plugin> stanza that specifies the name, class, and description for the plug-in.
<application-classes>
<plugin>
<plugin-name>ComplexPlugin</plugin-name>
<plugin-class>ComplexPlugin</plugin-class>
<description>A test plugin</description>
</plugin>
</application-classes>
The sample's ComplexPlugin.mon file contains the EPL code for the Apama application. It imports the plug-in and calls the various testx methods:
monitor ComplexPluginTest {
// Load the plugin
import "ComplexPlugin" as complex;
// To hold the return values
string str1;
string ret1;
float ret2;
// Opaque chunk value
chunk myChunk;
// Loop counter
integer i;
// Sequences for test2
sequence<integer> intSeq;
sequence<float> floatSeq;
sequence<boolean> boolSeq;
sequence<string> stringSeq;
action onload {
// Call test1 function
str1 := "Hello, Complex Plugin";
ret1 := complex.test1(42, 3.14159, true, str1);
log "complex.test1 = " + ret1 at INFO;
log "str1 = " + str1 at INFO;
// Initialize sequences
intSeq.setSize(10);
floatSeq.setSize(10);
boolSeq.setSize(10);
stringSeq.setSize(10);
i := 0;
while (i < 10) {
intSeq[i] := i;
floatSeq[i] := i.toFloat();
boolSeq[i] := true;
stringSeq[i] := "How long is a piece of string?";
i := i + 1;
}
// Call test2 function
ret2 := complex.test2(intSeq, floatSeq, boolSeq, stringSeq);
log "complex.test2 = " + ret2.toString() at INFO;
i := 0;
while (i < 10) {
log "intSeq[" + i.toString() + "] = " + intSeq[i].toString()
at INFO;
log "floatSeq[" + i.toString() + "] = " + floatSeq[i].toString()
at INFO;
log "boolSeq[" + i.toString() + "] = " + boolSeq[i].toString()
at INFO;
log "stringSeq[" + i.toString() + "] = " + stringSeq[i].toString()
at INFO;
i:= i + 1;
}
// Generate a new chunk
myChunk := complex.test3(20);
// Do some computation on the chunk
complex.test4(myChunk);
}
}