If the meaning of this blog isn't clear, there really should be more of a description here...
Mittwoch, 13. Juli 2016
Developing a custom code generator by example of SCXML
This blog post explains how to develop a custom code generator for the open source frameworkYAKINDU Statechart Tools. As an example, we will generateStatechart XML (SCXML) from a simple stop watch state machine. The stop watch use case is taken from theApache Commons SCXMLsite. The stop watch model consists of 4 different states (ready, running, stoppedandpaused) and the state transitions are triggered by events that represent the stop watch buttons (watch.start, watch.stop, watch.split, watch.unsplit, watch.reset).
Note that this example is not a full fletched Statechart XML code generator; only the basic concepts ofstates, transitionsand eventsare supported to showcase how to get started. We expect the following XML fragment to be generated from the model above:
Before starting with a code generator, we have to decide which of the two available meta models is most suitable for the use case. In model driven software development, a model always conforms to another model commonly referred to as a meta model. This meta model defines the building blocks a model can consist of. When developing a generator for YAKINDU SCT you can choose between two different meta models,SGraphandSExec. In broad terms,SGraphrepresents the structure of the modeled statemachine (States, Transitions, Events) whileSExeccontains sequences that encapsulates our execution semantics (for exampleChecks, Callsand Stateswitch).
The SGraph meta model is similar to the state machine meta model known from the UML. We have a top level elementStatechartthat contains a set ofStatesandTransitions.Transitionsmay have events and a source and a targetState. The following shows a very simplified version of SGraph.
In contrast, the SExec meta model defines anExecutionFlowthat contains a set ofExecutionStates. EveryExecutionStatehas an entry and an exit sequence of several Steps. EachStepcan be of a different type, for example aStateSwitchor aCheck.
If you want to generate code for a runtime environment that interprets a statechart structure (for example SCXML or Spring Statemachine) you should pick SGraph meta model. If you want to generate a code-only approach for another programming language, and you want to ensure that it behaves exactly in the way all the other code generators behave, you should pick the SExec meta model.
The exampleStatechart XML generator
Creating custom code generators is really easy. It is a first-level concept in YAKINDU Statechart Tools. You can develop them directly within your project workspace which has the nice benefit that adoptions to the model or the generator templates are executed instantly, hence you don't have to bother with long code-compile-test cycles. More information about how to setup a new code generator project is out of the scope of this blog post and can be found here.
When choosing the language for developing a new generator, you should consider using Xtend instead of Java. The syntax is familiar for every Java developer, but it adds some nice features like template expressions anddispatch methods that are really useful for developing code generators.
The example Statechart XML generator implemented with Xtend could look like this:
TheSCXMLGenerator class extends AbstractWorkspaceGeneratorto inherit some utility functions and implements the ISGraphGeneratorinterface (line 17). This interface defines which meta model will be used for the code generator. If you want to implement a code generator based on the ExecutionFlow meta model you would implementIExecutionFlowGeneratorinterface instead.
The generate function in line 23 uses Xtends template string to generate the SCXML header. Most of the text ist static except for the value of the initial attribute - this is calculated in theinitialStatefunction (line 30). Below the SCXMLheader, thegeneratefunction for the head region (parallel regions are not implemented) is called (line 26). This function (line 34) simply iterates over all states in this region and calls the generate function for states (line 40). Other vertex types, for exampleHistories,SynchronizationsorFinal Statesare filtered out. Last, thegeneratefunction for states creates a new XML element and iterates over all outgoing transitions to generate the nested transition element.
You can download the example generator project from our newexamples repository. However, this is just a first step towards a full fletched SCXML code generator. Feel free to take this example as a starting point but don't forget to contribute your extensions to our repository! If you have any questions or need further support on implementing custom code generators feel free to contact us.