ステートマシン図からC++言語のソースコードを出力するサンプル:ソースコード

以下のソースコードでは、見やすさの改善のため「sampleStateMachine」の文字列を「SSM」に置換しています。

ヘッダファイル


///////////////////////////////////////////////////////////
//  sampleCpp.h
//  Implementation of the Class sampleCpp
//  Created on:      2013/02/21 18:12:42
//  Original author: kouno
///////////////////////////////////////////////////////////

#if !defined(EA_BF77FCDC_E39D_4953_818B_53DB9AC0FD46__INCLUDED_)
#define EA_BF77FCDC_E39D_4953_818B_53DB9AC0FD46__INCLUDED_

class sampleCpp
{

public:
	sampleCpp();
	virtual ~sampleCpp();
	/**
	 * You need to set initial value to true to change behavior.
	 */
	bool lightMode;

	void InitializeAction();
	void TurnLeftActions();
	void TurnRightAction();
	void SetLight(bool flag);

	/* Begin - EA generated code for StateMachine */

public:
	enum StateType
	{
		ST_NOSTATE = 0,
		ST_SSM_lightOn,
		ST_SSM_initialize,
		ST_SSM_turning_right,
		ST_SSM_turning_right_StateA,
		ST_SSM_turning_right_StateB,
		ST_SSM_turning_right_StateC,
		ST_SSM_turning_left
	};

	enum Event	{
		E_NOEVENT = 0,
		E_left,
		E_right,
		E_changeState,
		E_light_ON
	};

	enum CommandType
	{
		Do,
		Entry,
		Exit
	};

	void initializeStateMachine();
	void performEvent(Event e);


private:
	StateType currState;
	StateType nextState;
	bool externalTransition;
	bool transcend;	StateType SSM_turning_right_history;

	void SSM_lightOn(CommandType command, Event e);
	void SSM_initialize(CommandType command, Event e);
	void SSM_turning_right(CommandType command, Event e);
	void SSM_turning_right_StateA(CommandType command, Event e);

	void SSM_turning_right_StateB(CommandType command, Event e);

	void SSM_turning_right_StateC(CommandType command, Event e);
	void SSM_turning_left(CommandType command, Event e);

	void StatesProc(StateType currState, CommandType command, Event e);


	/* End - EA generated code for StateMachine */

};
#endif // !defined(EA_BF77FCDC_E39D_4953_818B_53DB9AC0FD46__INCLUDED_)

ソースファイル


///////////////////////////////////////////////////////////
//  sampleCpp.cpp
//  Implementation of the Class sampleCpp
//  Created on:      2013/02/21 18:12:43
//  Original author: kouno
///////////////////////////////////////////////////////////

#include "sampleCpp.h"


sampleCpp::sampleCpp(){

	lightMode = false;

}




sampleCpp::~sampleCpp(){

}





void sampleCpp::InitializeAction(){

	/*implementation of Initialize*/
}


void sampleCpp::TurnLeftActions(){

	/*implementation of Turn Left*/
}


void sampleCpp::TurnRightAction(){

	/*implementation of Turn Right*/
}


void sampleCpp::SetLight(bool flag){

}


/* Begin - EA generated code for StateMachine */



void sampleCpp::SSM_lightOn(CommandType command, Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_left)
			{
				nextState = ST_SSM_turning_left;
			}
			else
			if (e == E_right)
			{
				nextState = ST_SSM_turning_right_StateA;
				transcend = true;
			}

			break;
		}
		case Entry:
		{
			//Entry Behaviors..
			SetLight(true);
			break;
		}
		case Exit:
		{
			//Exit Behaviors..
			SetLight(false);
			//Transition Actions

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::SSM_initialize(CommandType command, Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_right)
			{
				nextState = ST_SSM_turning_right_StateA;
				transcend = true;
			}
			else
			if (e == E_left)
			{
				nextState = ST_SSM_turning_left;
			}

			break;
		}
		case Entry:
		{
			//Entry Behaviors..
			InitializeAction();
			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::SSM_turning_right(CommandType command, Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			TurnRightAction();
			// State's Transitions
			if (e == E_right)
			{
				nextState = ST_SSM_turning_right_StateA;
				transcend = true;
			}
			else
			if (e == E_left)
			{
				nextState = ST_SSM_turning_left;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::SSM_turning_right_StateA(CommandType command, Event e)
{
	if (transcend == true || command == Do)
	{
		SSM_turning_right(command, e);
		if (currState != nextState)
		{
			transcend = true;
		}
	}

	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_changeState)
			{
				nextState = ST_SSM_turning_right_StateB;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::SSM_turning_right_StateB(CommandType command, Event e)
{
	if (transcend == true || command == Do)
	{
		SSM_turning_right(command, e);
		if (currState != nextState)
		{
			transcend = true;
		}
	}

	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_changeState)
			{
				nextState = ST_SSM_turning_right_StateC;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::SSM_turning_right_StateC(CommandType command, Event e)
{
	if (transcend == true || command == Do)
	{
		SSM_turning_right(command, e);
		if (currState != nextState)
		{
			transcend = true;
		}
	}

	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			// State's Transitions
			if (e == E_changeState)
			{
				nextState = ST_SSM_turning_right_StateA;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::SSM_turning_left(CommandType command, Event e)
{
	switch(command)
	{
		case Do:
		{
			// Do Behaviors..
			TurnLeftActions();
			// State's Transitions
			if (e == E_light_ON && (lightMode == true))
			{
				nextState = ST_SSM_lightOn;
			}
			else
			if (e == E_left)
			{
				nextState = ST_SSM_turning_left;
				externalTransition = true;
			}
			else
			if (e == E_right)
			{
				nextState = ST_SSM_turning_right_StateA;
				transcend = true;
			}

			break;
		}
		default:
		{
			break;
		}
	}
}

void sampleCpp::StatesProc(StateType currState, CommandType command, Event e)
{
	switch(currState)
	{
		case ST_SSM_lightOn:
		{
			SSM_lightOn(command, e);
			break;
		}

		case ST_SSM_initialize:
		{
			SSM_initialize(command, e);
			break;
		}

		case ST_SSM_turning_right:
		{
			SSM_turning_right(command, e);
			break;
		}

		case ST_SSM_turning_right_StateA:
		{
			SSM_turning_right_StateA(command, e);
			break;
		}

		case ST_SSM_turning_right_StateB:
		{
			SSM_turning_right_StateB(command, e);
			break;
		}

		case ST_SSM_turning_right_StateC:
		{
			SSM_turning_right_StateC(command, e);
			break;
		}

		case ST_SSM_turning_left:
		{
			SSM_turning_left(command, e);
			break;
		}
		default:
			break;
	}
}

void sampleCpp::initializeStateMachine()
{
	currState = ST_SSM_initialize;
	nextState = ST_NOSTATE;
	StatesProc(currState, Entry, E_NOEVENT);
}

void sampleCpp::performEvent(Event e)
{
	if ( currState == ST_NOSTATE )
	{
		return;
	}

	externalTransition = false;
	transcend = false;

	nextState = currState;
	StatesProc(currState, Do, e);

	if ( nextState == ST_NOSTATE )
	{
		StatesProc(currState, Exit, e);
		currState = ST_NOSTATE;
		return;
	}

	if ( currState != nextState || externalTransition == true)
	{
		StatesProc(currState, Exit, e);
		currState = nextState;
		StatesProc(nextState, Entry, e);
	}
}


/* End - EA generated code for StateMachine */