The action rules scripting engine's base concept is based on the Groovy scripting language.

Action rules scripting is an advanced feature for determing if action should be executed. Script must return boolean with true or false. According to returned value action will be executed. If true is returned, action will be executed, if false action will be skipped.

Action rules scripts syntax resembles the Groovy and Java syntax. It is designed to be readable and effective but not strict. Users familiar with scripting and programming should know, that some of the common features (like loops) are absent because of security and performance reasons.

Action scripts basics

  • You can use variables in scripts for storing values and working with them
  • You can use conditions and branching
  • You can use wide pallete of functions

Our first script using variable and function

if (JSON.getInteger('x') > 20) {
   return true // automated action will be executed
} else {
   return false
}

This script takes value from entity x and compares it. If its value will be higher that 20, script returns true and automated action will be executed, otherwise it sends false that will skip executing of automated action.

How to use JsonObject?

Data from receiver are obtained by sender in JSON format. You can manipulate it using those methods. For retrieving integer from JSON getInteger(String key) can be used.

public JsonObject clear()

Remove all the entries in this JSON object

public boolean containsKey(String key)

Does the JSON object contain the specified key?

Parameters:
key - the key

Returns:
true if it contains the key, false if not.

public JsonObject copy()

Deep copy of this JSON object.

Returns:
a copy where all elements have been copied recursively

public JsonObject copy(java.util.function.Function<Object,?> cloner)

Deep copy of this JSON object.


Unlike copy() that can fail when an unknown element cannot be copied, this method delegates the copy of such element to the cloner function and will not fail.


Parameters:
cloner - a function that copies custom values not supported by the JSON implementation

Returns:
a copy where all elements have been copied recursively

public String encode()

Encode this JSON object as a string.


Returns:
the string encoding.

public String encodePrettily()

Encode this JSON object a a string, with whitespace to make the object easier to read by a human, or other sentient organism.


Returns:
the pretty string encoding.

public Set<String> fieldNames()

Return the set of field names in the JSON objects


Returns:
the set of field names

public byte[] getBinary(String key)

Get the binary value with the specified key. JSON itself has no notion of a binary, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a Base64 encoded binary, which it decodes if found and returns.


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public byte[] getBinary(String key, byte[] def)

Like getBinary(String) but specifying a default value to return if there is no entry.

Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Boolean getBoolean(String key)

Get the Boolean value with the specified key


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public Boolean getBoolean(String key, Boolean def)

