private void ConfigureSM() { SM.Configure(BrokerState.BrokerInactive) .Permit(BrokerTrigger.BrokerSubmitsApplication, BrokerState.ApplicationAccepted); SM.Configure(BrokerState.ApplicationAccepted) .Permit(BrokerTrigger.PlanRejectsApplication, BrokerState.BrokerInactive) .PermitReentry(BrokerTrigger.BrokerAmendsApplication) .Permit(BrokerTrigger.PlanApprovesApplication, BrokerState.ApplicationApproved); SM.Configure(BrokerState.ApplicationApproved) .Permit(BrokerTrigger.BrokerSignsContract, BrokerState.BrokerCertified); SM.Configure(BrokerState.BrokerCertified) .OnEntry(() => { if (true) { SM.Fire(BrokerTrigger.SalesTimeframeCommences); } }) //simulate auto-advance .Permit(BrokerTrigger.SalesTimeframeCommences, BrokerState.BrokerActive); SM.Configure(BrokerState.BrokerActive) .Permit(BrokerTrigger.SalesTimeframeExpires, BrokerState.BrokerInactive); }
/// <summary> /// Auxiliary method to accompany state machine transition on console output /// </summary> /// <param name="trigger"></param> public void SmFireWithTrace(TTrigger trigger) { Console.WriteLine($"<<< BEFORE: {SM.State}"); Console.Write($" Executing {trigger}"); try { SM.Fire(trigger); Console.WriteLine(" - success!"); } catch (InvalidOperationException) { string permittedTriggers; switch (SM.PermittedTriggers.Count()) { case 0: //final state permittedTriggers = "no triggers are"; break; case 1: permittedTriggers = $" only {SM.PermittedTriggers.First()} is"; break; default: permittedTriggers = $" only {string.Join(" or ", SM.PermittedTriggers)} are"; break; } Console.WriteLine($" - ERROR (in {SM.State} state, {permittedTriggers} allowed!)"); } Console.WriteLine($">>> AFTER : {SM.State}"); }