public void SetDefinition(IStateMachineDefinition definition)
        {
            Affirm.ArgumentNotNull(definition, "definition");

            definition.Validate();

            Definition = definition;
        }
Exemplo n.º 2
0
        IStateMachine Start(IStateMachineDefinition def, ISMParameters paramters, IStateMachine sm)
        {
            Affirm.ArgumentNotNull(def, "definition");
            Affirm.ArgumentNotNull(sm, "machine");

            def.Validate();

            Log.Debug("SM with {0} is going to be started".FormIt(sm.GetType()));

            sm.SmId = Guid.NewGuid();
            _persistenceService.BuildUpDefinition(sm, def);

            var initialState = sm.Definition.GetInitialState();

            if (initialState == null)
            {
                throw new InvalidOperationException("{0}: can not find intitial state".FormIt(sm));
            }

            return(MoveToState(sm, initialState, paramters));
        }
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);
        }