public void WhenDeactivate() { var sm = new StateMachine <State, Trigger>(State.A); var expectedOrdering = new List <string> { "DeactivatedA", "DeactivatedC" }; var actualOrdering = new List <string>(); sm.Configure(State.A) .SubstateOf(State.C) .OnDeactivate(() => actualOrdering.Add("DeactivatedA")); sm.Configure(State.C) .OnDeactivate(() => actualOrdering.Add("DeactivatedC")); // should not be called for activation sm.OnTransitioned(t => actualOrdering.Add("OnTransitioned")); sm.Activate(); sm.Deactivate(); Assert.AreEqual(expectedOrdering.Count, actualOrdering.Count); for (int i = 0; i < expectedOrdering.Count; i++) { Assert.AreEqual(expectedOrdering[i], actualOrdering[i]); } }
public void WhenSyncActivateAsyncOnActivateAction() { var sm = new StateMachine <State, Trigger>(State.A); sm.Configure(State.A) .OnActivateAsync(() => TaskResult.Done); Assert.Throws <InvalidOperationException>(() => sm.Activate()); }
public void TriggerTransitionWithTruePredicate() { var machine = new StateMachine <Food, Trigger>(Food.Banana); machine.Configure(Food.Banana).PermitIf(Trigger.Bark, Food.Cake, () => true); machine.Initialize(); machine.Activate(Trigger.Bark); Assert.AreEqual(machine.CurrentState, Food.Cake); }
public void WhenActivateIsIdempotent() { var sm = new StateMachine <State, Trigger>(State.A); var actualOrdering = new List <string>(); sm.Configure(State.A) .SubstateOf(State.C) .OnActivate(() => actualOrdering.Add("ActivatedA")); sm.Configure(State.C) .OnActivate(() => actualOrdering.Add("ActivatedC")); sm.Activate(); actualOrdering.Clear(); sm.Activate(); Assert.AreEqual(0, actualOrdering.Count); }
public void Exit() { var machine = new StateMachine <DoorState, Interaction>(DoorState.Closed); bool indicator = false; machine.Configure(DoorState.Closed) .OnExit(() => indicator = true) .Permit(Interaction.Knock, DoorState.Open); machine.Initialize(); machine.Activate(Interaction.Knock); Assert.True(indicator); }
public void ConfigureStateMachine(TState initialState) { if (_isConfigured) { throw new Exception("StateMachine is already Configured"); } StateMachine = new StateMachine <TState, TTrigger>(initialState); DoConfigureStateMachine(); StateMachine.Activate(); _isConfigured = true; }
public void WhenTransitioning() { var sm = new StateMachine <State, Trigger>(State.A); var expectedOrdering = new List <string> { "ActivatedA", "DeactivatedA", "ExitedA", "OnTransitioned", "EnteredB", "ActivatedB", "DeactivatedB", "ExitedB", "OnTransitioned", "EnteredA", "ActivatedA" }; var actualOrdering = new List <string>(); sm.Configure(State.A) .OnActivate(() => actualOrdering.Add("ActivatedA")) .OnDeactivate(() => actualOrdering.Add("DeactivatedA")) .OnEntry(() => actualOrdering.Add("EnteredA")) .OnExit(() => actualOrdering.Add("ExitedA")) .Permit(Trigger.X, State.B); sm.Configure(State.B) .OnActivate(() => actualOrdering.Add("ActivatedB")) .OnDeactivate(() => actualOrdering.Add("DeactivatedB")) .OnEntry(() => actualOrdering.Add("EnteredB")) .OnExit(() => actualOrdering.Add("ExitedB")) .Permit(Trigger.Y, State.A); sm.OnTransitioned(t => actualOrdering.Add("OnTransitioned")); sm.Activate(); sm.Fire(Trigger.X); sm.Fire(Trigger.Y); Assert.AreEqual(expectedOrdering.Count, actualOrdering.Count); for (int i = 0; i < expectedOrdering.Count; i++) { Assert.AreEqual(expectedOrdering[i], actualOrdering[i]); } }
static void Main(string[] args) { var stateMachine = new StateMachine <Health, Activity>(Health.NonReproductive); stateMachine.Configure(Health.NonReproductive) .Permit(Activity.ReachPuberty, Health.Reproductive); stateMachine.Configure(Health.Reproductive) .Permit(Activity.Historectomy, Health.NonReproductive); stateMachine.Activate(); Console.WriteLine(stateMachine.State); Console.WriteLine("Reach puberty"); stateMachine.Fire(Activity.ReachPuberty); Console.WriteLine(stateMachine.State); }
private static void RunStateMachine(StateMachine <DocumentStatus, Trigger> sm, Document document) { //completing draft sm.Activate(); sm.Fire(Trigger.CompleteDraft); //approving foreach (var approveRequest in document.InternalApproveRequests.Union(document.ExternalApproveRequests)) { approveRequest.IsApproved = true; sm.Fire(Trigger.Approve); } //providing invoice number document.InvoiceNumber = "123"; sm.Fire(Trigger.ProvideInvoiceNumber); }
public void WhenTransitioningWithinSameSuperstate() { var sm = new StateMachine <State, Trigger>(State.A); var expectedOrdering = new List <string> { "ActivatedC", "ActivatedA", "DeactivatedA", "ActivatedB", "DeactivatedB", "ActivatedA", }; var actualOrdering = new List <string>(); sm.Configure(State.A) .SubstateOf(State.C) .OnActivate(() => actualOrdering.Add("ActivatedA")) .OnDeactivate(() => actualOrdering.Add("DeactivatedA")) .Permit(Trigger.X, State.B); sm.Configure(State.B) .SubstateOf(State.C) .OnActivate(() => actualOrdering.Add("ActivatedB")) .OnDeactivate(() => actualOrdering.Add("DeactivatedB")) .Permit(Trigger.Y, State.A); sm.Configure(State.C) .OnActivate(() => actualOrdering.Add("ActivatedC")) .OnDeactivate(() => actualOrdering.Add("DeactivatedC")); sm.Activate(); sm.Fire(Trigger.X); sm.Fire(Trigger.Y); Assert.AreEqual(expectedOrdering.Count, actualOrdering.Count); for (int i = 0; i < expectedOrdering.Count; i++) { Assert.AreEqual(expectedOrdering[i], actualOrdering[i]); } }
public void Act() { _stateMachine.Activate(); _stateMachine.Deactivate(); }