Developing Apama Applications > Writing Correlator Plug-ins > Writing Correlator Plug-ins in Java > Sample plug-ins in Java > A more complex plug-in
A more complex plug-in
The complex plug-in sample is comparable to the corresponding C and C++ complex plug-in samples.
The Java code for the complex_plugin class contains the public static methods: test1, test2, test3, and test4. It also contains an object, complex_chunk that represents a complex data type.
class complex_chunk
{
public long size;
public double[] data;
public complex_chunk(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 Java plugin with multiple methods, arrays and chunks.
*/
public class complex_plugin
{
/// 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 complex_chunk(l);
}
/// Takes a chunk from test3, mutates it and prints it
public static void test4(Object o)
{
complex_chunk cc = (complex_chunk) 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>complex_plugin</plugin-name>
<plugin-class>complex_plugin</plugin-class>
<description>A test plugin</description>
</plugin>
</application-classes>
The sample's complex_plugin.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 "complex_plugin" 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;

// Initialise 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);
}
}
Copyright © 2013 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.