コード例 #1
0
        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]);
            }
        }
コード例 #2
0
        public void WhenSyncActivateAsyncOnActivateAction()
        {
            var sm = new StateMachine <State, Trigger>(State.A);

            sm.Configure(State.A)
            .OnActivateAsync(() => TaskResult.Done);

            Assert.Throws <InvalidOperationException>(() => sm.Activate());
        }
コード例 #3
0
    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);
    }
コード例 #4
0
        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);
        }
コード例 #5
0
    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);
    }
コード例 #6
0
        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;
        }
コード例 #7
0
        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]);
            }
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: 94adi/DesignPatterns
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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]);
            }
        }
コード例 #11
0
ファイル: SimpleDefender.cs プロジェクト: hspirdal/darwin
 public void Act()
 {
     _stateMachine.Activate();
     _stateMachine.Deactivate();
 }