-
Notifications
You must be signed in to change notification settings - Fork 44
how to write a new processing class
In GSN, a processing class is a piece of code which acts in the final stage of data processing as sits between the wrapper and the data publishing engine. The processing class is the last processing stage on the data and its inputs are specified in the virtual sensor file.
All virtual sensors are subclass of the AbstractVirtualSensor
(package gsn.vsensor
). It requires its subclasses to implement the following three methods:
public boolean initialize();
public void dataAvailable(String inputStreamName, StreamElement se);
public void dispose();
initialize
is the first method to be called after object creation. This method should configure the
virtual sensor according to its parameters, if any, and return true
in case of success, false
otherwise. If this method returns false
, GSN will generate an error message in the log and
stops using the processing class hence stops loading the virtual sensor.
dispose
is called when GSN destroys the virtual sensor. It should release all system
resources in use by this virtual sensor. This method is typically called when we want to
shutdown the GSN instance.
dataAvailable
is called each time that GSN has data for this processing class, according
to the virtual sensor file. If the processing class produces data, it should encapsulate this data in a
StreamElement
object and deliver it to GSN by calling dataProduced(StreamElement se)
method.
Note that a processing class should always use the same StreamElement
structure for
delivering its output. Changing the structure type is not allowed and trying to do so will result
in an error. However, a virtual sensor can be configured at initialization time with the kind of
StreamElement
it will produce (e.g., setting the output type to be the super set of all the possible outputs and providing null whenever the value is missing).
This allows to produce different types of StreamElements
by the same VS depending on its usage.
But one instance of the VS will still be limited to produce the same structure type. If a virtual sensor really needs to produce several different stream elements, user must provide the set of all possibilities in the stream elements and provide Null
whenever the data item is not applicable.
The processing class can read the init parameters from the virtual sensor file: Example:
<class-name>gsn.vsensor.MyProcessor</class-name>
<init-params>
<param name="param1">DATA</param>
<param name="param2">1234</param>
</init-params>
And inside the processing class's initialization method:
String param1 = getVirtualSensorConfiguration().getMainClassInitialParams().get( "param1" );
String param2 = getVirtualSensorConfiguration().getMainClassInitialParams().get( "param2" );