MATLAB

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

Automated Transformation of
MATLAB Simulink and Stateflow Models
Ingo Strmer1, Dietrich Travkin2
1
Model Engineering Solutions, Berlin, stuermer@model-engineers.com
2
University of Paderborn, travkin@uni-paderborn.de

Abstract violations of block settings, model configurations, or


modeling styles that do not comply with such guide-
Model-based development and automatic code lines.
generation have become an established approach in In large-scale controller models, however, this can
embedded software development. Numerous model- add up to several hundred or even several thousand
ing guidelines are built to enable or improve code violations that must all be manually corrected by the
generation, increase code efficiency and to ease modeler. That is a cumbersome, complex, and ulti-
model comprehension. The manual verification of mately expensive task.
guideline conformance of complex models, followed A recent in-house case study at DaimlerChrysler
by the manual elimination of guideline violations showed us that up to 80% of guideline violations can
can be an error-prone and laborious task. In this be fixed fully or semi-automatically, i.e. involving
paper we present an approach which provides de- some degree of user interaction (ref. [6] for details).
velopers with tool support for the analysis and im- For this reason, we developed the MATLAB Simu-
provement of MATLAB Simulink and Stateflow mod- link and Stateflow Analysis and Transformation
els. As a result, developers are able to automatically Environment (MATE), which is fully integrated into
detect and interactively eliminate guideline viola- the MATLAB Simulink and Stateflow modeling
tions. Furthermore, they receive support in creating
environment. MATE provides support in detecting
guideline-compliant models, for example by instanti-
previously specified guideline violations, and in
ating modeling patterns from a generic pattern li-
addition, (in contrast to Model Advisor, MINT, and
brary.
MESA) enables to automatically apply predefined
1. Introduction model transformations, for example repair opera-
tions, layout improvements, and modeling pattern
Today, model-based development is common prac- instantiations.
tice in a wide range of automotive embedded soft- The MATE approach has already been briefly
ware development projects. In model-based de- presented in [6, 7]. In this practice report paper we
velopment de facto standard modeling and simula- focus on the model transformation functionality,
tion tools such as MATLAB Simulink and Stateflow provide more insight into the techniques and contrast
are used for specifying, designing, implementing, our work to other related research topics.
and checking the functionality of new controller The remainder of this paper is structured as fol-
functions. Quality and efficiency of the software are lows: In section 2 we motivate the usage of modeling
strongly dependent upon the quality of the model guidelines and emphasize the necessity for tool sup-
used for code generation. Moreover, the model qual- port in the analysis and repair of models. In section 3
ity influences readability and thus the development we describe typical model transformations provided
and maintenance input. For this reason best practices by MATE. The specification of model transforma-
are provided with the more commonly adopted mod- tions is depicted in section 4. Related work is ad-
eling guidelines such as the MathWorks Automotive dressed in section 5. Finally, conclusions are drawn
Advisory Board (MAAB) guidelines [3]. in section 6.
Commercial tools are available, such as the Simu-
link Model Advisor [1] or MINT [2], and research
prototypes like MESA [8], which help the modeler to
2. Modeling Guidelines
check a model with regard to guideline conformance. An agreement to follow certain modeling guidelines
These assist the developer, for example, in reporting is important to increase the comprehensibility (read-

57
2

Figure 1: Model analysis results and applicable transformation operations


ability) of a model, facilitate maintenance, ease test- In a recent in-house case study at DaimlerChrys-
ing, reuse, and extensibility, and simplify the ex- ler, we used the MINT framework to check a typical,
change of models between OEMs and suppliers. This large-scale controller model from the automotive
is the purpose of MAAB guidelines and patterns [3]. domain. The emphasis of this case study was on (a)
Such publicly available guidelines are often supple- evaluating the maturity of the model with regard to
mented by in-house sets of modeling guidelines [4, production code generation and (b) checking the
12], for example in order to check the models used model in regard to guideline compliancy. It turned
for production code generation. The adoption of such out that the model contained more than 2,000 guide-
guidelines can significantly improve the efficiency line violations. Upon closer inspection, we estimated
of generated code. that up to 45 percent (900) of these 2,000 rule viola-
There are more than 80 MAAB guidelines. Daim- tions could have been fixed automatically, if tools
lerChryslers model-based development guidelines such as MATE had been used [6]. Furthermore, we
[4] consist of more than 200 modeling rules and estimated that approximately 43 percent could have
patterns. Given this vast number of modeling rules, been repaired with user feedback (interactive repair
few reviewers are capable of keeping them all in operations). Only 8 percent actually required a man-
mind1. Furthermore, since purely manual model ual correction. A final 4 percent remained undefined,
reviewing and elimination of guideline violations is which meant that the modeler had to determine
a laborious and error-prone process, it becomes ex- whether the reported violation actually constituted an
pedient to check and repair models by means of infringement of a modeling guideline.
automated tools.

