Apama 10.7.2 | Developing Apama Applications | Developing EPL Plug-ins | Writing EPL Plug-ins in Python | Creating a plug-in using Python
 
Creating a plug-in using Python
 
Method signatures and types
Exceptions
Writing to the correlator log file
Sending events
The APIs for writing plug-ins in Python are documented in the API Reference for Python. 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()
Returns a logger object which can be used to write to the correlator log file. See also Writing to the correlator log file.
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
Note:
In Software AG Designer, you can easily do this by adding the above configuration to the config/CorrelatorConfig.yaml file. For further information, see YAML configuration file for the correlator.
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;
Note:
If your plug-in starts any Python background threads, you must ensure all such threads are stopped before unloading the plug-in. Failure to do so can cause the correlator to terminate with a Py_EndInterpreter message, indicating that this is not the last thread.
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();
}
}
}