ThingsBoard supports user-defined functions (UDF) for data processing in the
Rule Engine
and
Data Converters
.
The original programming language for the UDF is JavaScript. It is popular, well-known, and simple. We plan to support JavaScript forever.
Nevertheless, we have decided to provide an alternative to JavaScript. You may find our motivation below.
Motivation
ThingsBoard is written in Java and currently uses Java 11. There are two ways to execute the JS function in ThingsBoard:
A) use remote JS Executor microservice written in Node.js. It is the default way for ThingsBoard to run in a cluster/microservices mode;
B) use local JS Executor powered by Nashorn JS engine that runs inside the JVM. It is the default way for ThingsBoard while deployed in monolithic mode.
The Nashorn JS engine is now deprecated and is removed from Java 16. It is also relatively slow. Some users already called Nashorn to be the platform’s kryptonite.
That is why it was absolutely clear that we need a replacement to Nashorn.
Many users suggested
GraalVM
for its built-in polyglot feature, but it was not suitable for us for multiple reasons.
The most important reason is security and the ability to control every aspect of the UDF execution.
Besides, most UDFs are relatively simple functions that transform or filter data, and we want to find a more effective way to execute them.
Our search for existing Script/Expression Language (EL) implementations lead us to the
MVEL
.
The ThingsBoard Expression Language (TBEL) is basically a
fork
of MVEL with some important security constraints,
built-in memory management, and frequently used helper functions that are specific to ThingsBoard.
TBEL vs Nashorn
TBEL is lightweight and is super fast comparing to Nashorn. For example, the execution of 1000 simple scripts like:
“return msg.temperature > 20” took 16 seconds for Nashorn and 12 milliseconds for MVEL. More than 1000 times faster.
We have forked the TBEL codebase and added additional security improvements to ensure no CPU or memory abuse.
So, no need to run Nashorn with the sandbox environment.
Of course, TBEL is not as powerful as JS, but most of the use cases do not need this.
The one who requires JS flexibility may use remote
JS Executors
as usual.
TBEL vs JS Executors
JS Executors
is a separate microservice based on Node.js.
It is powerful and supports the latest JS language standards.
However, there is a noticeable overhead to execute JS functions remotely.
The Rule Engine and JS executors communicate through the queue.
This process consumes resources and introduces a relatively small latency (a few ms).
The latter may become a problem if you have multiple rule nodes chained into one rule chain.
TBEL execution consumes much less resources and has no extra latency for inter process communications.
TBEL Language Guide
TBEL is used to evaluate expressions written using Java syntax. Unlike Java however, TBEL is dynamically typed (with optional typing), meaning type qualification is not required in the source.
The TBEL expression can be as simple as a single identifier, or as complicated as an expression with method calls and inline collections.
Simple Property Expression
msg.temperature
In this expression, we simply have a single identifier (msg.temperature), which by itself, is what we refer to in TBEL as a property expression,
in that the only purpose of the expression is to extract a property out of a variable or context object.
TBEL can even be used for evaluating a boolean expression.
Assuming you are using TBEL in the Rule Engine to define simple script filter node:
returnmsg.temperature>10;
Like Java, TBEL supports the full gamut of operator precedence rules, including the ability to use bracketing to control execution order.
You may write scripts with an arbitrary number of statements using the semi-colon to denote the termination of a statement.
This is required in all cases except in cases where there is only one statement, or for the last statement in a script.
vara=2;varb=2;returna+b
Note the lack of semi-colon after ‘a + b’. New lines are not substitutes for the use of the semi-colon in MVEL.
Value Coercion
MVEL’s type coercion system is applied in cases where two incomparable types are presented by attempting to coerce the ‘’right’‘value to that of the type of the’‘left’’ value, and then vice-versa.
For example:
"123"==123;
This expression is true in TBEL because the type coercion system will coerce the untyped number 123 to a String in order to perform the comparison.
TBEL allows you to create Maps. We use our own implementation of the Map to control memory usage.
That is why, TBEL allows inline creation of the maps only. Most common operation with the map:
// Create new mapvarmap={"temperature":42,"nested":{"rssi":130}};// Change value of the keymap.temperature=0;// Add new keymap.humidity=73;// Check existance of the keyif(map.temperature!=null){// Null-Safe expressions using ?if(map.?nonExistingKey.smth>10){// Iterate through the mapforeach(element:map.entrySet()){// Get the keyelement.key// Get the valueelement.value;// remove value from the mapmap.remove("temperature");// get map sizemap.size();
Lists
TBEL allows you to create Lists. We use our own implementation of the List to control memory usage of the script.
That is why, TBEL allows inline creation of the lists only. For example:
// Create new listvarlist=["A","B","C"];// List elelement accesslist[0];// Add elementlist.add("F");// Add element using indexlist.add(3,"D");// Remove element by indexlist.remove(4);// Remove element by valuelist.remove("D");// Set element using indexlist[2]="C";list.set(2,"C");// Size of the listlist.size();// Get sub list - JS stylelist.slice(1,3);// Foreach for(item:list){varsmth=item;// For loop for(inti=0;i<list.size;i++){varsmth=list[i];
Arrays
TBEL allows you to create Arrays. To control the memory usage, we allow arrays of primitive types only. String arrays are automatically converted to lists.
// Create new arrayint[]array=newint[3];array[0]=1;array[1]=2;array[2]=3;str="My String";str[0];// returns 'M';functionsum(list){intresult=0;for(vari=0;i<list.length;i++){result+=list[i];returnresult;sum(array);// returns 6array[3]=4;// Will cause ArrayIndexOutOfBoundsException
Literals
A literal is used to represent a fixed-value in the source of a particular script.
String literals
String literals may be denoted by single or double quotes.
"This is a string literal"
'This is also string literal'
String escape sequences:
\ - Double escape allows rendering of single backslash in string.
\n - Newline
\r - Return
\u#### - Unicode character (Example: \uAE00)
### - Octal character (Example: \73)
Numeric literals
Integers can be represented in decimal (base 10), octal (base 8), or hexadecimal (base 16).
A decimal integer can be expressed as any number that does not start with zero.
125// decimal
An octal representation of an integer is possible by prefixing the number with a zero, followed by digits ranging from 0 to 7.
0353// octal
Hexidecimal is represented by prefixing the integer with 0x followed by numbers ranging from 0-9..A-F.
0xAFF0// hex
A floating point number consists of a whole number and a factional part denoted by the point/period character, with an optional type suffix.
10.503// a double94.92d// a double14.5f// a float
You can represent *BigInteger* and *BigDecimal* literals by using the suffixes B and I (uppercase is mandatory).
104.39484B // BigDecimal
8.4I // BigInteger
Boolean literals are represented by the reserved keywords true and false.
The null literal is denoted by the reserved keywords null or nil.
Using Java Classes
The TBEL implementation allows usage of some Java classes from the java.util and java.lang packages. For example:
varfoo=java.lang.Math.sqrt(4);
For the security purpose, usage of those classes is constrained. You are able to call both static and non-static methods, but you are not able to assign the instance of the class to the variable:
varlist=["A","B","C"];java.util.Collections.reverse(list);// allowedlist=newjava.util.ArrayList();// Not allowed
To simplify migration from the JS, we have added the JSON class with static methods: JSON.stringify and JSON.parse that work similar to JS. For example:
For the same purpose, we have added Date class that you are able to use without the package name.
Flow Control
If-Then-Else
TBEL supports full, C/Java-style if-then-else blocks. For example:
if(temperature>0){return"Greater than zero!";}elseif(temperature==-1){return"Minus one!";}else{return"Something else!";
Ternary statements
Ternary statements are supported just as in Java:
temperature>0?"Yes":"No";
Nested ternary statements are also supported.
Foreach
One of the most powerful features in TBEL is it’s foreach operator. It is similar to the for each operator in Java 1.5 in both syntax and functionality.
It accepts two parameters separated by a colon, the first is the local variable for the current element, and the second is the collection or array to be iterated.
For example:
sum=0;for(n:numbers){sum+=n;
Since TBEL treats Strings as iterable objects you can iterate a String (character by character) with a foreach block:
varbase64Str="eyJoZWxsbyI6ICJ3b3JsZCJ9";// Base 64 representation of the '{"hello": "world"}'varbytesStr=atob(base64Str);varbytes=stringToBytes(bytesStr);returndecodeToJson(bytes);// Returns '{"hello": "world"}'
stringToBytes
Converts input binary string to the list of bytes.
Converts the hex string to list of integer values, where each integer represents single byte.
Syntax:
*List hexToBytes(String hex)*
Parameters:
hex:string - the hex string with big-endian byte order.
Return value:
Parsed list of integer values.
Examples:
returnhexToBytes("BBAA");// returns [187, 170]
bytesToHex
Converts the list of integer values, where each integer represents single byte, to the hex string.
Syntax:
*String bytesToHex(List bytes)*
Parameters:
bytes:List of integer - the list of integer values, where each integer represents single byte.
Return value:
Hex string.
Examples:
returnbytesToHex([187,170]);// returns "BBAA"
bytesToBase64
Converts the byte array, to Base64 string.
Syntax:
String bytesToBase64(byte[] bytes)
Parameters:
bytes:List of integer - the list of integer values, where each integer represents single byte.
Return value:
Base64 string.
Examples:
returnbytesToBase64([42,73]);// returns "Kkk="
base64ToHex
Decodes the Base64 string, to hex string.
Syntax:
String base64ToHex(String input)
Parameters:
input:String - the Base64 string.
Return value:
Hex string
Examples:
returnbase64ToHex("Kkk=");// returns "2A49"
base64ToBytes
Decodes the Base64 string, to byte array.
Syntax:
byte[] base64ToBytes(String input)
Parameters:
input:String - the Base64 string.
Return value:
Byte array.
Examples:
returnbase64ToBytes("Kkk=");// returns [42, 73]
parseBytesToInt
Parses int from the byte array given the offset, length and optional endianness.
Syntax:
*int parseBytesToInt([byte[] or List ] data, int offset, int length[, boolean bigEndian])*
Parameters:
data:byte[] or List of Byte - the byte array.
offset:int - the offset in the array.
length:int - the length in bytes. Less then or equal to 4.
bigEndian:boolean - optional, LittleEndian if false, BigEndian otherwise.
Return value:
integer value.
Examples:
returnparseBytesToInt(newbyte[]{(byte)0xAA,(byte)0xBB,(byte)0xCC,(byte)0xDD},0,3,true);// returns 11189196 in Decimal or 0xAABBCC returnparseBytesToInt(newbyte[]{(byte)0xAA,(byte)0xBB,(byte)0xCC,(byte)0xDD},0,3,false);// returns 13417386 in Decimal or 0xCCBBAA
toFlatMap
Iterates recursive over the given object and creates a single level json object.
If the incoming message contains arrays, key for transformed value will contain index of the element.
As you can see, key1 and key3 was removed from output. Excluding works on any level with incoming keys.
toFlatMap(json, excludeList, pathInKey)
Expected behavior - get the single level object without paths in keys, without keys key2 and key4. key_to_overwrite should contain the value from key11.key_to_overwrite.
As you can see, key2 and key4 was excluded from the output and key_to_overwrite is second_level_value.
We use cookies to improve user experience. By continuing to browse this site, you agree the use of cookies, in accordance with our cookie policy.Accept