Ejemplo n.º 1
0
        public void Finish_WithTransitionInExitAction_NotExecuted()
        {
            const string StateA   = "StateA";
            const string StateA1A = "StateA1A";
            const string Transi1  = "Transi1";

            const string Event1             = "Event1";
            bool         transitionExecuted = false;

            StateMachineTemplate t = new StateMachineTemplate();

            t.Region(StateA, false);
            t.State(StateA, null, (s, ev, args) => s.SendTriggerEvent(Event1));
            t.Region(StateA1A, false);
            t.State(StateA1A);
            t.Transition(Transi1, new string[] { StateA1A }, new string[] { StateA1A }, Event1, null, (s, ev, args) => transitionExecuted = true);
            t.EndState();
            t.EndRegion();
            t.EndState();
            t.EndRegion();

            Assert.That(t.StateConfigurationMax, Is.EqualTo(2), "StateConfigurationMax wrong");
            Assert.That(t.ConcurrencyDegree, Is.EqualTo(1), "ConcurrencyDegree wrong");

            StateMachine stateMachine = t.CreateStateMachine(this);

            stateMachine.Startup();
            stateMachine.Finish();

            StateConfiguration nirvana = t.CreateStateConfiguration(new string[] { });

            Assert.That(stateMachine.ActiveStateConfiguration.ToString(), Is.EqualTo(nirvana.ToString()), "Active state not as expected.");

            Assert.That(transitionExecuted, Is.EqualTo(false), "Unintended transition during finish executed.");
        }
Ejemplo n.º 2
0
        public void CreateStateConfiguration_OrthogonalRegions_Successful()
        {
            // Act
            StateConfiguration s = m_stmTmplat.CreateStateConfiguration(new string[] { StateA1A1A, StateA1A2A });

            // Assert
            Assert.That(s, Is.Not.Null);
            Assert.That(s.ToString(), Is.EqualTo("StateA(StateA1A(StateA1A1A,StateA1A2A))"));
        }
Ejemplo n.º 3
0
        public void CreateStateConfiguration_SingleState_Successful()
        {
            // Act
            StateConfiguration s = m_stmTmplat.CreateStateConfiguration(StateB);

            // Assert
            Assert.That(s, Is.Not.Null);
            Assert.That(s, Is.Not.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateA })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s.ToString(), Is.EqualTo("StateB"));
        }
Ejemplo n.º 4
0
 private void TraceStateChange(StateMachine stateMachine,
                               StateConfiguration stateConfigurationFrom,
                               StateConfiguration stateConfigurationTo,
                               Transition transition)
 {
     Console.WriteLine("{0} ActiveState={1} entered through Transition={2}",
                       DateTime.Now.ToString("HH:mm:ss.fff"),
                       stateConfigurationTo.ToString(),
                       (transition != null) ? transition.Name : "Startup/Finish");
 }
Ejemplo n.º 5
0
        private void TraceStateChange(StateMachine stateMachine,
                                      StateConfiguration stateConfigurationFrom,
                                      StateConfiguration stateConfigurationTo,
                                      Transition transition)
        {
            string info = DateTime.Now.ToString("HH:mm:ss.fff") +
                          " ActiveState=\"" + stateConfigurationTo.ToString() + "\"" +
                          " Transition=" + ((transition != null) ? "\"" + transition.Name + "\"" : "Startup/Finish");

            Debug.Print(info);
            Display.WriteLine(info);
        }
Ejemplo n.º 6
0
        public void CreateStateConfiguration_PartialUnspecifiedOrthogonalRegions_Successful()
        {
            // Act
            StateConfiguration s = m_stmTmplat.CreateStateConfiguration(new string[] { StateA1A2A });

            // Assert
            Assert.That(s, Is.Not.Null);
            Assert.That(s, Is.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateA1A1A, StateA1A2A })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s, Is.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateA1A1B, StateA1A2A })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s, Is.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateA })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s, Is.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateA1A })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s, Is.Not.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateB })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s, Is.Not.EqualTo(m_stmTmplat.CreateStateConfiguration(new string[] { StateA1B })).Using(StateConfigurationIsMatching.Instance));
            Assert.That(s.ToString(), Is.EqualTo("StateA(StateA1A(*,StateA1A2A))"));
        }
