Creating a plug-in using Python
The APIs for writing plug-ins in Python are documented in the
API Reference for Python (Pydoc). The relevant classes are in the
apama.eplplugin module.
Note: EPL plug-ins written in Python support Python 3, which is shipped with Apama. They do not support Python 2.
To create a plug-in for EPL in Python, you have to create a class which inherits from apama.eplplugin.EPLPluginBase. Your class must provide a one-argument constructor and pass the argument verbatim to the EPLPluginBase constructor. For example:
import apama.eplplugin
class MyPluginClass(apama.eplplugin.EPLPluginBase):
def __init__(self, init):
super(MyPluginClass, self).__init__(init)
...
The base class provides two member functions to derived classes:
Member | Description |
getLogger() | |
getConfig() | Returns a dictionary of the configuration from the correlator configuration file. See also Using Python plug-ins. |
A single instance of your class is created for each time it is listed in the configuration file. Functions which you want to expose to EPL are member functions on that instance. To export a function to EPL, you need to declare a member function on the class and decorate it to indicate the name and signature of the function in EPL using the EPLAction decorator:
@EPLAction("getCounter", "action<string> returns integer")
def lookupCounter(name):
return counters[name].value()
You configure the plug-ins in the YAML configuration file for the correlator, in the eplPlugins stanza:
eplPlugins:
counterPlugin:
pythonFile: counters.py
class: MyPluginClass
You load the plug-ins into EPL by using the import statement in the monitor or event which wants to use the plug-in:
import "counterPlugin" as plugin;
Complete simple example
This example implements a plug-in which simply keeps a single global count.
from apama.eplplugin import EPLPluginBase,EPLAction
class CountPlugin(EPLPluginBase):
def __init__(self, init):
super(MyPluginClass, self).__init__(init)
self.count = 0
@EPLAction("action<integer>", "increment") # override name
def incrementCount(self, number):
self.count = self.count + number
@EPLAction("action<> returns integer")
def getCount(self):
return self.count
eplPlugins:
countPlugin:
pythonFile: ${PARENT_DIR}/countplugin.py
class: CountPlugin
monitor foo
{
import "countPlugin" as counter;
action onload()
{
on all A() {
counter.increment(1);
print "Current count: "+counter.getCount().toString();
}
}
}