Working with EFX types
Types that generators receive
The methods of your generators receive expressions whose types are already known. The toolkit finds and checks the types before it calls your code. You do not need to check types, find the types of fields, or work out whether an expression gives one value or several.
This page explains the types that you meet, and how they pass through the methods of your ScriptGenerator and
MarkupGenerator.
The classes of expressions
The toolkit has one class for each data type of EFX, and for each of the two cases: a single value or a sequence of
zero or more values. The page The expression and markup model lists them.
The classes keep the names of the types in Java, so the EFX types text and indicator become String… and
Boolean…: StringExpression, BooleanSequenceExpression and so on.
These are the types of the parameters and return values of the methods of your ScriptGenerator. References to
nodes of the notice, for example a lot or a procedure, have no value. They are always paths: NodePath for one
occurrence and NodeSequencePath for several. You meet them in the methods that deal with paths and contexts, such
as composeExistsCondition and composeCountOperation, not in the methods that compute values.
Where values come from
The Java class of an expression shows its data type, and also where its value comes from:
- Computed, for example
StringExpression -
A method of your
ScriptGeneratorcalculated the value from other values: by a concatenation, an arithmetic operation, a function call and so on. - Read from the notice, for example
StringPath -
The value comes from a place in the notice. A field reference such as
BT-00-Textgives a path, which points to an element of the XML. Paths implementPathExpression. They are not subclasses of the computed classes: aStringPathis not aStringExpression. - Literal, for example
StringLiteral -
The value is written directly in the EFX source code, such as
'hello'or42. A literal class is a subclass of the computed class of its type: aStringLiteralis aStringExpression.
A method that takes a reference, such as composeExistsCondition(PathExpression), receives the path itself. A
method that takes a value receives the value of the field instead. The toolkit first calls
composeFieldValueReference with the path, and then converts the result to the class of the parameter. So
composeStringConcatenation receives the value of BT-00-Text as a StringExpression, not as a StringPath.
The model also has classes for sequence literals, such as StringSequenceLiteral, but the EFX-2 translator does not
create them. It translates every list with composeList, also a list of literals given as the value of a sequence
parameter, such as ['works', 'services']. Your method receives the items, already translated, and returns a
computed sequence.
How the toolkit calls your methods
The parser reads an expression and gives its structure as a tree, the parse tree. The toolkit then goes through this
tree. For each part of the expression, such as an operator or a function call, it first translates the parts inside
it, its operands. It then checks the operands against the parameters that this part of the language accepts. If
they match, it calls the method of your ScriptGenerator with the translated operands, and uses your result as an
operand of the part around it.
The following example shows the translation of this EFX expression, from the context ND-Root:
concat('Hello ', BT-00-Text)
The toolkit calls these methods:
-
getStringLiteralEquivalent("'Hello '"), for the literal. Your method receives the literal as written in EFX, with its quotes, and returns aStringLiteral. -
composeFieldValueReference(StringPath), for the value of the field. The toolkit has found in the SDK metadata thatBT-00-Textis a text field that does not repeat in the context, so its path is aStringPath. -
composeStringConcatenation(List<StringExpression>), for the functionconcat. The list holds aStringLiteraland aStringExpression, the value of the field:// Your ScriptGenerator method receives expressions whose types are already known @Override public StringExpression composeStringConcatenation(List<StringExpression> list) { // list contains a StringLiteral ('Hello ') and a StringExpression (the value of BT-00-Text) return new StringExpression("[" + list.stream().map(Expression::getScript).collect(Collectors.joining(", ")) + "].join('')"); }
Each expression holds a script text: the piece of code in the target language that it stands for. Your methods
combine these pieces to build larger expressions in your target language.
Do not rely on the order of the calls beyond what the structure of the expression requires. The toolkit translates
the operands of an operation before the operation, but it can translate a literal before a field reference that is
written before it, as here: in BT-00-Number > 100, it calls getNumericLiteralEquivalent("100") before
composeFieldValueReference.
Common method patterns
The methods of your ScriptGenerator follow a few common patterns:
Binary operations
Methods that combine two expressions of the same type:
@Override
public BooleanExpression composeLogicalAnd(BooleanExpression left, BooleanExpression right) {
return Expression.instantiate(
operand(left, Precedence.AND) + " && " + operand(right, Precedence.COMPARISON),
Precedence.AND, BooleanExpression.class);
}
The helper method operand puts an operand in parentheses when the grouping of the EFX expression needs them, and
the result receives the precedence of the operation (see
Precedence of operations).
Comparisons
Comparisons accept any single-value type, and return an indicator (a boolean):
@Override
public BooleanExpression composeComparisonOperation(
ScalarExpression left, String operator, ScalarExpression right) {
// compare("==", a, b) gives true or false. The EFX operator goes to the helper as a text.
return new BooleanExpression("compare(" + getStringLiteralFromUnquotedString(operator).getScript()
+ ", " + left.getScript() + ", " + right.getScript() + ")");
}
The type of the parameter is ScalarExpression, the common base class, not a specific type such as
StringExpression. EFX compares values as XPath does, according to their type. In JavaScript, two Date objects
for the same moment are not equal with ===, so the example calls a helper of the runtime library, compare (see
Comparisons). If you prefer to write the comparison
in your method instead, check the data type with left.is(EfxDataType.Duration.class).
Type conversions
Methods that convert between types:
@Override
public StringExpression composeToStringConversion(NumericExpression number) {
return new StringExpression("String(" + number.getScript() + ")");
}
@Override
public StringExpression composeToStringConversion(BooleanExpression bool) {
return new StringExpression("String(" + bool.getScript() + ")");
}
There is one method for each input type, with the same name, so Java calls the right method by itself.
Sequence operations
Several methods work with sequence types. They usually take or return SequenceExpression, the base type of all
sequences:
@Override
public NumericExpression composeCountOperation(SequenceExpression list) {
return new NumericExpression(operand(list, Precedence.PRIMARY) + ".length");
}
@Override
public <T extends SequenceExpression> T composeList(
List<? extends ScalarExpression> list, Class<T> type) {
String items = list.stream().map(Expression::getScript).collect(Collectors.joining(", "));
return Expression.instantiate("[" + items + "]", type);
}
An iteration over sequences uses a for expression. For the EFX expression
for number:$x in [1, 2, 3] return $x * 2, the toolkit calls your method with the iterators and the expression
after return, both already translated. In an EFX iteration, the expression after return always reaches your
method as a sequence: a single value becomes a sequence of one value. So the toolkit calls the form of
composeForExpression that takes a SequenceExpression, and your result joins the sequences of all the rounds:
@Override
public <T extends SequenceExpression> T composeForExpression(
IteratorListExpression iterators, SequenceExpression expression, Class<T> type) {
// iterators.getScript() → for (let $x of [1, 2, 3])
// expression.getScript() → [$x * 2]
return Expression.instantiate(
"(() => { const __result = []; " + iterators.getScript()
+ " __result.push(..." + expression.getScript() + "); return __result; })()",
type);
}
The other form, which takes a ScalarExpression, gives one value for each round. The template translator uses it,
for example to format each number of a sequence.
composeList and composeForExpression use Expression.instantiate(script, type) rather than a constructor. This
method creates the right sequence class, for example StringSequenceExpression or NumericSequenceExpression,
according to the parameter Class<T>.
Generic methods
Some methods have a type parameter, so that they work with every type:
@Override
public <T extends TypedExpression> T composeConditionalExpression(
BooleanExpression condition, T whenTrue, T whenFalse, Class<T> type) {
return Expression.instantiate(
"(" + condition.getScript() + " ? " + whenTrue.getScript() + " : " + whenFalse.getScript() + ")",
type);
}
The parameter Class<T> type gives the class to return. To create an instance of that class, use
Expression.instantiate(script, type), the same method as in sequence operations.
Asking an expression for its type
The expressions that stand for values or paths implement TypedExpression, which gives you their EFX data type. The
expressions that are only parts of the structure of an expression, such as IteratorExpression,
IteratorListExpression and DeclarationExpression, do not:
// Check the data type
if (expression.is(EfxDataType.Duration.class)) {
// Handle duration-specific logic
}
// Get the data type class
Class<? extends EfxDataType> dataType = expression.getDataType();
This is useful in methods such as composeComparisonOperation, whose parameter type is a base class, when your
target language handles each type in its own way.
Types that only the metadata gives
The grammar of EFX describes only the syntax, so the parser does not know any types. The toolkit finds the type of each part of an expression during the translation: from the literals, from the declarations of variables and functions, from the results of the parts inside it, and from the SDK metadata for references.
For example, when EFX refers to BT-00-Text, the symbol resolver finds in the SDK metadata that it is a text field
that does not repeat in the current context, and gives its path as a StringPath. If the same field repeated, it
would give a StringSequencePath instead. Your methods never receive an expression whose type is unknown.
For how the toolkit finds these types, see Type system internals.
Error handling
If the types do not match, for example when a repeatable field is used where a single value is expected, the
toolkit throws a TypeMismatchException, and the translation stops. The message gives the line and the column, for
example:
line 1:21 Type mismatch. Operator '==' cannot take 'text*' and 'text'. Its left operand must be 'text'. Field 'BT-13-Text' may return multiple values from context 'ND-Root': use a sequence expression or change the context.
You do not need to catch or handle type errors in the methods of your ScriptGenerator. The toolkit checks the
operands of each part of the language before it calls the method of that part, so your method never receives
operands of the wrong type.
If your own method throws an exception, the translation stops with your exception. The toolkit does not try to recover from errors.
See also: