14
TP4: STATE MACHINE DESIGN PATTERN Definition Infrastructure Transition Code Conclusion

TP4: S TATE M ACHINE D ESIGN P ATTERN Definition Infrastructure Transition Code Conclusion

Embed Size (px)

Citation preview

Page 1: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

TP4: STATE MACHINE DESIGN PATTERN

DefinitionInfrastructureTransition CodeConclusion

Page 2: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

STATE MACHINES You use the state machine design pattern to implement

algorithms that can be explicitly described by a state diagram or flow chart. A state machine consists of a set of states and a transition function that maps to the next state.

Page 3: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

STATE MACHINE INFRASTRUCTURE1. While Loop: The follow of the state transition diagram is implemented

by the while loop. The while loop allows to execute the various states continually.

2. Case Structure: the individual states are represented by cases in the case structure. It contains a case for each state and the code to execute for each state.

3. Shift Register: A shift register on the while loop keeps track of the current state to the Case structure input

4. State Functionality Code: it is used to implement the function of the state(it is located in the case structure).

5. Transition Code: the transition code determines the next state in the sequence, we usually use the “enum” constant to determine the next state.

1

3

2

4

5

Page 4: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

You use enumerated type constants as case selectors to control the initialization and transition of state machines. The most common obstacle when implementing state machines with enumerated type constants is that if a user attempts to add or delete a state from the enumerated type constant, the remaining wires connected to the copies of this enumerated type constant break. One solution to this problem is to type define the enumerated constant. Creating a type defined enumerated type constant causes all the enumerated type constant copies to automatically update if you add or remove a state.

Enumerated Type Constant

CONTROLLING THE STATE MACHINE

Page 5: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

State Machines A state machine relies on user input or in-state calculation to determine which state to go to next. Many applications require an initialization state and a default state, where many different actions can be performed. State machines are commonly used to create user interfaces where different user actions send the user interface into different processing segments. Each processing segment acts as a state in the state machine. Each segment leads to another segment for further processing or waits for another user action.

Click on each button to see how different actions can lead to different states!

Page 6: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

TRANSITION CODE There are several common design patterns used to develop the

transition code. You choose the transition code design pattern based on the number of states you need to transition between:

• Transitioning to another state (Default Transition)• Transition between two possible states• Transition among two or more possible states

Page 7: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

DEFAULT TRANSITION

For the default transition, no code is needed to determine the next state, as there is only one possible choice.

Page 8: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

TRANSITION BETWEEN TWO STATES

There are several patterns commonly used to accomplish transition between two states. This method (in the figure) works well if you know that the individual state always moves between two states. But, If you need to modify the state to transition among more than two states, this solution would not work and would require a major modification of the transition. code.

Page 9: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

TRANSITION AMONG TWO OR MORE STATES

You can create a more scalable architecture by using one of the following methods for transition among multiple states:

1. Case Structure: Each case in a Case structure corresponds to an item in the enumerated type control, making it easy to read and understand the code. A Case structure is also scalable and you can add more transitions to a particular state by adding more cases to the Case structure. However, due to the nature of the Case structure, you cannot see the complete functionality of the transition code at a glance.

Page 10: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

TRANSITION AMONG TWO OR MORE STATES:CASE STRUCTURE EXPLICATION

This case structure means, In the “Analyzer Case”:If the string “Analysis”= “One” the next state will be “Start”If the string “Analysis”=“Two” the next state will be “Analyzer”If the string “Analysis”=“Three” the nest state will be “Error Handler.

Case1

Case2

Case3

Page 11: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

TRANSITION AMONG TWO OR MORE STATES

2. Transition Array: If you need more of the code to be visible than a Case structure allows, you can create a transition array for all the transitions that can take place in the transition code for a state.

Page 12: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

STATE MACHINE: CONCLUSION A state machine in LabVIEW consists of a While Loop, a Case

structure, a type defined enum constant and a shift register: Each state of the state machine is a separate case in the

Case structure. You place VIs and other code that the state should execute within the appropriate case.

A shift register stores the state (indicated by the enum constant)that should execute upon the next iteration of the loop.

The next state

will be either Shut

Down or Idle

Page 13: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

STATE MACHINE: CONCLUSION For the displayed VI, the possible states are Startup, Idle, Event

1, Event 2, and Shutdown. An enumerated constant stores the states. Each state has its own case in the Case structure. The outcome of one case determines the case that next executes. The shift register stores a value that determines which case runs next.

Page 14: TP4: S TATE M ACHINE D ESIGN P ATTERN  Definition  Infrastructure  Transition Code  Conclusion

STATE MACHINE DESIGN PATTERN An advantage of the state machine architecture is that each case

determines the next state, unlike Sequence structures that cannot skip a frame.

The state machine design can make the block diagram much smaller, and therefore easier to read and debug.

A disadvantage of the state machine design pattern is that it makes it possible to skip states. If two states in the structure are called at the same time, this model handles only one state, and the other state does not execute.