Variables

A variable gives a name to a value. EFX computes the value once, and you can then use it as often as you need. Every variable has a data type, which you write in its declaration, and receives its value when you declare it. Besides ordinary variables, EFX has three other kinds of variable:

  • context variables, which name a place in the notice;

  • dynamic variables, which hold the result of a call to an external service;

  • iterator variables, which take each item of a sequence in turn.

The last entry on this page explains where you can use each variable.

Variable declaration

A variable declaration creates a variable of a given type and gives it its value.

Syntax
LET <type>:$<variable-name> = <expression>;
LET <type>*:$<variable-name> = <sequence-expression>;
<type>:$<variable-name> = <expression>
<type>*:$<variable-name> = <sequence-expression>
<type>

the data type of the variable: text, indicator, number, date, time or duration (see Data types). The types context and dynamic have their own entries: Context variable and Dynamic variable.

*

makes the variable a sequence of values of that type (see Sequence).

<variable-name>

the name of the variable (see Identifiers). Names are case-sensitive and can contain hyphens, for example $lot-ids. You write the name directly after $, without a space.

<expression>

an expression of the declared type that gives a single value (scalar).

<sequence-expression>

an expression of the declared type, for example a sequence in square brackets, a field reference, a codelist reference, an iteration or a function that returns a sequence. You can also write a single value, which counts as a sequence of one value.

The first two forms are stand-alone declarations, and you end them with a semicolon. You write them in three places:

You write the last two forms in the WITH clause of a template line or a rule-set, before or after the context, separated by commas (see Context declaration and WITH clause).

A single expression cannot declare variables. It receives values through parameters instead (see Parameters of a single expression).

You can put spaces around the colon and the equals sign.

EFX computes the expression once, where you declare the variable, and the variable keeps that value. EFX reads a declaration at the beginning of a file or of a validation stage from the root of the notice. In a WITH clause, it also reads a variable written before the context from the root of the notice. A variable written after the context, however, is computed again for each occurrence of the context.

The expression must have the declared type. A variable declared without * holds a single value, so its expression cannot be one that may give several values, such as a reference to a repeatable field. A variable declared with * holds a sequence. Its expression can give a sequence, or a single value, which then becomes a sequence of one value.

Restrictions

  • You must give a variable its value when you declare it.

  • You cannot declare a name again where a variable of the same name is visible (see Scope of variables).

  • A name cannot start with two underscores, because such names are reserved (see Identifiers).

  • You can write stand-alone declarations only in the places listed above. For example, you cannot write them between template lines or between rule-sets.

Examples

LET number:$threshold = 100;
LET text*:$allowed = ['a', 'b', 'c'];
WITH ND-Root DISPLAY ${BT-00-Number > $threshold} ${BT-00-Text in $allowed};

Two variables declared at the beginning of a template file.

LET number*:$lengths = for text:$x in BT-13-Text return string-length($x);
WITH ND-Root DISPLAY ${sum($lengths)};

A sequence variable that receives the result of an iteration.

WITH ND-RepeatableNode, text:$label = concat('Item ', BT-00-Text-In-Repeatable-Node) DISPLAY ${$label};

A variable declared after the context. EFX computes it for each occurrence of ND-RepeatableNode.

---- STAGE 1a ----
LET date:$start = BT-00-StartDate;
WITH ND-Root, duration:$length = BT-00-EndDate - $start
  ASSERT $length <= P30D AS ERROR R-AB1-C2D FOR BT-00-EndDate IN *;

A stage variable and a variable of a rule-set, in a rules file.

From the SDK (validation/stage-5m.efx)
LET text* : $global-lot-ids = BT-137-Lot:rawValue;

A sequence variable declared at the beginning of a validation stage.

EFX-1 had no variable declarations: the only variables were iterator variables and parameters.

See also: Variable reference

Context variable

A context variable gives a name to the context of a template line or a rule-set. With this name, you can still read fields from that context after the context has changed.

Syntax
WITH context:$<variable-name> = <node-context>
WITH context:$<variable-name> = <field-context>
WITH <variable-list>, context:$<variable-name> = <node-context>, <variable-list>
<variable-name>

the name of the variable.

<node-context>

a node reference. It can start with a slash and have predicates and an index.

<field-context>

a field reference. It can start with a slash and have predicates and an index.

<variable-list>

optional. Ordinary variable declarations, written before or after the context (see Variable declaration).

A context variable takes the place of the context in the WITH clause of a template line or a rule-set: it declares the context and gives it a name. EFX displays the template line, or checks the rules of the rule-set, once for each occurrence of the context. Each time, the variable holds the current occurrence. For the rest of the WITH clause, the template line and the rules, see Context declaration and WITH clause.

