IegTools.Sequencer 3.0.0

dotnet add package IegTools.Sequencer --version 3.0.0                
NuGet\Install-Package IegTools.Sequencer -Version 3.0.0                
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="IegTools.Sequencer" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add IegTools.Sequencer --version 3.0.0                
#r "nuget: IegTools.Sequencer, 3.0.0"                
#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 IegTools.Sequencer as a Cake Addin
#addin nuget:?package=IegTools.Sequencer&version=3.0.0

// Install IegTools.Sequencer as a Cake Tool
#tool nuget:?package=IegTools.Sequencer&version=3.0.0                

IegTools.Sequencer

IegTools.Sequencer provides a fluent interface for creating easy-to-read and extensible sequences, eliminating the need for lengthy if/else statements.
The library is written in C# 12.0 and targets .NET Standard 2.0 (.NET (Core) and .NET Framework).

The library allows you to define:

  • Transition jobs: from one state to another state, when it should be triggered, and the action that should be invoked.
  • Force state on specified conditions.
  • Invoke actions on specified states.

Build Status

  workflow tests
  workflow complete

Table of Contents

Installation
Usage
States
State Tags
Validation
Handler
SequenceBuilder Extensions
Extensibility
Version Changes
Breaking Changes
Preview next Version v4.0

Installation

The library is available as a NuGet package.

Usage

Configure, build and run a sequence

Configuration .NET 6 style

A simple example configuration and usage for an OnTimer-sequence:

public class OnTimerExample
{
    private readonly ISequence _sequence;
    private readonly DefaultSequenceStates _state = new();
	
    public OnTimerExample() =>
        _sequence = SequenceConfig.Build();


    public void In(bool value)
    {
        LastValue = value;
        _sequence.Run();
    }

    private ISequenceBuilder SequenceConfig =>
        SequenceBuilder.Create()
            .SetInitialState(_state.Off)
            .DisableValidationForStates(_state.On)
            .AddForceState(_state.Off,                () => !LastValue)
            .AddTransition(_state.Off, _state.WaitOn, () => LastValue, () => _sequence.Stopwatch.Restart())
            .AddTransition(_state.WaitOn, _state.On,  () => _sequence.Stopwatch.IsExpired(OnDelay));
}

Top 🠉

Configuration .NET 5 style

A more complex example configuration for a pump-anti-sticking-sequence:

 private ISequenceBuilder SequenceConfig =>
        SequenceBuilder.Configure(builder =>
        {
            builder..SetInitialState("Paused")

            builder.AddForceState("Paused", () => !_onTimer.Out);
            
            builder.AddTransition("Paused", "Activated",
                () => _onTimer.Out,
                () => _countStarts = 1);
            
            builder.AddTransition("Activated", "Pump on",
                () => true,
                () => Stopwatch.Restart());
            
            builder.AddTransition("Pump on", "Pump off",
                () => Stopwatch.IsExpired(_settings.RunTime * _countStarts.Factorial()),
                () =>
                {
                    Stopwatch.Restart();
                    _countStarts++;
                });

            builder.AddTransition("Pump off", "Pump on",
                () => Stopwatch.Expired(_settings.PauseTime) && !sequenceDone());

            builder.AddTransition("Pump off", "Paused",
                () => Stopwatch.IsExpired(_settings.PauseTime) && sequenceDone(),
                () => _onTimer.In(false));

            bool sequenceDone() => _countStarts > _settings.PumpStartQuantity;
        });

Top 🠉

Config in Detail

  • Force state on condition:
    builder.AddForceState("ForceState", constraint)

  • State transition on condition (with optional action)
    builder.AddTransition("FromState", "ToState", constraint, action)

  • Action on state:
    builder.AddStateAction("State", action)

Top 🠉

States

States can be defined as strings or enums, internally they will be stored as strings.

Top 🠉

State Tags

[This feature will be deleted in the next major version (v4.0). The existing validators will detect if you have done the necessary changes and throw an exception if not. (the Initial-State and the ignored States should be defined explicitly)]

State-Tags can only be used with string-states. For enum-states there are other possibilities. (-> Validation Handler)

There are available two state tags as prefix for states

  • the IgnoreTag '!'
  • and the InitialStateTag '>'

IgnoreTag

Use the IgnoreTag as prefix for a state to tell the Validator not to check this state for counterpart-plausibility.

Example:

 .AddTransition("PrepareOff", "!Off", () => Stopwatch.Expired(MyTimeSpan));

InitialStateTag

Use the InitialStateTag as prefix for a state to tell the Sequence what state to start from.

Example:

 builder.AddForceState(">Paused", () => !_onTimer.Out);

[This feature will be deleted in the next major version (v4.0). The existing validators will detect if you have done the necessary changes and throw an exception if not. (the Initial-State and the ignored States should be defined explicitly)]

Top 🠉

Validation

The sequence will be validated on build.
_sequence = builder.Build();

Validation Handler:

  • InitialState must be defined
  • The InitialState must have a counterpart in a StateTransition
  • The Sequence must have at least two steps
  • Each 'NextStep' must have a counterpart StateTransition with a matching 'CurrentState'
  • Each 'CurrentState' must have a counterpart StateTransition with a matching 'NextStep' or ForceState

Validation could be disabled

  • completely turn off validation
    builder.DisableValidation()

  • or with specifying states that shouldn't be validated:
    builder.DisableValidationForStates("state1", "state2", ...)
    builder.DisableValidationForStates(Enum.State1, Enum.State2, ...)

  • or with the IgnoreTag '!':
    .AddTransition("PrepareOn", "!On", ...);

Top 🠉

Handler

Internally the Framework is working with Handler (you can write your own customized handler). The Handler describe what they are supposed to do within the sequence.

There are five default handler at the moment:

StateTransitionHandler

The StateTransitionHandler is responsible for the transition between two states. It switches the sequence from start-state to end-state when the sequence current state is the start-state and the state-transition-condition is true. Additionally, an action can be executed when the transition is done.

ContainsStateTransitionHandler

It's basically the same as the StateTransitionHandler, but it can handle multiple start-states to one end-state.

AnyStateTransitionHandler

It's basically the same as the ContainsStateTransitionHandler, but it can handle all start-states that contains the specified string to one end-state.

ForceStateHandler

Forces the sequence into the specified state when the force-state-condition is fulfilled. Additionally, an action can be executed when the force-transition is done.

StateActionHandler

Executes continuously the specified action when the sequence is in the specified state.

Top 🠉

SequenceBuilder Extensions

ExtensionMethods for existing Handler

All existing Handler can be added to a sequence via the SequenceBuilders ExtensionMethods.

AllowOnceIn(timeSpan)

Each Transition can be enhanced with the ExtensionMethod .AllowOnceIn(timeSpan). This prevents the transition from being triggered again within the specified timeSpan.

Example from an xUnit test:

    [Fact]
    public void Test_AllowOnlyOnceIn_set()
    {
        var x = 0;
        var builder = SequenceBuilder.Configure(builder =>
        {
            builder.SetInitialState("State1");
            builder.AddTransition("State1", "State2", () => true, () => x++)
                .AllowOnlyOnceIn(TimeSpan.FromSeconds(1))
                .DisableValidation();
        });

        var sut = builder.Build();

        sut.SetState("State1");

        for (int i = 0; i < 3; i++)
        {
            sut.Run();
            sut.SetState("State1");
        }

        x.Should().Be(1);
    }

For more examples take a look at the UnitTests.

Top 🠉

Extensibility

Write your own customized

  • Handler
  • Sequence
  • and Validator

TBD

Top 🠉

Version Changes

v2.2 → v3.0

  • new DefaultSequenceStates, a set of standard (string) states for a sequence
  • new builder.SetOnStateChangedAction(...);
  • changed builder.SetLogger(...) to builder.ActivateDebugLogging(...)
  • update NuGet packages
  • a bunch of internal changes

v2.1 → v2.2

  • new builder.SetLogger(...) methods

v2.0 → v2.1

  • new ExtensionMethod .AllowOnceIn(timeSpan)

Top 🠉

Breaking Changes

v2.2 → v3.0

  • changed builder.SetLogger(...) to builder.ActivateDebugLogging(...)

Top 🠉

Preview next Version v4.0

Removing the State-Tags (InitialStateTag and IgnoreTag) State Tags 🠉

Thinking about:
Renaming sequence.HasCurrentState(state) to sequence.IsInState(state)
Renaming sequence.HasAnyCurrentState(states) to sequence.IsInStates(state) or IsInAnyState(states)

Top 🠉

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
3.0.0 87 9/30/2024
3.0.0-rc.3 49 9/26/2024
3.0.0-rc.2 143 12/26/2023
3.0.0-rc.1 85 12/24/2023
3.0.0-alpha.5 80 12/23/2023
3.0.0-alpha.4 92 12/17/2023
3.0.0-alpha.3 71 12/17/2023
3.0.0-alpha.2 88 12/14/2023
3.0.0-alpha.1 73 12/13/2023
2.2.2-alpha.4 87 12/11/2023
2.2.2-alpha.3 82 12/10/2023
2.2.2-alpha.2 78 12/10/2023
2.2.2-alpha 313 12/9/2023
2.2.1 451 12/7/2023
2.2.0 330 12/7/2023
2.1.0 397 11/2/2023
2.0.2 615 4/13/2023
2.0.2-rc.1 101 4/10/2023
2.0.2-beta2 528 3/28/2023
2.0.2-beta1 499 3/27/2023
2.0.2-beta.3 98 4/4/2023
1.10.1 638 3/18/2023
1.10.0 644 3/18/2023
1.9.0 651 2/21/2023
1.8.0 729 1/9/2023
1.7.0 834 8/2/2022
1.6.0 788 8/1/2022
1.5.0 817 6/16/2022

v3.0.0 builder.SetOnStateChangedAction(...)