Use Expressions and Functions in Simulation Models

Use expressions and functions to customize the attributes, alerts, actions, and events for your simulation model.

The IoT digital twin simulator supports the Expression Language 3.0 syntax without curly braces around expressions. You can use all static functions from the JDK 8 java.lang.Math class. You can also make use of the additional simulation functions that are provided.

The following are some examples of simulation model fields where you can use expressions and functions:

  • In Attributes: Use expressions and functions to specify the Initial Value Expression and Value Expression for attributes. For example:

    • You can use the now() function to initialize the startTime attribute.

    • You can use $temp to initialize the value of mintemp with the value of the temp attribute.

    • You can use the numeric value 37.391838 to initialize the latitude attribute.

    • You can use Math.max($maxTemp, $temp) to calculate the value for the maxTemp attribute. Here, max is a function of the Math class, and $maxTemp and $temp are attributes.

  • In Alerts: Use a boolean expression to define the Condition Expression used for triggering the alert. For example: $temp >= $maxThreshold.

  • In Actions: Use expressions to set attribute and event values for actions. For example, you can set the startTime attribute to the following expression: $action.power ? now() : $startTime. Here, power is an action.

  • In Events: Use expressions to set the Attribute Expression when the event is on. For example: Set the $temp attribute to sinInRange(60, 80).

  • In Functions: Create your own functions using expressions and existing functions. Use the lambda syntax to define the body of a function.

    For example, you can define a user function called absDiff as (param1, param2) -> Math.abs(param2 - param1).

    You can now use the function anywhere expressions are allowed. For example: $attr0 > 50 ? absDiff($attr1, $attr2) : absDiff($attr3, $attr4).

Note:

You can refer to device model attributes using the $ prefix.

When using a single device model, you may use names such as $temperature, where temperature is the name of a device model attribute. If you have several device models, then outside a device model, such as in an events attributes expression, use fully qualified attribute names. For example: $urn:com:oracle:iot:device:temperature_sensor.temperature. If you do not qualify the attribute name, then the first device model is assumed, by default.

Additional Simulation Functions

Oracle IoT Digital Twin Simulator provides the following functions in addition to the static functions available in the JDK 8 java.lang.Math class:

  • public static double linearToTarget(double currentValue, double targetValue, double increment)

    Returns values that follow a linear pattern from currentValue to targetValue with the supplied increment.

  • static double logToTarget(double currentValue, double targetValue)

    Returns values that follow a logarithmic pattern from currentValue to targetValue.

  • static long now()

    Generates the current time in milliseconds.

  • static double randomInRange(double lowerValue, double upperValue)

    Generates random float values in the range defined by the arguments.

  • static double sinInRange(double lowerValue, double upperValue)

    Generates values that follow the sin(x) function pattern in the range defined by the arguments.

  • static double spikesInRange(double lowerValue, double upperValue, double currentValue)

    Generates one of two values, passed as lowerValue and upperValue, different from the current value, which is passed as the currentValue argument.