Software Language Engineering
Software Language Engineering
8. Generators
8.1. Forms of Generators
see also MC Handbook, Ch. 12, 13
http://www.se-rwth.de/
Farbe!
Definition: Generator and Code Generator
• A code generator transforms a set of input files to a set of source files in a programming language
Executable
Input files
Code generator source code
Code generation is the process of translating a source model into artefacts of an executable language.
manually Predefined
Predefined
written code predefined
components
components
components
parameterized
generator generated code
scripts + + included
code snippets runtime
templates
environment
code (RTE)
snippets
templates
models,
models model input output template reports,
loader AST AST engine code, etc.
Key:
uses/accesses
input/output CpD
Template
Template
Model Parser AST Java
Engine
Key:
uses/accesses
input/output CpD
transforms to
Transformation Transformation
Engine
Pretty
Model Parser AST AST' Java
Printer
output model AST
input model AST
• Template engines
transform a DSL model into output files using templates Template
DSL
language
• Properties of template-based conforms to
Model-to-Text-transformation:
+ relatively easy to adapt Model Template
- complex code generations
pollute templates with GPL code
http://www.se-rwth.de/
www.freemarker.org
Farbe!
Freemarker – The Principle
• Code generation defined by combination of data model (here: AST) and template
• Example:
SC MyAut FM Result
The name of the The name of the
1
+ automaton is
<b>${ast.name}</b>.
= automaton is
<b>MyAut</b>.
2
Freemarker directive:
fixed content
Pieces of Java are processed
by template engine
Template Person
AST
Engine Builder.java
• Freemarker templates are stored as files with extension .ftl The name of the
FM
automaton is
• A template consists of three languages that are interwoven <b>${ast.getName()}</b>.
• The target language:
can be any language; Freemarker knows nothing about it
(e.g. html, Java, etc.)
• The FM directives language:
controls the output (loops, conditionals, template calls etc.),
looks like <#if …>, <#assign …>
usual controls: if, switch, loop, template call + some comfort
• Expression language with callbacks to the underlying data model
looks like ${expression}
• ${object.attribute}
is shorthand for get-function object.getAttribute()
as extension: Freemarker can be configured to use direct attribute access if no get method exists
• Don’t pollute templates for technical code with business specific knowledge!
Although: we may use project specific templates for individual adaptation of commonly used templates
• Freemarker is a standalone product very helpful to produce text pages (Java, HTML, …)
free choice of target language
directives for controlling the output
expression language with built in types
strong connection to underlying Java data model
• Template based Model-to-Text transformations are easier to write than a simple pretty printer
http://www.se-rwth.de/
www.freemarker.org
Farbe!
Understanding Templates
• We look at:
Template APIs provided by MontiCore
tc: TemplateController
ast: Model representation of the abstract syntax
• Provides methods for typical template operations: • Aliases for Java methods improve readability
inclusion of sub templates
instantiation of helpers (e.g., a file handler) ${include(ʺmy.Templateʺ)} instead of
${tc.include(ʺmy.Templateʺ)}
• Each template call gets its own
TemplateController tc
Example aliases:
containing template-specific information, such as the
name of the current template include(template) tc.include(template)
error(message) log.error(message)
• The following gives an overview of some functions of warn(message) log.warn(message)
the tc
• MontiCore provides a dedicated logging mechanism for use from within templates:
delegates log messages to the central logging component
methods for different log message severities
• error(String msg)
• log.error(String msg)
announce errors from within templates
errors abort the generation immediately
${signature("className", "package")}
include(String templateName)
include(String templateName, ASTNode ast) {
includeArgs(String templateName, ASTNode node, List<Object> templateArguments)
includeArgs(String templateName, List<Object> templateArguments) {
• Similar methods, write(…) and writeArgs(...) execute a template and write result in a fresh file, e.g.:
• cd4c object helps to transform the source model to the intermediate CD-AST
Key:
uses/accesses
input/output CpD
transforms to Transformation Template
Transformation
Engine, cd4c
Template
Model Parser AST CD-AST' Java
Engine
Structure of output
input model AST
model AST: form of a class diagram
• Examples:
${cd4c.addMethod(ast, "de.monticore.sc2cd.StateSetStateMethod")}
${cd4c.method("public void setState("+ast.getName()+"_State k)")}
CD-AST Template-Hierarchy
CDDefinition
Factory.ftl Class.ftl Interface.ftl
* *
CDClass CDInterface
• Observations:
more than one template for the same type of ASTNode (colors)
hierarchy of AST roughly corresponds to template hierarchy
* * NImpl.java
CDClass CDInterface
• Observations here:
some templates produce full file artefacts
others describe parts of files corresponding to nonterminals of the target language
each file-production (e.g. factory) has one main template
CD-AST Template-Hierarchy
Statechart
* * *
State * Transition
Files = Java-CD
0..1
… …
0..3 0..2 Base <<abstract>> State
Code Action Invariant Event Statement event() state
handleEvent(Base bc)
setState(State s)
…
• Observations here:
templates correspond to java classes StateA
…
StateB
…
large templates with inner logic,
e.g. transition information is used inside State templates handleEvent(Base bc) handleEvent(Base bc)
http://www.se-rwth.de/
www.freemarker.org
Farbe!
Example Templates
• For Statecharts encoded using the design pattern CD for "State Pattern"
“State” (Gamma et.al. 1994)
… …
Statechart.ftl Main StateClass
AbstractState.ftl stimulus() state
handleStimulus(Main m)
ConcreteState.ftl setState(StateClass k)
…
…
… …
StateA StatebB
handleStimulus(Main m) handleStimulus(Main m)
• Sources:
• github.com:MontiCore/monticore