Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1: Parent state machine Base class a. Create a python class called StateMachine that has the following elements: - private variable __current_state which is

Part 1: Parent state machine Base class a. Create a python class called StateMachine that has the following elements: - private variable __current_state which is the current state of the state machine - private variable __state_machine which is a dictionary of the state table - an object property current_state which returns the value of __current_state - The following methods which should all call assert 0 with an error message indicating that the function needs to be implemented. - state_exists(test_state) checks to see if test_state is in _state_machine - event_exists(test_event) checks to see if test_event is in _state_machine - can_exit_state_with(test_event) checks to see if test_event would cause a state tranistion in the current state - get_all_states() returns all the current possible states in the FSM dictionary - get_all_events() returns a list of all unique events in the FSM dictionary - check_state_consistency() Perform some very basic checks on the FSM dictionary. Checks for consistency in: the input states, and that there is consistency between input and output states, i.e. that there is a defined input state for every output. If there is an error then an Exception is raised - initialise() is called first to set up the FSM object - event_handler(event, context) using the event passed in, attempts to cause a transition in the FSM. The context variable is used as additional information associated with the event. Remember that Python needs the parameter, self, for methods. // Part 2: Simple Client FSM a. Implement a Simple Client FSM class, ProtocolFSM, using the base parent Class - The FSM dictionary is a dictionary of dictionaries. it has the following format: _state_machine = {'state_1': {'event1': {'nxt_state': 'state_2', 'action': None}, 'event2': {'nxt_state': 'state_4', 'action': None}}, 'state_2': {'event2': {'nxt'state': 'state_3', 'action': None}}, 'state_3': {'event3': {'nxt_state': 'state_2', 'action': None}, 'event4': {'nxt_state': 'state_4', 'action': None}}, 'state_4': {'event1': {'nxt_state': 'state_1', 'action': None}}} Before moving on, using the example above, draw the state transition diagram. For the moment leave the action keys for the events as the NoneType - Using the information (state transition diagram and table) complete a FSM dictionary in your new class which fully implements the information - Implement the following functions and test them: - __init__, state_exists, event_exists, can_exit_state_with, get_all_states, get_all_events - Implement the check_state_consistency method. This should: 1. Make sure all the states in outer dictionary are present as 'nxt_state' entries and vice versa - this ensures states have been entered correctly 2. Ensure there is an 'init' state and an init event which transitions from it. 3. What other consistency check do you think would be a good idea? Maybe things that could give more descriptive errors? - Any issues should raise an exception with a suitable description - Implement the event_handler method. This should: 1. Ensure current state is in the FSM dictionary. throw exception if not 2. If the event is not a transition event return the current_state 3. If the event is a trasnition event, - Retreive the next state, - set the current state to this new state, - return the new state. - Implement the initialise() method. This should: 1. check for the init state in the FSM dictionary 2. set the current state to 'init' 3. call the event_handler method with event='init' and context=None Note: you will need to create a basic app that instantiates the class and calls the various methods to test the correct functionality. You will need to import your Base class with: from StateMachine import StateMachine // Part 3: Event Handlers In this part you will create event handlers to add additional functionality. This will involve adding functions and storing them as variables to be called later. Hints: - https://stackoverflow.com/questions/7898025/calling-modules-from-python-dir - https://stackoverflow.com/questions/27472704/store-functions-in-list-and-call-them-later - https://book.pythontips.com/en/latest/args_and_kwargs.html a. Add the following methods to your client class. - _init, _send_msg_0, _resend_msg_0, _rcv_ack_0, _send_msg_1, _resend_msg_1, _rcv_ack_1, _error. - Each method should have an input parameter of context - Each method should print what the action is, i.e. _send_msg_0 displays Sending Message 0 - Each method should print the context data if not null - Each method should return a string which is the method name Hint: you can call the event handlers directly to test functionality NOTE: You might want to add @current_state.setter and @state_machine.setter methods to your previous code to make it easier to build your classes b. Adapt the event_handler method so that: - When an event is found that causes the transition, the action is retrieved - If the action is not None, call the function, passing the context given to event_handler - When event_handler returns it should return the new state AND the output from the action method - Add all the action handler methods to the right place in the FSM dictionary.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions