Skip to content

Commit

Permalink
0.13.x
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolaj-milewski committed Aug 22, 2024
1 parent 147453a commit 12fbdf3
Show file tree
Hide file tree
Showing 185 changed files with 1,421 additions and 744 deletions.
26 changes: 16 additions & 10 deletions Core/Stateflows.Common/Activities/Classes/ActivityWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Stateflows.Activities;
using Stateflows.Activities.Events;
using System.Linq;

namespace Stateflows.Common.Activities.Classes
{
Expand All @@ -18,15 +16,23 @@ public ActivityWrapper(IBehavior consumer)
Behavior = consumer;
}

public Task<RequestResult<ExecutionResponse>> ExecuteAsync(Event initializationEvent, IEnumerable<object> inputTokens = null)
=> Behavior.RequestAsync(new ExecutionRequest()
{
InitializationEvent = initializationEvent,
InputTokens = inputTokens ?? new object[0],
});
public Task<RequestResult<ExecutionResponse>> ExecuteAsync(Event initializationEvent, Action<IInputContainer> inputBuilder = null)
{
var request = new ExecutionRequest() { InitializationEvent = initializationEvent };

inputBuilder?.Invoke(request);

return Behavior.RequestAsync(request);
}

public Task<RequestResult<ExecutionResponse>> ExecuteAsync(IEnumerable<object> inputTokens = null)
=> Behavior.RequestAsync(new ExecutionRequest() { InputTokens = inputTokens ?? new object[0] });
public Task<RequestResult<ExecutionResponse>> ExecuteAsync(Action<IInputContainer> inputBuilder = null)
{
var request = new ExecutionRequest();

inputBuilder?.Invoke(request);

return Behavior.RequestAsync(request);
}

public Task<SendResult> SendAsync<TEvent>(TEvent @event)
where TEvent : Event, new()
Expand Down
23 changes: 19 additions & 4 deletions Core/Stateflows.Common/Activities/Events/ExecutionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
using Newtonsoft.Json;
using Stateflows.Common;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;
using Stateflows.Common;
using Stateflows.Common.Utilities;

namespace Stateflows.Activities.Events
{
public sealed class ExecutionRequest : Request<ExecutionResponse>
public interface IInputContainer
{
void AddInputToken<TToken>(TToken token);

void AddInputTokens<TToken>(IEnumerable<TToken> tokens);
}

public sealed class ExecutionRequest : Request<ExecutionResponse>, IInputContainer
{
public Event InitializationEvent { get; set; } = new Initialize();

[JsonProperty(TypeNameHandling = TypeNameHandling.None)]
public IEnumerable<object> InputTokens { get; set; }
public List<TokenHolder> InputTokens { get; set; } = new List<TokenHolder>();

public void AddInputToken<TToken>(TToken token)
=> InputTokens.Add(new TokenHolder<TToken>() { Payload = token });

public void AddInputTokens<TToken>(IEnumerable<TToken> tokens)
=> InputTokens.AddRange(tokens.Select(token => new TokenHolder<TToken>() { Payload = token }));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Stateflows.Common;
using Stateflows.Activities.Events;

namespace Stateflows.Activities
{
public interface IActivityBehavior : IBehavior
{
Task<RequestResult<ExecutionResponse>> ExecuteAsync(Event initializationEvent, IEnumerable<object> inputTokens = null);
Task<RequestResult<ExecutionResponse>> ExecuteAsync(IEnumerable<object> inputTokens = null);
Task<RequestResult<ExecutionResponse>> ExecuteAsync(Event initializationEvent, Action<IInputContainer> inputBuilder = null);
Task<RequestResult<ExecutionResponse>> ExecuteAsync(Action<IInputContainer> inputBuilder = null);
}
}
18 changes: 9 additions & 9 deletions Core/Stateflows.Common/Events/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class Event
public virtual string Name => GetType().GetEventName();
}

//public class Event<TPayload> : Event
//{
// public Event()
// {
// Payload = default;
// }

// public TPayload Payload { get; set; }
//}
public class Event<TPayload> : Event
{
public Event()
{
Payload = default;
}

public TPayload Payload { get; set; }
}

public static class EventInfo<TEvent>
where TEvent : Event, new()
Expand Down
3 changes: 3 additions & 0 deletions Core/Stateflows.Common/Interfaces/IBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Task<RequestResult<CompoundResponse>> SendCompoundAsync(params Event[] events)
Task<RequestResult<TResponse>> RequestAsync<TResponse>(Request<TResponse> request)
where TResponse : Response, new();

Task<RequestResult<FinalizationResponse>> FinalizeAsync()
=> RequestAsync(new FinalizationRequest());

Task<RequestResult<ResetResponse>> ResetAsync(ResetMode resetMode = ResetMode.Full)
=> RequestAsync(new ResetRequest() { Mode = resetMode });

Expand Down
4 changes: 4 additions & 0 deletions Core/Stateflows.Common/Utilities/StateflowsJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public static T CloneObject<T>(T value)
where T : class
=> DeserializeObject(SerializePolymorphicObject(value)) as T;

[DebuggerStepThrough]
public static T Clone<T>(T value)
=> (T)DeserializeObject(SerializePolymorphicObject(value));

/// <summary>
/// Serializes the specified object to a JSON string.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Task IStateMachineObserver.AfterTransitionEffectAsync(ITransitionContext<Event>
return Task.CompletedTask;
}

