Maintaining and Using Syntax Variables
Overview
Syntax variables can be defined with organisation or laboratory scope, and subsequently used in syntaxes. Where a laboratory-scope syntax variable has the same name as an organisation-scope syntax variable, the laboratory-scope override is respected.
Process
Reserved Keywords
Some keywords are reserved by the system. These cannot be used to define new variables, but they can be used or overridden. These keywords include: TODAY, TIMEZONE, DATE, OFFSET.
Example of use:
myDate=TODAY.format('ddMMMyyyy') // use today's date-time
Example of overriding:
TODAY = "Today now is an string"
The TODAY variable is a Joda calendar object and is time-zone-aware.
The TIMEZONE is based on the time-zone of the current laboratory. This means that when $HH is used in a syntax, it offsets from GMT by the time zone offset for the current laboratory. If a laboratory's Time Zone is not defined, then the JVM default is used. Refer to Maintaining Laboratories.
In-built Syntax Variables
The system provides in-built syntax variables for date formatting, and are therefore pre-defined already, and do not need to be redefined in the syntax script.
An in-built syntax variable may be overridden by an organisation or laboratory-scope custom variable with the same name.
Example to apply custom formatting:
yyyy = TODAY.format('yy') // override the in-built yyyy variable
Example to set up a new date-time format using the in-built syntax variables:
myDate = TODAY.format('ddMMMyyyy') // example of output is 02Apr2021
In-build date and time syntax variables
Context-aware Dynamically Created Variables
When a syntax is generated, the system exposes various objects to the syntax. The values of attributes in these objects are established as dynamic variables. The type of objects exposed to the syntax script depends upon the type of syntax being generated.
These context-aware dynamic variables are referenced in a syntax script using the $ prefix with the object name and field name, then referencing the entity's value, as follows:
$[Objectname].[fieldname].asEllipseValue
Example:
$Job.code.asEllipseValue
Testing the syntax returns the field name, but on run-time it is evaluated to the actual value of the field.
If a syntax test determines that the object or field name is undefined, it is most likely not exposed for use for the type of syntax.
Objects and Attributes Exposed to Syntax Scripts based upon the Syntax Type
Dynamically Created Variables using Script
A variable can be set up dynamically at tun-time by running Groovy code contained within the syntax.
These dynamic variables are established in a syntax script using the $ prefix with the Groovy script in curly brackets, as follows:
${<GroovyScript>}
Example:
${String Lab=Laboratory.name.asEllipseValue
return Lab}
Every occurrence of ${...} in the syntax is evaluated as a string for the dynamic variable it represents, as follows:
- As long as it evaluates to a non-null string.
- As long as it does not contain '${' nor '}', because whatever is put in this field is treated as one string.
- If the result is not a string, the syntax generator automatically attempts to convert it to a string.
- Variables in ${...} are not prefixed with '$', for example, ${Job} and $Job are the same.
- To use $VarName, then do it outside ${...}.
- The syntax can contain multiple ${...}. For example, $Job.name${if foo then bar}${true}.
Example:
${/*
Comments
Comments
*/
def test = true
if (test)
return "it's a test"
else
return "won't see me"
}_${yyyy}_####returns:
it’s a test_2013_0001
Example:
${String myVar=Job.code.asEllipseValue
String otherVar=""
if (myVar=="[code]")
otherVar="A12"
else
otherVar="B12"+"C1234"
otherVar=otherVar+Job.description.asEllipseValue
if (otherVar.substring(0,2).equals("B1") )return otherVar
else
return "D12345"
}_${yyyy}_####
Defining Custom Variables in a Syntax Script
All custom variables need to be defined in the syntax script.
Variables are case sensitive. For example, ZZ is a different variable to zz.
def ZZ = "Hello world"
Example of attempting to declare a new variable using a keyword:
def DATE = XXXXX // this cannot be done as DATE is an in-built syntax variable
def date = "Something" // this can be done since variables are case-sensitive
Referencing Syntax Variables in a Syntax Script
Variables are reference in a syntax script using the $ prefix. For example:
$ZZ
Setting Values to Syntax Variables
Variables are defined with a simple equal statement.
Myvariable = 'anystring I like'
Syntax variables, either in-built or custom, can be combined in a syntax. For example:
MMMyyyy = TODAY.format('yy')+"'"+TODAY.format('yyyy') // example of output is Apr'2021
Variables can be processed with various string and reformatting functions.
Examples:
MyVar = 'my string'.toUpperCase() // uppercase a string
MyVar = 'My String'.toLowerCase() // owercase a string
