Пример #1
0
        private Transition GenerateNewTransition(State state, Symbol symbol)
        {
            var transitions       = _nfsm.Transitions;
            var multyState        = new MultyState(state);
            var newMultyStateTemp = new HashSet <State>();

            for (int i = 0; i < multyState.Count; i++)
            {
                var temp = transitions.FindAll(a => (multyState[i] == a.CurrentState && symbol == a.Symbol));
                if (temp.Count != 0)
                {
                    foreach (var tr in temp)
                    {
                        var func = GetEpsillonTransitionStates(tr.NextState);
                        foreach (var st in func)
                        {
                            newMultyStateTemp.Add(st);
                        }
                    }
                }
            }
            var oldState      = new State(multyState.ToString());
            var newMultyState = new MultyState(newMultyStateTemp);

            if (newMultyStateTemp.Count == 0)
            {
                return(new Transition(oldState, new State("#"), symbol));
            }
            newMultyState.Sort(_dict, _reverseDict);
            var newState = new State(newMultyState.ToString());

            return(new Transition(oldState, newState, symbol));
        }
Пример #2
0
        private State GetNewStartState()
        {
            var multState = new MultyState(GetEpsillonTransitionStates(_nfsm.StartState));

            multState.Sort(_dict, _reverseDict);
            return(multState.ToState());
        }
Пример #3
0
        private List <State> GetFinalStates(List <State> allStates)
        {
            var finalStates = new List <State>();

            foreach (var st in allStates)
            {
                var mult = new MultyState(st);
                for (int i = 0; i < mult.Count; i++)
                {
                    if (_nfsm.FinalStates.Contains(mult[i]))
                    {
                        finalStates.Add(st);
                        break;
                    }
                }
            }
            return(finalStates);
        }
Пример #4
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));
        }