diff --git a/Test/ConsoleApp/ConsoleApp.csproj b/Examples/ConsoleApp/ConsoleApp.csproj
similarity index 52%
rename from Test/ConsoleApp/ConsoleApp.csproj
rename to Examples/ConsoleApp/ConsoleApp.csproj
index e1d84f4..3fecda7 100644
--- a/Test/ConsoleApp/ConsoleApp.csproj
+++ b/Examples/ConsoleApp/ConsoleApp.csproj
@@ -2,11 +2,11 @@
Exe
- netcoreapp2.0
+ net6.0
-
+
diff --git a/Test/ConsoleApp/Program.cs b/Examples/ConsoleApp/Program.cs
similarity index 86%
rename from Test/ConsoleApp/Program.cs
rename to Examples/ConsoleApp/Program.cs
index 773316b..c728121 100644
--- a/Test/ConsoleApp/Program.cs
+++ b/Examples/ConsoleApp/Program.cs
@@ -1,5 +1,6 @@
-using RandomSolutions;
+using FluentStateMachine;
using System;
+using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp
@@ -11,21 +12,7 @@ enum Event { E0, E1, E2, E3 }
static async Task Main(string[] args)
{
- var fsm = CreateFsm();
- var events = new[] { Event.E1, Event.E2, Event.E0, Event.E1, Event.E3 };
-
- foreach (var e in events)
- {
- Console.WriteLine($"{fsm.Current}: {string.Join(", ", await fsm.GetEventsAsync())}");
- Console.WriteLine($"Result: {await fsm.TriggerAsync(e)}\n");
- }
-
- await fsm.ResetAsync();
- }
-
- static IStateMachine CreateFsm()
- {
- return new FsmBuilder(State.S1)
+ var fsm = new FsmBuilder(State.S1)
.OnJump(x => Console.WriteLine($"On jump to {x.Fsm.Current} from {x.PrevState}"))
.OnReset(x => Console.WriteLine($"On reset to {x.Fsm.Current} from {x.PrevState}"))
.OnTrigger(x => Console.WriteLine($"On trigger {x.Event}"))
@@ -42,6 +29,7 @@ static IStateMachine CreateFsm()
await Task.Delay(1000);
return "some data";
})
+
.On(Event.E2).JumpTo(State.S2)
.On(Event.E3).JumpTo(State.S3)
.State(State.S2)
@@ -50,6 +38,18 @@ static IStateMachine CreateFsm()
.State(State.S3)
.OnEnter(x => Console.WriteLine($"Final state"))
.Build();
+
+
+ var events = new[] { Event.E1, Event.E2, Event.E0, Event.E1, Event.E3 };
+
+ foreach (var e in events)
+ {
+ Console.WriteLine($"Current state: {fsm.Current}");
+ Console.WriteLine($"Available events: {string.Join(", ", fsm.GetEvents())}");
+ Console.WriteLine($"Result: {await fsm.TriggerAsync(e)}\n\n");
+ }
+
+ await fsm.ResetAsync();
}
}
}
diff --git a/StateMachine.sln b/FluentStateMachine.sln
similarity index 64%
rename from StateMachine.sln
rename to FluentStateMachine.sln
index d0e33bf..96bd9f7 100644
--- a/StateMachine.sln
+++ b/FluentStateMachine.sln
@@ -1,13 +1,20 @@
Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 15
-VisualStudioVersion = 15.0.27703.2018
+# Visual Studio Version 17
+VisualStudioVersion = 17.0.31717.71
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StateMachine", "StateMachine\StateMachine.csproj", "{D6D3C3E9-73EF-48E9-9D82-017BA0123C24}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentStateMachine", "FluentStateMachine\FluentStateMachine.csproj", "{D6D3C3E9-73EF-48E9-9D82-017BA0123C24}"
EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "Test\ConsoleApp\ConsoleApp.csproj", "{EA950F42-CB53-4119-8AB9-D9305AE8A32A}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "Examples\ConsoleApp\ConsoleApp.csproj", "{EA950F42-CB53-4119-8AB9-D9305AE8A32A}"
EndProject
-Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Test", "Test", "{4C1BDDED-BF17-4F95-B295-4AC93351993A}"
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{4C1BDDED-BF17-4F95-B295-4AC93351993A}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{35BB4148-B10F-4F43-833D-8FD5169DDA63}"
+ ProjectSection(SolutionItems) = preProject
+ .gitignore = .gitignore
+ LICENSE = LICENSE
+ README.md = README.md
+ EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/StateMachine/key.snk b/FluentStateMachine.snk
similarity index 100%
rename from StateMachine/key.snk
rename to FluentStateMachine.snk
diff --git a/FluentStateMachine/FluentStateMachine.csproj b/FluentStateMachine/FluentStateMachine.csproj
new file mode 100644
index 0000000..241f375
--- /dev/null
+++ b/FluentStateMachine/FluentStateMachine.csproj
@@ -0,0 +1,35 @@
+
+
+
+ net45;netstandard2.0;netstandard2.1;
+ FluentStateMachine
+ FluentStateMachine
+ true
+ ..\FluentStateMachine.snk
+ 1.0.0
+ 1.0.0
+ 1.0.0
+
+
+ Leonid Salavatov
+ Leonid Salavatov 2021
+ FluentStateMachine
+ FluentStateMachine
+ FluentStateMachine
+ .NET Finite-state machine (FSM) with a fluent interface
+ fluent statemachine finitestatemachine fsm dotnet
+ true
+ https://github.com/mustaddon/StateMachine
+ https://github.com/mustaddon/StateMachine
+ git
+ false
+ MIT
+
+
+
+
+
+ NET45
+
+
+
diff --git a/FluentStateMachine/FrameworkExt.cs b/FluentStateMachine/FrameworkExt.cs
new file mode 100644
index 0000000..c4a16ab
--- /dev/null
+++ b/FluentStateMachine/FrameworkExt.cs
@@ -0,0 +1,15 @@
+using System.Threading.Tasks;
+
+namespace FluentStateMachine
+{
+ internal static class FrameworkExt
+ {
+
+#if NET45
+ public static readonly Task CompletedTask = Task.FromResult(false);
+#else
+ public static readonly Task CompletedTask = Task.CompletedTask;
+#endif
+
+ }
+}
diff --git a/StateMachine/FsmArgs.cs b/FluentStateMachine/FsmArgs.cs
similarity index 85%
rename from StateMachine/FsmArgs.cs
rename to FluentStateMachine/FsmArgs.cs
index fc13e8d..bd70c7c 100644
--- a/StateMachine/FsmArgs.cs
+++ b/FluentStateMachine/FsmArgs.cs
@@ -1,8 +1,11 @@
-namespace RandomSolutions
+using System.Threading;
+
+namespace FluentStateMachine
{
public class FsmArgs
{
public IStateMachine Fsm { get; internal set; }
+ public CancellationToken CancellationToken { get; internal set; }
}
public class FsmResetArgs : FsmArgs
@@ -12,7 +15,7 @@ public class FsmResetArgs : FsmArgs
public class FsmDataArgs : FsmArgs
{
- public object[] Data { get; internal set; }
+ public object Data { get; internal set; }
}
public class FsmExitArgs : FsmDataArgs
diff --git a/StateMachine/FsmBuilder.cs b/FluentStateMachine/FsmBuilder.cs
similarity index 62%
rename from StateMachine/FsmBuilder.cs
rename to FluentStateMachine/FsmBuilder.cs
index 5c1fd23..d9baa12 100644
--- a/StateMachine/FsmBuilder.cs
+++ b/FluentStateMachine/FsmBuilder.cs
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
-namespace RandomSolutions
+namespace FluentStateMachine
{
public class FsmBuilder
{
@@ -13,12 +13,7 @@ public FsmBuilder(FsmModel model)
Model = model;
}
- readonly FsmModel Model;
-
- public FsmBuilder OnReset(Action> action)
- {
- return OnReset(x => { action(x); return FrameworkExt.CompletedTask; });
- }
+ private readonly FsmModel Model;
public FsmBuilder OnReset(Func, Task> action)
{
@@ -26,77 +21,42 @@ public FsmBuilder OnReset(Func, Tas
return this;
}
- public FsmBuilder OnExit(Action> action)
- {
- return OnExit(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnExit(Func, Task> action)
{
Model.OnExit = action;
return this;
}
- public FsmBuilder OnEnter(Action> action)
- {
- return OnEnter(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnEnter(Func, Task> action)
{
Model.OnEnter = action;
return this;
}
- public FsmBuilder OnJump(Action> action)
- {
- return OnJump(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnJump(Func, Task> action)
{
Model.OnJump = action;
return this;
}
- public FsmBuilder OnTrigger(Action> action)
- {
- return OnTrigger(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnTrigger(Func, Task> action)
{
Model.OnTrigger = action;
return this;
}
- public FsmBuilder OnFire(Action> action)
- {
- return OnFire(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnFire(Func, Task> action)
{
Model.OnFire = action;
return this;
}
- public FsmBuilder OnComplete(Action> action)
- {
- return OnComplete(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnComplete(Func, Task> action)
{
Model.OnComplete = action;
return this;
}
- public FsmBuilder OnError(Action> action)
- {
- return OnError(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmBuilder OnError(Func, Task> action)
{
Model.OnError = action;
@@ -143,7 +103,7 @@ public IStateMachine Build()
return new StateMachine(Model);
}
- const string _startNotContains = "States collection is not contains start point";
+ private const string _startNotContains = "States collection is not contains start point";
}
}
diff --git a/FluentStateMachine/FsmBuilderExtensions.cs b/FluentStateMachine/FsmBuilderExtensions.cs
new file mode 100644
index 0000000..29c8e53
--- /dev/null
+++ b/FluentStateMachine/FsmBuilderExtensions.cs
@@ -0,0 +1,56 @@
+using System;
+
+namespace FluentStateMachine
+{
+ public static class FsmBuilderExtensions
+ {
+ public static FsmBuilder OnReset(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnReset(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnExit(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnExit(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnEnter(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnEnter(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnJump(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnJump(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnTrigger(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnTrigger(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnFire(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnFire(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnComplete(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnComplete(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ public static FsmBuilder OnError(this FsmBuilder builder,
+ Action> action)
+ {
+ return builder.OnError(x => { action(x); return FrameworkExt.CompletedTask; });
+ }
+
+ }
+}
diff --git a/StateMachine/FsmConfig.cs b/FluentStateMachine/FsmConfig.cs
similarity index 64%
rename from StateMachine/FsmConfig.cs
rename to FluentStateMachine/FsmConfig.cs
index 0397193..49f6fd8 100644
--- a/StateMachine/FsmConfig.cs
+++ b/FluentStateMachine/FsmConfig.cs
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
-namespace RandomSolutions
+namespace FluentStateMachine
{
public class FsmConfig
{
@@ -35,33 +35,18 @@ public FsmEventConfig On(TEvent e)
};
}
- public FsmStateConfig OnEnter(Action> action)
- {
- return OnEnter(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmStateConfig OnEnter(Func, Task> action)
{
Model.OnEnter = action;
return this;
}
- public FsmStateConfig OnExit(Action> action)
- {
- return OnExit(x => { action(x); return FrameworkExt.CompletedTask; });
- }
-
public FsmStateConfig OnExit(Func, Task> action)
{
Model.OnExit = action;
return this;
}
- public FsmStateConfig Enable(Func, bool> fn)
- {
- return Enable(x => Task.FromResult(fn(x)));
- }
-
public FsmStateConfig Enable(Func, Task> fn)
{
Model.Enable = fn;
@@ -77,38 +62,18 @@ public class FsmEventConfig : FsmConfig
public FsmEventConfig On(TEvent e) => Parent?.On(e) ?? Root.On(e);
- public FsmEventConfig Execute(Func, object> fn)
- {
- return Execute(x => Task.FromResult(fn(x)));
- }
-
public FsmEventConfig Execute(Func, Task