Expressions

Introduction

What is an expression?

An expression is a mathematical sentence written with operators and operands. For example:

3x + y > z
    (not A) or B = C
    if A then 3 else 4/3 end
    8 >> 2

In Sym3 Operator, it is possible to use expressions instead of tags to control objects properties. For every object (2D Component, 3D Component, Window, etc), in the ‘Expression’ field the user can enter an expression. This screen shows an example of expressions used for the ‘Button Color’ and ‘Base Color’ for an EStop.

Avoid the use of a dash (’-’) in equipment names and aliases as these are interpreted as the minus operator and will lead to unpredictable results

In Sym3 Operator, the 18 operators are:

+, -, *, /, div, mod, =, <>, <, >, <=, >=, not, and, or, xor, <<, >>.
  • div: refers to Euclidian division

  • mod: is the Modulo (integer remainder after division)

  • xor: is exclusive OR

  • « and »: are shifts right and left, operating that way:

      5 << 2 = (101 in binary) << 2= (10100 in binary) = 20
      20 >> 1= (10100 in binary) >> 1 = (1010 in binary) = 10
    

The 5 different kinds of variables are:

  • Boolean, Integer (Short or Long), Double, String.
  • For each kind of variable, it can be either a constant or a tag.

Operator Rules

Brackets and priorities (operator precedence)

For a 1 Operand-1 Operator expression, Sym3 Operator treats these expressions as the same:

  • not A
  • (not A)
  • not (A)
  • not( A)

For a 2 Operands-1 Operator expression, Sym3 Operator treats these expressions as the same:

  • A op B
  • (A op B)
  • (A) op (B)

For an expression containing multiple operators, classic rules of brackets apply. In the absence of brackets the priorities used by Sym3 Operator are:

Priority Levels Operators
4 not
3 * ; / ; div ; mod ; and
2 + ; - ; or ; xor
1 « ; »
0 = ; <> ; < ; <= ; > ; >=
if same level of priority evaluated from left to right

Thus, in these expression examples:

A mod B « C or not E = F will be evaluated as ((A mod B)«(C or (not E)))=F
A div B and C will be evaluated as (A div B) and C

Expressions using ‘if then else’

Sym3 Operator user can also write an expression containing an “if-then-else” pattern. The classic pattern is:

if <Condition> then <Value1> else <Value2> end
    <Condition> is a Boolean tag, <Value1> and <Value2> are variables of any type.

Please note the statement rules:

  • The ’else ’ is optional.
  • or can be replaced by ""
  • if-then-else patterns can be set as Values inside if-then-else (nested)
  • ’end’ is mandatory (otherwise an error is reported)

Examples:

Expression BooleanTag = True BooleanTag = False
if BooleanTag then 1 else 2 end 1 2
if BooleanTag then 1 end 1
if BooleanTag then 1 else end 1
if BooleanTag then else 2 end 2
if BooleanTag then 1 else 2 ERROR ERROR
if BooleanTag then 1 ERROR ERROR
if BooleanTag then (if BooleanTag2 then 1 else 2 end) else (if BooleanTag2 then 3 else 4 end) end 1 if BooleanTag2 = True 2 otherwise 3 if BooleanTag2 = True 4 otherwise

Troubleshoot

Logs are created here:

C:\ProgramData\Sym3\Sym3 Operator\Logs\Server\Sym3.Operator.Server.TagExpression.log

When the binding is wrong

For example, when the binding is using a tag that is not listed in the tag list, you should see this error in the log file:

TagExpressions - **Wrong binding using the expression “**if (Tag1 = 4) and (Tag2 = 2) then 1 else 0 end**”, please check that tags used in this expression are valid and listed in the project**

Binding is correct but tag is invalid

For example, if the expression is using a tag that is defined but the address is wrong or the value is of BAD quality, you would expect an error like this in the log file:

TagExpressions - **Invalid tag used in expression “**if (Tag1 = 4) and (Tag2 = 2) then 1 else 0 end**”. Possible reason: the tag is of BAD quality.**

Expression is incorrect

If there is an error in the expression itself you will see an error like this:

TagExpressions - **Expression is incorrect, parsing failed. Expression “**if (Tag1 = 4) and (Tag2 = 2) then 1 else 0 end**”**

Please note that this error is preceded by another error like this:

TagExpressions - **Invalid tag used in expression “**if (Tag1 = 4) and (Tag2 = 2) then 1 else 0 end**”. Possible reason: the tag is of BAD quality.**

In this example above, the end is missing at the end of the expression