Ejemplo n.º 7
0
        public void StartupSendFinish_WithTraceFunctions_NotifiesPreviousAndActiveStateProperly()
        {
            // Arrange
            const string StateA   = "StateA";
            const string StateA1A = "StateA1A";
            const string StateA1B = "StateA1B";
            const string Transi1  = "Transi1";

            const string Event1 = "Event1";

            StateMachineTemplate t = new StateMachineTemplate();

            t.Region(StateA, false);
            t.State(StateA);
            t.Region(StateA1A, false);
            t.State(StateA1A);
            t.Transition(Transi1, new string[] { StateA1B }, Event1);
            t.EndState();
            t.State(StateA1B);
            t.EndState();
            t.EndRegion();
            t.EndState();
            t.EndRegion();

            StateConfiguration nirvana = t.CreateStateConfiguration(new string[] { });

            StateMachine       stateMachine = t.CreateStateMachine(this);
            int                stateChangeNotificationCount   = 0;
            StateConfiguration stateConfigurationFromNotified = null;
            StateConfiguration stateConfigurationToNotified   = null;

            stateMachine.TraceStateChange = delegate(StateMachine stm, StateConfiguration stateConfigurationFrom, StateConfiguration stateConfigurationTo, Transition transition)
            {
                stateChangeNotificationCount  += 1;
                stateConfigurationFromNotified = (StateConfiguration)stateConfigurationFrom.Clone();
                stateConfigurationToNotified   = (StateConfiguration)stateConfigurationTo.Clone();
            };

            // Act
            stateMachine.Startup();

            // Assert
            Assert.That(stateChangeNotificationCount, Is.EqualTo(1));
            Assert.That(stateConfigurationFromNotified.ToString(), Is.EqualTo(nirvana.ToString()));
            Assert.That(stateConfigurationToNotified.ToString(), Is.EqualTo(t.CreateStateConfiguration(StateA1A).ToString()));

            // Act
            stateMachine.SendTriggerEvent(Event1);

            // Assert
            Assert.That(stateChangeNotificationCount, Is.EqualTo(2));
            Assert.That(stateConfigurationFromNotified.ToString(), Is.EqualTo(t.CreateStateConfiguration(StateA1A).ToString()));
            Assert.That(stateConfigurationToNotified.ToString(), Is.EqualTo(t.CreateStateConfiguration(StateA1B).ToString()));

            // Act
            stateMachine.Finish();

            // Assert
            Assert.That(stateChangeNotificationCount, Is.EqualTo(3));
            Assert.That(stateConfigurationFromNotified.ToString(), Is.EqualTo(t.CreateStateConfiguration(StateA1B).ToString()));
            Assert.That(stateConfigurationToNotified.ToString(), Is.EqualTo(nirvana.ToString()));
        }
Ejemplo n.º 8
0
        private void SaveStateResume_WithHistory_UsesHistoryStateAfterResume(bool executeEntryActions)
