Skip to content

Commit

Permalink
redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
mustaddon committed Oct 1, 2021
1 parent 8bdd648 commit 56d5aca
Show file tree
Hide file tree
Showing 21 changed files with 591 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\StateMachine\StateMachine.csproj" />
<ProjectReference Include="..\..\FluentStateMachine\FluentStateMachine.csproj" />
</ItemGroup>

</Project>
32 changes: 16 additions & 16 deletions Test/ConsoleApp/Program.cs → Examples/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RandomSolutions;
using FluentStateMachine;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp
Expand All @@ -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<State, Event> CreateFsm()
{
return new FsmBuilder<State, Event>(State.S1)
var fsm = new FsmBuilder<State, Event>(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}"))
Expand All @@ -42,6 +29,7 @@ static IStateMachine<State, Event> CreateFsm()
await Task.Delay(1000);
return "some data";
})

.On(Event.E2).JumpTo(State.S2)
.On(Event.E3).JumpTo(State.S3)
.State(State.S2)
Expand All @@ -50,6 +38,18 @@ static IStateMachine<State, Event> 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();
}
}
}
17 changes: 12 additions & 5 deletions StateMachine.sln → FluentStateMachine.sln
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
35 changes: 35 additions & 0 deletions FluentStateMachine/FluentStateMachine.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0;netstandard2.1;</TargetFrameworks>
<AssemblyName>FluentStateMachine</AssemblyName>
<RootNamespace>FluentStateMachine</RootNamespace>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\FluentStateMachine.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<Version>1.0.0</Version>

<Company></Company>
<Authors>Leonid Salavatov</Authors>
<Copyright>Leonid Salavatov 2021</Copyright>
<PackageId>FluentStateMachine</PackageId>
<Product>FluentStateMachine</Product>
<Title>FluentStateMachine</Title>
<Description>.NET Finite-state machine (FSM) with a fluent interface</Description>
<PackageTags>fluent statemachine finitestatemachine fsm dotnet</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageProjectUrl>https://github.com/mustaddon/StateMachine</PackageProjectUrl>
<RepositoryUrl>https://github.com/mustaddon/StateMachine</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<NeutralLanguage />
<PackageReleaseNotes></PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
<DefineConstants>NET45</DefineConstants>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions FluentStateMachine/FrameworkExt.cs
Original file line number Diff line number Diff line change
@@ -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

}
}
7 changes: 5 additions & 2 deletions StateMachine/FsmArgs.cs → FluentStateMachine/FsmArgs.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace RandomSolutions
using System.Threading;

