This site contains the documentation that is relevant to older WSO2 product versions and offerings.
For the latest WSO2 documentation, go to https://wso2.com/documentation/.

JavaScript Extension

This extension allows you to include JavaScript functions within the Siddhi Query Language.

JavaScript function

Syntax

define function <function name>[<language name>] return <return type> {

   <operation of the function>

};
Extension TypeEval Script
DescriptionThe <operation of the function> is an operation of the <function name> that is defined in the <language name> language, and the output is returned in the <return Type> specified.
Parameters
  • function name: The name of the function.
  • language name: The language of the script. In this scenario, it should be JavaScript.
  • return type: The return type of the function (int, long, float, double, string, bool or object).
  • operation of the function: The logic of the function.
Example

define function concatFunction[JavaScript] return string {

   var str1 = data[0];

   var str2 = data[1];

   var responce = str1 + str2

   return responce;

};

The above function concatenates two strings and returns the output. The input parameters are automatically input as arrays, and they can be accessed as data[1] and data[0].

A stream can be defined as follows.

define stream TempStream(deviceID long, roomNo int, temp double);

The eval script defined above can be used in this stream as follows.

from TempStream

select concatFunction(roomNo,deviceID) as id, temp  

insert into DeviceTempStream;