Hi students. In this lecture, we're going to talk about how to draw a state machine diagram to capture all the important states and transitions within an object that exists in a software system. This lecture is about drawing state machine diagrams. This is an example of a state machine diagram. Within a state machine diagram, you have to specify all the states. For example, this is a state machine diagram for bank account for a bank software system. Then for a bank account then we may have states like incredit, overdrawn, frozen, or closed. We also have to specify all the transitions between the states. For example, when the balance becomes negative, we're going to move from the incredit state to the overdrawn state. Let's say the account balance remains negative for three months, then we're going to ask the bank manager to frozen the account. If we are indeed incredit state and we call this operation close account to close the account then we going to move from the incredit state to the close state. Then after closing the account for five-years, then we move into the final state. This is a special state within our state machine diagram. When we destroy the object, when we delete the object, we're going to move into the final state. Another special stage that we have within this state machine diagram is what we call the initial state. We specify it using a dot. When we create the object using a constructor, we go into this initial state. Then once the object is being created, it will be moved directly into the incredit state, so on and so forth. The state machine diagram describes the behavior inside an object and you have to specify all the possible states and all the possible transitions between the states within the state machine diagram and is a directed graph that shows the state of a single object using nodes and also the events that cause state changes, the arcs. It shows all the messages that an object can send and receive and it also describes all the possible states that an object can go into. It also describes all the possible states that an object can get into during it's lifetime. Lifetime means after we construct the object and before we destroy the object that means the lifetime. It's drawn for a single class to show the lifetime behavior of a single object. This is a more detailed version of a state machine diagram. Let's say if we know all the operations that we can perform within a class, then we can also declare the activities that we can perform within a state. For example, in the incredit state, we can deposit money or withdrawal money using this operations. Under the overdrawn state, we can also withdraw money and also deposit money using these two operations. Then when the balance is less than the overdraft limit, we can notify the manager. Now we can also specify all the details within a state if we know all the operations that we can perform within a class. That's why I say that this is a more detailed version of a state machine diagram because we provide more details within a state. This is how we specify a state. We use a rectangle to specify a state. Notice that a state has duration. That means once you go into, for example, the incredit state, you can stay within the incredit state for some time. As long as the balance is positive, you will remain in this incredit state. We define a state as a time during the life of an object when it satisfy some condition, performs some action or waits for an event. A state may be characterized by, for example, the value of one or more attributes. For example, we can determine whether a bank account is overdrawn or not based on the balance, the attribute balance that we have within the class or existent off a link to another object. For example, if you are talking about the rental copies that we have within a movie shop, a rental copy is being reserved by a customer. Then we put a link from the rental copy to a particular customer. In that sense the rental copy is being reserved by someone, is no longer available. That means we can determine the state of a rental copy by double-checking whether we have a link between a rental copy and a customer. If there is a link between the rental copy and the customer, that means the rental copy is no longer available. This are the two special states that we have within the state machine diagram. We use a dot to denote an initial state. That means when we construct the object using the constructor, the object will go into the initial state. Then we also have the final state, which is a dot within a circle. When we destroy the object or when we delete the object, then we go into the final state. If we have a state machine diagram with no initial state or final state, that may indicate looping behavior. That means you'd never go into the final state. Notice that states may be named it. That means within the state you have to provide the name of the state or a name-date. Those are what we call anonymous states. In this course whenever possible, we have to name the states. Because we have to make sure that all the stakeholders, when they read the state machine diagram, they will understand the meanings of the states. That's why I always recommend students to always name all the states. The definition of a transition is a change of states from an originating state to a successor state. Notice that the source and target states, they may be the same so we may go back to the same state. A transition takes zero time and cannot be interrupted. Why do we have this restriction? Because we want our state machine diagram to be always deterministic. Deterministic means you can always determine the state of a particular object that exist in a particular time. It's not possible for an object to exist in more than one states at one time, so that's what we mean by deterministic. Notice that without this restriction, then we may fire more than one transitions at a time. Eventually, we may have an object that exists in more than one states at one time, and this is not what we want. That's why we need this restriction to make sure that a state machine diagram is going to be always deterministic. This is how you specify a transition. You have to specify the event trigger, the guard condition, and also the effect list. Event trigger means when an event happened, we're going to move from one state to another state. Guard condition means that condition that you need to satisfy before you can fire the transition. Effect lists is an automatic procedural expression that should be executed when a transition fires. Something that has to be done when we fire a transition. Now let's first talk about event. An event is something that happens at an instantaneous point of time. There are different types of events that may happen within a state machine diagram. For example, call event. You're going to call some operations that you have within the class to perform something or change event. That means once you achieve some condition, then you're going to move from one state to another state. Or time event. For example, after 10 seconds or random dates, is equals to some date, then you can move from one state to another state. Or a signal event that means the receipt of an asynchronous communication from an object. In this course, we're going to focus on call event, change event, and time event only. These are some examples of call event, change event, and time event. Call events means when you call the operations that we have within the class to do something. For example, you can call deposit, the operation, to deposit some money into the account, or call this withdraw operation, to withdraw money from the account. These are what we call, call event. Change events means when some condition is being changed. For example, when balance is negative, then you're going to move from the credit state to the overdrawn state. Time event is, for example, after three months, you can move from the overdrawn state into the frozen state. These are the three major types of events that we have within a state machine diagram. Notice that events, they are processed one at a time. If the event does not trigger any transition, then is going to be ignored, and only one transition within a state machine diagram may fire, just because we want our state machine diagram to be always deterministic. There are two ways to get out of a state. Either automatic, that means once you finish all activities within a state, then you move to the next stage. Or non-automatic that means it's caused by any event, then once an event happen, you're going to move from one state to another state. We may have different actions and activities within a state machine diagram. If we are talking about an action, it's going to be instantaneous and cannot be interrupted. As mentioned before, for example, a transition is going to be instantaneous. It's going to be an action between states. We may also have activity which is going to take some time to complete and can be interrupted. For example, within a state, we have different activities that you're going to perform within the state. That those are what we call activities that we can interrupt and it's going to take some time to complete. These are of possible forms of state behavior. It may be no behavior that means you stay within a state until something happened, or you trigger an event, and then you go from one state to another state. Or you're going to do something within a state, or perform some entry activity within a state, or perform some exit activity within a state. What do we mean by do activity, entry activity, and exit activity? Let me show you some examples. This is an example, of a state machine diagram for enter password. Then when we enter this state, these are the things that we have to do. These are the entry activities. We have to set echo to star and then clear the password. These are things that you have to do when you enter the state. When you exit the state, this is the thing that you have to do. You have to set echo back to normal. Then when you are within the enter password state, these are things that you can do. For example, you can do key-press to enter password, or you can clear the password, or you can press on the help button to display the help menu, etc. These are the things that you can actually do within the state, and these are what we call the do activities within the state.