1
Experiences with model reviews are discussed in [5].

Figure 2: Eliminating line intersections by changing the port order

58
3
the Model Advisor. The report includes the sug-
gested repair operation Cascade (bottom of the
figure), which can be applied by clicking on the link.
These repair operations and other operations are also
applicable via context menu as shown in figure 1.
Product blocks with more than two operands, for
example (see figure 1, highlighted block), can auto-
matically be transformed into a cascade of product
blocks with two operands. If the Simulink model is
to be used for fixed-point code generation, this modi-
fication is necessary to determine scaling informa-
Figure 3: If-then-else modeling pattern (left) tion for intermediate results [12, 6, 7]. Since most
and corresponding pseudo-code (right) guideline violations can be fixed fully automatically,
or at least with few user interactions, such repair
operations can reduce the input required to repair the
3. Model Transformations model enormously.
MATE provides different kinds of operations that
modify a MATLAB Simulink and Stateflow model. 3.2 Layout Operations
In this section, we discuss (1) model repair opera- While modeling a system a developer has the option
tions, (2) layout operations, and (3) design pattern of applying beautifying model transformations via
instantiation. context menu. For example, it is possible to align
Simulink blocks horizontally or vertically with each
3.1 Model Repair Operations
other, or to avoid line intersections by changing the
After detecting guideline violations during a model port order (see figure 2; on the left the signal lines of
analysis phase (cf. [6, 10] for more details on analy- the Product block are crossing). Such model trans-
sis techniques), all applicable, predefined repair formations improve the arrangement of the model
operations are suggested to eliminate the reported elements, thus easing model comprehension without
violations. Figure 1 shows the analysis results of a significantly delaying the modeling process.
Simulink diagram using the report functionality of

Figure 4: Instantiated if-then-else modeling pattern (highlighted by colored background)

59
4
outPorts
:Outport
inPorts
sourcePort
targetPorts :Inport
block
...
... :Product no = 1
line
... name = Product1 line block
:Line targets
:Product
block outPorts ...
:Constant line targets
name = Product2 :Outport
:Line
name = Constant3 line block
type = int16
targetPorts :Inport
value = 4
block sourcePort no = 2
inPorts
outPorts
:Outport

Figure 5: Concrete syntax example Figure 6: Abstract syntax graph example


ated using a predefined transformation operation.
3.3 Design Pattern Instantiation
Therefore, the developer only has to select the de-
Guidelines like DaimlerChryslers modeling guide- sired operation via a context menu and to set the
lines [4] or the MAAB guidelines [3] usually contain number of desired decision branches in a pop-up
best practice modeling patterns for recurring model- dialog. It is no longer necessary to create and align
ing tasks, for example patterns for if-then-else or junctions and transitions manually to reach the re-
switch-case constructs in Stateflow diagrams. Fig- quired structure and layout. Only the conditions and
ure 3 illustrates a typical pattern to be used when actions have to be specified manually. The high-
modeling control structures, such as the one speci- lighted portion of figure 4 illustrates an instantiated
fied by the pseudo-code on the right in figure 3. if-then-else pattern.
The instantiation of modeling patterns during
model design can be very time-consuming, for ex- 4. Specifying Model Transformations
ample when the nesting depth of the control structure
increases. MATE speeds up this process and helps MATE is a framework which provides some exem-
developers comply with guidelines by enabling mod- plary model transformation operations, but first and
eling patterns to be instantiated from scratch with foremost offers the possibility to specify and apply
just a few clicks. Of course this possibility can also new ones.
be used to improve the layout of existing diagrams Guideline violation patterns and repair operations
by manually replacing non-compliant diagram struc- are specified using graph transformation rules (cf.
tures with automatically instantiated patterns. [9, 10]). These rules are applied on the abstract syn-
An if-then-else pattern complying to the one in tax graph, which is a representation of the model to
figure 3, for example, can automatically be instanti- be analyzed.

CascadeProductBlocksTransformationWithPorts::execute(): void

this

arguments[product]

product: Product
[success]
[failure]

