Mittwoch, 4. März 2015

SCT - Seamless Integration with C and Java type systems


Yakindu Statechart Tools is shipped with a powerful expression language to model the reactive behavior of a state machine. It is a statically typed programming language that is familiar to everyone who knows C or Java. It provides all basic expressions  like logical expressions,arithmetic expressions, bitwise arithmetic and bit shifting.

The language is built on a default, platform-independent type system that supports the primitive types Integer, Real, Boolean, String and Void out of the box. During code generation, these built in types are then mapped to reasonable platform specific types. For example, a String is mapped to a java.lang.String in Java and a char * in C/C++.

However, this platform-independence comes at a price: You can't leverage all the captabilities of the underlying technologie in your state machine and you have to write glue code to integrate the generated code with your platform. These issues are well illustrated when it comes to the definition of operations. In SCT, it is possible to define operations, which can be called from an expression:


operation max(int1: integer, int2: integer): integer 
entry / myVar = max(1,2) 

The Java code generator then generate interfaces for all defined operations that must be implemented by the user. For example, the Java generator creates an interface with a corresponding java method signature. This interface has to be implemented by the client and passed to the statemachine, which is shown in the following code snippet:

 
DefaultSMStatemachine statemachine = new DefaultSMStatemachine(); 
 
SCInterfaceOperationCallback callback = new SCInterfaceOperationCallback() { 
 @Override 
 public int max(int param1, int param2) { 
  return Math.max(param1, param2) // Delegate the call to the Java Lib
 } 
};
statemachine.getSCInterface().setSCInterfaceOperationCallback(callback);

 
But what if you want to develop a statemachine for the Java platform only? This is where Statechart Domains come into play. Select the Java Domain in the 'New Model Wizard' and get access to Java native types. For example, import java.util.Math in your statechart and access all methods like max, min or random directly from the statechart model!


Statechart Tools for Java

This integration is illustrated by the following example, a simple statechart that receives events with a value and puts them onto a Stack for later processing.

in event inEvent : Message
var stack : java.util.Stack<Message>

Note the definition of the event inEvent. The special feature about this is that the type of the event is the complex type Message - a Plain Old Java Object defined in a local Java Project containing a timestamp of type java.util.Date and a message of type String. Those fields can be accessed as properties from the statechart.



The variable stack is of type java.util.Stack with the type argument Message - the Java Type Integration even supports generic types! All (public) methods provided by java.util.Stack can be accessed directly from the Statechart, as you can see in the State MessageReceived when the currentMessage is pushed onto the stack. With Statechart Tools for Java you can develop your state machine in an object-oriented way like you are used to it from Java!


Statechart Tools for Embedded C

When creating a new statemachine model for the C Domain, it is possible to import C-header files and get access to all of the declared functions.The following example is a simplyfied version of a header from our Yakindu Smart Car.

After importing the C-Header (import: car_api.h), all defined complex types like Structs, Unions and Enumerations as well as TypeDefs and Functions become accessible directly from the Statechart model. The generated code integrates well with the imported headers so there is no need to write a bunch of glue code anymore!

Current implementation state

This is work in progress and still a lot of work to do. Although it is already possible to edit C and Java specific statecharts and generate code out of it, we still have to adopt the simulation engine to handle complex types. We expect to have a first version available in Q3/2015.