This documentation is for WSO2 CEP 2.1.0. View the home page of the latest release.

Writing Extentions to Siddhi

Siddhi supports custom codes within queries. You can implement Windows, Transformers, OutputAttributeProcessors, Conditions and Expressions in a pluggable manner using the current implementation.

Note

These are subjected to be changed in a future release.

Use of Namespcaes and Functions in Siddhi Extensions

Siddhi allows extensions to have namespaces and function names, enabling users to easily identify the extensions' behaviors when writing queries.

To add namespace and function names to the extension, use the following Java annotation in the extension class.

@SiddhiExtension(namespace = "testExt", function = "unique")
public class UniqueWindowProcessor extends WindowProcessor {
   ...
}

You can refer to the above class in the query as follows:

from StockExchangeStream[price >= 20]#window.testExt:unique(symbol)

insert into StockQuote symbol, price

The following sections explains how we can create different types of Siddhi Extensions,

Writing a Custom Window

To write a custom window, create a class extending "org.wso2.siddhi.core.query.processor.window.WindowProcessor" add the SiddhiExtionsion annotation, compile that class, and add the jar file to the class path <CEP_HOME>/repository/components/lib. Then add the fully-qualified class name for the implementation class in a new line, to the siddhi.extension file located at <CEP_HOME>/repository/conf/siddhi.

For example, if you have created the extension with namespace testExt and function name firstUnique, you can use that in the query as follows:

from StockExchangeStream[price >= 20]#window.testExt:firstUnique(symbol) 
insert into StockQuote symbol, price

Writing a Custom Transformer

To write a custom transformer, create a class extending "org.wso2.siddhi.core.query.processor.transform.TransformProcessor" add the SiddhiExtionsion Annotation, compile that class, and add the jar to the class path <CEP_HOME>/repository/components/lib. Then add the fully-qualified class name for the implementation class in a new line, to the siddhi.extension file located at <CEP_HOME>/repository/conf/siddhi.

For example, if you have created the extension with namespace math and function name std, you can use that in the query as follows:

from StockExchangeStream[price >= 20]#transform.math:std(price) 
insert into StockSTDStream price, standardDeviationn, count

Writing a Custom OutputAttributeProcessor

To write a custom output attribute processor, create a class implementing "org.wso2.siddhi.core.query.projector.attribute.factory.OutputAttributeProcessorFactory" and add SiddhiExtionsion annotation to this class. Also create appropriate OutputAttributeProcessors extending "org.wso2.siddhi.core.query.projector.attribute.handler.OutputAttributeProcessor", which is created by the OutputAttributeProcessorFactory based in the Attribute Type (Int, String, Long, Bool, Double, Float, etc). Then compile these classes and add the jar files to the class path at <CEP_HOME>/repository/components/lib. Then add the fully-qualified class name for the OutputAttributeProcessorFactory implementation class in a new line, to the siddhi.extension file located at <CEP_HOME>/repository/conf/siddhi.

For example, if you have created the extension with namespace currency and function name fromUSDtoEUR, you can use that in the query as follows:

from StockExchangeStream[price >= 20] 
insert into StockQuoteStream symbol, price as priceInUSD, currency:fromUSDtoEUR(price) as priceInEUDR

Writing a Custom Condition

To write a custom condition, create a class extending "org.wso2.siddhi.core.executor.conditon.AbstractGenericConditionExecutor", add the SiddhiExtionsion annotation, compile that class, and add the jar file to the class path <CEP_HOME>/repository/components/lib. Then add the fully-qualified class name for the implementation class in a new line, to the siddhi.extension file located at <CEP_HOME>/repository/conf/siddhi.

For example, if you have created the extension with namespace myExt, function name isInterested, you can use that in the query as follows:

from StockExchangeStream[price >= 20 and myExt:isInterested(symbol) 
insert into InterestedStockQuoteStream symbol, price

Writing a Custom Expression

To write a custom expression, create a class extending "org.wso2.siddhi.core.executor.expression.AbstractGenericExpressionExecutor", add the SiddhiExtionsion annotation, compile that class, and add the jar file to the class path <CEP_HOME>/repository/components/lib. Then add the fully-qualified class name for the implementation class in a new line, to the siddhi.extension file located at <CEP_HOME>/repository/conf/siddhi.

For example, if you have created the extension with namespace myExt and function name myMagicFuction, you can use that in the query as  follows:

from StockExchangeStream[price >= myExt:myMagicFuction(price) /100]
insert into InterestedStockQuoteStream symbol, price


Copyright © WSO2 Inc. 2005-2014