The expression and markup model

Before you implement a generator, you need to know the types that its methods receive and return. A script generator works with expressions, and a markup generator with markup. Both are wrappers, that is, objects that hold a piece of code in your target language. The translators pass them from method to method, and do not read the code inside, with one exception: the rules translator looks in the scripts of a rule for the variables that it uses (see Variables in the scripts of rules).

Expression: a wrapper for code in the target language

An Expression holds a script: a piece of code in your target language, such as the XPath cbc:RegulatoryDomain/normalize-space(text()) = 'other'. It has two properties:

getScript()

The script, as a String.

getPrecedence()

How strongly the operation at the top of the script holds its operands together (see Precedence).

Most methods work with a TypedExpression, which also gives its EFX data type:

getDataType()

The EFX data type of the expression, as a class of EfxDataType, such as EfxDataType.StringScalar.class.

is(Class<? extends EfxDataType>)

Whether the expression has the given data type, or a subtype of it. For example, fieldReference.is(EfxDataType.Date.class) is true for a date or a sequence of dates.

The toolkit has one class for each data type and for each of the two cases: a single value or a sequence.

Data type Single value Sequence

text

StringExpression

StringSequenceExpression

multilingual text

MultilingualStringExpression

MultilingualStringSequenceExpression

number

NumericExpression

NumericSequenceExpression

indicator

BooleanExpression

BooleanSequenceExpression

date

DateExpression

DateSequenceExpression

time

TimeExpression

TimeSequenceExpression

duration

DurationExpression

DurationSequenceExpression

These classes are for computed values. They are in the package eu.europa.ted.efx.model.expressions.scalar for single values, and eu.europa.ted.efx.model.expressions.sequence for sequences. Each data type also has two other kinds of classes, in the same packages:

  • Paths, such as StringPath and StringSequencePath, are references to fields or nodes of the notice. They are not subclasses of the classes of the table: a StringPath is not a StringExpression. They have a hierarchy of their own, and implement ScalarPath or SequencePath, and so PathExpression, which can convert itself with asScalar() and asSequence(). NodePath and NodeSequencePath refer to nodes, which have no value.

  • Literals, such as StringLiteral and NumericSequenceLiteral, are values written in the EFX expression. They are subclasses of the classes of the table: a StringLiteral is a StringExpression. They all implement LiteralExpression.

The type of each parameter tells your method what it receives. A method that receives a StringExpression can rely on it giving a single text. A method that receives a PathExpression can rely on it being a reference to the notice. The type checking of the translator has already made sure that the types match (see Working with EFX types).

Creating expressions

Each class has a constructor that takes the script:

return new StringExpression("concat(" + left.getScript() + ", " + right.getScript() + ")");

When you know the class only while the program runs, or when you want to set the precedence, use the static methods of Expression:

Expression.instantiate(String script, Class<T> type)

Creates an expression of the given class.

Expression.instantiate(String script, Precedence precedence, Class<T> type)

Creates it with a precedence.

Expression.from(Expression source, Class<T> type)

Creates an expression of another class with the same script and precedence, for example to use a StringPath as a StringExpression.

Expression.empty(Class<T> type)

Creates an expression with an empty script. The empty() methods of the classes, such as NumericExpression.empty(), do the same. The toolkit uses empty expressions for optional parts, such as a missing quantity of a label.

Precedence

The toolkit passes the expressions of the operands to your method, and you combine their scripts. When an operand is itself an operation, its script can need parentheses to keep the grouping of the EFX expression. For example, in XPath, (1 = 1) = true() and 1 = 1 = true() do not mean the same.

The Precedence of an expression shows how strongly the operation at the top of its script holds its operands together. The values are, from the weakest to the strongest:

  • PREFIX: an expression that starts with a keyword, such as for or every;

  • OR, AND, COMPARISON, ADDITIVE, MULTIPLICATIVE and UNARY;

  • PRIMARY: a literal, a path, a variable, a call or an expression in parentheses.

An expression created without a precedence is PRIMARY.

When your method writes an operation, it gives the result the precedence of that operation. It puts an operand in parentheses when the operand has a lower precedence than its place in the operation requires. To compare the precedence of the operand with the one required, use Precedence.bindsAtLeast(Precedence). The XPath script generator does this as follows:

protected static String operand(final Expression operand, final Precedence required) {
  return operand.getPrecedence().bindsAtLeast(required) ? operand.getScript()
      : "(" + operand.getScript() + ")";
}

@Override
public BooleanExpression composeLogicalAnd(BooleanExpression leftOperand,
    BooleanExpression rightOperand) {
  return Expression.instantiate(operand(leftOperand, Precedence.AND) + " and "
      + operand(rightOperand, Precedence.COMPARISON), Precedence.AND, BooleanExpression.class);
}

The left operand of and can itself be an and, so it needs at least AND. The right operand needs a higher precedence, COMPARISON, so that a and (b and c) keeps its parentheses. If the precedence rules of your target language differ from those of XPath, apply them in the same way.

Variables in the scripts of rules

To find out which variables a rule uses, the method ValidationRule.findReferencedVariables looks in the translated test of the rule for $ followed by the name of each variable, as the XPath script generator writes the reference to a variable. The Schematron generator uses the result to find the dynamic variables that a rule depends on (see Implementing a validator generator).

If your script generator writes the references to variables in another way, this search does not find them. Then your validator generator must find the variables of a rule in another way.

Markup: a wrapper for the output of templates

A Markup, in the package eu.europa.ted.efx.model.templates, holds a piece of your target markup language, such as an xsl:template in XSLT. Unlike an expression, it has neither a data type nor a precedence.

new Markup(String)

Creates a markup. A null script gives an empty markup.

script

The markup, as a public field.

join(Markup)

Gives a new markup with the script of the other markup added at the end.

Markup.empty(), isEmpty()

An empty markup, and whether a markup is empty.

The methods of a markup generator receive both kinds of wrappers: the expressions of the template, which your script generator has already translated, and the markup that other methods have already produced (see Implementing MarkupGenerator).

Practical patterns

Most methods of a generator follow the same pattern: read the scripts of the parameters, combine them in the syntax of the target language, and wrap the result in the class that the method returns. For example, the concatenation of texts in XPath and in JavaScript:

// XPath
@Override
public StringExpression composeStringConcatenation(List<StringExpression> list) {
  return new StringExpression("concat("
      + list.stream().map(Expression::getScript).collect(Collectors.joining(", ")) + ")");
}

// JavaScript
@Override
public StringExpression composeStringConcatenation(List<StringExpression> list) {
  return new StringExpression("["
      + list.stream().map(Expression::getScript).collect(Collectors.joining(", ")) + "].join('')");
}

Both results are calls, which have the highest precedence, so they need no precedence of their own. A method never needs to translate its operands itself: the toolkit has already called the right methods for them, in the right order.


See also: