コード例 #1
0
        /// <summary>
        /// Creates new state to the automata. ( This version takes State object as a parameter.)
        /// </summary>
        /// <param name="state">Previously created automata state object.</param>
        /// <returns>Returns the result of add operation is success or fail.</returns>
        public bool AddState(FAState state)
        {
            if (state is null)
            {
                return(false);
            }

            bool hasInitialState = this.States.Any(x => x.IsInitialState);

            // TR: Önceden eklenmiş bir başlangıç durumu varsa ve bu durumda başlangıç durumu olarak eklenmek isteniyorsa buradan geri dön.
            if ((hasInitialState && state.IsInitialState))
            {
                return(false);

                throw new MultipleFAInitalStateException();
            }

            if (States.Contains(state))
            {
                return(false);

                throw new DuplicateFAStateException();
            }

            this._states.Add(state);

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Runs the finite automata with input, then returns true if input is accepted.
        /// </summary>
        /// <param name="input">Input string.</param>
        /// <returns>If input is valid for the finite automata, that will be return true otherwise false.</returns>
        public bool Run(string input)
        {
            if (!IsValid)
            {
                return(false);
            }

            FAState      currentState = InitialState;
            FATransition currentTransition;

            foreach (char letter in input)
            {
                Console.Write("Transition {0} - {1} >>> ", letter, currentState.StateName);

                currentTransition = this.Transitions.FirstOrDefault(x => x.FromState.StateName == currentState.StateName && x.TransitionSymbol == letter);
                currentState      = currentTransition.ToState.RandomState();
                Console.WriteLine(currentState.StateName);
            }

            Console.WriteLine("Current State is {0}.(Final State: {1})", currentState.StateName, currentState.IsFinalState ? "Yes" : "No");
            if (currentState.IsFinalState)
            {
                return(true);
            }

            return(false);
        }
コード例 #3
0
        /// <summary>
        /// Creates new state to the automata.
        /// </summary>
        /// <param name="stateName">Name of the state.</param>
        /// <param name="isInitialState">Is the new state inital state?</param>
        /// <param name="isFinalState">Is the new state final state?</param>
        /// <returns></returns>
        public bool AddState(string stateName, bool isInitialState = false, bool isFinalState = false)
        {
            bool hasInitialState = this.States.Any(x => x.IsInitialState);

            // TR: Önceden eklenmiş bir başlangıç durumu varsa ve bu durumda başlangıç durumu olarak eklenmek isteniyorsa buradan geri dön.
            if ((hasInitialState && isInitialState))
            {
                return(false);

                throw new MultipleFAInitalStateException();
            }

            if (this.GetStateByName(stateName) != null)
            {
                return(false);

                throw new DuplicateFAStateException();
            }

            FAState state = new FAState(stateName, isInitialState, isFinalState);

            this._states.Add(state);

            return(true);
        }
コード例 #4
0
 /// <summary>
 /// Initializes Finite Automata Transition Object
 /// </summary>
 /// <param name="transitionSymbol">Symbol from alphabet that shows which transition will be </param>
 /// <param name="fromState">Shows transition source state</param>
 /// <param name="toState">Shows transition destination states.A state will be chosen when runtime.(Multiple value allowed for Non-deterministic FA)</param>
 /// <param name="direction"> Automata direction 0= Left, 1= Right</param>
 public FATransition(char transitionSymbol, FAState fromState, IEnumerable <FAState> toState, bool direction)
 {
     this._transitionSymbol = transitionSymbol;
     this._fromState        = fromState;
     this._toState          = toState;
     this._direction        = direction;
 }
コード例 #5
0
        public bool UpdateState(FAState state)
        {
            FAState previousState = this.GetStateByName(state.StateName);

            this._states.Remove(previousState);
            this._states.Add(state);
            return(true);
        }
コード例 #6
0
 /// <summary>
 /// Initializes Finite Automata Transition Object
 /// </summary>
 /// <param name="transitionSymbol">Symobl from alphabet that shows which transition will be </param>
 /// <param name="fromState">Shows transition source state</param>
 /// <param name="toState">Shows transition destination state </param>
 public FATransition(char transitionSymbol, FAState fromState, FAState toState)
 {
     this._transitionSymbol = transitionSymbol;
     this._fromState        = fromState;
     this._toState          = new List <FAState>()
     {
         toState
     };
 }
コード例 #7
0
        /// <summary>
        /// Creates new state to the automata.
        /// </summary>
        /// <param name="isInitialState">Is the new state inital state?</param>
        /// <param name="isFinalState">Is the new state final state?</param>
        /// <returns>Returns the result of add operation is success or fail.</returns>
        public bool AddState(bool isInitialState = false, bool isFinalState = false)
        {
            bool hasInitialState = this.States.Any(x => x.IsInitialState);

            // TR: Önceden eklenmiş bir başlangıç durumu varsa ve bu durumda başlangıç durumu olarak eklenmek isteniyorsa buradan geri dön.
            if ((hasInitialState && isInitialState))
            {
                return(false);

                throw new MultipleFAInitalStateException();
            }

            string  stateName = string.Concat("Q", _stateCounter++);
            FAState state     = new FAState(stateName, isInitialState, isFinalState);

            this._states.Add(state);

            return(true);
        }
コード例 #8
0
        /// <summary>
        /// Gets automata state by state name.
        /// </summary>
        /// <param name="name">A state name which will be get.</param>
        /// <returns>Returns FAState object by state name.</returns>
        private FAState GetStateByName(string name)
        {
            FAState state = this.States.FirstOrDefault(x => x.StateName.ToLower() == name.ToLower());

            return(state);
        }
コード例 #9
0
        /// <summary>
        /// Creates new transition link between states.
        /// </summary>
        /// <param name="symbol">Transition Symbol</param>
        /// <param name="fromStateName">Where will be transited from? State Name</param>
        /// <param name="toStateName">Where will be transited to? State Name ( Multiple values must be separated by comma: ",")</param>
        /// <returns>Returns the result of add operation is success or fail.</returns>
        public bool AddTransition(char symbol, string fromStateName, string toStateName, int direction = -1)
        {
            string[] toStateArray = toStateName.Split(',');

            if (AutomataType == FiniteAutomataType.DFA && toStateArray.Length > 1)
            {
                return(false);
            }

            // TR: Yapılacak geçiş, geçiş alfabesinde yoksa buradan geri dön.
            if (!_alphabet.Contains(symbol))
            {
                return(false);

                throw new InvalidFALetterException();
            }


            // TR: Kaynak durumu adına göre bul.
            FAState fromState = this.GetStateByName(fromStateName);

            if (fromState == null)
            {
                return(false);
            }

            // TR: Hedef durumları adına göre bul.
            List <FAState> toStatesList = new List <FAState>();

            foreach (string selectedState in toStateArray)
            {
                FAState toState = this.GetStateByName(selectedState);
                if (toState == null)
                {
                    return(false);
                }

                toStatesList.Add(toState);
            }

            FATransition transitionModel = null;

            if (direction == 0 || direction == 1)
            {
                transitionModel = new FATransition(symbol, fromState, toStatesList, direction == 1);
            }
            else
            {
                // TR: Geçiş nesnesini oluştur.
                transitionModel = new FATransition(symbol, fromState, toStatesList);
            }

            // TR: Geçiş daha önce tanımlanmışsa hata fırlat.
            bool hasTransition = this._transitions.Any(x => x.CompareTo(transitionModel) == 0);

            if (hasTransition)
            {
                return(false);

                throw new DuplicateFATransactionException();
            }

            this._transitions.Add(transitionModel);
            return(true);
        }
コード例 #10
0
 /// <summary>
 /// Initializes Finite Automata Transition Object
 /// </summary>
 /// <param name="transitionSymbol">Symbol from alphabet that shows which transition will be </param>
 /// <param name="fromState">Shows transition source state</param>
 /// <param name="toState">Shows transition destination states.A state will be chosen when runtime.(Multiple value allowed for Non-deterministic FA) </param>
 public FATransition(char transitionSymbol, FAState fromState, IEnumerable <FAState> toState)
 {
     this._transitionSymbol = transitionSymbol;
     this._fromState        = fromState;
     this._toState          = toState;
 }