namespace FluentStateMachine
{
public class FsmArgs<TState, TEvent>
{
public IStateMachine<TState, TEvent> Fsm { get; internal set; }
public CancellationToken CancellationToken { get; internal set; }
}

public class FsmResetArgs<TState, TEvent> : FsmArgs<TState, TEvent>
Expand All @@ -12,7 +15,7 @@ public class FsmResetArgs<TState, TEvent> : FsmArgs<TState, TEvent>

public class FsmDataArgs<TState, TEvent> : FsmArgs<TState, TEvent>
{
public object[] Data { get; internal set; }
public object Data { get; internal set; }
}

public class FsmExitArgs<TState, TEvent> : FsmDataArgs<TState, TEvent>
Expand Down
46 changes: 3 additions & 43 deletions StateMachine/FsmBuilder.cs → FluentStateMachine/FsmBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;

namespace RandomSolutions
namespace FluentStateMachine
{
public class FsmBuilder<TState, TEvent>
{
Expand All @@ -13,90 +13,50 @@ public FsmBuilder(FsmModel<TState, TEvent> model)
Model = model;
}

readonly FsmModel<TState, TEvent> Model;

public FsmBuilder<TState, TEvent> OnReset(Action<FsmResetArgs<TState, TEvent>> action)
{
return OnReset(x => { action(x); return FrameworkExt.CompletedTask; });
}
private readonly FsmModel<TState, TEvent> Model;

public FsmBuilder<TState, TEvent> OnReset(Func<FsmResetArgs<TState, TEvent>, Task> action)
{
Model.OnReset = action;
return this;
}

public FsmBuilder<TState, TEvent> OnExit(Action<FsmExitArgs<TState, TEvent>> action)
{
return OnExit(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnExit(Func<FsmExitArgs<TState, TEvent>, Task> action)
{
Model.OnExit = action;
return this;
}

public FsmBuilder<TState, TEvent> OnEnter(Action<FsmEnterArgs<TState, TEvent>> action)
{
return OnEnter(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnEnter(Func<FsmEnterArgs<TState, TEvent>, Task> action)
{
Model.OnEnter = action;
return this;
}

public FsmBuilder<TState, TEvent> OnJump(Action<FsmEnterArgs<TState, TEvent>> action)
{
return OnJump(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnJump(Func<FsmEnterArgs<TState, TEvent>, Task> action)
{
Model.OnJump = action;
return this;
}

public FsmBuilder<TState, TEvent> OnTrigger(Action<FsmTriggerArgs<TState, TEvent>> action)
{
return OnTrigger(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnTrigger(Func<FsmTriggerArgs<TState, TEvent>, Task> action)
{
Model.OnTrigger = action;
return this;
}

public FsmBuilder<TState, TEvent> OnFire(Action<FsmTriggerArgs<TState, TEvent>> action)
{
return OnFire(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnFire(Func<FsmTriggerArgs<TState, TEvent>, Task> action)
{
Model.OnFire = action;
return this;
}

public FsmBuilder<TState, TEvent> OnComplete(Action<FsmCompleteArgs<TState, TEvent>> action)
{
return OnComplete(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnComplete(Func<FsmCompleteArgs<TState, TEvent>, Task> action)
{
Model.OnComplete = action;
return this;
}

public FsmBuilder<TState, TEvent> OnError(Action<FsmErrorArgs<TState, TEvent>> action)
{
return OnError(x => { action(x); return FrameworkExt.CompletedTask; });
}

public FsmBuilder<TState, TEvent> OnError(Func<FsmErrorArgs<TState, TEvent>, Task> action)
{
Model.OnError = action;
Expand Down Expand Up @@ -143,7 +103,7 @@ public IStateMachine<TState, TEvent> Build()
return new StateMachine<TState, TEvent>(Model);
}

const string _startNotContains = "States collection is not contains start point";
private const string _startNotContains = "States collection is not contains start point";
}

}
56 changes: 56 additions & 0 deletions FluentStateMachine/FsmBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;

namespace FluentStateMachine
{
public static class FsmBuilderExtensions
{
public static FsmBuilder<TState, TEvent> OnReset<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmResetArgs<TState, TEvent>> action)
{
return builder.OnReset(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnExit<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmExitArgs<TState, TEvent>> action)
{
return builder.OnExit(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnEnter<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmEnterArgs<TState, TEvent>> action)
{
return builder.OnEnter(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnJump<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmEnterArgs<TState, TEvent>> action)
{
return builder.OnJump(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnTrigger<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmTriggerArgs<TState, TEvent>> action)
{
return builder.OnTrigger(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnFire<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmTriggerArgs<TState, TEvent>> action)
{
return builder.OnFire(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnComplete<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmCompleteArgs<TState, TEvent>> action)
{
return builder.OnComplete(x => { action(x); return FrameworkExt.CompletedTask; });
}

public static FsmBuilder<TState, TEvent> OnError<TState, TEvent>(this FsmBuilder<TState, TEvent> builder,
Action<FsmErrorArgs<TState, TEvent>> action)
{
return builder.OnError(x => { action(x); return FrameworkExt.CompletedTask; });
}

}
}
Loading

0 comments on commit 56d5aca

Please sign in to comment.