Exemplo n.º 1
0
 public static void CheckState(this IStateMachineDefinition def, string stateId)
 {
     if (def.GetStateById(stateId) == null)
     {
         throw new ArgumentException("StateMachine: State was not found!");
     }
 }
        /// <summary>
        /// Creates or returns state
        /// </summary>
        /// <param name="def">Definition</param>
        /// <param name="id">id of state machine</param>
        /// <param name="action">action to be executed on given state</param>
        /// <returns></returns>
        public static IState GetOrCreateState(this IStateMachineDefinition def, string id, Action <IState> action = null)
        {
            var state = def.GetStateById(id);

            if (state == null)
            {
                state = new State()
                {
                    id = id
                };
                def.States.Add(state);
            }

            if (action != null)
            {
                action(state);
            }

            return(state);
        }
Exemplo n.º 3
0
        public virtual void BuildUpDefinition(IStateMachine sm, IStateMachineDefinition def)
        {
            Affirm.ArgumentNotNull(sm, "sm");
            Affirm.ArgumentNotNull(def, "def");

            def.Validate();

            sm.CurrentState = def.GetStateById(sm.CurrentStateId);

            var childContainer = _serviceProvider.CreateScope();

            //childContainer.RegisterInstance(typeof(IStateMachine), sm);
            //childContainer.RegisterInstance(sm.GetType(), sm);
            sm.Container = childContainer;

            var actionFabric  = sm.Container.ServiceProvider.GetRequiredService <IActionFabric>();
            var triggerFabric = sm.Container.ServiceProvider.GetRequiredService <ITriggerFabric>();

            Action <IEnumerable <IActionHolder> > buildUpActions = (s) =>
            {
                foreach (var act in s)
                {
                    act.NestedAction = actionFabric.Get(act.Code);
                    if (act.NestedAction == null)
                    {
                        throw new StateMachineException(sm.SmId, $"Can not resolve {act.Code} from ation fabric");
                    }
                    act.NestedAction.StateMachine = sm;
                }
            };


            foreach (var state in def.States)
            {
                state.StateMachine = sm;

                if (state.EnterActions != null)
                {
                    buildUpActions(state.EnterActions);
                }

                if (state.ExitActions != null)
                {
                    buildUpActions(state.ExitActions);
                }
            }

            foreach (var tran in def.Transitions)
            {
                tran.StateMachine = sm;

                if (tran.Trigger != null &&
                    !string.IsNullOrEmpty(tran.Trigger.Code))
                {
                    var trigger = triggerFabric.Get(tran.Trigger.Code);

                    if (trigger == null)
                    {
                        throw new StateMachineException(sm.SmId, $"Can not resolve {tran.Trigger.Code} from trigger fabric");
                    }

                    trigger.StateMachine      = sm;
                    tran.Trigger.NestedAction = trigger;
                }
            }

            sm.SetDefinition(def);
        }