java.lang.Object
io.vertx.core.json.JsonObject
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 |
|
Does the JSON object contain the specified key? Parameters: Returns: |
|
Deep copy of this JSON object. Returns: |
|
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: Returns: |
public String encode() |
Encode this JSON object as a string. Returns: |
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: |
public Set<String> fieldNames() |
Return the set of field names in the JSON objects Returns: |
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: Returns: |
public byte[] getBinary(String key, byte[] def) |
Like getBinary(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public Boolean getBoolean(String key) |
Get the Boolean value with the specified key Parameters: Returns: |
public Boolean getBoolean(String key, Boolean def) |
Like getBoolean(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
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: Returns: |
public Buffer getBuffer(String key, Buffer def) |
Like getBuffer(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public Double getDouble(String key) |
Get the Double value with the specified key Parameters: Returns: |
public Double getDouble(String key, Double def) |
Like getDouble(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public Float getFloat(String key) |
Get the Float value with the specified key Parameters: Returns: |
|
Like getFloat(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
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: Returns: |
|
Like getInstant(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public Integer getInteger(String key) |
Get the Integer value with the specified key Parameters: Returns: |
public Integer getInteger(String key, Integer def) | Like getInteger(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public JsonArray getJsonArray(String key) |
Get the JsonArray value with the specified key Parameters: Returns: |
public JsonArray getJsonArray(String key, JsonArray def) | Like getJsonArray(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public JsonObject getJsonObject(String key) |
Get the JsonObject value with the specified key Parameters: Returns: |
public JsonObject getJsonObject(String key, JsonObject def) | Like getJsonObject(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public Long getLong(String key) |
Get the Long value with the specified key Parameters: Returns: |
public Long getLong(String key, Long def) | Like getLong(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
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: |
public Number getNumber(String key) |
Get the Number value with the specified key Parameters: Returns: |
public Number getNumber(String key, Number def) | Like getNumber(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
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: Returns: |
public String getString(String key, String def) | Like getString(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public Object getValue(String key) |
Get the value with the specified key, as an Object with types respecting the limitations of JSON.
Parameters: Returns: |
public Object getValue(String key, Object def) | Like getValue(String) but specifying a default value to return if there is no entry. Parameters: Returns: |
public boolean isEmpty() |
Is this object entry? Returns: |
public Iterator<Map.Entry<String,Object>> iterator() |
Get an Iterator of the entries in the JSON object. Returns: |
public static JsonObject mapFrom(Object obj) |
Create a JsonObject from the fields of a Java object. Faster than calling `new JsonObject(Json.encode(obj))`. Parameters: |
public <T> T mapTo(Class<T> type) |
Instantiate a Java object from a JsonObject. Faster than calling `Json.decodeValue(Json.encode(jsonObject), type)`. Parameters: |
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: Returns: |
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: Returns: |
public JsonObject put(String key, Object value) |
Put an Object into the JSON object with the specified key. Parameters: Returns: |
public JsonObject putNull(String key) |
Put a null value into the JSON object with the specified key. Parameters: Returns: |
public int readFromBuffer(int pos, Buffer buffer) | |
public Object remove(String key) |
Remove an entry from this object. Parameters: Returns: |
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 |
public Buffer toBuffer() |
Encode this JSON object as buffer. Returns: |
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 |
|
---|---|
|
stores boolean value True / False, used usually as a result of functions in branching expressions |
|
stores a string of characters |
|
stores an integer number |
|
stores an decimal number |
|
represents an Date object in gregorian format |
|
represents the Date part of the date for using in date and time functions |
|
represents the Time part of the date for using in date and time functions |
|
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 ".").