Complete Code Generation from UML State Machine
Total Page:16
File Type:pdf, Size:1020Kb
Complete Code Generation from UML State Machine Van Cam Pham, Ansgar Radermacher, Sebastien´ Gerard´ and Shuai Li CEA, LIST, Laboratory of Model Driven Engineering for Embedded Systems, P.C. 174, Gif-sur-Yvette, 91191, France Keywords: UML State Machine, Code Generation, Semantics-conformance, Efficiency, Events, C++. Abstract: An event-driven architecture is a useful way to design and implement complex systems. The UML State Machine and its visualizations are a powerful means to the modeling of the logical behavior of such an archi- tecture. In Model Driven Engineering, executable code can be automatically generated from state machines. However, existing generation approaches and tools from UML State Machines are still limited to simple cases, especially when considering concurrency and pseudo states such as history, junction, and event types. This paper provides a pattern and tool for complete and efficient code generation approach from UML State Ma- chine. It extends IF-ELSE-SWITCH constructions of programming languages with concurrency support. The code generated with our approach has been executed with a set of state-machine examples that are part of a test-suite described in the recent OMG standard Precise Semantics Of State Machine. The traced execution results comply with the standard and are a good hint that the execution is semantically correct. The generated code is also efficient: it supports multi-thread-based concurrency, and the (static and dynamic) efficiency of generated code is improved compared to considered approaches. 1 INTRODUCTION Completeness: Existing tools and approaches mainly focus on the sequential aspect while the concurrency The UML State Machine (USM) (Specification and of state machines is limitedly supported. Pseudo Bars, 2007) and its visualizations are efficient to states are not rigorously supported by existing tools model the behavior of event-driven architecture. such as Rhapsody (IBM, 2016a). Designers are then Tools and approaches are proposed to automatically restricted to a subset of USM concepts during design. translate USMs into executable code in the context Efficiency: Code generated from tools such as of Model-Driven Engineering (MDE) (Mussbacher Rhapsody (IBM, 2016b) and FXU (Pilitowski and et al., 2014). Derezinska,˜ 2007) depends on the libraries provided However, despite many advantages of MDE and by the tool vendor, which makes the generated code USM, they are not widely adopted as a recent survey non portable. Event processing speed and executable revealed (Whittle et al., 2014). This is partially due file size of generated code are not optimized (Charfi to poor support for code generation (Forward et al., et al., 2012). 2010). On one hand, the usefulness and semantics of Semantics: The semantics of UML State Machine USM are being empowered by OMG by providing is defined by a recent OMG-standardized: Precise more concepts and their precise semantics such as Semantics of State Machine (PSSM) (OMG, 2016). pseudo states and composite state machines. On the This standard is not (yet) taken into account for val- other hand, existing code generation tools and ap- idating the runtime execution semantics of generated proaches have some issues regarding completeness, code. semantics and efficiency of generated code. Existing Given the above issues, the objective of this paper approaches either support a subset of USM modeling is to present a novel code generation pattern and its concepts or handle composite state machines by flat- tooling support. The latter offers efficient code gen- tening into simple ones with a combinatorial explo- erated from USMs with full concepts to reduce the sion of states, and excessive generated code (Badred- modeling-implementation gap. din et al., 2014a). Specifically, the following lists The proposed pattern extends IF-ELSE construc- some of the current issues: tions with our support for concurrency. Runtime ex- ecution of generated code is experimented with the 208 Pham V., Radermacher A., GÃl’rard S. and Li S. Complete Code Generation from UML State Machine. DOI: 10.5220/0006274502080219 In Proceedings of the 5th International Conference on Model-Driven Engineering and Software Development (MODELSWARD 2017), pages 208-219 ISBN: 978-989-758-210-3 Copyright c 2017 by SCITEPRESS – Science and Technology Publications, Lda. All rights reserved Complete Code Generation from UML State Machine PSSM test suite. the relative time of occurrence. Once emitted, it trig- To sum up, the contributions of this paper are: (1) gers the transition. In other words, the state, which is an approach and tooling support for code generation the source vertex of a transition triggered by a time from USMs with full features; (2) an empirical study event, will remain active for a maximal amount of on the semantic-conformance and efficiency of gen- time specified by the time event. A change event has erated code; and (3) application of the tool to a case a boolean expression and is fired if the expression’s study. value changes from false to true. Note that unlike call We assume that readers of this paper have knowl- and signal events, time and change events are auto- edge about UML State Machine and its basic execu- matically fired inside the class. tion semantics. Deferred Events: A state can specify to defer some The remaining of this paper is organized as fol- events. It means that if an event specified as deferred, lows: Section 2 describes the modeling of applica- it will be not processed while the state remains ac- tions using UML State Machines. Section 3 mentions tive. The deference of events is used to postpone the the features of our tool. Thread-based concurrency processing of some low-priority events while the state is designed in Section 4. Based on this design, a code machine is in a certain state. generation approach is proposed in Section 5. The im- We support all of these events to model event- plementation and empirical evaluation are reported in driven reactive applications. Section 6. The application of our tool to a case study is presented in Section 7. Section 8 discusses related work. The conclusion and future work are presented in Section 9. 3 FEATURES Our pattern and tool has some features compared to 2 STATE MACHINES AND UML other tools as followings: Completeness: Our tool supports all state machine EVENTS vertexes and transitions including all pseudo states and transition kinds such as external, local, and in- This section presents overview of using UML State ternal. Hence, the tool improves flexibility of using Machines for modeling and designing reactive soft- UML State Machines to express architecture behav- ware applications. A state machine is used for de- ior. For the moment, our tool cannot deal with transi- scribing the behavior of either a class in object- tions from an entry point to an exit point. We believe oriented design or a component in component-based that these transitions are not used in reality. This is be- design. In the following, we commonly use the term cause the contradictory semantics of entry points and class. exit points. In UML, entry points and exit points rep- The state machine processes external and internal resent entering points and exit points of a compoiste events. UML defines four event types: CallEvent, state, respectively. They provide encapsulation of the SignalEvent, TimeEvent, ChangeEvent. A call event insides of the state. The entry points allow users to is associated with an operation/method and emitted customize the way to enter the composite state instead if the operation is invoked. The processing of call of the default entering way while the exit points allow events is synchronous meaning that it runs within the to customize the exiting way. For example, the Enp thread of the operation caller. The processing of other entry point in Fig. 1 allows the S5 sub-state of the events is asynchronous meaning that these events re- S1 composite state to be active instead of S3 by the ceived by the class are stored in an event queue which default entering way. is maintained by the class at runtime for later process- ing. A signal event is associated with a UML signal Event Support: Our tool promotes four UML event type containing data. It is emitted if the class receives types and event deference mechanism, which are able an instance of the signal type. From a programming to express synchronous and asynchronous behaviors perspective, we provide an API sendSignal to send and exchange data between components/classes. the signal instance from environment code or other UML-conformance: A recent specification formal- classes to the class and store the event in the queue. izing the Precise Semantics of UML State Machine A time event specifies the time of occurrence rela- (PSSM) is under standardization of the OMG. It de- tive to a starting time. The latter is defined as the time fines a test suite with 66 test cases for validating the when a state with an outgoing transition triggered by conformance of runtime execution of code generated the time event is entered. The time event is emit- from UML State Machines. We have experimented ted if this accepting state remains active longer that our tool with the test suite. Traced execution results 209 MODELSWARD 2017 - 5th International Conference on Model-Driven Engineering and Software Development of 62/66 test cases comply with the standard and are, initialization. The design of threads is based on the therefore, a good hint that the execution is semanti- thread pool pattern, which initializes all threads at cally correct. once, and the paradigm ”wait-execute-wait”. In the State Machine Configuration: Asynchronous latter, a thread waits for a signal to execute its asso- events such as signal events, change events, and time ciated method and goes back to the wait point if it events are stored in an event queue.