Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/scope per event #28

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions Core/Stateflows/StateMachines/Engine/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ internal sealed class Executor : IDisposable

public StateMachinesRegister Register { get; set; }

public IServiceProvider ServiceProvider => Scope.ServiceProvider;
public IServiceProvider ServiceProvider => ScopesStack.Peek().ServiceProvider;

private readonly IServiceScope Scope;
//private readonly IServiceScope Scope;
private readonly Stack<IServiceScope> ScopesStack = new Stack<IServiceScope>();

public Executor(StateMachinesRegister register, Graph graph, IServiceProvider serviceProvider, StateflowsContext stateflowsContext, Event @event)
{
Register = register;
Scope = serviceProvider.CreateScope();
ScopesStack.Push(serviceProvider.CreateScope());
Graph = graph;
Context = new RootContext(stateflowsContext, this, @event);
var logger = ServiceProvider.GetService<ILogger<Executor>>();
Expand All @@ -41,7 +42,18 @@ public Executor(StateMachinesRegister register, Graph graph, IServiceProvider se

public void Dispose()
{
Scope.Dispose();
//Scope.Dispose();
}

public void BeginScope()
{
ScopesStack.Push(ServiceProvider.CreateScope());
}

public void EndScope()
{
var scope = ScopesStack.Pop();
scope.Dispose();
}

public readonly RootContext Context;
Expand Down Expand Up @@ -332,6 +344,8 @@ private async Task<EventStatus> DoProcessAsync<TEvent>(TEvent @event)
private async Task<bool> DoGuardAsync<TEvent>(Edge edge)
where TEvent : Event, new()
{
BeginScope();

var context = new GuardContext<TEvent>(Context, edge);

await Inspector.BeforeTransitionGuardAsync(context);
Expand All @@ -340,23 +354,30 @@ private async Task<bool> DoGuardAsync<TEvent>(Edge edge)

await Inspector.AfterGuardAsync(context, result);


return result;
}

private async Task DoEffectAsync<TEvent>(Edge edge)
where TEvent : Event, new()
{
BeginScope();

var context = new TransitionContext<TEvent>(Context, edge);

await Inspector.BeforeEffectAsync(context);

await edge.Effects.WhenAll(Context);

await Inspector.AfterEffectAsync(context);

EndScope();
}

public async Task<bool> DoInitializeStateMachineAsync(InitializationRequest @event)
{
BeginScope();

var result = false;

if (
Expand Down Expand Up @@ -384,57 +405,79 @@ public async Task<bool> DoInitializeStateMachineAsync(InitializationRequest @eve
await Inspector.AfterStateMachineInitializeAsync(context);
}

EndScope();

return result;
}

public async Task DoFinalizeStateMachineAsync()
{
BeginScope();

var context = new StateMachineActionContext(Context);
await Inspector.BeforeStateMachineFinalizeAsync(context);

await Graph.Finalize.WhenAll(Context);

await Inspector.AfterStateMachineFinalizeAsync(context);

EndScope();
}

public async Task DoInitializeStateAsync(Vertex vertex)
{
BeginScope();

var context = new StateActionContext(Context, vertex, Constants.Initialize);
await Inspector.BeforeStateInitializeAsync(context);

await vertex.Initialize.WhenAll(Context);

await Inspector.AfterStateInitializeAsync(context);

EndScope();
}

public async Task DoFinalizeStateAsync(Vertex vertex)
{
BeginScope();

var context = new StateActionContext(Context, vertex, Constants.Finalize);
await Inspector.BeforeStateFinalizeAsync(context);

await vertex.Finalize.WhenAll(Context);

await Inspector.AfterStateFinalizeAsync(context);

EndScope();
}

public async Task DoEntryAsync(Vertex vertex)
{
BeginScope();

var context = new StateActionContext(Context, vertex, Constants.Entry);
await Inspector.BeforeStateEntryAsync(context);

await vertex.Entry.WhenAll(Context);

await Inspector.AfterStateEntryAsync(context);

EndScope();
}

private async Task DoExitAsync(Vertex vertex)
{
BeginScope();

var context = new StateActionContext(Context, vertex, Constants.Exit);
await Inspector.BeforeStateExitAsync(context);

await vertex.Exit.WhenAll(Context);

await Inspector.AfterStateExitAsync(context);

EndScope();
}

private async Task DoConsumeAsync<TEvent>(Edge edge)
Expand Down
42 changes: 33 additions & 9 deletions Core/Stateflows/StateMachines/Engine/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,19 @@ public async Task<EventStatus> ProcessEventAsync<TEvent>(BehaviorId id, TEvent @

executor.Context.SetEvent(ev);

var status = await ExecuteBehaviorAsync(ev, result, stateflowsContext, graph, executor);

results.Add(new RequestResult(ev, ev.GetResponse(), status, new EventValidation(true, new List<ValidationResult>())));

executor.Context.ClearEvent();
executor.BeginScope();
try
{
var status = await ExecuteBehaviorAsync(ev, result, stateflowsContext, graph, executor);

results.Add(new RequestResult(ev, ev.GetResponse(), status, new EventValidation(true, new List<ValidationResult>())));
}
finally
{
executor.EndScope();

executor.Context.ClearEvent();
}
}

compoundRequest.Respond(new CompoundResponse()
Expand All @@ -90,7 +98,15 @@ public async Task<EventStatus> ProcessEventAsync<TEvent>(BehaviorId id, TEvent @
}
else
{
result = await ExecuteBehaviorAsync(@event, result, stateflowsContext, graph, executor);
executor.BeginScope();
try
{
result = await ExecuteBehaviorAsync(@event, result, stateflowsContext, graph, executor);
}
finally
{
executor.EndScope();
}
}

await executor.DehydrateAsync();
Expand All @@ -116,9 +132,17 @@ public async Task<EventStatus> ProcessEventAsync<TEvent>(BehaviorId id, TEvent @
{
executor.Context.SetEvent(initializationRequest);

await executor.InitializeAsync(initializationRequest);

executor.Context.ClearEvent();
executor.BeginScope();
try
{
await executor.InitializeAsync(initializationRequest);
}
finally
{
executor.EndScope();

executor.Context.ClearEvent();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions Examples/Examples.Common/SomeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Examples.Common
public class SomeEvent : Event
{
public string TheresSomethingHappeningHere { get; set; } = "What it is ain't exactly clear";
public int DelaySize { get; set; }
}
}
96 changes: 96 additions & 0 deletions Tests/Activity.IntegrationTests/Tests/ServiceScopes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Activity.IntegrationTests.Classes.Tokens;
using Microsoft.Extensions.DependencyInjection;
using Stateflows.Activities.Typed;
using Stateflows.Common;
using StateMachine.IntegrationTests.Utils;
using System.Runtime.CompilerServices;

namespace Activity.IntegrationTests.Tests
{
public class Service
{
public Service()
{
Value = Random.Shared.Next().ToString();
}

public readonly string Value;
}

public class ScopeAction1 : ActionNode
{
private readonly Service service;
public ScopeAction1(Service service)
{
this.service = service;
}

public override Task ExecuteAsync()
{
ServiceScopes.Value1 = service.Value;

return Task.CompletedTask;
}
}

public class ScopeAction2 : ActionNode
{
private readonly Service service;
public ScopeAction2(Service service)
{
this.service = service;
}

public override Task ExecuteAsync()
{
ServiceScopes.Value1 = service.Value;

return Task.CompletedTask;
}
}

[TestClass]
public class ServiceScopes : StateflowsTestClass
{
public static string Value1 = string.Empty;
public static string Value2 = string.Empty;

[TestInitialize]
public override void Initialize()
=> base.Initialize();

[TestCleanup]
public override void Cleanup()
=> base.Cleanup();

protected override void InitializeStateflows(IStateflowsBuilder builder)
{
builder
.AddActivities(b => b
.AddActivity("scopes", b => b
.AddInitial(b => b
.AddControlFlow<ScopeAction1>()
)
.AddAction<ScopeAction1>(b => b
.AddControlFlow<ScopeAction2>()
)
.AddAction<ScopeAction2>()
)
)

.ServiceCollection.AddScoped<Service>()
;
}

[TestMethod]
public async Task SeparateScopesOnActions()
{
if (ActivityLocator.TryLocateActivity(new ActivityId("scopes", "x"), out var a))
{
await a.InitializeAsync();
}

Assert.AreNotEqual(Value1, Value2);
}
}
}
Loading
Loading