Skip to content

Commit

Permalink
move Handler and Validators to SequenceData, rework all Validators
Browse files Browse the repository at this point in the history
  • Loading branch information
egreiner committed Dec 14, 2023
1 parent 2058b46 commit fffdf1a
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static class AllowOnlyOnceInExtension
/// <param name="timeSpan">The timespan in which the execution of the transition action is is allowed only once</param>
public static ISequenceBuilder AllowOnlyOnceIn(this ISequenceBuilder builder, TimeSpan timeSpan)
{
var lastHandler = builder.Configuration.Handler.LastOrDefault();
var lastHandler = builder.Data.Handler.LastOrDefault();
if (lastHandler is not null)
lastHandler.AllowOnlyOnceIn(timeSpan);

Expand Down
2 changes: 1 addition & 1 deletion src/IegTools.Sequencer/ISequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface ISequence
/// <summary>
/// Set the sequence-configuration
/// </summary>
ISequence SetConfiguration(SequenceConfiguration configuration);
ISequence SetConfiguration(SequenceConfiguration configuration, SequenceData data);


/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/IegTools.Sequencer/ISequenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public interface ISequenceBuilder
/// </summary>
SequenceConfiguration Configuration { get; init; }

/// <summary>
/// The sequence data
/// </summary>
SequenceData Data { get; }


/// <summary>
/// Activates debug logging messages
Expand Down
9 changes: 6 additions & 3 deletions src/IegTools.Sequencer/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/// </summary>
public class Sequence : ISequence
{
private SequenceData _data;

/// <inheritdoc />
public SequenceConfiguration Configuration { get; private set; }

Expand All @@ -34,13 +36,13 @@ public class Sequence : ISequence

/// <inheritdoc />
public bool IsRegisteredState(string state) =>
Configuration.Handler.Any(x => x.IsRegisteredState(state));
_data.Handler.Any(x => x.IsRegisteredState(state));


/// <inheritdoc />
public virtual ISequence Run()
{
foreach (var handler in Configuration.Handler)
foreach (var handler in _data.Handler)
{
var executed = handler.ExecuteIfValid(this);
if (executed && !handler.ResumeSequence)
Expand All @@ -55,9 +57,10 @@ public virtual ISequence Run()


/// <inheritdoc />
public ISequence SetConfiguration(SequenceConfiguration configuration)
public ISequence SetConfiguration(SequenceConfiguration configuration, SequenceData data)
{
Configuration = configuration;
_data = data;
CurrentState = configuration.InitialState;
return this;
}
Expand Down
23 changes: 12 additions & 11 deletions src/IegTools.Sequencer/SequenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
/// </summary>
public class SequenceBuilder : ISequenceBuilder
{
private readonly IValidator<SequenceConfiguration> _validator;
private readonly IValidator<SequenceBuilder> _validator;

private SequenceBuilder(IValidator<SequenceConfiguration> validator) =>
private SequenceBuilder(IValidator<SequenceBuilder> validator) =>
_validator = validator;


/// <inheritdoc />
public SequenceConfiguration Configuration { get; init; } = new();

/// <inheritdoc />
public SequenceData Data { get; } = new();

/// <inheritdoc />
public ISequenceBuilder ActivateDebugLogging(ILogger logger, EventId eventId, Func<IDisposable> loggerScope = null)
public ISequenceBuilder ActivateDebugLogging(ILogger logger, EventId eventId, Func<IDisposable>? loggerScope = null)
{
Configuration.Logger = logger;
Configuration.EventId = eventId;
Expand All @@ -43,17 +45,17 @@ public ISequenceBuilder ActivateDebugLogging(ILogger logger, EventId eventId, Fu
AddDefaultValidators();

if (!Configuration.DisableValidation)
_validator?.ValidateAndThrow(Configuration);
_validator?.ValidateAndThrow(this);

return CreateSequence<TSequence>();
}

private ISequence CreateSequence<TSequence>() where TSequence : ISequence, new()
{
var sequence = new TSequence().SetConfiguration(Configuration);
var sequence = new TSequence().SetConfiguration(Configuration, Data);

// TODO create log-adapter and add it to the handler
foreach (var handler in Configuration.Handler)
foreach (var handler in Data.Handler)
{
handler.Sequence = sequence;
}
Expand Down Expand Up @@ -86,7 +88,7 @@ public static ISequenceBuilder Create() =>
/// Creates a new Sequence-Builder for configuration in .NET 6 style.
/// This is good for short crispy configs.
/// </summary>
public static ISequenceBuilder Create(IValidator<SequenceConfiguration> validator) =>
public static ISequenceBuilder Create(IValidator<SequenceBuilder> validator) =>
new SequenceBuilder(validator);


Expand All @@ -108,8 +110,7 @@ public static ISequenceBuilder Configure(Action<ISequenceBuilder> configurationA
/// </summary>
/// <param name="validator">Custom validator</param>
/// <param name="configurationActions">The action.</param>
public static ISequenceBuilder Configure(IValidator<SequenceConfiguration> validator,
Action<ISequenceBuilder> configurationActions)
public static ISequenceBuilder Configure(IValidator<SequenceBuilder> validator, Action<ISequenceBuilder> configurationActions)
{
var sequenceBuilder = Create(validator);
configurationActions.Invoke(sequenceBuilder);
Expand All @@ -120,15 +121,15 @@ public static ISequenceBuilder Configure(IValidator<SequenceConfiguration> valid
/// <inheritdoc />
public ISequenceBuilder AddHandler<T>(T handler) where T : IHandler
{
Configuration.Handler.Add(handler);
Data.Handler.Add(handler);
return this;
}


/// <inheritdoc />
public ISequenceBuilder AddValidator<T>() where T : IHandlerValidator, new()
{
Configuration.Validators.Add(new T());
Data.Validators.Add(new T());
return this;
}

Expand Down
16 changes: 4 additions & 12 deletions src/IegTools.Sequencer/SequenceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,33 @@
/// </summary>
public class SequenceConfiguration
{
// TODO the logger-adapter will be created in builder and injected in the handler
// /// <summary>
// /// The logger that can be used for logging
// /// </summary>
// public ILoggerAdapter LoggerAdapter { get; set; }


/// <summary>
/// TODO move to logger-adapter
/// The logger that can be used for logging
/// </summary>
public ILogger Logger { get; set; }

/// <summary>
/// TODO move to logger-adapter
/// The logger-scope
/// </summary>
public Func<IDisposable> LoggerScope { get; set; }

/// <summary>
/// TODO move to logger-adapter
/// The EventId for logging
/// </summary>
public EventId EventId { get; set; }



/// <summary>
/// The sequence-handler that describe how the sequence is supposed to work
/// </summary>
public List<IHandler> Handler { get; } = new();

/// <summary>
/// All validators
/// </summary>
public IList<IHandlerValidator> Validators { get; set; } = new List<IHandlerValidator>();



/// <summary>
/// The complete validation will be disabled
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions src/IegTools.Sequencer/SequenceData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace IegTools.Sequencer;

using System.Collections.Generic;
using Handler;
using Validation;

/// <summary>
/// The sequence data
/// </summary>
public class SequenceData
{
/// <summary>
/// The sequence-handler that describe how the sequence is supposed to work
/// </summary>
public List<IHandler> Handler { get; } = new();

/// <summary>
/// All validators
/// </summary>
public IList<IHandlerValidator> Validators { get; set; } = new List<IHandlerValidator>();
}
16 changes: 8 additions & 8 deletions src/IegTools.Sequencer/Validation/AnyStateTransitionValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class AnyStateTransitionValidator : HandlerValidatorBase, IHandler


/// <inheritdoc />
public bool Validate(ValidationContext<SequenceConfiguration> context, ValidationResult result)
public bool Validate(ValidationContext<SequenceBuilder> context, ValidationResult result)
{
var isValid = true;

Expand Down Expand Up @@ -49,10 +49,10 @@ public bool Validate(ValidationContext<SequenceConfiguration> context, Validatio
/// otherwise you have created an dead-end.
/// Use '!' as first character to tag an state as dead-end with purpose.
/// </summary>
private bool HandlerIsValidatedFrom(SequenceConfiguration config)
private bool HandlerIsValidatedFrom(SequenceBuilder builder)
{
var transitions = config.Handler.OfType<AnyStateTransitionHandler>().ToList();
var allTransitions = config.Handler.OfType<IHasToState>().ToList();
var transitions = builder.Data.Handler.OfType<AnyStateTransitionHandler>().ToList();
var allTransitions = builder.Data.Handler.OfType<IHasToState>().ToList();
if (transitions.Count == 0) return true;

_handlerFrom = new List<AnyStateTransitionHandler>();
Expand All @@ -61,10 +61,10 @@ private bool HandlerIsValidatedFrom(SequenceConfiguration config)
// each StateTransition should have an counterpart so that no dead-end is reached
foreach (var transition in transitions)
{
foreach (var state in transition.FromStates.Where(x => ShouldBeValidated(x, config)))
foreach (var state in transition.FromStates.Where(x => ShouldBeValidated(x, builder)))
{
if (allTransitions.All(x => state != x.ToState) &&
state != config.InitialState)
state != builder.Configuration.InitialState)
_handlerFrom.Add(transition);
}
}
Expand All @@ -77,9 +77,9 @@ private bool HandlerIsValidatedFrom(SequenceConfiguration config)
/// otherwise you have created an dead-end.
/// Use '!' as first character to tag an state as dead-end with purpose.
/// </summary>
private bool HandlerIsValidatedTo(SequenceConfiguration config)
private bool HandlerIsValidatedTo(SequenceBuilder builder)
{
var result = HandlerIsValidatedTo<AnyStateTransitionHandler>(config);
var result = HandlerIsValidatedTo<AnyStateTransitionHandler>(builder);
_handlerTo = result.list.ToList();

return result.isValid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class ContainsStateTransitionValidator : HandlerValidatorBase, IHa


/// <inheritdoc />
public bool Validate(ValidationContext<SequenceConfiguration> context, ValidationResult result)
public bool Validate(ValidationContext<SequenceBuilder> context, ValidationResult result)
{
var isValid = true;

Expand Down Expand Up @@ -53,10 +53,10 @@ public bool Validate(ValidationContext<SequenceConfiguration> context, Validatio
/// otherwise you have created an dead-end.
/// Use '!' as first character to tag an state as dead-end with purpose.
/// </summary>
private bool HandlerValidatedFrom(SequenceConfiguration config)
private bool HandlerValidatedFrom(SequenceBuilder builder)
{
var transitions = config.Handler.OfType<ContainsStateTransitionHandler>().ToList();
var allTransitions = config.Handler.OfType<IHasToState>().ToList();
var transitions = builder.Data.Handler.OfType<ContainsStateTransitionHandler>().ToList();
var allTransitions = builder.Data.Handler.OfType<IHasToState>().ToList();
if (transitions.Count == 0) return true;

_handlerFrom = new List<ContainsStateTransitionHandler>();
Expand All @@ -67,7 +67,7 @@ private bool HandlerValidatedFrom(SequenceConfiguration config)
{
if (transitions.All(x => !x.ToState.Contains(transition.FromStateContains)) &&
////allTransitions.All(x => transition.FromState != x.ToState) &&
!config.InitialState.Contains(transition.FromStateContains))
!builder.Configuration.InitialState.Contains(transition.FromStateContains))
_handlerFrom.Add(transition);
}

Expand All @@ -79,9 +79,9 @@ private bool HandlerValidatedFrom(SequenceConfiguration config)
/// otherwise you have created an dead-end.
/// Use '!' as first character to tag an state as dead-end with purpose.
/// </summary>
private bool HandlerValidatedTo(SequenceConfiguration config)
private bool HandlerValidatedTo(SequenceBuilder builder)
{
var result = HandlerIsValidatedTo<ContainsStateTransitionHandler>(config);
var result = HandlerIsValidatedTo<ContainsStateTransitionHandler>(builder);
_handlerTo = result.list.ToList();

return result.isValid;
Expand Down
6 changes: 3 additions & 3 deletions src/IegTools.Sequencer/Validation/ForceStateValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class ForceStateValidator : HandlerValidatorBase, IHandlerValidato
private List<ForceStateHandler> _handler;

/// <inheritdoc />
public bool Validate(ValidationContext<SequenceConfiguration> context, ValidationResult result)
public bool Validate(ValidationContext<SequenceBuilder> context, ValidationResult result)
{
if (HandlerIsValidated(context.InstanceToValidate)) return true;

Expand All @@ -29,9 +29,9 @@ public bool Validate(ValidationContext<SequenceConfiguration> context, Validatio
/// Each 'Force.State' must have an corresponding 'Transition.FromState(s) '
/// otherwise you have created an dead-end.
/// </summary>
private bool HandlerIsValidated(SequenceConfiguration config)
private bool HandlerIsValidated(SequenceBuilder builder)
{
var result = HandlerIsValidatedTo<ForceStateHandler>(config);
var result = HandlerIsValidatedTo<ForceStateHandler>(builder);
_handler = result.list.ToList();

return result.isValid;
Expand Down
Loading

0 comments on commit fffdf1a

Please sign in to comment.