About the SemEL Language

SemEL is the scripting language used to define programs and procedural logic specific to apps.

Note: The SemEL language is only available for Factory Flow Simulation.

This page discusses:

Syntax

The SemEL syntax is a subset of the ECMAScript 2016 (ES7) and can be executed by SemBEE.

Note: SemEL is case-sensitive.

Variables Declaration

A variable is a storage location associated with an identifier that contains a value. The SemEL language lets you work with several types of variables, detailed in Data Types.

The variable name must begin with a letter (A-Z or a-z) followed by letters, digits (0-9) or underscores (_). The scope of variables is global to the SemEL script.

You can declare a variable using the var keyword and assign it a value using the = operator.

var x;
var y = 5;
var s = "A string";
var array = [];
var myAsset = new Asset();
Important: let and const are not supported.

Reserved Keywords

The variable name cannot be one of the following reserved keywords:

var for if else true false of continue break new

Input and Output Variables

When a process invokes the SemEL script, it can transmit input parameters and retrieve output parameters. Those parameters are seen by the script as variables, and do not need to be declared.

Statements

A SemEL script is made up of statements, or instructions and each of which ends with a semicolon (;).

Block Statements

Statements can be grouped in block statement using braces {...}.

Conditional Statements

The if and else keywords are used to write conditional statements.

if (condition)
  statement1
else
  statement2

Multiple if...else statements can be nested to create an else if clause:

if (condition1)
  statement1
else if (condition2)
  statement2
else if (condition3)
  statement3
// …
else
  statementN

You can also use an if statement to check if an object is defined or not:

if (myObject)
  statement
else
  //Log error

Loop Statements

  1. For Statements:

    The for statement creates a loop with three optional expressions.

    for (expression1; expression2; expression3) {
      // code block to be executed
    }
    
    Expression1 is executed once before the code block is executed. Expression2 defines the condition for executing the code block. Expression3 is executed each time after the code block has been executed.

  2. For [… ] of Statements:

    The for...of statement executes a loop that operates on a sequence of values from an iterable object.

    for (var element of iterable)
      statement
    

    For each iteration, element receives a value from the sequence defined in the iterable object and statement is executed.

    You can use a block statement to execute several statements at once.

    Note: Iterable objects can be arrays or multivalued properties of a structured object.

    The following example illustrates how to compute the sum of array values:

    var sum = 0;
    var myArray = [1, 2, 3];
    for (var nb of myArray) {
      sum += nb;
    }
    // Expected output: sum = 6
    

  3. Continue and Break Statements

    The continue statement terminates the execution of the statements in the current iteration of the current loop, and continues the execution of the loop with the next iteration.

    The break statement terminates the current loop statement and transfers program control to the statement following the terminated statement.

Comments

The code after the double slashes // is treated as a comment. Comments are ignored and will not be executed:

var x = 5; // I will be executed
// x = 6; I will NOT be executed 

White Spaces

SemEL ignores multiple spaces, tabulations, and line breaks. You can use them to make your scripts more readable.

Examples

The following example illustrates SemEL syntax:

Example 1

var res = 1;
for (var i=1; i<=N; i++)
{
  res = res * i;
}
Result = (res + NumberOfIteration) % 100;

Example 2

Result = 0;
for (var i=0; i<Counter; i++)
{
  Result += i;
}
Result += Salt;
if (Result % 2 == 0)
  Routing = "Add";
else 
  Routing = "Subtract";

Example 4

This example illustrates the use of conditional statements:

if (hour < 18) {
  greeting = "Good day";
} else {
  greeting = "Good evening";
}

Example 3

This example illustrates how to compute the factorial of an integer n:

var fact = 1;
for (var i = 1; i <= n; i++) {
  fact *= i;
}

Troubleshooting

SemEL handles two types of error: syntax and build time errors. The following table list the potential errors and their solutions.

Note: Typing errors are not detected at built time and result in runtime errors, managed by the app running the script.
Error Message Type Diagnostic Example
"missing ';' at '}'" at line 2:0 Syntax Missing ‘;’ at the end of the line.
var i = 10
Unable to find variable: results" at line 2 Build time Incorrect variable name.
var result = 0 results++;
mismatched input ';' expecting " at line 1:15 Syntax Extra ‘;’.
var result = 0;;
mismatched input '.' expecting ';'" at line 1:17 Syntax Wrong format
var result = 10.0.0;
no viable alternative at input 'elseif ('" at line 5:34 Syntax Incorrect keyword used
var temp = 10;
var result = "";
if (temp < 0)
result = "Cold";
elseif (temp > 30)
result = "Hot";
else
result = "Unknown";
Unable to evaluate expression" at line 2 Build time Read a property without any other action
var result = 0
result.length;
Returned value cannot be discarded" at line 2 Build time Discard the returned value
var result = 0
result + 10;
Unknown property : PII on Math object" at line 1 Build time Incorrect property name
var result = Math.PII;
Unknown Function Call : flooor" at line 1 Build time Incorrect function name
var result = Math.flooor(-42.6);
Unknown property : hasAsset on object: WS" at line 2 Build time Incorrect property name (hasAssets)
var assetList = WS.hasAsset
Unknown property : namee on object: myAsset" at line 1 Build time Incorrect property name
myAsset.namee = "John"