ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Ada Deterministic State Machine with Event Handling

Ada

Goal -- WPM

Ready
Exercise Algorithm Area
1with Ada.Text_IO;
2use Ada.Text_IO;
3
4package State_Machine_Pkg is
5
6-- Define the possible states of the state machine
7type State_Type is (
8State_Idle,
9State_Active,
10State_Error
11);
12
13-- Define the possible events that can trigger state transitions
14type Event_Type is (
15Event_Start,
16Event_Stop,
17Event_Reset,
18Event_Fault
19);
20
21-- Procedure to process an incoming event and transition the state
22procedure Process_Event (Current_State : in out State_Type;
23Input_Event : in Event_Type);
24
25-- Function to get the current state as a string
26function State_To_String (S : State_Type) return String;
27
28-- Function to get the event as a string
29function Event_To_String (E : Event_Type) return String;
30
31end State_Machine_Pkg;
32
33package body State_Machine_Pkg is
34
35-- Procedure to process an event
36procedure Process_Event (Current_State : in out State_Type;
37Input_Event : in Event_Type) is
38begin
39Put_Line ("Processing event " & Event_To_String(Input_Event) & " in state " & State_To_String(Current_State));
40
41-- Use a case statement to handle transitions based on current state and event
42case Current_State is
43when State_Idle =>
44case Input_Event is
45when Event_Start =>
46Current_State := State_Active;
47Put_Line ("Transitioned to State_Active.");
48when Event_Reset =>
49-- Stay in State_Idle, no change
50Put_Line ("Staying in State_Idle.");
51when Event_Stop | Event_Fault =>
52-- Ignore these events in State_Idle
53Put_Line ("Event ignored in State_Idle.");
54end case;
55
56when State_Active =>
57case Input_Event is
58when Event_Stop =>
59Current_State := State_Idle;
60Put_Line ("Transitioned to State_Idle.");
61when Event_Fault =>
62Current_State := State_Error;
63Put_Line ("Transitioned to State_Error.");
64when Event_Start | Event_Reset =>
65-- Ignore these events in State_Active
66Put_Line ("Event ignored in State_Active.");
67end case;
68
69when State_Error =>
70case Input_Event is
71when Event_Reset =>
72Current_State := State_Idle;
73Put_Line ("Transitioned to State_Idle.");
74when Event_Start | Event_Stop | Event_Fault =>
75-- Ignore these events in State_Error
76Put_Line ("Event ignored in State_Error.");
77end case;
78end case;
79end Process_Event;
80
81-- Function to convert state to string
82function State_To_String (S : State_Type) return String is
83begin
84case S is
85when State_Idle => return "State_Idle";
86when State_Active => return "State_Active";
87when State_Error => return "State_Error";
88end case;
89end State_To_String;
90
91-- Function to convert event to string
92function Event_To_String (E : Event_Type) return String is
93begin
94case E is
95when Event_Start => return "Event_Start";
96when Event_Stop => return "Event_Stop";
97when Event_Reset => return "Event_Reset";
98when Event_Fault => return "Event_Fault";
99end case;
100end Event_To_String;
101
102end State_Machine_Pkg;
Algorithm description viewbox

Ada Deterministic State Machine with Event Handling

Algorithm description:

This Ada code implements a deterministic finite state machine (FSM) that transitions between states based on incoming events. It defines states like `State_Idle`, `State_Active`, and `State_Error`, and events such as `Event_Start` and `Event_Fault`. The `Process_Event` procedure is the core logic, determining the next state based on the current state and the received event. This is crucial for systems requiring predictable behavior, such as control systems in industrial automation or embedded devices where state management must be robust and unambiguous.

Algorithm explanation:

The `State_Machine_Pkg` defines an FSM with three states (`State_Idle`, `State_Active`, `State_Error`) and four events (`Event_Start`, `Event_Stop`, `Event_Reset`, `Event_Fault`). The `Process_Event` procedure uses nested `case` statements to implement the state transition logic. The outer `case` selects transitions based on the `Current_State`, and the inner `case` selects based on the `Input_Event`. For each state-event combination, it either transitions to a new state, stays in the current state, or ignores the event. The `State_To_String` and `Event_To_String` functions are helpers for logging. This FSM is deterministic because for any given state and event, there is exactly one defined next state. The time complexity of `Process_Event` is O(1) as it involves a fixed number of comparisons and assignments, regardless of the number of states or events. Space complexity is also O(1) as it only uses a few variables to track the current state and event.

Pseudocode:

PACKAGE State_Machine_Pkg IS
   TYPE State_Type IS (State_Idle, State_Active, State_Error);
   TYPE Event_Type IS (Event_Start, Event_Stop, Event_Reset, Event_Fault);

   PROCEDURE Process_Event(Current_State : in out State_Type; Input_Event : in Event_Type);
   FUNCTION State_To_String(S : State_Type) RETURN String;
   FUNCTION Event_To_String(E : Event_Type) RETURN String;
END State_Machine_Pkg;

PACKAGE BODY State_Machine_Pkg IS
   PROCEDURE Process_Event(Current_State : in out State_Type; Input_Event : in Event_Type) IS
   BEGIN
      CASE Current_State IS
         WHEN State_Idle =>
            CASE Input_Event IS
               WHEN Event_Start => Current_State := State_Active;
               WHEN Event_Reset => NULL; -- Stay in State_Idle
               WHEN Event_Stop | Event_Fault => NULL; -- Ignore
            END CASE;
         WHEN State_Active =>
            CASE Input_Event IS
               WHEN Event_Stop => Current_State := State_Idle;
               WHEN Event_Fault => Current_State := State_Error;
               WHEN Event_Start | Event_Reset => NULL; -- Ignore
            END CASE;
         WHEN State_Error =>
            CASE Input_Event IS
               WHEN Event_Reset => Current_State := State_Idle;
               WHEN Event_Start | Event_Stop | Event_Fault => NULL; -- Ignore
            END CASE;
      END CASE;
   END Process_Event;

   FUNCTION State_To_String(S : State_Type) RETURN String IS
   BEGIN
      CASE S IS
         WHEN State_Idle => RETURN "State_Idle";
         WHEN State_Active => RETURN "State_Active";
         WHEN State_Error => RETURN "State_Error";
      END CASE;
   END State_To_String;

   FUNCTION Event_To_String(E : Event_Type) RETURN String IS
   BEGIN
      CASE E IS
         WHEN Event_Start => RETURN "Event_Start";
         WHEN Event_Stop => RETURN "Event_Stop";
         WHEN Event_Reset => RETURN "Event_Reset";
         WHEN Event_Fault => RETURN "Event_Fault";
      END CASE;
   END Event_To_String;
END State_Machine_Pkg;