Symbol resolver architecture

Overview

During the translation of EFX, the interface SymbolResolver gives information about what the parts of the eForms SDK mean. The parser handles the syntax, and the CallStack manages the names declared in EFX, such as variables and functions. The SymbolResolver gives the meaning of the fixed names of the SDK: fields, nodes and codelists.

This page explains the role of the SymbolResolver, the design of its interface, and the patterns on which it rests.

The symbol resolution problem

EFX expressions contain references to eForms entities:

BT-00-Text                    // Field reference
ND-Lot                        // Node reference
[...notice-type]              // Codelist reference

These are symbols: names for which the translator must find the information that they stand for:

  • the XPath that locates BT-00-Text in the XML;

  • its data type;

  • whether it is repeatable;

  • its parent node.

This information is in the metadata of the eForms SDK, not in the EFX grammar. The SymbolResolver reads it for the translator.

Static and dynamic symbols

EFX has two kinds of symbols, which the translator looks up in different ways:

Kind Examples Looked up by

Static symbols

Fields (BT-00-Text), nodes (ND-Lot), codelists (notice-type)

SymbolResolver, in the SDK metadata

Dynamic symbols

Variables ($x), functions (?my-function), templates (my-template)

CallStack, according to the scope

The SDK defines the static symbols, and they never change during the translation. The EFX code declares the dynamic symbols, which follow the rules of scope.

The SymbolResolver handles only static symbols. Its name says what it does: it resolves the symbols of the SDK, that is, it finds their metadata. The CallStack finds variables and functions.

The SymbolResolver interface

The interface provides several groups of operations:

Path resolution

Its main task is to find the paths that lead to the data of a symbol in the notice:

// Absolute paths from the document root
PathExpression getAbsolutePathOfField(String fieldId);
PathExpression getAbsolutePathOfNode(String nodeId);

// Relative paths from a context
PathExpression getRelativePathOfField(String fieldId, String contextId);
PathExpression getRelativePathOfNode(String nodeId, String contextId);

It returns the paths as PathExpression objects, which hold a text with its type. The syntax of the path, such as XPath or JSONPath, depends on the implementation.

Type information

With the types of fields, the translator writes code with the right types:

String getTypeOfField(String fieldId);           // e.g., "text", "code", "number"
String getRootCodelistOfField(String fieldId);   // For code-type fields

Hierarchy

The SDK defines a hierarchy of nodes and fields:

String getParentNodeOfField(String fieldId);
String getRootNodeId();
PathExpression getRootPath();

Repeatability analysis

An important question is whether a field or node gives one value or several:

boolean isFieldRepeatableFromContext(String fieldId, String contextNodeId);
boolean isNodeRepeatableFromContext(String nodeId, String contextNodeId);

Repeatability depends on the context. A field that has one value in its parent node can give several values when the translator reads it from a higher context, because a node between them repeats.

The path methods use repeatability too. They return the path as an object of a class that matches the type of the field and its repeatability from the context: a StringPath for a text field with one value, a StringSequencePath for a text field with several values, a NodePath or a NodeSequencePath for a node, and so on. The translator relies on this class for its type checks, so a symbol resolver of your own must return the right class.

Codelists and notice subtypes

For validation and for writing code, the resolver can list the codes of a codelist, and the notice subtypes of the SDK:

List<String> expandCodelist(String codelistId);
List<SdkNoticeSubtype> getAllNoticeSubtypes();

Attribute fields

Some fields are XML attributes rather than XML elements:

boolean isAttributeField(String fieldId);
String getAttributeNameFromAttributeField(String fieldId);
PathExpression getAbsolutePathOfFieldWithoutTheAttribute(String fieldId);

With these methods, the translator writes correct XPath for fields that are attributes.

Implementation: XPathSymbolResolver and SdkSymbolResolver

The toolkit splits the implementation into two classes:

XPathSymbolResolver

An abstract class that holds all the logic: the paths, the types, the repeatability, the attribute fields and so on. It does not know where the metadata comes from. It reads the fields, nodes, codelists and data types through abstract methods: fieldById, nodeById, codelistById, dataTypeById, allNodes and getAllNoticeSubtypes.

SdkSymbolResolver

A subclass that reads the metadata from the files of an SDK on disk, with the repositories of the library eforms-core-java: the fields and nodes from fields/fields.json, the codelists from codelists, and the notice subtypes from notice-types/notice-types.json.

@SdkComponent(versions = { "1", "2" }, componentType = SdkComponentType.SYMBOL_RESOLVER)
public class SdkSymbolResolver extends XPathSymbolResolver {

