Skip to content

Commit d552c1b

Browse files
feat: Expose unmet guard conditions
Add a method to return list of non-permissible triggers from the current state, along with the descriptions of the un-met guard conditions.
1 parent 3f1477d commit d552c1b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/Stateless/StateMachine.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ public IEnumerable<TriggerDetails<TState, TTrigger>> GetDetailedPermittedTrigger
147147
.Select(trigger => new TriggerDetails<TState, TTrigger>(trigger, _triggerConfiguration));
148148
}
149149

150+
/// <summary>
151+
/// Gets any triggers for the current state that are not permissible, and the descriptions
152+
/// of the unmet trigger conditions associated with them.
153+
/// </summary>
154+
/// <param name="args"></param>
155+
/// <returns></returns>
156+
public IEnumerable<Tuple<TTrigger, string[]>> GetTriggersWithUnmetConditions(params object[] args)
157+
{
158+
return CurrentRepresentation.GetTriggersWithUnmetGuardConditions(args);
159+
}
160+
150161
StateRepresentation CurrentRepresentation
151162
{
152163
get

src/Stateless/StateRepresentation.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ public IEnumerable<TTrigger> GetPermittedTriggers(params object[] args)
330330
return result;
331331
}
332332

333+
public IEnumerable<Tuple<TTrigger, string[]>> GetTriggersWithUnmetGuardConditions(params object[] args)
334+
{
335+
List<Tuple<TTrigger, string[]>> unmetConditions = new List<Tuple<TTrigger, string[]>>();
336+
337+
var behaviours = TriggerBehaviours.Values.SelectMany(x => x);
338+
foreach (var triggerBehaviour in behaviours)
339+
{
340+
if (triggerBehaviour.GuardConditionsMet(args))
341+
continue;
342+
343+
var conditions = triggerBehaviour.UnmetGuardConditions(args);
344+
unmetConditions.Add(new Tuple<TTrigger, string[]>(triggerBehaviour.Trigger, conditions.ToArray()));
345+
}
346+
347+
if (Superstate != null)
348+
unmetConditions.AddRange(Superstate.GetTriggersWithUnmetGuardConditions(args));
349+
350+
return unmetConditions;
351+
}
352+
333353
internal void SetInitialTransition(TState state)
334354
{
335355
InitialTransitionTarget = state;

0 commit comments

Comments
 (0)