Ejemplo n.º 1
0
        public int GetStateToTransit(Transition transition)
        {
            // Check whether given parameter is valid
            if (isTransitionValid(transition)) { }

            // Check whether state has transition and return state id the transition is pointing to
            if (stateTransitions.ContainsKey(transition.TransitionID))
            {
                return stateTransitions[transition.TransitionID];
            }

            // if transition wasn't found, return -1, which is default id for non-existing state. In other words transition points to state itself.
            return -1;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Performs a transition to a new state. 
        /// </summary>
        /// <param name="transition"></param>
        public void PerformTransition(Transition transition)
        {
            // Check the state's transitions and get the id of the state where to transit. If state doesn't have transition, it returns -1, which means looping back to itself
            int statetoTransit = _currentState.GetStateToTransit(transition);

            if(statetoTransit != -1){
                foreach(State newState in _states){
                    if (newState.StateID == statetoTransit) {
                        // Before transiting to new state, let the old state finish up and the new one to initialize.
                        _currentState.onLeavingState();
                        _currentState = newState;
                        _currentState.onEnteringState();
                    }//if
                }//foreach
            }//if
        }
Ejemplo n.º 3
0
        // Class Behaviour
        public void AddTransition(Transition transition, State state)
        {
            // Check that the given parameters are valid.
            if (transition==null || state == null)
            {
                Console.WriteLine("ERROR in State AddTransition: given parameters are not valid!");
                return;
            }//if

            // Check for duplicates - a deterministic finite automata cannot have a transition with a same id multiple times.
            if (stateTransitions.ContainsKey(transition.TransitionID))
            {
                Console.WriteLine("ERROR in State AddTransition: transition with id of " + transition.TransitionID + " is already included in state's transitions!");
                return;
            }

            // Add transition
            stateTransitions.Add(transition.TransitionID, state._stateID);
        }
Ejemplo n.º 4
0
        public void RemoveTransition(Transition transition)
        {
            //Check whether given parameter is valid.
            if (isTransitionValid(transition)) { }

            //Check whether given transitions exists and remove transition from state
            if (stateTransitions.ContainsKey(transition.TransitionID))
            {
                stateTransitions.Remove(transition.TransitionID);
                Console.WriteLine("Removed transition with the id of " + transition.TransitionID.ToString() +
                    " and name of " + transition.TransitionName + " from state " + _stateName + " with id " + _stateID);
                return;
            }//if

            //If transition wasn't found, write error
            Console.WriteLine("ERROR in RemoveTransition: transition was not found in state's transitions");
        }
Ejemplo n.º 5
0
        private bool isTransitionValid(Transition transition)
        {
            if (transition == null)
            {
                Console.WriteLine("ERROR: given transition parameter is null");
                return false;
            }//if

            return true;
        }
Ejemplo n.º 6
0
        //TEST: Success
        private void CreateTestTransitions()
        {
            _transitionA = new Transition("a", 0);
            _transitionB = new Transition("b", 0);

            Console.WriteLine("Transition name: " + _transitionA.TransitionName + " transition ID: " + _transitionA.TransitionID.ToString());
            Console.WriteLine("Transition name: " + _transitionB.TransitionName + " transition ID: " + _transitionB.TransitionID.ToString());
        }