  protected SdkFieldRepository fieldById;
  protected SdkNodeRepository nodeById;
  protected Map<String, SdkCodelist> codelistById;
  protected Map<String, SdkNoticeSubtype> noticeTypesById;
  protected SdkDataTypeRepository dataTypeById;

  // ...

  @Override
  protected SdkField fieldById(final String fieldId) {
    return this.fieldById.get(fieldId);
  }
}

So, to read the metadata from another store, such as a database, extend XPathSymbolResolver and implement only these loading methods. The paths it gives are XPath paths. For another kind of path, such as JSON paths, implement SymbolResolver yourself.

XPathSymbolResolver is not thread-safe: it completes the links between fields and nodes when it first needs them, and keeps some results in memory, without synchronisation. Keep each resolver in one thread, or synchronise the access to it.

One resolver for each SDK version

The ComponentFactory keeps one symbol resolver for each SDK version and qualifier in memory, so that it does not load the metadata again and again:

public static synchronized SymbolResolver getSymbolResolver(final String sdkVersion, final String qualifier,
    final Path sdkRootPath) throws InstantiationException {
  VersionQualifier key = ComponentFactory.INSTANCE.new VersionQualifier(sdkVersion, qualifier);

  // Check if already exists
  SymbolResolver existing = instances.get(key);
  if (existing != null) {
    return existing;
  }

  // Create new instance (this can throw InstantiationException)
  SymbolResolver newInstance = ComponentFactory.INSTANCE.getComponentImpl(sdkVersion,
      SdkComponentType.SYMBOL_RESOLVER, qualifier, SymbolResolver.class, sdkVersion, sdkRootPath);

  // Store and return
  instances.put(key, newInstance);
  return newInstance;
}

This follows the flyweight pattern: all the translations that use the same SDK version share the same resolver. Because the resolver is not thread-safe, translations that share it must not run in parallel threads, unless you synchronise them.

Computing repeatability

A field gives several values from a context in these cases:

  1. the field itself is repeatable;

  2. the field is multilingual: it has one XML element for each language;

  3. a repeatable node lies between the parent node of the field and the context. The context node itself does not count, even if it repeats, because the translation is then inside one of its occurrences.

When the context is the field itself, the field gives one value, even if it is repeatable or multilingual. From the context of another field, a field repeats as it does from the parent node of that field.

The component system

The EFX Toolkit uses a system of components to support several SDK versions. The annotation @SdkComponent registers each implementation with the major versions of the SDK that it supports, as SdkSymbolResolver does above with versions = { "1", "2" }. The ComponentFactory then finds the right implementation for the version in use:

getSymbolResolver("1.10.0", sdkPath)  →  SdkSymbolResolver (matches "1")
getSymbolResolver("2.0.0", sdkPath)   →  SdkSymbolResolver (matches "2")

In this way, the toolkit supports several SDK versions at the same time. For the qualifier, which tells apart several implementations for the same version, see Component registration.

Design patterns

Abstract data type

The interface SymbolResolver follows the pattern of an abstract data type. It defines operations, and hides how they work. The code that calls it works with field identifiers, and receives paths, types and indicators, never the data structures of the SDK.

Because of this:

  • there can be several implementations: one that reads the SDK, one that stands in for it in tests, or one that reads another store of data;

  • an implementation can change without any change to the code that calls it;

  • the interface states clearly what information is available.

Separation of concerns

The interface keeps clear limits between the tasks of the components:

Task Component

Reading the SDK metadata

SymbolResolver

Scope of variables and functions

CallStack

Syntax of the target language

ScriptGenerator

Context

ContextStack

The SymbolResolver does not write code in the target language. It gives the information that other components use to write code.

Relationship to other components

The SymbolResolver works with several components of the translator:

ContextStack

Uses getAbsolutePathOfField, getRelativePathOfField and the same methods for nodes, to compute the paths of each new context.

Expression visitor

Asks for the path of each field and node reference from the current context, which gives its type and repeatability. It also asks for the type of fields, their attributes, their privacy settings and the codes of codelists.

The ScriptGenerator does not call the SymbolResolver. It receives the paths that the resolver gave, already typed, and turns them into code, for example with contextualizePath.

Example

For the field reference BT-00-Text, read from the context ND-Root, the expression visitor asks the resolver for the path of the field from the context:

PathExpression path = symbols.getRelativePathOfField("BT-00-Text", "ND-Root");
// A StringPath if BT-00-Text has one value from ND-Root, a StringSequencePath otherwise

The class of the path carries the type and the number of values, so the visitor does not need to ask for them separately. It then passes the path to the script generator, for example to composeFieldValueReference, when it needs the value of the field.


See also: