# Expressions in AdoScript â€‹

Every definition of expression attributes is started with the keyword EXPR. The result type is is defined with the attribute type: and the default formula is defined with the attribute expr:. Every time you create an instance (a model, object, or connector), this formula will be used to compute the result value of the expression. By setting the modifier fixed:, you make the expression attribute a fixed expression. The user will the not be able to change the formula in the Modelling Toolkit. The formula itself (defined in the attribute expr:) must never be longer than 3600 characters. For expressions with result type double, the attribute format can be used to specify the number of digits that should be displayed on the user interface. Note: the number of digits displayed on the user interface do not affect the internal precision of the expression result value.

**AdoScript vs. Expressions** â€‹

As expression implement "formulas" to calculate attribute values, the following table provides a comparision with the AdoScript language and to what extend the implementation approaches differ.

| **AdoScript** | | **Expressions** | | Allows embedding external functionality | | No external functionality | | Read and write access to most attributes | | Read access to most attributes, write access only to own attribute | | Must be triggered explicitly by the user | | Are triggered automatically | | Can embed Expressions | | N/A | | Can not be changed by the modeler | | Can be changed by the modeler if not defined as "fixed" | | Usually synchronous execution | | Can be synchronous or asynchronous (idle-processing) | | Any complexity | | Usually less complex than AdoScripts | | | | Careful with closed models (values can be outdated) |

## Types of Expressions â€‹

**LeoExpressions** â€‹

- Provide a basic set of functions and operators
- Support for calculation of values, manipulation of strings and other basic operations
- Used inside LEO based languages

**CoreExpressions** â€‹

- Extension of LeoExpressions
- Only used in EXPRESSION attributes

**AdoScriptExpressions** â€‹

- Extension of LeoExpressions
- Additional functions can be created (using the keyword FUNCTION)
- Only used in AdoScripts

## Expressions - Operations â€‹

|**Logical Operators**| | AND , OR , NOT | | Boolean expressions | |**Comparison Operators**| | < , <= , > , >==, <>, != | | Bigger, smaller, equal, diverse | | **Arithmetical Operators** | | + , - , * , / - (unary) | | | | **String Operators** | | s + t | | Concatenation of Strings s and t | | n * s | | Replication: String s is replicated n-times | | s / t | | Count: how often can String s be found in t | | s SUB i | | The i-th character in String s | | LEN s | | Length of Strings s | | **Conversion Operators** | | STR val | | String representation of Value val | | VAL str | | Numerical representation of Strings str | | CMS measurePTS measure | | Conversion of a Unit (in cm or points) to a real number (e.g.: CMS 3.5cm Ã 3.5). | | CM realPT real | | Conversion of a real number to a Unit (in cm or points; e.g.: CM 3.5 Ã 3.5cm). | | uistr (val, n) | | Conversion of a real number to a string in the local format (OS) with n digits. | | uival ( str ) | | Conversion of a String in the local format (OS) to a real number. | | **Sequence Operators** | | , | | The comma is used to define a sequence of expressions. The result is always the value of the last expression. | | | | |

## Expressions â€“ Predefined functions â€‹

| **Mathematical Functions** | | abs(x)max(x, y) min(x, y)pow(x, y) sqrt(x)exp(x)log(x) log10(x) | | Arithmetic functions | | sin(x) cos(x) tan(x)asin(x) acos(x) atan(x)sinh(x) cosh(x) tanh(x) | | Trigonometric functions | | random() | | Random value0 >= n < 1 | | round(x) | | Round-to-nearest, i.e. if decimal >= 0.5 | | floor(x) ceil(x) | | Round up/down | | **String Functions** | | search(source, pattern, start) | | Searches in source for pattern, starting at start (0-based), returns index or -1 | | bsearch(source,pattern,start) | | Search begins at end of source string (backwards) | | copy(source, from, count) | | Copies count characters from source beginning at from (0-based) | | replall(source, pattern, new) | | Replaces all occurrences of pattern in source with new | | lower(source) | | Transforms to lower-case | | upper(source) | | Transforms to upper-case | | mstr(string) | | Puts the string between "" and escapes special characters | | **List Functions** | | tokcnt(source[,sep]) | | Counts tokens in source separated by sep (default = single whitespace) | | tokcat(source1, source2 [,separator]) | | Concatenates two lists | | tokunion(source1, source2[, separator]) | | Union of two lists | | tokisect(source1, source2 [, separator]) | | Intersection of two lists | | tokdiff(source1, source2 [, separator]) | | Difference of two lists | | **Color Functions** | | rgbval(colorname) | | 24bit RGB-Value of the color (by name) | | rgbval(r, g, b) | | Calculates the RGB-Value for the provided color values. |