Like getBoolean(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Buffer getBuffer(String key)

Get the Buffer value with the specified key. JSON itself has no notion of a binary, this extension complies to the RFC-7493, so this method assumes there is a String value with the key and it contains a Base64 encoded binary, which it decodes if found and returns.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Buffer getBuffer(String key, Buffer def)

Like getBuffer(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Double getDouble(String key)

Get the Double value with the specified key


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public Double getDouble(String key, Double def)

Like getDouble(String) but specifying a default value to return if there is no entry.

Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Float getFloat(String key)

Get the Float value with the specified key

Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public Float getFloat(String key, Float def)

Like getFloat(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present


public java.time.Instant getInstant(String key)


Get the instant value with the specified key. JSON itself has no notion of a temporal types, this extension allows ISO 8601 string formatted dates with timezone always set to zero UTC offset, as denoted by the suffix "Z" to be parsed as a instant value. YYYY-MM-DDTHH:mm:ss.sssZ is the default format used by web browser scripting. This extension complies to the RFC-7493 with all the restrictions mentioned before. The method will then decode and return a instant value.


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public java.time.Instant getInstant(String key, java.time.Instant def)

Like getInstant(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Integer getInteger(String key)

Get the Integer value with the specified key


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public Integer getInteger(String key, Integer def)

Like getInteger(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public JsonArray getJsonArray(String key)

Get the JsonArray value with the specified key


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public JsonArray getJsonArray(String key, JsonArray def)

Like getJsonArray(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public JsonObject getJsonObject(String key)

Get the JsonObject value with the specified key


Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public JsonObject getJsonObject(String key, JsonObject def)

Like getJsonObject(String) but specifying a default value to return if there is no entry.


Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Long getLong(String key)


Get the Long value with the specified key

Parameters:
key - the key to return the value for

Returns:
the value or null if no value for that key

public Long getLong(String key, Long def)

Like getLong(String) but specifying a default value to return if there is no entry.

Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Map<String,Object> getMap()


Get the underlying Map as is. This map may contain values that are not the types returned by the JsonObject and with an unpredictable representation of the value, e.g you might get a JSON object as a JsonObject or as a Map.

Returns:
the underlying Map.

public Number getNumber(String key)


Get the Number value with the specified key

Parameters:
key - the key to lookup

Returns:
the value or def if no entry present

public Number getNumber(String key, Number def)

Like getNumber(String) but specifying a default value to return if there is no entry.

Parameters:
key - the key to return the value for
def - the default value to use if the entry is not present

Returns:
the value or null if no value for that key

public String getString(String key)


Get the string value with the specified key, special cases are addressed for extended JSON types Instant, byte[] and Enum which can be converted to String.

Parameters:
key - the key to return the value for

Returns:
the value string representation or null if no value for that key

public String getString(String key, String def)

Like getString(String) but specifying a default value to return if there is no entry.

Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public Object getValue(String key)


Get the value with the specified key, as an Object with types respecting the limitations of JSON.

  • Map will be wrapped to JsonObject
  • List will be wrapped to JsonArray
  • Instant will be converted to String
  • byte[] will be converted to String
  • Enum will be converted to String

Parameters:
key - the key to lookup

Returns:
the value

public Object getValue(String key, Object def)

Like getValue(String) but specifying a default value to return if there is no entry.

Parameters:
key - the key to lookup
def - the default value to use if the entry is not present

Returns:
the value or def if no entry present

public boolean isEmpty()


Is this object entry?

Returns:
true if it has zero entries, false if not.


public Iterator<Map.Entry<String,Object>> iterator()


Get an Iterator of the entries in the JSON object.

Returns:
an Iterator of the entries

public static JsonObject mapFrom(Object obj)


Create a JsonObject from the fields of a Java object. Faster than calling `new JsonObject(Json.encode(obj))`.
null when obj is null.

Parameters:
obj - The object to convert to a JsonObject.

public <T> T mapTo(Class<T> type)


Instantiate a Java object from a JsonObject. Faster than calling `Json.decodeValue(Json.encode(jsonObject), type)`.

Parameters:
type - The type to instantiate from the JsonObject.

public JsonObject mergeIn(JsonObject other)


Merge in another JSON object. This is the equivalent of putting all the entries of the other JSON object into this object. This is not a deep merge, entries containing (sub) JSON objects will be replaced entirely.

Parameters:
other - the other JSON object

Returns:
a reference to this, so the API can be used fluently

public JsonObject mergeIn(JsonObject other, boolean deep)

Merge in another JSON object. The merge is deep (recursive) to the specified level. If depth is 0, no merge is performed, if depth is greater than the depth of one of the objects, a full deep merge is performed.

Parameters:
other - the other JSON object
depth - depth of merge

Returns:
a reference to this, so the API can be used fluently

public JsonObject put(String key, Object value)

Put an Object into the JSON object with the specified key.

Parameters:
key - the key
value - the value

Returns:
a reference to this, so the API can be used fluently

public JsonObject putNull(String key)


Put a null value into the JSON object with the specified key.

Parameters:
key - the key

Returns:
a reference to this, so the API can be used fluently

public int readFromBuffer(int pos, Buffer buffer)

public Object remove(String key)


Remove an entry from this object.

Parameters:
key - the key

Returns:
the value that was removed, or null if none

public int size()


Get the number of entries in the JSON object

Returns:the number of entries

public java.util.stream.Stream<Map.Entry<String,Object>> stream()


Get a Stream over the entries in the JSON object. The values in the stream will follow the same rules as defined in getValue(String), respecting the JSON requirements. To stream the raw values, use the storage object stream instead:

jsonObject
.getMap()
.stream()

Returns:
a Stream

public Buffer toBuffer()


Encode this JSON object as buffer.

Returns:
the buffer encoding.

public void writeToBuffer(Buffer buffer)



Usecase

You can store values into variables and use them into expressions or as a script result. The typing of variables is dynamic thus in most of the cases there is no need to think of a variable type. 

Example of declaring universal variables which are dynamically typed. You can assign values to variables for later use.

In this example we will execute automated action only during weekends when temperature goes above 20.

day = JSON.getString('day')
temperatute = JSON.getInteger('temp')

If ((day=="Saturday" || day=="Sunday") && temperature>20){
	return true
}else{
	return false
}

You can retype a variable or function return by adding as nameOfType:

double b = value() as double

Type name


boolean

stores boolean value True / False, used usually as a result of functions in branching expressions

String

stores a string of characters

int

stores an integer number

double

stores an decimal number

Date

represents an Date object in gregorian format

LocalDate

represents the Date part of the date for using in date and time functions

LocalTime

represents the Time part of the date for using in date and time functions

DateTime

represents the DateTime object for using in date and time functions

The values function returns a String value. String cannot be handled as a number unless it is properly converted. The conversion usually consists of normalization (ex.: round all numbers to 2 decimal places) or cleanup (ex.: replace all "," with ".").

More scripting language basics

Transformation scripts topics


  • No labels