//TEST: Success. private void CreateTestStates() { _testStateA = new DefaultState("A", 0); _testStateB = new DefaultState("B", 0); Console.WriteLine("State name is: " + _testStateA.StateName + " and ID: " + _testStateA.StateID.ToString()); Console.WriteLine("State name is: " + _testStateB.StateName + " and ID: " + _testStateB.StateID.ToString()); }
// 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); }
/// <summary> /// Adds the given state to dfa. /// </summary> /// <param name="stateToAdd"></param> public void AddState(State stateToAdd) { //Check whether given state already exists in DFA or the given state has same ID or name as some other state in DFA foreach (State existingState in _states) { if(existingState.StateID == stateToAdd.StateID){ Console.WriteLine("ERROR: Cannot add state to the dfa with same id!"); return; }//if if (existingState.StateName.Equals(stateToAdd.StateName)) { Console.WriteLine("ERROR: Cannot add state to the dfa with same name!"); return; }//if }//foreach // Add the state _states.Add(stateToAdd); }
/// <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 }
/// <summary> /// Removes the given state from dfa. /// </summary> /// <param name="StateToRemove"></param> public void RemoveState(State StateToRemove) { // Check whether the state to be removed is current state. Removing current state is not allowed. if(StateToRemove.StateID == _currentState.StateID){ Console.WriteLine("ERROR: Cannot remove state that is currently active."); return; }//if // Loops through dfa's states and compares their id to given state's id. If match is found, state is removed from dfa. foreach(State existingState in _states){ if(existingState.StateID == StateToRemove.StateID){ _states.Remove(StateToRemove); Console.WriteLine("Removed state " + StateToRemove.StateName + " with an id of " + StateToRemove.StateID.ToString() + " from dfa."); return; }//if }//foreach //If state was not found, print error Console.WriteLine("ERROR: state " + StateToRemove.StateName + " with an id of " + StateToRemove.StateID.ToString() + " was not found in dfa."); }