Expressions
LoyJoy provides a powerful expression language that allows you to evaluate expressions in experiences. The expression language is a subset of Jakarta Expression Language simplified for security concerns and allows you to access and combine process variables and functions programmatically.
Expressions can be used in conditions, in variable value assignments, and in interpolated texts such as chat message bubbles Welcome ${firstname} of age ${customerAgeYears()}
or email templates. Expressions can range from simple expressions such as gender == "male" ? "Dear Mr." : "Dear Ms."
to arbitrarily complex expressions such as stringContains(getParam("haystack"), "needle")
.
Literals
As literals you can use true
, false
, integers such as 1
or 2
, floating point numbers such as 1.5
or 3.14
, strings such as "foo"
, and null
.
The decimal point of floating point numbers is written as a dot .
and not as a comma ,
.
Arithmetic Operators
There are 5 arithmetic operators:
- Addition
+
such as1 + 2
or1 + someVar
- Subtraction
-
such as1 - 2
or1 - someVar
- Multiplication
*
such as2 * 4
or4 * someVar
- Division
/
such as4 / 2
or4 / someVar
- Remainder (modulo)
%
such as8 % 3
or8 % someVar
Relational Operators
There are 6 relational operators:
- Equal
==
such as1 == 1
or1 == someVar
- Not equal
!=
such as1 != 2
or1 != someVar
- Less than
<
such as1 < 2
or2 < someVar
- Greater than
>
such as3 > 2
or3 > someVar
- Less or equal
<=
such as1 <= 2
or2 <= someVar
- Greater or equal
>=
such as3 >= 2
or3 >= someVar
Logical Operators
There are 3 logical operators:
- And
&&
/and
such as2 > 1 && true != false
or2 > 1 and true != false
- Or
||
/or
such as2 > 1 || true != false
or2 > 1 or true != false
- Not
!
such as!someVar
Empty Operator
The empty operator can be used to determine if a value is null or empty such as empty someVar
. In the example it returns true
if the variable someVar
does not exist, is not set, or null
, or an empty string, and false
otherwise.
Conditional Operator
The condition operator evaluates a condition, and depending on the condition result returns one of two results such as age >= 18 ? "Welcome" : "Not old enough"
.
Parentheses
Parentheses can be used to change precedence, such as a * (b + c)
.
Arrays
Array elements can be accessed by index such as someArray[0]
or someArray[someIndex]
. The first array index is 0. The last array index is array length - 1.
Objects
Object properties can be accessed by .
such as someObject.someProperty
or []
such as someObject["someProperty"]
. The index can be an expression such as someObject[someVar]
. Typical use cases for accessing object properties are when accessing properties of JSON objects retrieved from APIs.
Functions
LoyJoy provides a growing list of functions that can be called from expressions such as customerAgeYears() >= 18
or isoLocalDate()
.
Variables
You can use variables in expressions if they have been generated before in the process such as cheesePreference == "strong"
.
Type Conversion
In LoyJoy all variables are texts (type String
). Thus if you want to use variables as numbers or booleans, it might be necessary to make a type conversion. LoyJoy provides several such functions for type conversion:
- Conversion to integer number with
toBigInteger
,toLong
,toInteger
,toShort
,toByte
such astoBigInteger("2")
ortoBigInteger(someVar)
. BigInteger can handle larger integer numbers than Long. Long can handle larger integer numbers than Integer, or Short, or Byte. In most cases BigInteger is recommended. - Conversion to floating point number with
toBigDecimal
,toDouble
,toFloat
such astoBigDecimal("2.5")
ortoBigDecimal(someVar)
. BigDecimal can handle larger floating point numbers than Double. Double can handle larger floating point numbers than Float. In most cases BigDecimal is recommended. - Conversion to boolean with
toBoolean
such astoBoolean("true")
ortoBoolean(someVar)
.