You use a context variable before :: in a context override: $name::field reads the field from the occurrence that the variable holds. You need this where the context is no longer the one that the variable names: in nested template lines, inside iterations and inside predicates. You can also use the variable in the variables declared after it in the same WITH clause.

You can also use a context variable that holds a field as a value. It gives the value of that occurrence of the field, with the data type of the field. A context variable that holds a node has no value.

You can also declare context variables in iterations, with context:$name in … (see Iterator variable).

Restrictions

  • A WITH clause has at most one context, so it declares at most one context variable. The context variable is the context itself, so it cannot appear among the variables before or after the context.

  • You cannot declare a context variable with LET, and a single expression cannot declare one.

Examples

WITH context:$lot = ND-RepeatableNode DISPLAY ${BT-00-Text-In-Repeatable-Node};
    WITH ND-RepeatableInRepeatableNode DISPLAY ${BT-23-Text} (${$lot::BT-00-Number-In-Repeatable-Node});

The nested template line reads a field of the occurrence of ND-RepeatableNode that the outer line names.

---- STAGE 1a ----
WITH context:$sub = ND-SubNode, text:$name = $sub::BT-01-SubNode-Text
  ASSERT $name != '' AS ERROR R-AB1-C2D FOR BT-01-SubNode-Text IN *;

A context variable used in a variable declared after it.

WITH context:$n = BT-13-Number DISPLAY ${$n + 1};

A context variable that holds a number field, used as a number.

In EFX-1, you could declare context variables only in iterations. Naming the context of a template line or a rule-set is new in EFX-2.

Dynamic variable

A dynamic variable holds the result of a call to a dynamic function, that is, a function that an external service evaluates when the notice is validated.

Syntax
LET dynamic:$<variable-name> = ?<function-name>(<argument>, <argument>, …);
dynamic:$<variable-name> = ?<function-name>(<argument>, <argument>, …)
<variable-name>

the name of the variable.

<function-name>

the name of a dynamic function declared in the same rules file (see Dynamic function declaration).

<argument>

one expression for each parameter of the function, of the type of that parameter. You call a function without parameters with empty parentheses.

The first form is a stand-alone declaration: you write it at the beginning of a rules file or of a validation stage. You write the second form in the WITH clause of a rule-set, before or after the context. These are the same places as for other variables (see Variable declaration).

The value of a dynamic variable is the response of the service. It is an indicator, and you can use it wherever an indicator is expected, also in the declaration of other variables. If you use a variable rather than call the function in each rule, several rules can share one call.

When the call fails, the rules whose condition uses the variable do not report a violation. Instead, the validation reports the failure itself, with the severity set in the ON ERROR clause of the dynamic function declaration.

Restrictions

  • You can declare dynamic variables in rules files only.

  • The value must be a single call of a dynamic function and nothing else: the call cannot be part of a larger expression.

  • You cannot give the result of a dynamic function to a variable of another type, such as indicator. Use the type dynamic.

Examples

ENDPOINT 'registry' AT 'https://api.example.org';
LET dynamic:?is-registered(text:$id) CALL API 'registry';

---- STAGE 1a ----
LET dynamic:$registered = ?is-registered(BT-00-Identifier);

WITH ND-Root
  ASSERT $registered AS ERROR R-AB1-C2D FOR BT-00-Identifier IN *;

A dynamic variable declared at the beginning of a validation stage and used in a rule.

ENDPOINT 'registry' AT 'https://api.example.org';
LET dynamic:?is-registered(text:$id) CALL API 'registry';

---- STAGE 1a ----
WITH ND-RepeatableNode, dynamic:$registered = ?is-registered(BT-00-Text-In-Repeatable-Node), text:$status = if $registered then 'known' else 'unknown'
  ASSERT $status == 'known' AS WARNING R-AB1-C2D FOR BT-00-Text-In-Repeatable-Node IN *;

A dynamic variable declared after the context. The function is called for each occurrence of ND-RepeatableNode, and another variable uses the result.

Dynamic variables are new in EFX-2. EFX-1 could read fields of other notices, with notice(…)/BT-…. EFX-2 no longer accepts this: a rules file receives information from outside the notice through dynamic functions.

Iterator variable

An iterator variable takes each item of a sequence in turn. You use it in an iteration or in a quantified expression, that is, an expression that tests whether some or every item of a sequence meets a condition.

Syntax
<type>:$<variable-name> in <sequence>
context:$<variable-name> in <node-context>
context:$<variable-name> in <field-context>
<type>

the type of the items: text, indicator, number, date, time or duration.

<variable-name>

the name of the variable.

<sequence>

an expression that gives a sequence of the given type. You can also write a single value, which counts as a sequence of one value.

<node-context>, <field-context>

a node or field reference. It can start with a slash and have predicates and an index.

