Reference

Gate

Package Trax.CoreNamespaces Trax.Core.Train, Trax.Core.DecisionsSource Monad.Decisions.cs, Tracks.cs

Routes on the probability that the answer to a yes/no question is yes: a Yes track, a No track, and optionally an Unsure track for the band between them.

Signatures

// Train<TInput, TReturn>; MonadTask<TInput, TReturn> and Monad<TInput, TReturn> have the same, public
 
// Asks the decider how likely yes is for the TState in Memory, then routes
protected MonadTask<TInput, TReturn> Gate<TState, TQuestion>(
    Func<GateTracks<TInput, TReturn>, GateTracks<TInput, TReturn>> gate,
    string? asking = null
);
 
// Routes on a YesNoDecision<TQuestion> an earlier Decide put in Memory; asks nothing
protected MonadTask<TInput, TReturn> Gate<TQuestion>(
    Func<GateTracks<TInput, TReturn>, GateTracks<TInput, TReturn>> gate
);

Parameters

ParameterDescription
TStateThe value in Memory the question is about (asking form)
TQuestionA marker type naming the question, usually carrying [Asks]. Memory is keyed by type, so each yes/no question a train asks needs its own.
gateDeclares the tracks on the GateTracks<TInput, TReturn> it is handed
askingThe question. Defaults to the [Asks] attribute on TQuestion.

GateTracks<TInput, TReturn>

MethodDescription
Yes(Func<MonadTask, MonadTask> then, double atLeast = 0.5)Taken when the probability of yes is at least atLeast. Required.
No(Func<MonadTask, MonadTask> then, double below = 0.5)Taken when it is below below. Required.
Unsure(Func<MonadTask, MonadTask> then)Taken when it is at least below and below atLeast. Without it, that band fails the run.
Shadow<TDecider>()Asking form only. Also puts the question to TDecider and records whether its answer would have taken the same track, by this gate's bars. Never acted on. See Decide.
WaitForShadows(TimeSpan wait)Asking form only. How long to wait for the shadows once the live answer is in. Defaults to five seconds.

With the defaults there is no band between the bars.

Returns

MonadTask<TInput, TReturn>, the next link of the chain.

Behavior

  • With the asking form, asks as Decide does.
  • A yes/no answer has no separate confidence: the probability is the answer, so the bars are on the probability itself.
  • Puts TrackTaken<TQuestion> in Memory (Yes, No or Unsure) and tells the observer, as Switch does. An Unsure outcome is a declared track, not an overruled decision, so its TrackTaken carries no fallback reason.
  • A gate missing its Yes or No track, or whose No bar is above its Yes bar, is refused.

Example

using Trax.Core.Functional;
using Trax.Effect.Services.ServiceTrain;
 
public class DeliverMessageTrain : ServiceTrain<Message, Delivery>
{
    protected override Task<Either<Exception, Delivery>> Junctions() =>
        Gate<Message, ContainsThreat>(gate => gate
                .Yes(t => t.Chain<Block>(), atLeast: 0.8)
                .No(t => t.Chain<Deliver>(), below: 0.2)
                .Unsure(t => t.Chain<HoldForReview>()))
            .Resolve();
}

See also