Task IStateMachineObserver.AfterTransitionGuardAsync(IGuardContext<Event> context, bool guardResult)
Task IStateMachineObserver.AfterTransitionGuardAsync(ITransitionContext<Event> context, bool guardResult)
{
SequenceBuilder.TransitionGuard(context.Event.Name, context.SourceState.Name, context.TargetState?.Name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,4 @@
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Stateflows.Common\Stateflows.Common.csproj" />
</ItemGroup>

</Project>
9 changes: 5 additions & 4 deletions Core/Stateflows/Activities/ActivitiesDependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Stateflows.Common;
using Stateflows.Common.Interfaces;
using Stateflows.Common.Registration.Interfaces;
using Stateflows.Activities.Engine;
using Stateflows.Activities.Context;
using Stateflows.Activities.Registration;
using Stateflows.Activities.EventHandlers;
using Stateflows.Activities.Registration.Builders;
using Stateflows.Activities.Registration.Interfaces;
using System;
using Stateflows.Activities.Context;
using Stateflows.Activities.Context.Interfaces;

namespace Stateflows.Activities
{
Expand Down Expand Up @@ -48,6 +48,7 @@ private static ActivitiesRegister EnsureActivitiesServices(this IStateflowsBuild
.AddSingleton<IActivityEventHandler, SubscriptionHandler>()
.AddSingleton<IActivityEventHandler, UnsubscriptionHandler>()
.AddSingleton<IActivityEventHandler, NotificationsHandler>()
.AddSingleton<IActivityEventHandler, SetGlobalValuesHandler>()
.AddTransient(provider =>
ContextHolder.ActivityContext.Value ??
throw new InvalidOperationException($"No service for type '{typeof(IActivityContext).FullName}' is available in this context.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Stateflows.Activities
{
public sealed class IterativeActivityNode<TToken> : IBaseStructuredActivityNode
public sealed class IterativeActivityNode<TToken> : IStructuredActivityNode
{
public static string Name => ActivityNode<IterativeActivityNode<TToken>>.Name;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/Stateflows/Activities/Classes/ParalellActivityNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Stateflows.Activities
{
public sealed class ParallelActivityNode<TToken> : IBaseStructuredActivityNode
public sealed class ParallelActivityNode<TToken> : IStructuredActivityNode
{
public static string Name => ActivityNode<ParallelActivityNode<TToken>>.Name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Stateflows.Activities
{
public sealed class StructuredActivityNode : ActivityNode, IBaseStructuredActivityNode
public sealed class StructuredActivityNode : ActivityNode, IStructuredActivityNode
{
public static string Name => ActivityNode<StructuredActivityNode>.Name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
using Stateflows.Activities.Engine;
using Stateflows.Activities.Context.Interfaces;
using Stateflows.Activities.Inspection.Interfaces;
using System.Collections.Generic;
using Stateflows.Activities.Extensions;
using System.Linq;
using Stateflows.Utils;

namespace Stateflows.Activities.Context.Classes
{
Expand All @@ -11,34 +15,44 @@ internal class ActivityInitializationContext<TInitializationEvent> :
IRootContext
where TInitializationEvent : Event, new()
{
public ActivityInitializationContext(RootContext context, NodeScope nodeScope, TInitializationEvent initializationEvent)
public ActivityInitializationContext(RootContext context, NodeScope nodeScope, TInitializationEvent initializationEvent, List<TokenHolder> inputTokens)
: base(context, nodeScope)
{
InitializationEvent = initializationEvent;
InputTokens = inputTokens ?? new List<TokenHolder>();
}

public ActivityInitializationContext(BaseContext context, TInitializationEvent initializationEvent)
public ActivityInitializationContext(BaseContext context, TInitializationEvent initializationEvent, List<TokenHolder> inputTokens)
: base(context)
{
InitializationEvent = initializationEvent;
InputTokens = inputTokens ?? new List<TokenHolder>();
}

IActivityContext IActivityActionContext.Activity => Activity;

public TInitializationEvent InitializationEvent { get; }

public List<TokenHolder> InputTokens;

public void Output<TToken>(TToken token)
=> OutputRange(new TToken[] { token });

public void OutputRange<TToken>(IEnumerable<TToken> tokens)
=> InputTokens.AddRange(tokens.Select(token => token.ToTokenHolder()).ToArray());
}

internal class ActivityInitializationContext :
ActivityInitializationContext<Event>,
IActivityInitializationInspectionContext,
IRootContext
{
public ActivityInitializationContext(BaseContext context, Event initializationEvent)
: base(context, initializationEvent)
public ActivityInitializationContext(BaseContext context, Event initializationEvent, List<TokenHolder> inputTokens)
: base(context, initializationEvent, inputTokens)
{ }

public ActivityInitializationContext(RootContext context, NodeScope nodeScope, Event initializationEvent)
: base(context, nodeScope, initializationEvent)
public ActivityInitializationContext(RootContext context, NodeScope nodeScope, Event initializationEvent, List<TokenHolder> inputTokens)
: base(context, nodeScope, initializationEvent, inputTokens)
{ }

IActivityInspectionContext IActivityInitializationInspectionContext.Activity => Activity;
Expand Down
2 changes: 1 addition & 1 deletion Core/Stateflows/Activities/Context/ContextHolder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Threading;
using Stateflows.Activities.Context.Interfaces;
using Stateflows.Common;

namespace Stateflows.Activities.Context
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public interface ITypedActionContext : IActivityNodeContext
{ }

public interface IActionContext : ITypedActionContext, IInput, IOutput
public interface IActionContext : ITypedActionContext, IInput, IActionOutput
{ }

public interface IActionContext<out TToken> : IActionContext, ITokenContext<TToken>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ public interface IOutput
void Output<TToken>(TToken token);

void OutputRange<TToken>(IEnumerable<TToken> tokens);
}

public interface IActionOutput : IOutput
{
void PassTokensOfTypeOn<TToken>();

void PassAllTokensOn();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
using System;

namespace Stateflows.Activities.Context.Interfaces
namespace Stateflows.Activities.Context.Interfaces
{
public interface IFlowContext
{
ISourceNodeContext SourceNode { get; }

INodeContext TargetNode { get; }

Type TokenType { get; }

int Weight { get; }
}

public interface IIncomingFlowContext : IFlowContext
{
bool Activated { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stateflows.Activities.Context.Interfaces
{
public interface IActivityInitializationContext<out TInitializationRequest> : IActivityActionContext
public interface IActivityInitializationContext<out TInitializationRequest> : IActivityActionContext, IOutput
where TInitializationRequest : Event
{
TInitializationRequest InitializationEvent { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

namespace Stateflows.Activities.Context.Interfaces
{
public interface IExceptionContext
{
INodeContext ProtectedNode { get; }

INodeContext NodeOfOrigin { get; }
}

public interface IExceptionHandlerContext<out TException> : IExceptionContext, IActivityNodeContext, IOutput
public interface IExceptionHandlerContext<out TException> : IExceptionContext, IActivityNodeContext, IActionOutput
where TException : Exception
{
TException Exception { get; }
Expand Down
Loading

0 comments on commit 12fbdf3

Please sign in to comment.