What is a state machine?

[00:00] Alright, so in this set of slides, I'm going to explain what a state machine is. And as I start, the first thing that I would like to point out is if you go to the Dialogflow CX documentation page, you will notice that the first thing it starts with is it says Dialogflow CX provides a new way of designing agents, taking a state machine approach to agent design.

[00:21] So, what is the state machine? It's a concept from computer science, and if you go to, let's say, if you go to Wikipedia, you can see that, unfortunately, this is a very complex explanation and I would not call it the most simple explanation for the state machine.

[00:46] There's no need for you to get this complex, although a good thing is that the state machine definition does talk about this vending machine, and I think that that's probably the simplest definition that you can have for a state machine.

[01:04] So, the vending machine is a good example of a state machine because it goes from state to state depending on how much money you have added into it. So depending on the state, that is the balance that you have in your coins that you have added to the vending machine, you can actually choose different products, and depending on the state, that is the balance, you will get a different amount of money back from the machine if, let's say, the product that you selected is less than the amount that you paid.

[01:35] I'm going to take a simple example here. Let's say that you start with zero balance. Okay, that is one state, that is the state the vending machine is in, the state of zero balance. And you add 25 cents or a quarter, right at that point, the balance has now become 25 cents. That is the new state. The new state is where the balance in the vending machine is 25 cents.

[02:07] You add one more quarter, and now the state has become 50 cents. That is the balance of the amount in the vending machine is now 50 cents, and then you add another quarter, it gets to 75 cents and so on.

[02:27] Now let's make it a bit more useful and interesting. Let's say that you have exactly three products in your vending machine, okay? And those are Snickers bar, which costs exactly 25 cents. You can have a Twix, which is exactly 50 cents, or a KitKat, which costs exactly 75 cents.

[02:52] So you start with zero balance, and then you insert a quarter into the vending machine. Now, at that point, because you only have a balance of 25 cents, the only product that you can buy is the Snickers bar. If you were to select any of the other products, as you might know, the vending machine will usually complain that you don't have enough balance to buy any other product, at least any other products in this list.

[03:14] And then let's say you add one more 25 cents. Now you can select either Snickers or Twix. And if you were to select Snickers and you asked for the refund, you will get 25 cents back. On the other hand, if you select Twix, that means your balance has now come down to zero, which means that you don't get any money back. If you were to ask for a refund, it will just say that it's empty balance or something like that.

[3:44] And finally, if you were to add one more quarter, at that point, you can select any of these three items. So, you can either go for the Snickers or the Twix or the KitKat. In fact, you can even go for one Snickers and then one Twix, and the vending machine should still be able to handle that because the sum of these two is only 20 75 cents, which you do have in that balance. That's the basic example of a vending machine.

[4:13] I think that understanding this is sufficient for you to understand how the agents are constructed in Dialogflow CX. I don't think from what I've seen till now, I don't think that you need to complicate this any further than just understanding a basic state machine like the vending machine example that I have shown here.