SinelaboreRT Header Logo

SinelaboreRT

It's better when it's simple!

User Tools

Site Tools


Sidebar

UML-Tool specific Intro

Language Backends

Getting started

How-To

Examples

Designers Toolbox

There are better ways to model state machines than using spread sheets!

In the past different μC manufacturers have published application notes about the benefit of using state machines for the design of embedded software. An example is the application note SLAA402 from Texas Instruments (TI). It suggests to generate source code based on a spread sheet table. Nowadays several affordable UML modeling tools are available supporting the efficient design of state machines in a graphical way. SinelaboreRT generates production quality source code from state diagrams created with many different UML tools. Give it a try!

Latest Changes

wiki:backends:java_lang

Generating Java Code

To generate Java code call the code generator with the command line flag ’-l java’. The generator generates just one Java class which implements the complete state machine. This has the benefit that your Java project does not become bloated with all kinds of helper classes. If required an optional base class can be specified in the config file. Also the package (Namespace=…) can be defined there. The events that can be sent to the machine are defined in a public enumeration.

Read section “Generating Java Code” for all the details.

Code example:

/* Command line options: -v -p SSC -l java -o Testcase testcase.xml   */
/* This file is generated from testcase.xml - do not edit manually  */
/* Generated on: Thu Feb 13 19:52:06 CET 2020 / version 4.1 */
 
 
 
public class Testcase extends TestcaseBase
{
 
	public enum States {
		S1,
		S2,
		S21,
		S22,
		__UNKNOWN_STATE__
	}
 
	public enum Events  {
		ev2,
		ev1,
		ev4,
		TESTCASE_NO_MSG
	}
 
 
	// flag if initialized
	protected boolean m_initialized=false;
 
	States stateVar;
	States stateVarS2;
 
	// State handler class default ctor
	public Testcase()
	{
	}
 
	/* Helper(s) to reset history */
	public void resetHistoryS2(){stateVarS2=States.S22;}
 
	/* Helper to get innermost active state id */
	public States getInnermostActiveState() {
		if(isInS22()){
			return States.S22;
		}else if(isInS21()){
			return States.S21;
		}else if(isInS1()){
			return States.S1;
		}else{
			return States.__UNKNOWN_STATE__;
		}
	}
 
	// Helper(s) to find out if the machine is in a certain state
	public boolean isInS1(){return (((stateVar== States.S1)) ? (true) : (false));}
	public boolean isInS2(){return (((stateVar== States.S2)) ? (true) : (false));}
	public boolean isInS21(){return (((stateVarS2== States.S21)&&(stateVar== States.S2)) ? (true) : (false));}
	public boolean isInS22(){return (((stateVarS2== States.S22)&&(stateVar== States.S2)) ? (true) : (false));}
 
 
 
 
	// Reinitialize the state machine
	public void reInitialize(){
			m_initialized=false;
			initialize();
	}
 
	public void initialize(){
 
		if(m_initialized==false){
 
			m_initialized=true;
 
			// Set state vars to default states
			stateVar = States.S1; /* set init state of top state */
			stateVarS2 = States.S22; /* set init state of S2 */
 
 
			//call on entry code of default states
			System.out.print("Entry S1\n");
 
 
 
		}
 
	}
 
	protected void testcaseChangeToState(States state){
		stateVar=state;
	}
 
	protected void testcaseChangeToStateS2(States state){
		stateVarS2 = state;
	}
 
 
 
 
	public int processEvent(Events msg){
 
		int evConsumed = 0;
 
 
 
		if(m_initialized==false) return 0;
 
 
		switch (stateVar) {
 
			case S1:
				if(msg==Events.ev1){
					if(getI()==1){
						/* Transition from S1 to S21 */
						evConsumed=1;
						/* OnExit code of state S1 */
						System.out.print("Exit S1\n");
 
 
						/* OnEntry code of state S21 */
						System.out.print("Entry S21\n");
 
						/* adjust state variables  */
						stateVar = States.S2;
						stateVarS2 = States.S21;
					}else{
						/* Transition from S1 to S22 */
						evConsumed=1;
						/* OnExit code of state S1 */
						System.out.print("Exit S1\n");
 
 
 
						/* adjust state variables  */
						stateVar = States.S2;
						stateVarS2 = States.S22;
					} /*end of event selection */
				}else if(msg==Events.ev2){
					if(getI()==1){
						/* Transition from S1 to S2 */
						evConsumed=1;
						/* OnExit code of state S1 */
						System.out.print("Exit S1\n");
 
						stateVar = States.S2;/* Default in entry chain  */
						stateVarS2 = States.S22;/* Default in entry chain  */
 
					}else{
						/* Transition from S1 to S1 */
						evConsumed=1;
						/* OnExit code of state S1 */
						System.out.print("Exit S1\n");
 
						/* OnEntry code of state S1 */
						System.out.print("Entry S1\n");
 
						/* adjust state variables  */
						stateVar = States.S1;
					} /*end of event selection */
				}else{
					/* Intentionally left blank */
				} /*end of event selection */
			break; /* end of case S1  */
 
			case S2:
 
				switch (stateVarS2) {
 
					case S21:
						if(msg==Events.ev4){
							if(getI()==1){
								/* Transition from S21 to S21 */
								evConsumed=1;
								/* OnExit code of state S21 */
								System.out.print("Exit S21\n");
 
								/* OnEntry code of state S21 */
								System.out.print("Entry S21\n");
 
								/* adjust state variables  */
								stateVarS2 = States.S21;
							}else{
								/* Transition from S21 to S1 */
								evConsumed=1;
								/* OnExit code of state S21 */
								System.out.print("Exit S21\n");
 
 
								/* OnEntry code of state S1 */
								System.out.print("Entry S1\n");
 
								/* adjust state variables  */
								stateVar = States.S1;
								stateVarS2 = States.S22;
							} /*end of event selection */
						}else{
							/* Intentionally left blank */
						} /*end of event selection */
					break; /* end of case S21  */
 
					case S22:
					break; /* end of case S22  */
 
					default:
						/* Intentionally left blank */
					break;
				} /* end switch S2 */
			break; /* end of case S2  */
 
			default:
				/* Intentionally left blank */
			break;
		} /* end switch stateVar_root */
		return evConsumed;
	}
}
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
wiki/backends/java_lang.txt · Last modified: 2020/02/15 12:54 by pmueller