Skip to content

2025‐05‐13 New developer utilities for model‐to‐model creation or transformation

Gunnar at MBition edited this page May 13, 2025 · 1 revision

New utils for model-to-model manipulation

Introduction

The table-based translation (described in a previous article and implemented in rule_translator.py) can be used to declaratively describe a large part of the translation rules for a particular model-to-model translation.

However, sometimes the interface languages have different design that makes things more challenging. A particular issue is when the structure of the input and output language are fundamentally different. For example, one language might allow for locally defined types inside of a message/method definition whereas another might require the types to be in a separate part of the AST. In those cases one node in a particular level of the Abstract Syntax Tree might not directly match the location in the other Abstract Syntax Tree.

For the rule-based translation, this creates a problem. In simpler cases, methods like Delegate() can be specified in the input-table. That method stores the found value temporarily, to be picked up processed at a later stage of the table-defined sequence. In more complex cases however, this is not enough. We may then be helped by not trying to do the full translation within one single operation, but instead split it up.

There are two recent utility implementations that will support the creation of the resulting AST in separate steps.

Query/find functions

A set of functions will be added to a utilities file that enable pattern-matching against the tree and getting the resulting nodes back as a collection. The functions are for example find_by_name(), find_by_name_and_type(), and a more generic find_by_field_values(...) (exact names may change). The last variation will allow any filter-pattern to be specified - in other words the value of particular fields within a node need to match the given values to be part of the returned result. That generic function could be used to find nodes with a particular datatype, enumeration items with a particular value, or anything really!

These functions take a root node to start the search from which can be a root reference for any sub-tree. The functions will either operate only on the direct descendants of that root node, or recursively as deep as the tree goes.

Finally, a variation of the recursive-match-and-find function named prune() will allow to remove nodes/subtrees that matches the given filter pattern.

Merge functions

The second function that will help is the support for merging any Abstract Syntax Trees. This merge function was designed to support processing of multiple layers - overlay files that modify an original interface definition, or new layer types that extend the model with technology-specific (deployment layer) information defined for that layer.

Overlaying a new file/tree with the merge function supports adding nodes, modifying them, and also deleting nodes.

Complementary usage

These features together open up for a powerful tool set for developers of IFEX processing:

Examples:

  • Use the table-based translation method to create most of the result, but lacking some details that aren't easily modeled in one session. - Or use the table-based method multiple times to create multiple sub-results.
  • Select/find particular nodes using the find-functions and get back a collection of them
  • Merge a subtree into another, at any given location (specify the root node)

With these features together, it should be possible to have a sequence of steps that allow for more complex translations.