#endif
        {
            ActionRecorder recorder = new ActionRecorder();

            TemplateFactory createTemplate = delegate()
            {
                StateMachineTemplate t = new StateMachineTemplate(StateMachineOptions.UseDoActions);
                //## Begin StateMachineTemplateSaveStateResumeWithHistory
                // Generated from <file:S:\StaMa_State_Machine_Controller_Library\StaMaShapesMaster.vst> page "UT_SaveStateResumeTests"
                // at 07-25-2015 16:55:36 using StaMaShapes Version 2300
                t.Region("StateA", false);
                t.State("StateA", recorder.CreateAction("EnterA"), recorder.CreateAction("ExitA"), recorder.CreateDoAction("DoA"));
                t.Region("StateA1A", true);
                t.State("StateA1A", recorder.CreateAction("EnterA1A"), recorder.CreateAction("ExitA1A"), recorder.CreateDoAction("DoA1A"));
                t.Transition("T1", "StateA1B", null, null, null);
                t.EndState();
                t.State("StateA1B", recorder.CreateAction("EnterA1B"), recorder.CreateAction("ExitA1B"), recorder.CreateDoAction("DoA1B"));
                t.Transition("T2", "StateB1A", "Event1", null, null);
                t.EndState();
                t.EndRegion();
                t.EndState();
                t.State("StateB", recorder.CreateAction("EnterB"), recorder.CreateAction("ExitB"), recorder.CreateDoAction("DoB"));
                t.Transition("T4", "StateA", "Event2", null, null);
                t.Region("StateB1A", true);
                t.State("StateB1A", recorder.CreateAction("EnterB1A"), recorder.CreateAction("ExitB1A"), recorder.CreateDoAction("DoB1A"));
                t.Transition("T3", "StateB1B", null, null, null);
                t.EndState();
                t.State("StateB1B", recorder.CreateAction("EnterB1B"), recorder.CreateAction("ExitB1B"), recorder.CreateDoAction("DoB1B"));
                t.EndState();
                t.EndRegion();
                t.EndState();
                t.EndRegion();
                //## End StateMachineTemplateSaveStateResumeWithHistory
                return(t);
            };

            StateMachineTemplate t1 = createTemplate();

            t1.SerializationSignatureGenerator = TestSignatureGenerator;
            StateMachine s1 = t1.CreateStateMachine();

            s1.Startup();
            s1.SendTriggerEvent(null);
            s1.SendTriggerEvent("Event1");

            StateConfiguration expectedActiveStateS1 = t1.CreateStateConfiguration("StateB1B");

            Assert.That(s1.ActiveStateConfiguration.ToString(), Is.EqualTo(expectedActiveStateS1.ToString()), "Precondition not met: Unexpected state save state.");
            Assert.That(recorder.RecordedActions, Is.EqualTo(new String[] { "EnterA", "EnterA1A", "DoA", "DoA1A", "ExitA1A", "EnterA1B", "DoA", "DoA1B", "ExitA1B", "ExitA", "EnterB", "EnterB1A", "DoB", "DoB1A", "ExitB1A", "EnterB1B", "DoB", "DoB1B" }), "Precondition not met: Unexpected action execution sequence.");
            recorder.Clear();

            // Act
            MemoryStream memoryStream = new MemoryStream();

            s1.SaveState(memoryStream);
            memoryStream.Flush();
            memoryStream.Position = 0;

            StateMachine s2 = t1.CreateStateMachine();

            s2.Resume(memoryStream, executeEntryActions);

            // Assert
            Assert.That(s2.ActiveStateConfiguration.ToString(), Is.EqualTo(s1.ActiveStateConfiguration.ToString()), "State mismatch after Resume.");
            String[] expectedActions = executeEntryActions ? new String[] { "EnterB", "EnterB1B", "DoB", "DoB1B" } : new String[] { };
            Assert.That(recorder.RecordedActions, Is.EqualTo(expectedActions), "Unexpected entry or do actions during state machine resume with history.");
            recorder.Clear();
            s2.SendTriggerEvent("Event2");
            Assert.That(s2.ActiveStateConfiguration.ToString(), Is.EqualTo(t1.CreateStateConfiguration("StateA1B").ToString()), "Unexpected state machine behavior after Resume.");
            Assert.That(recorder.RecordedActions, Is.EqualTo(new String[] { "ExitB1B", "ExitB", "EnterA", "EnterA1B", "DoA", "DoA1B" }), "Unexpected entry or do actions during state machine resume.");

            // Compatibility test assertions
            // Data obtained from
            // FileStream fileStream = new FileStream("TestData.dat", FileMode.Create);
            // s1.SaveState(fileStream);
            // fileStream.Close();
            // PowerShell> gc -encoding byte "TestData.dat" |% {write-host ("0x{0:X2}," -f $_) -noNewline ""}; write-host
            StringBuilder sb = new StringBuilder();

            Byte[] actualBytes = memoryStream.ToArray();
            for (int i = 0; i < actualBytes.Length; i++)
            {
                sb.Append("0x" + actualBytes[i].ToString("X2") + ", ");
            }
#if !MF_FRAMEWORK
            Console.WriteLine("Actual bytes:");
            Console.WriteLine(sb.ToString());
#else
            Debug.Print("Actual bytes:");
            Debug.Print(sb.ToString());
#endif
            Byte[] compatibilityTestData2300 = new Byte[] { 0xAA, 0x00, 0x23, 0xA1, 0x47, 0x28, 0x7E, 0x31, 0x2D, 0x7B, 0x53, 0x74, 0x61, 0x74, 0x65, 0x41, 0x28, 0x7E, 0x31, 0x23, 0x7B, 0x53, 0x74, 0x61, 0x74, 0x65, 0x41, 0x31, 0x41, 0x2C, 0x53, 0x74, 0x61, 0x74, 0x65, 0x41, 0x31, 0x42, 0x2C, 0x7D, 0x29, 0x2C, 0x53, 0x74, 0x61, 0x74, 0x65, 0x42, 0x28, 0x7E, 0x31, 0x23, 0x7B, 0x53, 0x74, 0x61, 0x74, 0x65, 0x42, 0x31, 0x41, 0x2C, 0x53, 0x74, 0x61, 0x74, 0x65, 0x42, 0x31, 0x42, 0x2C, 0x7D, 0x29, 0x2C, 0x7D, 0x29, 0xA2, 0x01, 0x00, 0xA4, 0x01, 0x00, 0xA5, 0x08, 0x53, 0x74, 0x61, 0x74, 0x65, 0x42, 0x31, 0x42, 0xA3, 0x02, 0x00, 0xA4, 0x01, 0x00, 0xA5, 0x08, 0x53, 0x74, 0x61, 0x74, 0x65, 0x41, 0x31, 0x42, 0xA4, 0x01, 0x00, 0xA5, 0x08, 0x53, 0x74, 0x61, 0x74, 0x65, 0x42, 0x31, 0x42, };
            Assert.That(memoryStream.ToArray(), Is.EqualTo(compatibilityTestData2300), "Compatibility test failed. Written data different from version 2300.");

            MemoryStream         memoryStream2300 = new MemoryStream(compatibilityTestData2300);
            StateMachineTemplate t3 = createTemplate();
            t3.SerializationSignatureGenerator = null;
            StateMachine s3 = t3.CreateStateMachine();
            s3.Resume(memoryStream2300, false);
            Assert.That(s3.ActiveStateConfiguration.ToString(), Is.EqualTo(expectedActiveStateS1.ToString()), "State mismatch after Resume.");
        }
Ejemplo n.º 9
0
        private void TraceStateChange(StateMachine stateMachine, StateConfiguration stateConfigurationFrom, StateConfiguration stateConfigurationTo, Transition transition)
        {
            string textContent = "Current state \"" + stateConfigurationTo.ToString() + "\"" + ((transition != null) ? "\nLast transition \"" + transition.Name + "\"" : String.Empty);

            this.infoTextbox.TextContent = textContent;
        }