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

Expose unmet guard conditions #581

Closed
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
11 changes: 11 additions & 0 deletions src/Stateless/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public IEnumerable<TriggerDetails<TState, TTrigger>> GetDetailedPermittedTrigger
.Select(trigger => new TriggerDetails<TState, TTrigger>(trigger, _triggerConfiguration));
}

/// <summary>
/// Gets any triggers for the current state that are not permissible, and the descriptions
/// of the unmet trigger conditions associated with them.
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public IEnumerable<Tuple<TTrigger, string[]>> GetTriggersWithUnmetConditions(params object[] args)
{
return CurrentRepresentation.GetTriggersWithUnmetGuardConditions(args);
}

StateRepresentation CurrentRepresentation
{
get
Expand Down
20 changes: 20 additions & 0 deletions src/Stateless/StateRepresentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,26 @@ public IEnumerable<TTrigger> GetPermittedTriggers(params object[] args)
return result;
}

public IEnumerable<Tuple<TTrigger, string[]>> GetTriggersWithUnmetGuardConditions(params object[] args)
{
List<Tuple<TTrigger, string[]>> unmetConditions = new List<Tuple<TTrigger, string[]>>();

var behaviours = TriggerBehaviours.Values.SelectMany(x => x);
foreach (var triggerBehaviour in behaviours)
{
if (triggerBehaviour.GuardConditionsMet(args))
continue;

var conditions = triggerBehaviour.UnmetGuardConditions(args);
unmetConditions.Add(new Tuple<TTrigger, string[]>(triggerBehaviour.Trigger, conditions.ToArray()));
}

if (Superstate != null)
unmetConditions.AddRange(Superstate.GetTriggersWithUnmetGuardConditions(args));

return unmetConditions;
}

internal void SetInitialTransition(TState state)
{
InitialTransitionTarget = state;
Expand Down