EvalExp()

Format

EvalExp(Expr [, ParamsArray])

Returns

Evaluates the specified expression. Optionally it can also override the value of context parameters for the duration of the evaluation.

Behaves in exactly the same way as the Eval() function, except that the first parameter’s type is expression and not a single value string. 

Use caution when nesting the EvalExp() functions, as they can quickly become complicated and difficult to troubleshoot.

Inputs

Expr: The expression to be evaluated.
Behaviour: Required
Dimension: Expression, MultiCollection
Valid data types: Expression

ParamsArray: The context parameters to be used to override the default context parameters of the expression.
Behaviour: Optional [0..n]
Dimension: Collection, SingleValue, Matrix, MultiCollection
Valid data types: Null, Boolean, DateTime, Decimal, Integer, String, Duration, Error

Note: These parameters must be specified in pairs. The first parameter must be the name of an expression context parameter and the second parameter must be the value of it.
E.g. EvalExp({myTag}, "sampleMethod", "Raw"). This will evaluate the {myTag} expression with the sample method set to "Raw" in the expression context. Note the use of {} around the tag name.

How to format data types

When to use

Both Eval() and EvalExp() can be used for evaluating expressions with overridden context parameters. The difference is that the first parameter of Eval() is a string while the first parameter of EvalExp() is an expression.

Here is an example showing the difference (without using context parameters, for simplicity):

Eval("1 + 2") -> returns 3
EvalExp(1 + 2) -> returns 3

The most obvious difference is that Eval() expects a string and therefore the first parameter must be wrapped in quotation marks. As EvalExp() expects an expression parameter, it doesn’t require quotes.

Most of the time, you would need EvalExp() to hard code expressions which need to be evaluated with overridden context parameters.

Calling EvalExp() with only 1 parameter would be the same as not using EvalExp() at all.

Using EvalExp with Tag Syntax:

The following expression:

EvalExp({myTag}, "sampleMethod", "Raw", "sampleInterval", {du'01:00'})

Is another way of writing:

{myTag, sampleMethod = "Raw", sampleInterval = {du'01:00'}}

The Sampling() function is also similar. This expression:

EvalExp({myTag} * {myTag2}, "sampleMethod", "LastKnownValue", "sampleInterval", {du'01:00'})

Is the same as:

Sampling({myTag} * {myTag2}, "LastKnownValue", {du'01:00'})

Which is the same as:

{myTag, sampleMethod = "LastKnownValue", sampleInterval = {du'01:00'}} * {myTag2, sampleMethod = "LastKnownValue", sampleInterval = {du'01:00'}}

Examples

Expression: EvalExp("{" + entity + ":attribute}")
Expected result: Returns "{entity:attribute}" as a string because that’s the expression, and no data fetch will occur.

Overriding Context Parameters

You can use EvalExp() to override context parameter values. For example, in the following function:

Integer(EvalExp(param[sampleInterval], "sampleInterval", {du'01:00'}))

P2 Explorer shows a flat trend for the expression, with a constant value of 3,600 instead of using the trend's default Sample Interval.

You can also supply your own parameters. For example:

EvalExp(param[myTestParameter] * 2, "myTestParameter", 5)

The P2 Explorer trend displays this as a flat line of 10.

Release History

  • EvalExp() 4.6.5
    • Changed parameters to support MultiCollection values.
  • EvalExp() 4.0
    • Initial version

Comments are closed