targets
l1: Line
destroy
targets outports sourcePort
l2: Line product pOut: Outport
destroy
destroy inports
targetPorts create
l3: Line ip: Inport sourcePort
destroy create lOut: Line
targetPorts
newOut: Line
create create
targetPorts create
create sourcePort
inPorts
newIn1: Inport
create create
outPorts
newProduct: Product newOut: Outport
create create
newIn2: Inport
create
create inPorts

Figure 7: Specification of a model transformation

60
5
Element
handle: String
ChartBlock Although the model is incomplete (many block
qualifiedName: String
... MathOperation
types are missing) the provided abstractions enable
to traverse any Simulink model. The class Block for
ports
System
containedBlocks
Port Product
example can be used for any currently unknown
Sum

RootSystem
Block block type. Otherwise the concrete classes like
blockType: String Constant
Subsystem name: String targetBlock ChartBlock are used.
... BusCreator
subsystem BusSelector
Specifying transformations using graph transfor-
Line
SubsystemBlock
sourceBlock
... mation rules has many advantages. In particular, this
simplifies traversal and modification of object struc-
Figure 8: Excerpt from the simulink meta-model tures. In some cases, however, graph transformation
rules may also become complex, for example when
specifying complex layout algorithms. Using our
The abstract syntax graph is an object structure. In Simulink and Stateflow meta-model, it is also possi-
this context each object represents an element in a ble to specify model analysis and transformation
Simulink or Stateflow diagram (i.e. concrete syntax). operations by implementing them manually in Java
The links between objects represent element rela- and integrating them into MATE. The modeler thus
tions, e.g. containment. Figure 6 illustrates a part of has two alternative ways of specifying the desired
an abstract syntax graph representing the two pro- model operations.
duct blocks and a connected constant block shown in
figure 5. Each product block in the Simulink diagram
5. Related Work
is represented by an object of type Product, each
signal line by a Line object, etc. Attributes name, Well-known examples of other MATLAB Simulink
type, value and others (not illustrated) contain addi- and Stateflow analysis tools are MathWorks Model
tional information about the elements. Advisor [1] and MINT [2]. Both rely on the execu-
A graph transformation rule modifies the abstract tion of MATLAB M-scripts to identify modeling
syntax graph, i.e. it creates or removes objects, adds, rule violations. Other tools like MESA [8] enable the
removes or reconnects links, or changes attribute specification and verification of OCL constraints.
values. However, none of these tools provide support for
Figure 7 illustrates an exemplary graphic specifi- repairing a model, improving its layout, and instanti-
cation of a model transformation. The transformation ating design patterns. MATE, in contrast, in addition
is specified using story diagrams [9], a type of UML to the possibility of specifying guideline violation
activity diagrams with nested collaboration dia- patterns, which can be automatically detected in a
grams. Given a Product object, i.e. a product block, model (see [6] for more details), supports model
as a starting point, this transformation tries to find transformations.
the specified object structure in the abstract syntax Furthermore, the implementation of guideline
graph, then it removes all the elements marked with checks in M-script is a great deal more complex than
destroy and adds all the elements marked with implementing them with the MATE approach. With
create. In this case the transformation replaces MATE, the rules are specified on a higher level of
product blocks with more than two operands by a abstraction using graph transformation rules. The
cascade of product blocks with only two operands. rule implementation is generated from this high-level
With a graphic specification of a transformation, specification. Moreover, MATE uses an object-
i.e. a story diagram, MATE offers the option of gen- oriented model of Simulink and Stateflow diagrams,
erating Java code. The code can be nested into the which acts as a further layer of abstraction. It hides
Model Advisor framework so that the specified the Simulink and Stateflow API operations (M-
transformation can be applied to Simulink and script) behind a uniform, consistent interface for
Stateflow models. querying and modifying all model elements.
The Simulink and Stateflow models are traversed The MESA project [8] also focuses on the appli-
and modified using M-script commands, but these cation of a high-level analysis specification language
are encapsulated in the access methods of an object- with code generation support. However MESA does
oriented meta-model for Simulink and Stateflow not rely on graph and rule-based specification tech-
diagrams. A small excerpt from this model is shown niques like MATE. Instead it uses the textual and
in figure 8. logic-based Object Constraint Language (OCL) of
OMG. This makes descriptions of object structures

