Skip to content

rALF Snippet Compiler

lunkpeter edited this page Sep 30, 2015 · 4 revisions

Basic Concept

The EMDW-MC framework uses the Reduced ALF language (rALF) to specify the dynamic behavior of engineering UML models. These rALF action code snippets are attached to UML model elements, and once the UML model is processed by the EMDW workflow, the rALF snippets will be parsed, validated an converted into code snippets of a specified general purpose programming language (C++).

rALF snippet processing

Basic concept

According to the figure above, rALF action code snippets are processed in the following way:

  1. A rALF action code snippet is added to any Opaque Behavior or Opaque Expression

  2. The snippet is parsed and validated: Parsing results contain the abstract syntax tree (AST) of the given rALF snippet and any parsing/validation errors.

  3. Based on the parsing results, the snippet compiler creates the actual C++ code snippets which will be used during code generation.

Architectural Overview

High-level architecture

As it can be seen on the figure above, the rALF workflow can be accessed via using the rALF generator API. This component uses numerous sub-components to provide an easy-to use interface for processing rALF snippets.

Components

  • rALF Generator API: Main API class of the rALF workflow. It uses a rALF parser to parse the specified rALF action code, and a rALF snippet compiler to create C++ snippets based on the parsed action code.
  • rALF Parser API: API class that can be used to parse any given rALF action code snippet. It uses Xtext framework elements, and returns ParsingResult objects. These parsing results contain the AST of the given rALF code fragment, and the parsing/validation errors as well.
  • rALF Snippet Compiler: Component responsible for generating C++ snippets based on the parsing results. It uses the following sub-components.
  • UML Context Provider: Component that is responsible for the UML context. The UML context contains any primitive and user defined UML types/signals/classes/associations of the given UML instance model. It also can fetch any operations and properties of a given Class, as well as to determine the type of the 'this' variable.
  • UML Descriptor Factory: UML descriptors map UML elements(Types, Operations, rALF variables...) to their corresponding CPP model counterparts, and create the string representations of these elements using C++ templates.
  • Snippet Model: The C++ snippets created are stored in a hierarchical structure. This way, apart from the code itself, auxiliary information can be stored in each snippet as well.

Plug-ins

  • com.incquerylabs.uml.ralf: Main plug-in of the rALF component. It specifies the rALF language using Xtext, defines an elaborate Type System using XSemantics, and also defines the main interfaces regarding UML context support. The snippet compiler relies on the rALF language model itself, and the IUMLContextProvider interface.

  • com.incquerylabs.uml.ralf.snippetcompiler: Plugin that contains the snippet compiler,and the generator API, which can be used to access the rALF processing workflow.

  • com.incquerylabs.emdw.cpp.common: This plug-in contains descriptors, and their corresponding factories, as well as classes responsible for querying the C++ string representations of UML objects.

  • com.incquerylabs.emdw.snippet.model: This plugin defines the snippet model, and also provide a simple snippet serializer class.

Support for additional Programming languages

Extension

During the design of the rALF language processing framework, the main focus point was on creating a generic and easy to extend framework for processing rALF action code and generating snippets in various general purpose languages. The rALF parser and snippet compiler components are independent from the target language, for each one of these languages (C++/C/Java...), various sets of descriptors and descriptor factories can be defined. This way a significant portion of the framework can be reused.

Snippet model

Snippet model

The created C++ snippets are stored in the aforementioned snippet model. As displayed on the figure above, Snippets can form a hierarchical structure that contains the string representation of a C++ snippet. This is done, as this way each snippet model instance can also contain auxiliary information apart from the code itself.

In order to serialize these snippets, the ReducedAlfSnippetTemplateSerializer defined in the snippet.model plug-in can be used.

Planned future auxiliary information

As mentioned before, the main reason behind the incorporation of the snippet model, is that this way snippets themselves can store important life cycle related information. In detail this will include dependency relations between UML elements and Snippets. This way if an UML model element is changed, only the snippets related to it will be re-generated.

Input-Output Objects

Input objects:

The snippet compiler defines the visit(EObject o) dynamic dispatch method, which will accept any sub-tree of a rALF AST. Typically however, visit is used with a Statements typed object as a parameter. This object servers as the root object of the rALF language model, thus this way the snippet compiler will process the entire rALF action code fragment.

Output objects:

As mentioned before, the snippet compiler creates Snippet objects, which contain the created C++ code.

rALF Snippet Compiler Mechanism

Compiler mechanism

At first, the abstract syntax tree (AST) the rALF action code snippet is created by the rALF parser. The snippet compiler traverses this AST using post-order tree traversal. This way code fragments representing the leaf nodes of each expression tree are created first. These fragments are later embedded into the more complex expressions and statements.

Descriptors

As mentioned before, the element names and language constructs of the rALF language might differ from of those of the target language. These differences are corrected by UML descriptors. These elements create the corresponding target elements for each rALF element. At this point, rALF elements need to be mapped to C++ specific constructs. This is achieved by a three-layer descriptor structure. Following the UML-XtUML-CPP transformation chain, CPP elements equivalent to the UML element references contained in the rALF AST are queried. Based on these CPP constructs the C++ code snippets are created.

#API Usage Examples Usage examples of the rALF snippet compiler can be found here

Project Location

The snippet compiler project can be found in the EMDW-MC repository here

The list of supported rALF objects can be found here

Clone this wiki locally