Simpleflow Script Reference
- Script Outline and Guidelines
- Variables
- Data Types
- Type Casting
- Operators
- Expressions
- Template Strings
- Script Parameters
- Rule Control Flow
- Emitters
- Functions
- Comments
- Error Handling
Simpleflow Script Outline
<let_statement>*
<general_statement>*
<rule_statement_block>*
<general_statement>*
[<end_rule>]
<general_statement>*
general_statement:
<emitter>*
or <invoke_function>*
or <set statement>*
Guidelines
- All
let
statements (declare and initialize variables) must be declared in the beginning of the script. you must declare variables if you wish to modify further in the script using set statement. - Each statement must end with a new line and a statement can be written in multiple lines.
- All keywords must be written in lower case (
let, set, message, error, output, rule, when, then, exit, end rule, partial, true, false, none,...
). Variable name, property name, function’s parameter name and function name are not case sensitive. - Keywords cannot be used for variable name, function’s parameter name and property name. Example:
arg.message
or$Send(message: '')
are not valid, it will throw an exception, but you can use keywords by changing the letter case.arg.Message
is valid but notarg.message
.
Variables
let <variable_name> [, <error_handler_variable_name>] = expression
Modify value of a variable
[partial] set <variable_name> [, <error_handler_variable_name>] = expression
set
statement can be used to modify the value of a variable that has been declared using let statement. partial
keyword can be used to modify certain properties of an object.
Update properties of an object:
partial set arg = { RegistrationDate: currentDate, IsActive: true }
Data Types
Data Type | Description/Examples |
---|---|
Number | let x = 1 #Inferred to be of type int let y = 2.3 #Inferred to be of type decimal let z = -442.33 #Inferred to be of type decimal |
String | In Simpleflow, single quotes (' ') or double quotes (" ") can be used to create string literals. let name = 'test' |
Boolean | let hasValue = true let allow = false |
Date | Use date function to declare a variable as date type. let birthday = $date(y:1980, m: 1, d: 1 ) |
Object | Object type can be defined using JSON format. Object will be created and evaluated only when it will be supplied as a parameter to a function. Until it gets created or activated by a function call, you cannot use a partial set on this object. |
List | Simpleflow supports only list type
|
# Example
let user = none #inferred to be of type object
set user = 2
Type Casting
Simpleflow does not offer any built-in type casting functionality but this can be achieved by registering custom functions to convert a type to another type.
Example
var script =
@"
let int = 10 #inferred to be of type int
let decimal = 20.0 #inferred to be of type decimal
# convert decimal to int
set int = $decimal_to_int(value: decimal)
output int
";
var functionRegister = new FunctionRegister()
.Add("decimal_to_int",
(Func<decimal, int>)DecimalToInt);
var output = SimpleflowEngine.Run(script, new object(), functionRegister);
private static int DecimalToInt(decimal value)
{
return Convert.ToInt32(value);
}
Operators
Operator Type | Operators |
---|---|
Arithmetic | + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Remainder) |
Logical | and, or, not |
Relational | <, <=, >, >=, == , !=, in |
Example - in operator
rule when 5 in [2,3,5] then
message 'exists'
rule when not (5 in [2,3]) then
message "doesn't exist"
Expressions
let v = 2 + 3 * (3 * arg.value);
Template Strings
Template strings are strings delimited with backtick (`) characters, allowing for multi-line strings for string interpolation with embedded object identifiers.
let to = "John";
let from = "Chris";
let v = ` Hi {to},
.....
.....
Thanks,
{from}
`
Script Parameters
arg
and context
parameters available in Simpleflow script. arg
represents the input to the script.
Context Properties:
Property | Description |
---|---|
context.HasErrors | Returns true if there are any errors emitted |
context.HasMessages | Returns true if there are any messages emitted |
context.CancellationToken | Returns cancellation token that has been supplied to a script |
context.CancellationToken.IsCancellationRequested | Returns true if cancellation has been requested for that context |
CancellationToken can be supplied through context options. Example:
SimpleflowEngine.Run(script, <argument>,
new FlowContextOptions {
CancellationToken = <your cancellation token here>
});
Rule Control Flow
rule when expression then
<statement..1>
<statement..2>
<statement..N>
[end rule]
end rule
is optional and it can be used to terminate the rule scope. Simpleflow does not support nested rules. if you need to write nested rules, declare a variable and write a conditional expression and use it in rules.
Example:
let highPriority = arg.priority == 'high'
# run rules when highPriority is true
rule when highPriority and arg.type == 'Gold' then
$ProcessGoldCustomer(customer: arg)
rule when highPriority and arg.type == 'Silver' then
$ProcessSilverCustomer(customer: arg)
Emitters
Emitters are a kind of special function to collect the stream of data and outputs, which are helpful to read the data from script execution.
Emitter Type | Syntax |
---|---|
message | message <expression> |
error | error <expression> |
output | output <variable/property> |
exit | exit |
Note: exit
can be used to terminate the script execution, and output
does not support expression.
Comments
It supports single line and multi line comments.
# Single line comment using hash symbol
/*
Multi-line comment
*/
Error Handling
v1.0.4
By specifying a second variable along with the first one in a let
or set
statement, you can capture error if occurred.
let x, err = 2 / 0
rule when err then
message `Error has occurred: {err.Message}`
end rule
set x, err2 = 5 + 3
rule when not err2 then
message "No error"
When you use set
to update a variable and you want to catch the error as well, if an error occurs then you don’t need declare the error variable using let
as you declare to capture regular value.