Esempio n. 1
0
        public IStateMachine Start(IStateMachineDefinition def, ISMParameters parameters, string key)
        {
            Affirm.ArgumentNotNull(key, "key");
            var sm = _sMFactory.Get(key);

            return(Start(def, parameters, sm));
        }
Esempio n. 2
0
        private ITransition GetNextTransition(IStateMachine sm, ISMParameters parameters)
        {
            Affirm.ArgumentNotNull(sm, "sm");

            var possibleTransitions = sm.Definition.GetTransitions(sm.CurrentState);

            if (possibleTransitions == null)
            {
                throw new InvalidOperationException("{0}: no transitions found {1}".FormIt(sm));
            }

            var triggers = possibleTransitions.Select(_ => new { trigger = _.Trigger, tran = _ }).ToList();

            var theOne = triggers.Where(_ => _.trigger.Invoke(_.tran, parameters)).ToList();

            if (!theOne.Any())
            {
                throw new NoTrueTriggerException(sm.SmId, "{0}: no true trigger result found for state {1}".FormIt(sm, sm.CurrentStateId));
            }

            if (theOne.Count() > 1)
            {
                throw new TooManyTriggersException(theOne.Select(_ => _.trigger).ToList(), sm.SmId, "{0}: too many triggers are true {1}".FormIt(sm, sm.CurrentStateId));
            }

            return(theOne.First().tran);
        }
 /// <summary>
 /// Execution of the action with parameters from state machine push
 /// </summary>
 /// <param name="TransitionParameters">Parameters from state machine push <see cref="ISMService.Push(IStateMachine, ISMParameters)"/></param>
 public void Invoke(ISMParameters TransitionParameters)
 {
     if (NestedAction != null)
     {
         NestedAction.Invoke(Parameters, TransitionParameters);
     }
 }
Esempio n. 4
0
        public void ExitState(IState state, ISMParameters parameters)
        {
            Affirm.ArgumentNotNull(state, "state");

            if (state.ExitActions != null)
            {
                InvokeActionList(state, parameters, state.ExitActions);
            }
        }
Esempio n. 5
0
        private void InvokeActionList(IState state, ISMParameters parameters, IEnumerable <IActionHolder> source)
        {
            Affirm.ArgumentNotNull(state, "state");

            if (source != null)
            {
                foreach (var act in GetOrdered(source))
                {
                    act.Invoke(parameters);
                }
            }
        }
Esempio n. 6
0
        private IStateMachine MoveToState(IStateMachine sm, IState state, ISMParameters parameters)
        {
            Affirm.ArgumentNotNull(sm, "sm");
            Affirm.ArgumentNotNull(state, "state");

            Log.Debug("{0} is going to be moved to state {1}".FormIt(sm, state.id));

            _stateProcessor.ProcessState(state, parameters);

            sm.SetCurrentState(state);

            return(sm);
        }
        /// <summary>
        /// Execution of the action with parameters from state machine push
        /// </summary>
        /// <param name="transition">Transition to which trigger belongs <see cref="ITransition"/></param>
        /// <param name="TransitionParameters">Parameters from state machine push <see cref="ISMService.Push(IStateMachine, ISMParameters)"/></param>
        public bool Invoke(ITransition transition, ISMParameters TransitionParameters)
        {
            if (NestedAction != null)
            {
                Log.Debug("trigger {0} for tansactions {1} is going to be executed".FormIt(NestedAction.GetType().ToString(), transition.Text));

                var result = NestedAction.Invoke(transition, Parameters, TransitionParameters);

                var inv = Inverted ? !result : result;

                Log.Debug("Trigger result is {0}".FormIt(inv));

                return(inv);
            }

            return(false);
        }
Esempio n. 8
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));
        }
Esempio n. 9
0
        public IStateMachine Start(IStateMachineDefinition def, ISMParameters parameters, Type smType)
        {
            Affirm.ArgumentNotNull(smType, "smType");
            var sm = _serviceProvider.GetService(smType) as IStateMachine;

            if (sm == null)
            {
                throw new ArgumentNullException("Given type {0} is not assingable from IStateMachine or can not be resolved".FormIt(smType));
            }

            try
            {
                Start(def, parameters, sm);
            }
            catch
            {
                sm?.Dispose();

                throw;
            }

            return(sm);
        }
Esempio n. 10
0
        public IStateMachine Push(IStateMachine sm, ISMParameters parameters)
        {
            Affirm.ArgumentNotNull(sm, "sm");

            Log.Debug("{0} going to be pushed".FormIt(sm));

            if (sm.Finished)
            {
                throw new InvalidOperationException("{0} is finished already! can not push!".FormIt(sm));
            }

            _stateProcessor.ExitState(sm.CurrentState, parameters);

            var nextState = GetNextTransition(sm, parameters).TargetStateId;

            var state = sm.Definition.GetStateById(nextState);

            if (state == null)
            {
                throw new InvalidOperationException("{0}: State {1} not found!".FormIt(sm, nextState));
            }

            return(MoveToState(sm, state, parameters));
        }
Esempio n. 11
0
 public T Start <T>(ISMParameters parameters, IStateMachineDefinition definition) where T : class, IStateMachine
 {
     return(Start(definition, parameters, typeof(T)) as T);
 }
 public void SetParameters(ISMParameters parameters)
 {
     Parameters = parameters as SMParametersCollection;
 }