## Expressions â€“ Control structures â€‹

| **Expressions** | | set (var, expr) | | Expr will be stored in var. Variable var is created implicitly. |

| cond (cond1, expr1, cond2, expr2, ..., expr_else) | | Evaluate cond1, if true return expr1, if false return next condition or return expr_else. |

| while (cond, loopexpr [, resultexpr]) | | While cond is true, evaluate loopexpr. Return resultexpr. |

| fortok (varname, source, sep, loopexpr [, resultexpr]) | | For each element in the list source, evaluate loopexpr. The current element is stored in varname. The list elements are separated by sep. Return resultexpr. |

## Expressions â€“ Error handling, type checks â€‹

| **Error handling** |

| try (expr, failexpr) | | Returns expr, if it succeeds, otherwise returns failexpr. |

| **Type check** |

| type (expr) | | Returns the type of the expression. Possible values: "string", "integer", "real", "measure", "time", "expressionâ€ž or "undefinedâ€ž. |

## Expressions in AdoScript â€‹

**Core Expressions** â€‹

- Are used to define attributes with the type EXPRESSION
- Can access functions for

**AdoScript Expressions** â€‹

- Are used in AdoScript
- Can be externalized in functions
- Can access externalized function (defined through keyword FUNCTION)

**Functions for Core Expressions** â€‹

The following functions can be used in Core Expressions:

| aval() | | rcount() | | asum() | | avalf() | | row() | | amax() | | maval() | | rasum() | | awsum() | | paval() | | allobjs() | | pmf() | | pavalf() | | aql() | | class() | | irtmodels() | | prevsl() | | mtype() | | irtobjs() | | nextsl() | | mtclasses() | | profile() | | | | mtrelns() | | ctobj() | | | | allcattrs() | | cfobj() | | | | alliattrs() | | conn() | | | | allrattrs() |

Additionally all LEO expressions and functions can be used

**Attributes of the type EXPRESSION** â€‹

- An expression attribute contains both a formula and the calculated value.
- There are two modes for using expression: fixed and editable.
- Fixed expressions store the formula in the default value of the attribute.
- An error massage will be returned, if an error occurs when evaluating a formula.
- The last valid result is returned, if an inter-model expression can not be evaluated (when trying to access a not loaded model)
- Expression attributes are always evaluated when an event occurs which can change the value. The changes are shown directly in the user interface.

**Definition of expressions as an attribute** â€‹

**Syntax**

| *ExprDefinition*: | | **EXPR type** : *ResultType* [**format** : *FormatString*] **expr** :[**fixed** :]*CoreExpression* | | | | | | *ResultType* : | | **double** | **integer** | **string** | **time** | | | | |

**Example**

EXPR type: string expr: (""Name = " + aval("Name")")

## AdoScript Expressions Application â€‹

Expressions can be used directly as arguments of calls and be embedded directly in AdoScript code. Parenthesis are used to delimit the arguments of an expression.

|SET n: (copy (vn, 0, 1) + ". " + nn) IF ( cond( type( n ) = "integer", n = 1, 0 ) ) { ... }| |EXECUTE ("SET n:(" + n + ")")|

Expressions can also be moved to dedicated functions so they can be reused.

**Functions in AdoScript** â€‹

It is possible to define LEO expressions as reusable functions through the keyword FUNCTION.

**Syntax** â€‹

| *FunctionDefinition*: | | **FUNCTION** *functionName*[: **global**] { *FormalFuncParameter*} **return** :expression | | | | | | *FormalFuncParameter*: | | *paramName*: *TypeName* | | | | | | *TypeName* : | | **string** | **integer** | **real** | **measure** | **time** | **expression** | **undefined** | | | | |

**Example** â€‹

|#define function| |FUNCTION helloWorld world:string| |return: ("Hello " + world + "!")|

|#call function| |SET hello:(helloWorld("world"))| |CC "AdoScript" INFOBOX (hello)|