Exemplo n.º 1
0
        /// <summary>
        /// Registers the given state in the object map.
        /// </summary>
        /// <param name="state">The state you want to register.</param>
        public void RegisterState(SMState state)
        {
            if (this.commissioned)
            {
                throw new SMException("Unable to register states to a commissioned object map!");
            }

            if (!this.objectMap.ContainsKey(state))
            {
                this.objectMap.Add(state, state);
            }
            else
            {
                TraceManager.WriteAllTrace(string.Format("Warning! State '{0}' has already been registered!", state.Name),
                                           StateMachineController.SMC_INFO);
            }
        }
Exemplo n.º 2
0
        /// <see cref="ISMState.AddInternalTrigger"/>
        public void AddInternalTrigger(ISMState targetState, StateMachineController.TransitionHandler handler, RCSet <ISMState> neededStates)
        {
            if (this.stateMachine.Commissioned)
            {
                throw new SMException("Unable to add trigger to a commissioned state machine");
            }
            if (targetState == null)
            {
                throw new ArgumentNullException("targetState");
            }
            if (neededStates == null || neededStates.Count == 0)
            {
                throw new ArgumentNullException("neededStates");
            }

            SMState target = this.stateMachine.GetState(targetState.Name);

            if (target != null)
            {
                ISMState[] neededStatesArray = new ISMState[neededStates.Count];
                int        i = 0;
                foreach (ISMState st in neededStates)
                {
                    neededStatesArray[i] = st;
                    i++;
                }
                //RCSet<SMState> neededStateObjects = new RCSet<SMState>();
                //foreach (ISMState s in neededStates)
                //{
                //    SMState sObj = this.stateMachine.StateObjectMap.GetStateObject(s);
                //    if (null != sObj)
                //    {
                //        neededStateObjects.Add(sObj);
                //    }
                //    else { throw new SMException("State '" + s.Name + "' was not found in the object map!"); }
                //}
                SMInternalTrigger intTrigger =
                    new SMInternalTrigger(this, target, handler, new SMOperator(SMOperatorType.AND, neededStatesArray));
                this.stateMachine.RegisterTrigger(intTrigger);
            }
            else
            {
                throw new SMException("State '" + targetState.Name + "' doesn't exist!");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Constructs a trigger object.
        /// </summary>
        public SMTrigger(SMState source, SMState target, StateMachineController.TransitionHandler handler)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (source.SM != target.SM)
            {
                throw new SMException("Transition between states in different state machines not allowed!");
            }

            this.sourceState       = source;
            this.targetState       = target;
            this.transitionHandler = handler;
            this.sourceState.SM.CurrentStateChangedEvt += this.CurrentStateChanged;
        }
Exemplo n.º 4
0
        /// <see cref="IStateMachine.AddState"/>
        public void SetInitialState(ISMState state)
        {
            if (this.commissioned)
            {
                throw new SMException("Unable to set the initial state to a commissioned state machine!");
            }
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            SMState stateObj = GetState(state.Name);

            if (null == stateObj)
            {
                throw new ArgumentException("The state '" + state.Name + "' was not found in state machine '" + this.name + "'!");
            }

            this.currentState = stateObj;
        }
Exemplo n.º 5
0
 /// <summary>
 /// This function executes the firing of this state machine.
 /// </summary>
 /// <returns>
 /// True if a state change happened, false otherwise.
 /// </returns>
 public bool ExecuteFiring()
 {
     if (null != this.triggerHasBeenFired)
     {
         SMState prevState = this.currentState;
         this.currentState = this.triggerHasBeenFired.TargetState;
         this.triggerHasBeenFired.CallTransitionMethod();
         this.stateChanged          = true;
         this.evtArgs.currentState  = currentState;
         this.evtArgs.previousState = prevState;
         //this.CurrentStateChangedEvt(currentState, prevState);
         this.triggerHasBeenFired = null;
         this.stateMethodCalled   = false;
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 6
0
        public SMInternalTrigger(SMState source, SMState target, StateMachineController.TransitionHandler handler, SMOperator op)
            : base(source, target, handler)
        {
            //if (neededStates == null || neededStates.Count == 0) { throw new ArgumentNullException("neededStates"); }
            if (op == null)
            {
                throw new ArgumentNullException("op");
            }

            //this.sourceStateIsCurrent = false;
            RCSet <ISMState> neededStatesSet = new RCSet <ISMState>();

            op.CollectAllStates(ref neededStatesSet);
            this.operatorToCheck = op;

            this.neededStates = new Dictionary <ISMState, bool>();

            //RCSet<StateMachine> sms = new RCSet<StateMachine>();
            foreach (ISMState s in neededStatesSet)
            {
                SMState state = this.sourceState.SM.StateObjectMap.GetStateObject(s);
                if (state == null)
                {
                    throw new SMException("The state '" + s.Name + "' was not found in the object map!");
                }

                if (this.sourceState.SM != state.SM && this.sourceState.SM.SmController == state.SM.SmController)// && !sms.Contains(state.SM))
                {
                    //sms.Add(state.SM);
                    this.neededStates.Add(state, false);
                    state.SM.CurrentStateChangedEvt += this.CurrentStateChanged;
                    state.SM.CommissionedEvt        += this.StateMachineCommissioned;
                }
                else
                {
                    throw new SMException("Internal trigger operator error!");
                }
            }
        }
Exemplo n.º 7
0
        /// <see cref="IStateMachine.AddState"/>
        public ISMState AddState(string name, StateMachineController.StateHandler handler)
        {
            if (this.commissioned)
            {
                throw new SMException("Unable to add state to a commissioned state machine!");
            }
            if (name == null || name.Length == 0)
            {
                throw new ArgumentNullException("name");
            }

            if (!this.states.ContainsKey(name))
            {
                SMState newState = new SMState(name, handler, this);
                this.states.Add(name, newState);
                this.stateObjectMap.RegisterState(newState);
                return(newState);
            }
            else
            {
                throw new SMException("State '" + name + "' already exists in state machine '" + this.name + "'!");
            }
        }
Exemplo n.º 8
0
        /// <see cref="ISMState.AddExternalTrigger"/>
        public ISMTrigger AddExternalTrigger(ISMState targetState, StateMachineController.TransitionHandler handler)
        {
            if (this.stateMachine.Commissioned)
            {
                throw new SMException("Unable to add trigger to a commissioned state machine");
            }
            if (targetState == null)
            {
                throw new ArgumentNullException("targetState");
            }

            SMState target = this.stateMachine.GetState(targetState.Name);

            if (target != null)
            {
                SMExternalTrigger extTrigger = new SMExternalTrigger(this, target, handler);
                this.stateMachine.RegisterTrigger(extTrigger);
                return(extTrigger);
            }
            else
            {
                throw new SMException("State '" + targetState.Name + "' doesn't exist!");
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Called by the state machine when the current state has been changed.
 /// </summary>
 /// <param name="currState">The new current state.</param>
 /// <param name="prevState">The state that was previously the current state.</param>
 protected abstract void CurrentStateChanged(SMState currState, SMState prevState);
Exemplo n.º 10
0
 public CurrentStateChangedEvtArgs(SMState currState, SMState prevState)
 {
     currentState  = currState;
     previousState = prevState;
 }