Generated Java operator
The operator spl.utility.JavaOp can generate Java™ source templates for the specific invocation of the operator.
The generated templates include generated interfaces specific to the input and output port schemas. These interfaces include getter and setter methods corresponding to the type and name of each attribute in the port's schema. These templates are generated by the SPL compiler when the JavaOp invocation has the generated parameter set to true.
Start with this example.
stream <int32 id, ustring tag, list<float64> data> CoolStream
= JavaOp(SENSORS)
{
param
generated: true;
className: "com.acme.streams.CoolOperator";
classLibrary: "";
}
Now look through the key aspects of the generated abstract operator.
First, the abstract class definition.
package com.acme.streams;
// Required imports from Streams API (only one shown)
import com.ibm.streams.operator.AbstractOperator;
public class AbstractCoolOperator extends AbstractOperator
Then, the input ports.
public interface IPort0 {
public int get_id();
public long[] get_timestamps();
public short[][] get_readings();
public String get_tag();
public Tuple getTuple();
}
// Tuples for port 0 are delivered to the operator via this method.
protected abstract void process0(IPort0 tuple) throws Exception;
// Interface for output port 0
public interface OPort0 {
public int get_id();
public String get_tag();
public double[] get_data();
public void set_id(int value);
public void set_tag(String value);
public void set_data(double[] value);
public OutputTuple getOutputTuple();
}
// Method to obtain output description for port 0
public StreamingOutput<OPort0> getOutput0();
// Method to submit tuples to port 0.
public boolean submit0(OPort0 tuple) throws Exception { … }
The template for the concrete operator class is created only if the source file does not exist. Here is an example of a generated template, showing two methods that need to be completed (marked with TODO) to provide the required function.
package com.acme.streams;
// Required imports from Streams API (only one shown)
import com.ibm.streams.operator.OperatorContext;
public class CoolOperator extends AbstractCoolOperator {
@Override
public void initialize(OperatorContext context) throws Exception {
super.initialize(context);
// TODO Auto-generated method stub
}
@Override
protected void process0(IPort0 tuple) throws Exception {
// TODO Auto-generated method stub
}
}
package com.acme.streams;
import com.ibm.streams.operator.OperatorContext;
public class CoolOperator extends AbstractCoolOperator {
@Override
public void initialize(OperatorContext context) throws Exception {
super.initialize(context);
}
@Override
protected void process0(IPort0 tuple) throws Exception {
Oport0 outputTuple = getOutput0().newTuple();
outputTuple.set_id(tuple.get_id());
outputTuple.set_tag(tuple.get_tag());
long[] timestamps = tuple.get_timestamps();
short[][] readings = tuple.get_readings();
// calculate data from timestamps & readings
double[] data = …
outputTuple.set_data(data);
submit0(outputTuple);
}