61
6
not as easy to understand as the graph transformation [5] Ingo Strmer, Mirko Conrad, Ines Fey, Heiko
rules used by MATE. Drr: Experiences with Model and Autocode
Reviews in Model-based Software Develop-
A comparable approach to MATE is aquintos
ment. Proc. of 3rd International ICSE Work-
PREEvision tool suite, which also uses graphic shop on Software Engineering for Automotive
specifications for model analysis and transforma- Systems (SEAS06), pp. 45-51, 2006.
tions. However in contrast to MATE, PREEvision is
[6] Ingo Strmer, Ingo Kreuz, Wilhelm Schfer,
aimed towards model-to-model transformation tech- Andy Schrr: The MATE Approach: Enhanced
niques (e.g. transformation of a Simulink and Simulink and Stateflow Model Transfor-
Stateflow model into an ASCET/SD model) instead mation, Proc. of MathWorks Automotive Con-
of model modifications. PREEvision does not focus ference (MAC), 2007.
directly on the analysis of modeling guideline viola- [7] Ingo Strmer, Heiko Drr, Holger Giese, Udo
tions and does not support model repair functional- Kelter, Andy Schrr, Albert Zndorf: Das
ities with user feedback or pattern instantiation. MATE Projekt visuelle Spezifikation von
There are further research approaches that support MATLAB/Simulink/Stateflow Analysen und
Transformationen, Proc. of Dagstuhl Work-
model analysis and transformation [13, 14], but these
shop Modell-basierte Entwicklung eingebette-
focus on UML and Statechart diagrams. ter Systeme III (MBEES 2007), pp. 83-93,
2007.
6. Conclusion [8] Tibor Farkas, Christian Hein, Tom Ritter:
Automatic Evaluation of Modelling Rules and
Adopting modeling guidelines for the design of auto-
Design Guidelines, Proc. of 2nd Workshop
motive controller models is vital. The huge number From code centric to model centric software
of modeling rules and patterns that the developer engineering: Practices, Implications and
must keep in mind calls for tool support in order to ROI, Bilbao, Spain, 2006.
create, check, and repair controller models with re- [9] Thorsten Fischer, Jrg Niere, Lars Torunski,
gard to guideline compliancy. Furthermore, the and Albert Zndorf: Story Diagrams: A new
equally large number of violated guidelines makes Graph Rewrite Language based on the Unified
model reworking cumbersome and prone to error. Modeling Language, Proc. of the 6th Interna-
MATE supports developers in analyzing MATLAB tional Workshop on Theory and Application of
Graph Transformation (TAGT), Paderborn,
Simulink and Stateflow models and automatically or
Germany (G. Engels and G. Rozenberg, eds.),
interactively transforming such models into guide- LNCS 1764, pp. 296--309, Springer Verlag,
line-compliant ones. In addition, MATE provides November 1998.
high-level editor functions, which help developers to
[10] Jrg Niere, Wilhelm Schfer, Jrg P. Wadsack,
design guideline-compliant models by generating Lothar Wendehals, and Jim Welsh, Towards
predefined and configurable modeling patterns or Pattern-Based Design Recovery, Proc. of the
using layout operations. 24th International Conference on Software
Engineering (ICSE), Orlando, Florida, USA,
References pp. 338--348, ACM Press, May 2002.
[11] aquintos GmbH: PREEVision,
[1] The MathWorks, Inc.: MATLAB Model Advi- http://www.aquintos.info/de/produkte/
sor, http://www.mathworks.com/products/ index_preevision.php, 2007
matlab/, http://www.mathworks.com/access/
helpdesk/help/toolbox/simulink/, 2006. [12] dSPACE GmbH: Modeling Guidelines for
MATLAB/Simulink/Stateflow and TargetLink,
[2] Ricardo, Inc., MINT, p. 37, V2.0.
http:/www.ricardo.com/mint, 2006.
[13] M. Huhn, M. Mutz, K. Diethers, B. Florentz,
[3] The Mathworks, Inc.: The MathWorks Automo- M. Daginnus: Applications of Static Analysis
tive Advisory Board (MAAB: Control Algo- on UML Models in the Automotive Domain,
rithm Modeling Guidelines Using MATLAB, Proc. of Formal Methods for Automation and
Simulink, and Stateflow. Version 2.0, Safety in Railway and Automotive Systems,
http://www.mathworks.com/industries/auto/ 2004
maab.html, 2007.
[14] M. Huhn, M. Mutz, B. Florentz: A Lightweigth
[4] Model Engineering Solutions: e-Guidelines Approach to Critical Embedded Systems De-
Server, http://www.model-engineers.com/ sign using UML, Proc. of 3rd International
products-services/products/e-guidelinesbr- Workshop on Critical Systems Development
server.html, 2007. with UML, 2004.

62

You might also like