You declare an iterator variable in the list of iterators of for … return (see Iteration), or of some … satisfies and every … satisfies (see Quantified expression). EFX computes the expression after return or satisfies once for each item, with the variable holding that item. You separate several iterators with commas, and each iterator can use the variables of the iterators before it.

An iterator variable of type context holds each occurrence of a node or field in turn. You use it before :: in a context override, to read fields from that occurrence. An iteration does not change the context: without $name::, EFX reads a reference in the return or satisfies expression from the context of the whole expression. You can also use an iterator variable of type context that holds a field as a value, with the data type of the field.

You can use the variable only inside the iteration or quantified expression that declares it (see Scope of variables).

Restrictions

  • The type must match the type of the items of the sequence.

  • An iterator variable holds one item, so you cannot give it a sequence type such as text*.

  • You cannot put a context override before the node or field of a context iterator.

  • You cannot use a name that is already visible (see Scope of variables).

Examples

for text:$x in BT-13-Text return upper-case($x)
for date:$d in BT-13-Date return year($d)
for text:$x in BT-13-Text, text:$y in BT-14-Text[BT-14-Text == $x] return $y

The second iterator uses the variable of the first.

some context:$n in ND-RepeatableNode satisfies $n::BT-00-Text-In-Repeatable-Node is present

$n holds each occurrence of ND-RepeatableNode in turn, and $n:: reads the field from that occurrence.

In EFX-1, you declared a duration iterator variable with measure: instead of duration:, and you could not put a space between the type and the colon.

See also: Context variable

Scope of variables

The scope of a variable is the part of a file in which the variable is visible, that is, in which you can refer to it.

A variable is visible from its declaration to the end of the part of the file that contains the declaration, as the table shows:

Declared Visible in

at the beginning of a template file

the rest of the file: the later declarations of variables, dictionaries, user-defined functions and callable templates, and all the template lines, including those of the header, summary and navigation sections

at the beginning of a rules file

the rest of the file: the later declarations and all the validation stages

at the beginning of a validation stage

the rest of that stage: its later declarations and all its rule-sets

in the WITH clause of a template line, including a context variable

that template line and the lines nested under it

in the WITH clause of a rule-set, including a context variable

the rest of the WITH clause and all the rules of the rule-set

as an iterator variable

the later iterators of the same list and the expression after return or satisfies

as a parameter of a single expression, user-defined function or callable template

that expression, the body of the function or the content of the template

You can refer to a variable only after its declaration. In a list of declarations, such as the variables of a WITH clause, each declaration can use the variables declared before it, but not those declared after it.

Two variables with the same name cannot be visible at the same place, so you cannot declare a name again where a variable of that name is visible. In other words, a variable cannot hide another one:

  • a template line nested under a line that declares $name cannot declare $name again;

  • a rule-set cannot declare a variable with the name of a stage variable or of a global variable;

  • an iteration cannot use the name of a visible variable.

You can, however, declare the same name in places that do not overlap: in two template lines when neither is nested under the other, in two rule-sets, in two validation stages, or in two separate iterations.

Names are case-sensitive: $total and $Total are two different variables.

Restrictions

  • A variable cannot have the name of a user-defined function, callable template, dictionary or dynamic function declared in the same file, even though you write their names differently (with $, with ?, or without a prefix).

Examples

LET number:$max = 100;
WITH ND-RepeatableNode, text:$name = BT-00-Text-In-Repeatable-Node DISPLAY ${$name};
    WITH ND-RepeatableInRepeatableNode DISPLAY ${$name}: ${BT-23-Text};
WITH ND-SubNode, text:$name = BT-01-SubNode-Text DISPLAY ${$name} ${$max};

$max is visible in the whole file. The first $name is visible in its own line and in the nested line. The last line is not nested under the first, so it can declare its own $name.

LET number:$max = 100;
---- STAGE 1a ----
LET number:$min = 0;
WITH ND-RepeatableNode, number:$n = BT-00-Number-In-Repeatable-Node
  ASSERT $n >= $min AS ERROR R-AB1-C2D FOR BT-00-Number-In-Repeatable-Node IN *
  ASSERT $n <= $max AS ERROR R-AB1-C2E FOR BT-00-Number-In-Repeatable-Node IN *;
WITH ND-RepeatableInRepeatableNode, number:$n = count(BT-23-Text)
  ASSERT $n <= $max AS ERROR R-AB1-C2F FOR BT-23-Text IN *;
---- STAGE 1b ----
WITH ND-Root
  ASSERT count(BT-13-Text) <= $max AS ERROR R-AB1-C2G FOR BT-13-Text IN *;

$max is visible in both stages, $min only in stage 1a, and each rule-set has its own $n.

count(for text:$x in BT-13-Text return $x) + count(for text:$x in BT-14-Text return $x)

Two separate iterations can use the same name.

In EFX-1, the only variables were iterator variables and parameters, each visible in its iteration or expression.