Lecture thumbnail 0:00 / 0:00 Typically, state machines are constructed using appropriate state machine libraries.

But we’re going to look at how to implement one yourself.

So we’re not going to use any external library.

We’ll just have a bunch of states and then a bunch of transitions between those states and we’re going

to orchestrate the entire state machine.

So what I’m going to emulate is a phone call.

You pick up the phone and you dial a number and you can be talking to somebody or you can be put on

hold, something like that.

So for this system to work, we need two things.

We need a set of states such as the phone being off the hook or connecting or being placed on hold,

and also a bunch of triggers.

Now, triggers are simply events that happen in the system.

Like for example, when you dial a call, you dial a call.

And so you transition from the phone simply being off the hook to the phone, actually connecting to

whoever you just dialed.

So we’re going to implement all of this and we’ll begin by implementing the states.

So I’m going to have an enum called State, and we’re going to have four states.

We’re going to have off the hook.

We’re going to have a state for connecting another state for connected and another state for on hold.

In addition, we’re going to have a bunch of transitions between those states.

So we’re going to have transitions such as call dialed.

We’ll have hung up, hung up, we’ll have call connected, placed on hold, maybe taken off, hold and

left message if you leave a message on some of these answering machine.

Okay, So this is how we implement the state as well as the triggers.

So these are going to be part of the trigger enum.

And now we’re going to string this all together.

Now, as you may have guessed, what happens is you transition from one state to another using these

triggers.

So we’re going to make a dictionary which is going to store all of the transition information.

So private static dictionary.

So we’re going to transition from a state to a set of possible states dependent upon their trigger.

So here I’m going to make a list of pairs of trigger and state like so.

And this is all going to be called rules.

So that’s going to be a new this big massive dictionary and I’m going to initialize it in place as well.

Okay.

So let me just explain once again what’s going on.

You are in a particular state and for that particular state, what you can do is you can get a list

of all the states you can transition to together with the triggers which are required to transition

to that particular state.

So, for example, if you are in the off hook state, what you can do is you can transition to the connecting

state as you dial a number and actually try to connect to somebody.

So here we define a list of possible trigger and state pairs, and there’s only one in this case.

And this is where you dial a call.

So call dialed.

And what happens as you dial the call is you are now in the connecting state.

So this is how you define the state machine.

And you’re going to have more of these definitions.

I’m just going to paste a few of them in because, well, we don’t have time to type them all in.

So here are a few more like if you’re connecting, then you can transition to an off hook state.

If you hang up and you can transition to a connected state if the call is in fact connected.

So we have a bunch of these rules and now we can orchestrate the state machine so we can actually run

the state machine and see how it works.

So we’re going to begin with an initial state, var state equals state dot off hook, and from then

on we’re going to make an infinite loop.

So I’m going to have this and there is no terminating condition in my state machine.

There is no condition where you actually hang the phone back on the hook.

So we’re going to have this infinite loop.

And first of all, we’re going to specify what state we’re actually in right now.

So I’m going to write line and I’m just going to say the phone is currently and then specify the state,

and then we’re going to give the user an option to select a particular trigger.

So I’m going to write line, select a trigger.

There we go.

And then of course, we’re going to have some sort of for each iterating all of the possible transitions

which can happen from the current state.

And that’s essentially accessing the dictionary by key and getting the list of possible states.

So it’s rather easy to do.

So we’re going to say for each and we’re going to go through all of the rules, sorry, that should

be in here.

We’re going to go through all the rules at the current state.

And here I’m just going to call this var X for now.

But of course we also might want to show the index.

So let’s actually convert it to some sort of for and I’m going to get rid of this for now.

So we’re going to have an ordinary for loop and then I only want the transition.

I don’t want to tell the user what state they’re going to transition to.

I just want to offer the set of transitions.

So we’re going to say var t and then ignore the state equals rules at state at index I.

So this way you get the actual transition and then we can right line the transition so we can write

line, for example, that we have the index, which is I dot, and then the actual transition.

So this is the set of transitions you have and you get to pick one of them.

So we capture the input.

We say int input equals in dot parse console, dot read line.

I’m not doing any error checking here, but time is short and then I will get the state that we can

transition to.

So var underscore s equals rules at state at input.

So we got the input from zero to however many transitions are possible and we get the state for this

transition and we set the current state to the state that we got.

And this is an infinite loop.

So let’s actually run this and let’s see how the whole thing works.

All right, So the phone is currently off the hook and I get to select a trigger.

There’s only one trigger right now.

We can dial a call, so I select zero and then the phone is currently connecting.

And maybe the call did get connected, in which case we take a one and the phone is currently connected.

So we can either leave a message or we can hang up or we can be placed on hold.

So if we are placed on hold, then the phone is currently on hold and we get to select a new trigger

like the phone may be taken off hold.

And then of course, what I can do is I can leave a message and then the phone is off the hook again.

So I left a message and now once again, I’m back to the starting state and I can dial a new call if

I want to.

So this is how you can implement a very simple state machine without using any external libraries.

Just by making a dictionary with a list of all the possible transitions and then orchestrating the whole

thing so that you transition from one state to another.

Play Play Stop Play Play Play Start Play information alert