Exemple #1
0
        public void StateTransitionedToSameStateTest()
        {
            var stateMachine = new StateMachine();

            IStateMachineEvent e1 = stateMachine.AddNewEvent("Event1");
            IStateMachineEvent e2 = stateMachine.AddNewEvent("Event2");

            var initial = stateMachine.AddNewState("Initial") as StateMachineState;
            var first   = stateMachine.AddNewState("First") as StateMachineState;

            initial.AddNewTransition(e1, first);

            first.AddNewTransition(e1, initial);
            first.AddNewTransition(e2, first);

            var context = new StateMachineContext(stateMachine, initial, null);

            context.Start();

            context.HandleEvent(e2);
            Assert.That(context.CurrentState, Is.SameAs(initial));

            context.HandleEvent(e1);
            Assert.That(context.CurrentState, Is.SameAs(first));

            context.HandleEvent(e2);
            Assert.That(context.CurrentState, Is.SameAs(first));
        }
        public virtual IStateTransition AddNewTransition(IStateMachineEvent trigger, IStateMachineState destination)
        {
            var transition = new StateTransition(this, trigger, destination);

            TransitionList.Add(transition);
            return(transition);
        }
 public StateTransition(IStateMachineState parent, IStateMachineEvent triggerEvent,
                        IStateMachineState targetState) : base(parent.StateMachine)
 {
     ParentState  = parent;
     TriggerEvent = triggerEvent;
     TargetState  = targetState;
 }
        public virtual IStateTransition AddNewTransition(IStateMachineEvent trigger, IStateMachineState destination, List <string> rolesRequired)
        {
            var transition = new StateTransition(this, trigger, destination, rolesRequired);

            TransitionList.Add(transition);
            return(transition);
        }
        public static IStateTransition FindFirstMatchingTransition(this IStateMachineContext context,
                                                                   IStateMachineEvent transitionEvent)
        {
            if (context == null || context.CurrentState == null)
            {
                return(null);
            }

            if (transitionEvent == null)
            {
                throw new ArgumentNullException("transitionEvent");
            }

            if (context.CurrentState.Transitions == null)
            {
                return(null);
            }

            foreach (IStateTransition transition in context.CurrentState.Transitions)
            {
                if (transition.Matches(transitionEvent, context))
                {
                    return(transition);
                }
            }
            return(null);
        }
Exemple #6
0
        public virtual IStateMachineEventAction AddNewEventAction(string actionTaskName,
                                                                  IStateMachineEvent eventIdentifier,
                                                                  params object[] actionArgs)
        {
            var action = new StateMachineEventAction(this, actionTaskName, eventIdentifier, actionArgs);

            EventActionList.Add(action);
            return(action);
        }
 public StateTransition(IStateMachineState parent, IStateMachineEvent triggerEvent,
                        IStateMachineState targetState, List <string> rolesRequired)
     : base(parent.StateMachine)
 {
     ParentState   = parent;
     TriggerEvent  = triggerEvent;
     TargetState   = targetState;
     RolesRequired = rolesRequired;
 }
        public bool ExecuteTrigger(StateChart stateChart, IStateMachineEvent stateMachineEvent)
        {
            if (TriggerEvent == stateMachineEvent)
            {
                stateChart.PrepareTransition(TargetStateVertex);
                return(true);
            }

            return(false);
        }
Exemple #9
0
        public void TransitionActionTest()
        {
            var stateMachine = new StateMachine();

            stateMachine.RegisterTaskAssembly(GetType().Assembly);

            stateMachine.AddStateInitializationAction("TestyTheTest");

            IStateMachineEvent e1 = stateMachine.AddNewEvent("Event1");
            IStateMachineEvent e2 = stateMachine.AddNewEvent("Event2");


            var initial = stateMachine.AddNewState("Initial") as StateMachineState;
            var first   = stateMachine.AddNewState("First") as StateMachineState;

            initial.AddNewTransition(e1, first);

            first.AddNewTransition(e1, initial);
            first.AddNewTransition(e2, first);

            first.AddStateFinalizationAction("TestyTheTest::AlternateMethod");

            var context = new StateMachineContext(stateMachine, null, null);

            context.Start();

            TestStateMachineTask.Clear();

            context.HandleEvent(e1);

            Assert.That(TestStateMachineTask.PhaseHistory.Count, Is.EqualTo(1));

            context.HandleEvent(e2);

            Assert.That(TestStateMachineTask.PhaseHistory.Count, Is.EqualTo(3));

            //Global init, Local Finalize, Global init
            Assert.That(TestStateMachineTask.PhaseHistory[0], Is.EqualTo(StateTransitionPhase.StateInitialization));
            Assert.That(TestStateMachineTask.PhaseHistory[1], Is.EqualTo(StateTransitionPhase.StateFinalization));
            Assert.That(TestStateMachineTask.PhaseHistory[2], Is.EqualTo(StateTransitionPhase.StateInitialization));

            //first inits, first finalizes, first inits
            Assert.That(TestStateMachineTask.StateHistory[0], Is.SameAs(first));
            Assert.That(TestStateMachineTask.StateHistory[1], Is.SameAs(first));
            Assert.That(TestStateMachineTask.StateHistory[1], Is.SameAs(first));

            context.HandleEvent(e1);

            //first finalizes, initial inits
            Assert.That(TestStateMachineTask.StateHistory[3], Is.SameAs(first));
            Assert.That(TestStateMachineTask.StateHistory[4], Is.SameAs(initial));

            Assert.That(TestStateMachineTask.PhaseHistory[3], Is.EqualTo(StateTransitionPhase.StateFinalization));
            Assert.That(TestStateMachineTask.PhaseHistory[4], Is.EqualTo(StateTransitionPhase.StateInitialization));
        }
        protected void add_event(string eventName, object eventIdentifier, ActionDelegate taskDelegate)
        {
            EnsureName();

            CurrentEvent = BuildEvent(eventName, eventIdentifier);

            if (taskDelegate != null)
            {
                taskDelegate();
            }

            CurrentEvent = null;
        }
Exemple #11
0
        public void ContextEventTest()
        {
            //Use just this assembly to load tasks from
            var stateMachine = new StateMachine();

            IStateMachineEvent e1 = stateMachine.AddNewEvent("Event1");
            IStateMachineEvent e2 = stateMachine.AddNewEvent("Event2");

            var initial = stateMachine.AddNewState("Initial") as StateMachineState;
            var first   = stateMachine.AddNewState("First") as StateMachineState;

            initial.AddNewTransition(e1, first);

            first.AddNewTransition(e1, initial);
            first.AddNewTransition(e2, first);

            var context = new StateMachineContext(stateMachine, null, null);

            context.Start();

            _beforeCount = 0;
            _afterCount  = 0;
            _cancel      = true;

            context.BeforeStateChanged += context_BeforeStateChanged;
            context.AfterStateChanged  += context_AfterStateChanged;

            context.HandleEvent(e1);
            Assert.That(_beforeCount, Is.EqualTo(1));
            Assert.That(_afterCount, Is.EqualTo(0));
            Assert.That(context.CurrentState, Is.SameAs(initial));

            _beforeCount = 0;
            _afterCount  = 0;
            _cancel      = false;

            context.HandleEvent("Event1");

            Assert.That(_beforeCount, Is.EqualTo(1));
            Assert.That(_afterCount, Is.EqualTo(1));
            Assert.That(context.CurrentState, Is.SameAs(first));

            context.HandleEvent(e2);

            Assert.That(_beforeCount, Is.EqualTo(2));
            Assert.That(_afterCount, Is.EqualTo(2));
            Assert.That(context.CurrentState, Is.SameAs(first));
        }
        protected void add_transition(string eventName, string targetStateName)
        {
            if (CurrentState != null)
            {
                var transition = new TransitionDef
                {
                    ParentState   = CurrentState,
                    TriggerEvent  = eventName,
                    TargetState   = targetStateName,
                    RolesRequired = new List <string>()
                };

                PendingTransitions.Add(transition);
                CurrentEvent = StateMachine.FindEventByName(eventName);
            }
        }
Exemple #13
0
        public virtual bool ExecuteTrigger(StateChart stateChart, IStateMachineEvent stateMachineEvent)
        {
            if (Transitions == null)
            {
                return(false);
            }

            foreach (var transition in Transitions)
            {
                if (transition.ExecuteTrigger(stateChart, stateMachineEvent))
                {
                    return(true);
                }
            }

            return(false);
        }
        protected void AddEventAction(string eventName, string taskName, object[] args)
        {
            EnsureName();

            IStateMachineEvent evt = StateMachine.FindEventByName(eventName);

            if (evt != null)
            {
                if (CurrentState != null)
                {
                    CurrentState.AddNewEventAction(taskName, evt, args);
                }
                else
                {
                    StateMachine.AddNewEventAction(taskName, evt, args);
                }
            }
        }
Exemple #15
0
        public void ShouldExecuteAssignedActionWhenEventIsReceived()
        {
            var stateMachine = new StateMachine();

            stateMachine.RegisterTaskAssembly(GetType().Assembly);

            IStateMachineEvent e1 = stateMachine.AddNewEvent("Event1");//.Action("TestyTheTest");
            IStateMachineEvent e2 = stateMachine.AddNewEvent("Event2");

            stateMachine.AddNewEventAction("TestyTheTest", e1);

            var initial = stateMachine.AddNewState("Initial") as StateMachineState;
            var first   = stateMachine.AddNewState("First") as StateMachineState;

            initial.AddNewEventAction("TestyTheTest", e1);


            initial.AddNewTransition(e1, first);

            first.AddNewTransition(e1, initial);
            first.AddNewTransition(e2, first);

            var context = new StateMachineContext(stateMachine, null, null);

            context.Start();

            TestStateMachineTask.Clear();

            context.HandleEvent(e1);

            Assert.That(TestStateMachineTask.PhaseHistory.Count, Is.EqualTo(2));

            context.HandleEvent(e2);

            Assert.That(TestStateMachineTask.PhaseHistory.Count, Is.EqualTo(2));

            Assert.That(TestStateMachineTask.EventHistory[0], Is.SameAs(e1));

            context.HandleEvent(e1);

            Assert.That(TestStateMachineTask.EventHistory.Count, Is.EqualTo(3));
            Assert.That(TestStateMachineTask.EventHistory[1], Is.SameAs(e1));
        }
 protected void add_transition(string eventName, string targetStateName, string rolesRequired)
 {
     if (CurrentState != null)
     {
         var transition = new TransitionDef
         {
             ParentState   = CurrentState,
             TriggerEvent  = eventName,
             TargetState   = targetStateName,
             RolesRequired = new List <string>()
         };
         foreach (string role in rolesRequired.ToString().Replace("(", "").Replace(")", "").Split(new char[] { ',' }))
         {
             transition.RolesRequired.Add(role);
         }
         PendingTransitions.Add(transition);
         CurrentEvent = StateMachine.FindEventByName(eventName);
     }
 }
Exemple #17
0
        public void CreateAndRunSimpleStateMachineTest()
        {
            var stateMachine = new StateMachine();

            IStateMachineEvent e1 = stateMachine.AddNewEvent("Event1");
            IStateMachineEvent e2 = stateMachine.AddNewEvent("Event2");

            var initial = stateMachine.AddNewState("Initial") as StateMachineState;
            var first   = stateMachine.AddNewState("First") as StateMachineState;
            var second  = stateMachine.AddNewState("Second") as StateMachineState;

            initial.AddNewTransition(e1, first);
            initial.AddNewTransition(e2, second);

            first.AddNewTransition(e1, initial);
            first.AddNewTransition(e2, second);

            second.AddNewTransition(e1, initial);
            second.AddNewTransition(e2, first);

            var context = new StateMachineContext(stateMachine, "Initial", null);

            context.Start();

            context.HandleEvent(e1);
            Assert.That(context.CurrentState, Is.SameAs(first));

            context.HandleEvent(e1);
            Assert.That(context.CurrentState, Is.SameAs(initial));

            context.HandleEvent(e2);
            Assert.That(context.CurrentState, Is.SameAs(second));

            context.HandleEvent(e2);
            Assert.That(context.CurrentState, Is.SameAs(first));

            context.HandleEvent(e2);
            Assert.That(context.CurrentState, Is.SameAs(second));

            context.HandleEvent(e1);
            Assert.That(context.CurrentState, Is.SameAs(initial));
        }
            private static bool UpdateNonInstantEvents(StateMachine stateMachine, ref List <Event> nonInstantEvents, float currentTime)
            {
                for (int i = 0; i < nonInstantEvents.Count;)
                {
                    Event evnt = nonInstantEvents[i];
                    IStateMachineEvent stateMachineEvent = evnt as IStateMachineEvent;

                    float eventTime = currentTime - evnt.GetTime();
                    bool  timeUp    = eventTime >= evnt.GetDuration();

                    if (timeUp)
                    {
                        stateMachineEvent.End(stateMachine);
                        nonInstantEvents.RemoveAt(i);
                    }
                    else
                    {
                        eEventTriggerReturn status = stateMachineEvent.Update(stateMachine, eventTime);

                        switch (status)
                        {
                        case eEventTriggerReturn.EventFinished:
                            nonInstantEvents.RemoveAt(i);
                            break;

                        case eEventTriggerReturn.EventOngoing:
                            break;

                        case eEventTriggerReturn.EventFinishedExitState:
                            return(true);
                        }

                        i++;
                    }
                }

                return(false);
            }
        protected void CommitPendingTransitions()
        {
            foreach (TransitionDef transition in PendingTransitions)
            {
                IStateMachineEvent triggerEvent = StateMachine.FindEventByName(transition.TriggerEvent);
                IStateMachineState targetState  = StateMachine.FindStateByName(transition.TargetState);

                if (triggerEvent == null)
                {
                    throw new Exception("A non-existent event was specified in a transition for " +
                                        transition.ParentState.StateName + " : " +
                                        transition.TriggerEvent);
                }

                if (targetState == null)
                {
                    throw new Exception(
                              "A non-existent target state was specified in a transition for " +
                              transition.ParentState.StateName + " : " + transition.TargetState);
                }

                ((StateMachineState)transition.ParentState).AddNewTransition(triggerEvent, targetState, transition.RolesRequired);
            }
        }
 public StateChartSignal(IStateMachineEvent stateMachineEvent)
 {
     this._stateMachineEvent = stateMachineEvent;
 }
            private static IEnumerator PerformState(StateMachine stateMachine, TimelineState state, Timeline timeline)
            {
                if (timeline != null && timeline._events.Length > 0)
                {
                    ITimelineStateMachineTimer timer = TimelineStateMachine.GetTimer(stateMachine.gameObject);

                    float        currentTime      = 0.0f;
                    List <Event> nonInstantEvents = new List <Event>();

                    int   eventIndex   = 0;
                    Event currentEvent = timeline._events[eventIndex];

                    while (currentEvent != null || nonInstantEvents.Count > 0)
                    {
                        IStateMachineEvent currentStateMachineEvent = currentEvent as IStateMachineEvent;

                        if (currentStateMachineEvent == null && currentEvent != null)
                        {
                            throw new System.Exception("Event doesn't implement IStateMachineEvent");
                        }

                        float nextEventTime = currentEvent != null ? timeline._events[eventIndex].GetTime() : 0.0f;

                        //Wait until event time
                        while (currentTime < nextEventTime || (currentEvent == null && nonInstantEvents.Count > 0))
                        {
                            currentTime += timer.GetDeltaTime();
#if DEBUG
                            TimelineStateMachineDebug.OnTimelineStateTimeProgress(stateMachine, state, currentTime);
#endif

                            //Updated non instant events, if any now wants to exit the state then break out of coroutine
                            if (UpdateNonInstantEvents(stateMachine, ref nonInstantEvents, currentTime))
                            {
                                EndNonInstantEvents(stateMachine, ref nonInstantEvents);
                                yield break;
                            }

                            yield return(null);
                        }

                        if (currentEvent == null)
                        {
                            break;
                        }

                        //Trigger event
                        eEventTriggerReturn status = currentStateMachineEvent.Trigger(stateMachine);

                        switch (status)
                        {
                        case eEventTriggerReturn.EventFinished:
                            //Do nothing, just move on to next event
                            break;

                        case eEventTriggerReturn.EventFinishedExitState:
                            //Exit state so break out of coroutine
                            EndNonInstantEvents(stateMachine, ref nonInstantEvents);
                            yield break;

                        case eEventTriggerReturn.EventOngoing:
                            //Track timed event, move on to next event
                            nonInstantEvents.Add(currentEvent);
                            break;
                        }

                        //Get next
                        currentEvent = ++eventIndex < timeline._events.Length ? timeline._events[eventIndex] : null;
                    }
                }

#if DEBUG
                TimelineStateMachineDebug.OnTimelineStateStoped(stateMachine);
#endif

                yield break;
            }
Exemple #22
0
 public void Trigger(IStateMachineEvent trigger)
 {
     currentStateVertex?.ExecuteTrigger(this, trigger);
 }
 public StateMachineEventAction(IStateMachine stateMachine, string actionName, IStateMachineEvent triggerEvent, params object[] actionArgs)
     : base(stateMachine, actionName, actionArgs)
 {
     TriggerEvent = triggerEvent;
 }
 public StateTransition OnEvent(IStateMachineEvent trigger)
 {
     TriggerEvent = trigger;
     return(this);
 }
        public virtual IStateMachineEventAction AddNewEventAction(string actionTaskName, IStateMachineEvent triggerEvent, params object[] actionArgs)
        {
            var action = new StateMachineEventAction(StateMachine, actionTaskName, triggerEvent, actionArgs);

            EventActionList.Add(action);
            return(action);
        }
Exemple #26
0
 public StateTransition Event(IStateMachineEvent trigger)
 {
     return(Transition().OnEvent(trigger));
 }