Grumson.Utilities.Fsm 1.0.1

dotnet add package Grumson.Utilities.Fsm --version 1.0.1                
NuGet\Install-Package Grumson.Utilities.Fsm -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Grumson.Utilities.Fsm" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Grumson.Utilities.Fsm --version 1.0.1                
#r "nuget: Grumson.Utilities.Fsm, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Grumson.Utilities.Fsm as a Cake Addin
#addin nuget:?package=Grumson.Utilities.Fsm&version=1.0.1

// Install Grumson.Utilities.Fsm as a Cake Tool
#tool nuget:?package=Grumson.Utilities.Fsm&version=1.0.1                

The Fsm is a C# library designed for personal use.

Fsm Project Documentation

Overview

The Fsm (Finite State Machine) project provides a generic implementation of a finite state machine in C#. It is designed to be flexible and reusable for various applications where state management and transitions are crucial. The project consists of two main classes: Fsm<TState, TEvent, TTransition> and FsmTransition<TState, TTransition>.

FsmTransition<TState, TTransition>

Description

FsmTransition<TState, TTransition> represents a transition in the finite state machine. It encapsulates the information about the next state and the transition trigger.

Properties

  • NextState: The state to transition to.
  • Transition: The trigger for the transition.

Constructors

  • FsmTransition(TState nextState, TTransition transition): Initializes a new instance of the FsmTransition class with the specified next state and transition trigger.

Fsm<TState, TEvent, TTransition>

Description

Fsm<TState, TEvent, TTransition> represents the finite state machine itself. It manages states, transitions between them based on events, and notifies about these changes through events.

Properties

  • OnTransition: An event that is triggered when a transition occurs.
  • OnStateChange: An event that is triggered when the state changes.

Constructors

  • Fsm(TState initialState, Dictionary<TState, Dictionary<TEvent, FsmTransition<TState, TTransition>>> transitions): Initializes a new instance of the Fsm class with the specified initial state and transitions.

Public Methods

  • Trigger(TEvent trigger): Triggers a transition based on the specified event. If the transition is valid, it changes the state and notifies about the transition and state change. If the transition is invalid, it throws an InvalidOperationException.
  • GetCurrentState(): Returns the current state of the finite state machine.
  • SetCurrentState(): Sets the current state of the finite state machine to the specified state.

Events

  • Action<TTransition> OnTransition: Triggered when a transition occurs.
  • Action<TState> OnStateChange: Triggered when the state changes.

Usage Example

Below is a simple example of how to use the Fsm project to manage states and transitions:


// Define states and events
enum States { Idle, Running, Stopped }
enum Events { Start, Stop }

// Define transitions
var transitions = new Dictionary<States, Dictionary<Events, FsmTransition<States, Events>>>
{
    { States.Idle, new Dictionary<Events, FsmTransition<States, Events>>
        {
            { Events.Start, new FsmTransition<States, Events>(States.Running, Events.Start) }
        }
    },
    { States.Running, new Dictionary<Events, FsmTransition<States, Events>>
        {
            { Events.Stop, new FsmTransition<States, Events>(States.Stopped, Events.Stop) }
        }
    }
};

// Create the FSM
var fsm = new Fsm<States, Events, Events>(States.Idle, transitions);

// Subscribe to events
fsm.OnTransition += transition => Console.WriteLine($"Transition: {transition}");
fsm.OnStateChange += state => Console.WriteLine($"State changed to: {state}");

// Trigger transitions
fsm.Trigger(Events.Start); // Output: Transition: Start
                            //         State changed to: Running
fsm.Trigger(Events.Stop);  // Output: Transition: Stop
                            //         State changed to: Stopped

Changelog

This section outlines the changes and improvements made in each version of the Fsm.

Version 1.0.1 - 2023-07-19

Added

  • SetCurrentState() method to set the current state of the finite state machine.

Improved

Fixed

Version 1.0.0 - 2023-07-15

  • Initial release of the Fsm.

This documentation provides a basic overview and usage of the Fsm project. For more detailed information, refer to the source code and comments within the Fsm.cs and FsmTransition.cs files.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 95 7/19/2024
1.0.0 70 7/16/2024