The development of embedded software differs from the development of traditional software, mainly due to hard constrains related to the embedded platform and ...
SIM 2011 – 26th South Symposium on Microelectronics
173
GenCode: A tool for generation of Java code from UML class models Abilio G. Parada, Eliane Siegert, Lisane B. de Brisolara {agparada,esiegert,lisane}@inf.ufpel.edu.br Universidade Federal de Pelotas
Abstract The development of embedded software differs from the development of traditional software, mainly due to hard constrains related to the embedded platform and the tiny time-to-market. Allied to the high complexity found on emergent embedded systems, these difficulties motivate for the use of Model-driven Engineering (MDE) approaches to provide abstraction and automation for embedded software design process. To enable the effective use of the MDE paradigm, tools are required to capture models, transform models, and to generate code. This paper presents the development of a tool to support the use of MDE in the embedded software development, which reads a UML model, capturing its elements and generating code from them. The developed tool prototype is able to generate skeleton of Java code from UML class diagrams.
1.
Introduction
Embedded software differs from traditional software, since engineers should take into consideration the hardware platform in order to develop efficient algorithms to run on it, considering memory size, performance and power constrains. Besides, engineers should worry about the speed of product delivery (timeto-market), while producing good quality software at small costs and, consequently, competitive products. At the same time, the complexity of embedded applications grows up following advances in processing power of embedded devices. Usually models are used to deal with high complexity [1] because model-based approaches offer several benefits. Firstly, models have fewer details so are easiest to build, thus facilitating the understanding and detection of a problem. The Unified Modeling Language (UML) [2] has become the standard modeling language for object-oriented software development and has also been used in the embedded domain [1][3]. To automate the process of embedded software development and deal with the complexity of emergent embedded software, a new paradigm promises automation and abstraction. This paradigm is known as ModelDriven Engineering – MDE [4], because it considers the model as a primitive artifact of the software project. Several authors advocate its use in embedded systems design [5][6]. With MDE models are not only used in the software documentation, the software models are used for all software engineering phases (analysis, design, implementation, and testing). Following this paradigm, models evolved and are transformed until to be able to obtain an implementation from it, speeding the process of software development and offers abstraction for engineers. To allow the effective use of the MDE paradigm, tools are required to capture models, transform models, and generate code. This paper presents the development of a tool to support the embedded software development process, which reads a UML model, capturing its elements and generating code. The tool prototype is able to generate skeleton of Java code from UML class diagrams. This paper is organized as following. Concepts of the MDE, including used standards and tools are presented in Section 2. Section 3 presents the proposed tool, presenting its structure, describing the model capturing and code generation approaches, and illustrating its functionality. Finally, Section 4, presents conclusions and future works.
2.
Background
The Model-Driven Engineering – MDE considers the models as part of the software production process, differing from the other software engineering paradigms that use the models only for documentation. In MDE approaches, models are considered as important as the code, because they can be transformed into other models or codes. This way, automation is supported, speeding up processes, and facilitating errors detection. Several MDE approaches are based on UML models, which are stored using the XMI [7] model interchange standard, which is based on XML (eXtensible Markup Language). Thus, the first functionality required for a MDE supporting tool is the model capturing. After capturing the model, it must be transformed into code in the target language. This process typically uses a template engine to transform model into code, given the format specified by the template. Templates are a flexible approach to convert models to text. The most popular template engine is named Velocity [7]. Other methods to generate source code include the use of rules, writing programs that generate programs (code generators), as used in this work.
SIM 2011 – 26th South Symposium on Microelectronics
174
3.
The code generator
This section discusses the development of a prototype of a code generation tool. This prototype is able to generate Java code from UML class diagrams. The proposed tool is divided in two main steps: XML file capturing and Java code generation. The next subsections describe these steps, discuss the tool developing and demonstrate its functionality.
3.1.
Capturing information from XML file
The first step consists in capturing information from UML diagrams, represented using the XMI standard. This step is divided in two substeps. The first substep is the reading of “xmi:id” that consists in an identification of the diagram components (class, attribute, operation, association and properties of UML tool). This information is captured and stored into a trie (retrieval tree) to facilitate and accelerate the search. This is required because in the file has references forwards, for example, the class X has a attribute that is a reference to the class Y, and the information of the class Y is next the information of the class X. The last substep is responsible for storing and organizing information captured using an object-oriented approach like a UML structure, which contains the information of the model, and sub divide on components of UML like, for example, classes, packages, associations, attributes, methods, and parameters.
3.2.
Code generation from UML class model
After capturing the model, it must be transformed in Java code. This process uses the structure generated at first step. The method used to generate code is a simple conversion from information of the model captured in first step to text. This is the usual approach and it is used by [8]. Using object-oriented approach, each element of structure is responsible to its code generation that is performed by the method named GenCode. For example, the Class element, which represents a UML class, is responsible to generate code for an instance of the relative class found at the model The code generated by the tool includes specification of package, specification of super class (if there is a specialization/generation relationship between classes), declaration of attributes (defined explicitly in the model) considering its type, and method signatures generation. Beside of the methods declared into the model, constructor methods, and get and set methods to access private and protected attributes are also generated. The constructor method generation includes the superclass constructor invocation, if necessary, and initialization of the class attributes. When the attributes are of primitive type, these are defined and comments are generated to remember the programmer to initialize them. Besides the attributes explicitly defined in the class model, other attributes are generated to represent return parameters of methods or relationship between classes (association, aggregation or composition). When one attribute represents a relationship and its multiplicity is “*”, this attribute is declared as an Array of objects of the associated class, otherwise the attribute is declared as a reference for one object of the related class. Methods in a UML model have parameters that can be set as in, out, inout or return. The method signature generation includes definition of method visibility, and type for all parameters including return. The method return type is defined according to the type of output parameter. A method can have more than one output parameter, if this is defined with another type, a comment is generated to warn the designer. The declarations of out parameters are into the scope of the method and declaration of the return is in the class scope. When there is an inheritance hierarchy including an abstract class, abstract methods defined in the abstract class are generated as concrete methods into the code of its immediate concrete subclass. Beside the code itself, the tool generates also clarifying comments and warning comments into a local of code.
3.3.
Tool developing
The tool prototype was implemented using the Java language. Fig. 1 illustrates the UML class diagram of the tool prototype built using Papyrus [9], an open source tool for UML2 modeling. The tool input is a XML file and for the output, a directory with the name of model is created where the generated Java code is enclosed.
SIM 2011 – 26th South Symposium on Microelectronics
175
Figure 7: Class diagram – Structural view of tool prototype
3.4.
Tool validation
Fig. 2 illustrates a fragment of the code generated by the tool using as input the UML class diagram depicted in Fig. 1. Although code was generated for all classes, the Fig. 2 depicted code generated for the class “Parser”. In the generated code, firstly there are the class definition (line 3) and the list of attributes (line 5 to 13), in which is possible to observe attributes generated as an Array because of the multiplicity of the classes relationship. In line 17 is declared a variable to store the value returned by the method named “manipulate” (line 65). Method "get" and "set" are found from line 30 until line 60 and class methods from line 62 until 91.
Figure 2: Code generated from the model loaded in the tool – class “Parser”
Figure 3: Code generated from the model loaded in the tool – class “Attribute Fig. 3 illustrates the generated code from the class “Attribute” of UML class diagram from Fig. 1. In this code is possible to note the declaration of this class as subclass of the superclass named "DataModel", and the
SIM 2011 – 26th South Symposium on Microelectronics
176
call of the superclass constructor method (line 13) inside of the constructor method of class “Attribute” (from line 12 until 15), and the declaration of a method early defined as abstract in the superclass (line 53 until 56).
4.
Conclusions and Future Work
This paper presented a new tool to generate Java code from UML class diagram. As the generator uses only class diagrams, only static information are used in the code generation, which limits the code generation supported by this tool. As future work, we plan to extend the tool in order to support also other diagrams, enabling a more complete code generation, including the sequence of method calls. Since UML extensions (profiles) are available for the embedded domain, the capturing of UML models should be also extended to allow the capturing of stereotypes used for this specific profiles, allowing consider this information also during the model analysis or code generation.
5.
References
[1]
SELIC, B. (2003). Models, software models, and UML. UML for real: Design of embedded realtime systems (pp. 1-16). Boston: Kluwer Academic Publishers.
[2]
OMG. Unified Modeling Language (UML). Available at: http://www.omg.com/.
[3]
BRISOLARA, Lisane; KREUTZ, Márcio Eduardo; CARRO, Luigi . UML as front-end language for embedded systems design. In: Luis Gomes; Joao M. Fernandes. (Org.). Behavioral Modeling for Embedded Systems and Technologies: Applications for Design and Implementation. Hershey: IGI Global, 2009, Chapter 1, p. -. ISBN: 978-1-60566-750-8.
[4]
SELIC, B. UML 2: A model-driven development tool. Model-Driven Software Development. IBM Systems Journal, Riverton, v. 45, n. 3, p. 607-620, 2006.
[5]
TERRIER, F.; GERARD, S. MDE Benefits for Distributed, Real Time and Embedded Systems. In: From Model-Driven Design to Resource Management for Distributed Embedded Systems. Springer Boston v. 225/2006. p. 15-24. Jan, 2007.
[6]
ESPINOZA, H.; CANCILA, D.; SELIC, B.; GÉRARD, S. Challenges in Combining SysML and MARTE for Model-Based Design of Embedded Systems. In: Proc. of the 5th European Conference on Model Driven Architecture - Foundations and Applications, 2009.
[7]
OMG. XMI 2.1.1: XML Model Interchange. (OMG document formal/2007-12-01). Available at: .
[8]
Usman, M. and Nadeem, A. (2009). “Automatic generation of Java code from UML diagrams using UJECTOR”. International Journal of Software Engineering and its applications (IJSEIA), Daegu, vol. 3, No. 2 (April), pp. 21-37.
[9]
Draft Tutorial for Profile usage in Papyrus, Papyrus. http://www.papyrusuml.org