Esempio n. 1
0
        private State GetNewStartState()
        {
            var multState = new MultyState(GetEpsillonTransitionStates(_nfsm.StartState));

            multState.Sort(_dict, _reverseDict);
            return(multState.ToState());
        }
Esempio n. 2
0
        public FiniteStateMachine Build(FiniteStateMachine nfsm)
        {
            _nfsm = nfsm;

            _dict        = new Dictionary <State, int>();
            _reverseDict = new Dictionary <int, State>();
            for (int i = 0; i < _nfsm.States.Count; i++)
            {
                _dict.Add(_nfsm.States[i], i);
                _reverseDict.Add(i, _nfsm.States[i]);
            }

            var newStartState = GetNewStartState();

            _alphabet = GenerateAlphabet();

            var states = new List <State>();

            states.Add(newStartState);

            var transitions = new List <Transition>();

            var queue = new Queue <State>();



            foreach (var symbol in _alphabet)
            {
                var temp1 = GenerateNewTransition(newStartState, symbol);
                transitions.Add(temp1);
                var temp = temp1.NextState;
                if (!states.Contains(temp))
                {
                    queue.Enqueue(temp);
                }
            }

            while (true)
            {
                if (queue.Count == 0)
                {
                    break;
                }
                var nowState = queue.Dequeue();

                var tempMult = new MultyState(nowState);
                tempMult.Sort(_dict, _reverseDict);
                nowState = tempMult.ToState();

                if (!states.Contains(nowState))
                {
                    states.Add(nowState);
                }

                foreach (var symbol in _alphabet)
                {
                    var temp1 = GenerateNewTransition(nowState, symbol);

                    if (temp1.NextState == "#")
                    {
                        if (transitions.Contains(temp1))
                        {
                            break;
                        }
                    }
                    transitions.Add(temp1);
                    var temp = temp1.NextState;


                    if (!states.Contains(temp) && !queue.Contains(temp))
                    {
                        queue.Enqueue(temp);
                    }
                }
            }


            var finalStates = GetFinalStates(states);

            states.Remove("#");
            transitions.RemoveAll(a => (a.CurrentState == "#" || a.NextState == "#"));
            finalStates.RemoveAll(a => (a == "#"));


            return(new FiniteStateMachine(states, transitions, finalStates, newStartState));
        }