Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Readme.md

Chapter - 8 : The Orthogonal States

Orthogonal states can be considered as a Meta State which can run multiple parallel internal states, i.e they can run independently of each other. An orthogonal state is useful when when a state want to have multiple inner states.

Just like creating multiple event handlers, the construct mpl::list is used to create multiple inner states.

8.1 : Creating a Orthogonal States

The following code creates an orthogonal state '3' Inner states, each capable of running on its own.

// The main state
struct firstState;

// Forward Declarator : Multiple Inner States
struct firstState_Inner_1;
struct firstState_Inner_2;
struct firstState_Inner_3;

// Creating state machine with firstState
struct statemachine : sc::state_machine<statemachine, firstState> {};

// Making firstState as orthogonal state
struct firstState : sc::simple_state<firstState, statemachine,
	mpl::list<firstState_Inner_1, firstState_Inner_2, firstState_Inner_3 >>
  {};

// Create constructors of firstState inner states
struct firstState_Inner_1 : sc::simple_state<firstState_Inner_1, firstState::orthogonal<0>> {
	firstState_Inner_1() { cout << "In State => firstState_Inner_1" << endl; }
};

struct firstState_Inner_2 : sc::simple_state<firstState_Inner_2, firstState::orthogonal<1>> {
	firstState_Inner_2() { cout << "In State => firstState_Inner_2" << endl; }
};

struct firstState_Inner_3 : sc::simple_state<firstState_Inner_3, firstState::orthogonal<2>> {
	firstState_Inner_3() { cout << "In State => firstState_Inner_3" << endl; }
};

// Run the statemachine to see the output

int main() {
	statemachine sm;
	// This will start with 3 Initial state
	sm.initiate();
	return 0;
}

8.2 : Orthogonal State Transitions

The code above contains something unusual which we haven't used till now in our code. It is

struct firstState_Inner_1 : sc::simple_state<firstState_Inner_1,firstState::orthogonal<0>>

A Meta State which is created as Orthogonal State is creates Orthogonal Regions. The inner states can be part of any Orthogonal Regions. The state transitions can happen only between orthogonal regions of a state and not between different orthogonal states.

The code above, declares that the state firstState_Inner_1 is part of an Orthogonal Region called <0>.

To transition from firstState_Inner_1 to any other state (for example TestState_OrthoRegion_0 ) can happen only if the state is declared as to be part of Orthogonal Region called <0>

// Create Teamp Event
struct event1 : sc::event<event1> {};
// Create another state in orthogonal Region <0>

struct TestState_OrthoRegion_0 : sc::simple_state<TestState_OrthoRegion_0, firstState::orthogonal<0>> {
	TestState_OrthoRegion_0() { cout << "In State => TestState_OrthoRegion_0" << endl; }
};

// Trigger change in state from firstState_Inner_1
struct firstState_Inner_1 : sc::simple_state<firstState_Inner_1, firstState::orthogonal<0>> {
	firstState_Inner_1() { cout << "In State => firstState_Inner_1" << endl; }
	typedef sc::transition<event1, TestState_OrthoRegion_0> reactions;
};

// Trigger the transfromation from main
int main() {
	statemachine sm;
	sm.initiate();
  sm.process_event(event1());
	return 0;
}

The code will initiate a transition from firstState_Inner_1 to TestState_OrthoRegion_0 in the orthogonal region <0>. Similar state transitions can be written for different orthogonal regions.

8.3 : Conclusion

In this chapter, we've learned how to create and manage orthogonal states.