Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / Win32

Visual Modeling of Complex Reactive Systems with Harel UML StateCharts

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
8 Sep 2009LGPL310 min read 43.6K   692   35  
This article presents a commercial-grade cross-platform Harel UML StateChart Open-Source application framework named StateWizard for concurrent, distributed, and real-time reactive system development with simplicity, efficiency, and scalability.
/* State Chart:

PowerDown -> Join1 -> Cond1 -> PowerUp -> 
    ^                                   |  
    -------------------------------------

PowerUp (Playing <---> Pause)

Refer to Readme.txt for more information.
*/

#include "sme.h"

#include "Player.h"

#include "EventId.h"
#include <stdio.h>
#include "sme_cross_platform.h"
#include "sme_debug.h"

#ifdef SME_CURR_DEFAULT_PARENT
#undef SME_CURR_DEFAULT_PARENT
#endif
#define SME_CURR_DEFAULT_PARENT Player

SME_HANDLER_CLASS_DEF(CPlayer)

/* Define Root (Composite-state) of Player*/
SME_BEGIN_ROOT_COMP_STATE_DEF(Player, PlayerEntry, PlayerExit)
SME_ON_INIT_STATE(SME_NULL_ACTION, PowerDown)
SME_END_STATE_DEF

/* Define state PowerDown */
SME_BEGIN_LEAF_STATE_DEF_P(PowerDown, PowerDownEntry, PowerDownExit)
//SME_ON_EVENT(EXT_EVENT_ID_POWER, OnPowerDownEXT_EVENT_ID_POWER, PowerUp) // Test Case: transit to a composite state
//SME_ON_EVENT(EXT_EVENT_ID_POWER, OnPowerDownEXT_EVENT_ID_POWER, Cond1) // Test Case: transit to a conditional pseudo state
SME_ON_EVENT_WITH_GUARD(EXT_EVENT_ID_POWER, Guard1_func, OnPowerDownEXT_EVENT_ID_POWER, Join1) // Test Case: transit to a join pseudo state
SME_END_STATE_DEF

SME_BEGIN_SUB_STATE_DEF_P(PowerUp)
SME_ON_EVENT(EXT_EVENT_ID_POWER,OnPowerUpEXT_EVENT_ID_POWER,PowerDown)
SME_END_STATE_DEF

enum {COND_EV_COND1, COND_EV_COND2};
/******************************************************************************** */
/* Define Conditional Note: No Guard for pseudo state.*/
SME_BEGIN_COND_STATE_DEF_P(Cond1, Cond1_func)
SME_ON_EVENT(COND_EV_COND1, CondAct1, Playing) 
SME_ON_EVENT(COND_EV_COND2, CondAct2, Pause)
SME_ON_EVENT(SME_EVENT_COND_ELSE, CondActElse, PowerUp)
SME_END_STATE_DEF

/* Define Join */
SME_BEGIN_JOIN_STATE_DEF_P(Join1) /*Test Case: a pseudo state -> a pseudo state */
SME_ON_JOIN_TRAN(JoinAct, Cond1)
SME_END_STATE_DEF

SME_END_COMP_STATE_DEF(Player)

SME_OBJ_DEF(Player1, Player)
SME_OBJ_DEF(Player2, Player)

BOOL CPlayer::Guard1_func(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("Guard1_func\n");
	return TRUE;
}

BOOL CPlayer::GuardTimer2_func(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	//return TRUE;
	return FALSE; /* Filter out timer2 events. */
}

int CPlayer::Cond1_func(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("Cond1_func\n");
	return SME_EVENT_COND_ELSE; 
}

int CPlayer::CondAct1(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("CondAct1c\n");
	return 0;
}

int CPlayer::CondAct2(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("CondAct2\n");
	return 0;
}

int CPlayer::CondActElse(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("CondActElse\n");
	return 0;
}

int CPlayer::JoinAct(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("JoinAct\n");
	return 0;
}

int Timer1Proc(SME_OBJ_T *pDestObj, unsigned long nSequenceNum)
{
	printf("Timer1 SeqNum=%d time out\n", nSequenceNum);
	//XSleep(3000); // Test case: Timer event overflow
	return 0;
}

int CPlayer::OnTimer2Proc(SME_OBJ_T *pDestObj, SME_EVENT_T *pEvent)
{
	printf("Timer2 SeqNum=%d time out\n", pEvent->nSequenceNum);
	return 0;
}


int CPlayer::PlayerEntry(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PlayerEntry\n");
	return 0;
}

int CPlayer::PlayerExit(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PlayerExit\n");
	return 0;
}


int CPlayer::PowerDownEntry(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PowerDownEntry\n");
	return 0;
}


int CPlayer::PowerDownExit(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PowerDownExit\n");
	return 0;
}


int CPlayer::PowerUpEntry(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PowerUpEntry\n");
	return 0;
}


int CPlayer::PowerUpExit(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PowerUpExit\n");
	return 0;
}


int CPlayer::PlayingEntry(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PlayingEntry\n Start 2 Timers\n");
	m_Timer1 = XSetTimer(this, 3000, Timer1Proc);
	m_Timer2 = XSetEventTimer(this, 1000);
	return 0;
}


int CPlayer::PlayingExit(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PlayingExit\n Stop 2 timers\n");
	if (m_Timer1)
		XKillTimer(m_Timer1);
	if (m_Timer2)
		XKillTimer(m_Timer2);

	return 0;
}


int CPlayer::PauseEntry(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PauseEntry\n");
	return 0;
}


int CPlayer::PauseExit(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("PauseExit\n");
	return 0;
}


int CPlayer::OnPowerDownEXT_EVENT_ID_POWER(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("OnPowerDownEXT_EVENT_ID_POWER\n");
	return 0;
}


int CPlayer::OnPlayingEXT_EVENT_ID_PAUSE_RESUME(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("OnPlayingEXT_EVENT_ID_PAUSE_RESUME\n");
	return 0;
}


int CPlayer::OnPauseEXT_EVENT_ID_PAUSE_RESUME(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("OnPauseEXT_EVENT_ID_PAUSE_RESUME\n");
	return 0;
}


int CPlayer::OnPowerUpEXT_EVENT_ID_POWER(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	// Memorize the history state of the composite state PowerUp
	//SME_UPDATE_STATE_TRAN_DEST(PowerDown, 2, pObj->pHistoryState)
	
	printf("CPlayer::OnPowerUpEXT_EVENT_ID_POWER\n");
	return 0;
}

//////////////////////////
int CSuperPlayer::OnPauseEXT_EVENT_ID_PAUSE_RESUME(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{
	printf("OnPauseEXT_EVENT_ID_PAUSE_RESUME\n");
	return 0;
}


int CSuperPlayer::OnPowerUpEXT_EVENT_ID_POWER(SME_OBJ_T *pObj, SME_EVENT_T *pEvent)
{

	printf("CSuperPlayer::OnPowerUpEXT_EVENT_ID_POWER\n");
	
	return 0;
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions