Exemplo n.º 1
0
 internal StateConfiguration(Dictionary <TState, StateRepresentation <TState, TTrigger> > representations,
                             TState currentState)
 {
     Representations            = representations;
     CurrentStateRepresentation = StateConfigurationHelper.FindOrCreateStateRepresentation(currentState,
                                                                                           representations);
 }
        internal static TriggerRepresentation <TTrigger> FindOrCreateTriggerRepresentation <TState, TTrigger>(
            TTrigger trigger,
            StateRepresentation <TState, TTrigger> representation)
        {
            var rep = FindTriggerRepresentation(trigger, representation);

            return(rep ?? CreateTriggerRepresentation(trigger, representation));
        }
        internal static StateRepresentation <TState, TTrigger> CreateStateRepresentation <TState, TTrigger>(TState state,
                                                                                                            Dictionary <TState, StateRepresentation <TState, TTrigger> > representations)
        {
            var rep = new StateRepresentation <TState, TTrigger>(state);

            representations[state] = rep;
            return(rep);
        }
        internal static TriggerRepresentation <TTrigger> CreateTriggerRepresentation <TState, TTrigger>(
            TTrigger trigger,
            StateRepresentation <TState, TTrigger> representation)
        {
            var rep = new TriggerRepresentation <TTrigger>(trigger);

            representation.Triggers.Add(rep);
            return(rep);
        }
Exemplo n.º 5
0
        internal static void FireCore <TState, TTrigger>(TTrigger trigger, RawStateMachineBase <TState, TTrigger> machine,
                                                         bool raiseInvalidStateOrTrigger = true)
        {
            var currentStateRepresentation = machine.CurrentStateRepresentation;


            var triggerRep = DiagnosticsHelper.FindAndEvaluateTriggerRepresentation(trigger, machine,
                                                                                    raiseInvalidStateOrTrigger);

            if (triggerRep == null)
            {
                return;
            }

            // Catch invalid parameters before execution.

            Action <Transition <TState, TTrigger> > triggerAction = null;

            try { triggerAction = (Action <Transition <TState, TTrigger> >)triggerRep.OnTriggerAction; }
            catch (InvalidCastException)
            {
                if (raiseInvalidStateOrTrigger)
                {
                    machine.RaiseInvalidTrigger(trigger);
                }

                return;
            }

            StateRepresentation <TState, TTrigger> nextStateRep = null;

            if (StateConfigurationHelper.CheckFlag(triggerRep.TransitionFlags,
                                                   TransitionFlag.DynamicState))
            {
                var dynamicState = ((Func <DynamicState <TState> >)triggerRep.NextStateRepresentationWrapper)();
                if (!dynamicState.CanTransition)
                {
                    return;
                }

                var state = dynamicState.ResultingState;
                nextStateRep = StateConfigurationHelper.FindStateRepresentation(state,
                                                                                machine.Representations);

                if (nextStateRep == null)
                {
                    if (raiseInvalidStateOrTrigger)
                    {
                        machine.RaiseInvalidState(state);
                    }
                    return;
                }
            }
            else
            {
                nextStateRep = (StateRepresentation <TState, TTrigger>)triggerRep.NextStateRepresentationWrapper;
            }


            var transition = new Transition <TState, TTrigger>(currentStateRepresentation.State, nextStateRep.State, trigger);

            machine.RaiseTransitionStarted(nextStateRep.State);

            // Current exit
            var currentExit = currentStateRepresentation.OnExitAction;

            currentExit?.Invoke(transition);

            // Trigger entry
            triggerAction?.Invoke(transition);

            // Next entry
            var nextEntry = nextStateRep.OnEntryAction;

            nextEntry?.Invoke(transition);

            var pastState = machine.CurrentState;

            machine.CurrentStateRepresentation = nextStateRep;
            machine.RaiseTransitionExecuted(pastState);
        }
 internal static TriggerRepresentation <TTrigger> FindTriggerRepresentation <TState, TTrigger>(
     TTrigger trigger,
     StateRepresentation <TState, TTrigger> representation)
 {
     return(representation.Triggers.Find(x => x.Trigger.Equals(trigger)));
 }