Thursday, June 29, 2017

SysML2Python Code Generation

By Markus Schäffer and Matthias Bauer

Introduction

The Object Management Group's (OMG) Systems Modelling Language (OMG SysML) can be used to describe various kind of systems. After modelling a system, one scenario is that software engineers use these models to write any form of software. For example, software experts may write code for a system that has to be developed. Another scenario is that one wants to simulate the system, e.g., to learn something about its implications. When starting to write code, soon the problem arises how to reuse the work done in the modelling phase. One possible way are Model-to-Text transformations. Model-to-Text transformations, as a part of Model Driven Software Engineering (MDSE), enable to generate code from models. Thus one can save time in the development phase, since software engineers do not have to start writing code from scratch.
Hence, to address the problem of reusing SysML models for further work, a model driven code generation approach was used in this work. In particular, a code generator is proposed to translate different SysML diagram kinds into respective Python code. This code generator relies on the Eclipse Modelling Framework (EMF) and the SysML implementation of the UML and SysML modelling tool Papyrus. In order to show the practical usage of the code generator an example of a coffee machine serves as an input for our SysML2Python code generator. As a result with the SysML2Python code generator, we were able to successfully generate Python code out of our supported SysML diagram types. These supported diagram types are the Block Definition Diagram (BDD), Internal Block Diagram (IBD) and Parametric Diagram (PAR).

SysML Diagram Types (dark blue: Supported by SysML2Python Code Generator)

   Used Technology

  • Python, which is an universally applicable, object-oriented interpreter for high-level programming
  • Object Management Group (OMG) Systems Modelling Language (OMG SysML), which is a general-purpose language for systems engineering applications
  • Xtend, which is a programming language and was used to write the SysML2Python code generator
  • Papyrus, which is a tool for graphical modelling of UML, SysML and other modelling languages

Implementation

Our SysML2Python code generator takes an .uml file as input file, which holds the SysML Features attached with Stereotypes in form of a tree, and generates one Python module as output file.
In Papyrus, the information of the modelled system is represented in a single file, regardless of the number of actual modelled diagrams. For example, in case of an Internal Block Diagram the information of its own and its base the Block Definition diagram is stored in a single .uml. This file serves as input for the code generator.
The code generator generates one output file (the Python module of the .uml file). The main reason for a single file is that the generated code can easily be imported by another Python program. For each element in the .uml file the type of the element is queried first (via use of the instanceof operator). Then, the corresponding method is invoked, which holds the individual structure for each type. The next code snippet shows this structure. The polymorphic generateElement method invokes the corresponding method of the type of the actual element (see line 5 in the next code snippet).

fsa.generateFile('''<<ModelName>>.py''', '''
"""GENERATED BLOCKS"""
<<FOR EObject obj : root.eContents>>
    <<IF obj instanceof org.eclipse.uml2.uml.Class>>
        <<generateElement(obj)>>
    <<ENDIF>>
<<ENDFOR>>
''')

Conclusion and Future Work

The objective of this project was to generate Python code from SysML Block Definition-, Internal Block- and Parametric Diagrams. The proposed solution called 'SysML2Python Code Generator', which uses the Eclipse Modelling Framework, Xtend and Papyrus. allows to generate code from .uml files that contain a SysML model. The generated code can easily be imported by other Python applications and has great potential to be used as a basis for further programs. A specially interesting application would be to use the generated code as a base for simulations of SysML models. Nevertheless, further study on generating code from SysML equations is needed to address the issue of using more complex equations in parametric diagrams (e.g. to have built-in functionality to simulate the behaviour of the system over time).

3 comments: