IegTools.Sequencer
3.0.0-alpha.5
See the version list below for details.
dotnet add package IegTools.Sequencer --version 3.0.0-alpha.5
NuGet\Install-Package IegTools.Sequencer -Version 3.0.0-alpha.5
<PackageReference Include="IegTools.Sequencer" Version="3.0.0-alpha.5" />
paket add IegTools.Sequencer --version 3.0.0-alpha.5
#r "nuget: IegTools.Sequencer, 3.0.0-alpha.5"
// Install IegTools.Sequencer as a Cake Addin #addin nuget:?package=IegTools.Sequencer&version=3.0.0-alpha.5&prerelease // Install IegTools.Sequencer as a Cake Tool #tool nuget:?package=IegTools.Sequencer&version=3.0.0-alpha.5&prerelease
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# 11.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
Table of Contents
Installation
Usage
States
State Tags
Validation
Handler
SequenceBuilder Extensions
Extensibility
Version Changes
Breaking Changes
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 ISequence _sequence;
public OnTimerExample() =>
_sequence = SequenceConfig.Build();
public void In(bool value)
{
LastValue = value;
_sequence.Run();
}
private ISequenceBuilder SequenceConfig =>
SequenceBuilder.Create()
.AddForceState(">Off", () => !LastValue)
.AddTransition(">Off", "PrepareOn", () => LastValue, () => _sequence.Stopwatch.Restart())
.AddTransition("PrepareOn", "!On", () => _sequence.Stopwatch.Expired(MyTimeSpan));
}
Configuration .NET 5 style
A more complex example configuration for a pump-anti-sticking-sequence:
private ISequenceBuilder SequenceConfig =>
SequenceBuilder.Configure(builder =>
{
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.Expired(_settings.RunTime * _countStarts.Factorial()),
() =>
{
Stopwatch.Restart();
_countStarts++;
});
builder.AddTransition("Pump off", "Pump on",
() => Stopwatch.Expired(_settings.PauseTime) && !sequenceDone());
builder.AddTransition("Pump off", ">Paused",
() => Stopwatch.Expired(_settings.PauseTime) && sequenceDone(),
() => _onTimer.In(false));
bool sequenceDone() => _countStarts > _settings.PumpStartQuantity;
});
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)
States
States can be defined as strings or enums, internally they will be stored as strings.
State Tags
[I tend to delete this feature, it's to much magic, the Initial-State and ignored States should be defined explicit.]
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 an 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 an state to tell the Sequence what state to start from.
Example:
builder.AddForceState(">Paused", () => !_onTimer.Out);
Validation
The sequence will be validated on build.
_sequence = builder.Build();
Validation Handler:
- InitialState must be defined
- The InitialState must have an counterpart in a StateTransition
- The Sequence must have at least two steps
- Each 'NextStep' must have a counterpart StateTransition with an matching 'CurrentState'
- Each 'CurrentState' must have a counterpart StateTransition with an 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", ...);
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:
- The StateTransitionHandler
- The ContainsStateTransitionHandler
- The AnyStateTransitionHandler
- The ForceStateHandler
- The StateActionHandler
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.
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.
Extensibility
Write your own customized
- Handler
- Sequence
- and Validator
TODO Documentation
Version Changes
v2.1 → v2.2
- new builder.SetLogger(...) methods
v2.0 → v2.1
- new ExtensionMethod .AllowOnceIn(timeSpan)
Breaking Changes
so far none
Product | Versions 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. |
-
.NETStandard 2.0
- FluentValidation (>= 11.8.1)
- JetBrains.Annotations (>= 2023.3.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
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 | 85 | 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-alpha.5 builder.SetOnStateChangedAction(...)