Ejemplo n.º 1
0
        public void Should_ReturnTrue_When_IsValid_AutomataIsValidDFA()
        {
            List <char> alphabet = new List <char> {
                'a', 'b', 'c'
            };
            FiniteAutomata dfaTest = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            _ = dfaTest.AddState(true);                 // q0
            _ = dfaTest.AddState();                     // q1
            _ = dfaTest.AddState(isFinalState: true);   // q2

            _ = dfaTest.AddTransition('a', "q0", "q1"); // TR: q0 'a' geçiþi ile q1'e gider
            _ = dfaTest.AddTransition('b', "q0", "q2"); // TR: q0 'b' geçiþi ile q2'ye gider.
            _ = dfaTest.AddTransition('c', "q0", "q0"); // TR: q0 'c' geçiþi ile q0'a gider.

            _ = dfaTest.AddTransition('a', "q1", "q0"); // TR: q1 'a' geçiþi ile q0'a gider.
            _ = dfaTest.AddTransition('b', "q1", "q1"); // TR: q1 'b' geçiþi ile q1'e gider.
            _ = dfaTest.AddTransition('c', "q1", "q2"); // TR: q1 'c' geçiþi ile q2'ye gider.

            _ = dfaTest.AddTransition('a', "q2", "q0"); // TR: q2 'a' geçiþi ile q0'a gider.
            _ = dfaTest.AddTransition('b', "q2", "q2"); // TR: q2 'b' geçiþi ile q2'ye gider.
            _ = dfaTest.AddTransition('c', "q2", "q2"); // TR: q2 'c' geçiþi ile q2'ye gider.

            Assert.IsTrue(dfaTest.IsValid);
        }
Ejemplo n.º 2
0
        public void Test_NFA_RUN_Should_Return_False_Invalid_Input()
        {
            var         inputString = "10001";
            List <char> alphabet    = new List <char> {
                '0', '1'
            };
            FiniteAutomata nfaTest = new FiniteAutomata(AutomataType.NFA, alphabet);

            _ = nfaTest.AddState("A", isInitialState: true, false); //A
            _ = nfaTest.AddState("B", false, false);                //B
            _ = nfaTest.AddState("C", false, isFinalState: true);   //C

            _ = nfaTest.AddTransition('0', "A", "A");               // TR: A '0'
            _ = nfaTest.AddTransition('1', "A", "B,C");             // TR: A '1'

            _ = nfaTest.AddTransition('0', "B", "A");               // TR: B '0'
            _ = nfaTest.AddTransition('1', "B", "A,C");             // TR: B '1'

            _ = nfaTest.AddTransition('0', "C", "A,B");             // TR: C '0'
            _ = nfaTest.AddTransition('1', "C", "C");               // TR: C '1'

            var result = nfaTest.Run(inputString);

            Assert.AreEqual(false, result);
        }
        public void Should_ReturnsDFAAutomata_When_Convert2DFAToDFA_WithValid2DFA()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata twdfaTest = new FiniteAutomata(FiniteAutomataType.TwoWayDFA, alphabet);

            _ = twdfaTest.AddState("q0", isInitialState: true);
            _ = twdfaTest.AddState("q1");
            _ = twdfaTest.AddState("q2");
            _ = twdfaTest.AddState("q3", isFinalState: true);

            _ = twdfaTest.AddTransition('0', "q0", "q1", 1);
            _ = twdfaTest.AddTransition('1', "q0", "q2", 1);

            _ = twdfaTest.AddTransition('0', "q1", "q3", 0);
            _ = twdfaTest.AddTransition('1', "q1", "q2", 0);

            _ = twdfaTest.AddTransition('0', "q2", "q2", 1);
            _ = twdfaTest.AddTransition('1', "q2", "q3", 1);

            _ = twdfaTest.AddTransition('0', "q3", "q1", 1);
            _ = twdfaTest.AddTransition('1', "q3", "q2", 0);

            FiniteAutomataConverter automataConverter = new FiniteAutomataConverter();
            FiniteAutomata          converterDFA      = automataConverter.Convert2DFAToDFA(twdfaTest);

            if (converterDFA.InitialState.StateName == twdfaTest.InitialState.StateName &&
                converterDFA.FinalState.Count() == twdfaTest.FinalState.Count())
            {
                Assert.Pass();
            }

            Assert.Fail();
        }
        public void Should_ReturnsDFAAutomata_When_ConvertNFAToDFA_WithValidNFA()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata nfaTest = new FiniteAutomata(FiniteAutomataType.NFA, alphabet);

            _ = nfaTest.AddState("A", isInitialState: true); //A
            _ = nfaTest.AddState("B");                       //B
            _ = nfaTest.AddState("C", isFinalState: true);   //C

            _ = nfaTest.AddTransition('0', "A", "A");        // TR: A '0' geçişiyle A'ya gider.
            _ = nfaTest.AddTransition('1', "A", "B,C");      // TR: A '1' geçişiyle B ya da C'ye gider.

            _ = nfaTest.AddTransition('0', "B", "A");        // TR: B '0' geçişiyle B'ye gider.
            _ = nfaTest.AddTransition('1', "B", "A,C");      // TR: B '1' geçişiyle A ya da C'ye gider.

            _ = nfaTest.AddTransition('0', "C", "A,B");      // TR: C '0' geçişiyle A ya da B'ye gider.
            _ = nfaTest.AddTransition('1', "C", "C");        // TR: C '1' geçişiyle C'ye gider.

            FiniteAutomata dfaTest = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            _ = dfaTest.AddState("A", isInitialState: true);
            _ = dfaTest.AddState("B&C", isFinalState: true);
            _ = dfaTest.AddState("A&B");
            _ = dfaTest.AddState("A&C", isFinalState: true);
            _ = dfaTest.AddState("A&B&C", isFinalState: true);

            _ = dfaTest.AddTransition('0', "A", "A");
            _ = dfaTest.AddTransition('1', "A", "B&C");

            _ = dfaTest.AddTransition('0', "B&C", "A&B");
            _ = dfaTest.AddTransition('1', "B&C", "A&C");

            _ = dfaTest.AddTransition('0', "A&B", "A");
            _ = dfaTest.AddTransition('1', "A&B", "A&B&C");

            _ = dfaTest.AddTransition('0', "A&C", "A&B");
            _ = dfaTest.AddTransition('1', "A&C", "B&C");

            _ = dfaTest.AddTransition('0', "A&B&C", "A&B");
            _ = dfaTest.AddTransition('1', "A&B&C", "A&B&C");


            FiniteAutomataConverter automataConverter = new FiniteAutomataConverter();
            FiniteAutomata          converterDFA      = automataConverter.ConvertNFAToDFA(nfaTest);

            if (converterDFA.InitialState.StateName == dfaTest.InitialState.StateName &&
                converterDFA.FinalState.Count() == dfaTest.FinalState.Count() &&
                converterDFA.States.Count() == dfaTest.States.Count() &&
                converterDFA.Transitions.Count() == dfaTest.Transitions.Count() &&
                converterDFA.States.Count() == dfaTest.States.Count() &&
                converterDFA.Transitions.Last().TransitionSymbol == dfaTest.Transitions.Last().TransitionSymbol)
            {
                Assert.Pass();
            }

            Assert.Fail();
        }
Ejemplo n.º 5
0
        public void Test_Should_ReturnsDFAAutomata_When_ConvertNFAToDFA_WithValidNFA()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata nfaTest = new FiniteAutomata(AutomataType.NFA, alphabet);

            _ = nfaTest.AddState("A", isInitialState: true, false); //A
            _ = nfaTest.AddState("B", false, false);                //B
            _ = nfaTest.AddState("C", false, isFinalState: true);   //C

            _ = nfaTest.AddTransition('0', "A", "A");               // TR: A '0'
            _ = nfaTest.AddTransition('1', "A", "B,C");             // TR: A '1'

            _ = nfaTest.AddTransition('0', "B", "A");               // TR: B '0'
            _ = nfaTest.AddTransition('1', "B", "A,C");             // TR: B '1'

            _ = nfaTest.AddTransition('0', "C", "A,B");             // TR: C '0'
            _ = nfaTest.AddTransition('1', "C", "C");               // TR: C '1'

            FiniteAutomata dfaTest = new FiniteAutomata(AutomataType.DFA, alphabet);

            _ = dfaTest.AddState("A", isInitialState: true);
            _ = dfaTest.AddState("B&C", isFinalState: true);
            _ = dfaTest.AddState("A&B", false, false);
            _ = dfaTest.AddState("A&C", isFinalState: true);
            _ = dfaTest.AddState("A&B&C", isFinalState: true);

            _ = dfaTest.AddTransition('0', "A", "A");
            _ = dfaTest.AddTransition('1', "A", "B&C");

            _ = dfaTest.AddTransition('0', "B&C", "A&B");
            _ = dfaTest.AddTransition('1', "B&C", "A&C");

            _ = dfaTest.AddTransition('0', "A&B", "A");
            _ = dfaTest.AddTransition('1', "A&B", "A&B&C");

            _ = dfaTest.AddTransition('0', "A&C", "A&B");
            _ = dfaTest.AddTransition('1', "A&C", "B&C");

            _ = dfaTest.AddTransition('0', "A&B&C", "A&B");
            _ = dfaTest.AddTransition('1', "A&B&C", "A&B&C");


            Convertor      automataConverter = new Convertor();
            FiniteAutomata converterDFA      = automataConverter.NFAToDFA(nfaTest);

            if (converterDFA.InitialState().StateName == dfaTest.InitialState().StateName &&
                converterDFA.FinalState().Count() == dfaTest.FinalState().Count() &&
                converterDFA.states.Count() == dfaTest.states.Count() &&
                converterDFA.transitions.Count() == dfaTest.transitions.Count() &&
                converterDFA.transitions.Last().transitionSymbol == dfaTest.transitions.Last().transitionSymbol)
            {
                Assert.Pass();
            }

            Assert.Fail();
        }
Ejemplo n.º 6
0
        public void Should_ReturnTrue_When_AddState_MultipleAddFinalState()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            _ = automata.AddState(isFinalState: true);
            Assert.True(automata.AddState(isFinalState: true));
        }
Ejemplo n.º 7
0
        public void Should_ReturnFalse_When_AddTransition_WithInvalidToStateName()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            _ = automata.AddState("q1", isInitialState: true);
            _ = automata.AddState("q2", isFinalState: true);

            Assert.False(automata.AddTransition('a', "q1", "qqqq"));
        }
Ejemplo n.º 8
0
        public void Should_ReturnTrue_When_AddTransition_WithValidSymbol()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            _ = automata.AddState("q1", isInitialState: true);
            _ = automata.AddState("q2", isFinalState: true);

            Assert.True(automata.AddTransition('a', "q1", "q2"));
        }
Ejemplo n.º 9
0
        private FAState GetOptimalState(FAState sourceState, IEnumerable <FATransition> filterTransition, FiniteAutomata DFA)
        {
            FAState selectedState = null;
            IEnumerable <FAState> finalStateFilter = filterTransition.SelectMany(x => x.ToState.Where(y => y.IsFinalState));

            if (finalStateFilter.Count() == 1)
            {
                selectedState = finalStateFilter.First();
            }
            else
            {
                bool xLoop = filterTransition.All(x => x.FromState == sourceState &&
                                                  x.ToState.Any(y => y.StateName == sourceState.StateName) &&
                                                  x.FromState.IsFinalState);
                if (xLoop)
                {
                    string  emptySName = "Empty";
                    FAState emptySet   = new FAState(emptySName);
                    if (!DFA.States.Any(x => x.StateName == emptySet.StateName))
                    {
                        _ = DFA.AddState(emptySet);

                        foreach (char symbol in DFA.Alphabet)
                        {
                            _ = DFA.AddTransition(symbol, emptySName, emptySName);
                        }
                    }
                    selectedState = emptySet;
                    return(selectedState);
                }

                IEnumerable <FAState> selfRefStates = filterTransition.Where(x => x.ToState.Any(y => y == sourceState && x.FromState == sourceState) && !x.Direction)
                                                      .Select(x => x.FromState);
                FAState selfRefSt = selfRefStates.FirstOrDefault();
                if (selfRefSt != null)
                {
                    FAState updState = new FAState(selfRefSt.StateName, selfRefSt.IsInitialState);
                    DFA.UpdateState(updState);
                    selectedState = updState;
                    return(selectedState);
                }


                IEnumerable <FAState> selfReferenceFilter = filterTransition.SelectMany(x => x.ToState.Where(y => y != sourceState));
                if (selfReferenceFilter.Count() == 1)
                {
                    selectedState = selfReferenceFilter.First();
                }
                else
                {
                    selectedState = filterTransition.First().FromState;
                }
            }

            return(selectedState);
        }
Ejemplo n.º 10
0
        public void Should_ReturnTrue_When_AddState_WithoutParamter()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            Assert.IsTrue(automata.AddState());
        }
Ejemplo n.º 11
0
        public void Should_ReturnTrue_When_AddState_PassStateNameAndInitialStateAndFinalState()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            Assert.True(automata.AddState("State Name", isInitialState: true, isFinalState: true));
        }
Ejemplo n.º 12
0
        public void Should_ReturnTrue_When_AddState_WithOnlyPassIsFinalStateParameter()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            Assert.IsTrue(automata.AddState(isFinalState: true));
        }
Ejemplo n.º 13
0
        public void Should_ReturnFalse_When_AddState_WithNullFaState()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);
            FAState        state    = null;

            Assert.False(automata.AddState(state));
        }
Ejemplo n.º 14
0
        public void Should_ReturnTrue_When_AddState_WithFAState()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);
            FAState        state    = new FAState("State");

            Assert.True(automata.AddState(state));
        }
Ejemplo n.º 15
0
        public void Should_Equal_When_InitalState_ComparedByInitialState()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.NFA, alphabet);

            FAState state = new FAState("A", isInitialState: true);

            automata.AddState(state);

            Assert.AreEqual(state, automata.InitialState);
        }
Ejemplo n.º 16
0
        public void Should_ReturnTrue_When_UpdateState_WithFAState()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.TwoWayDFA, alphabet);
            FAState        state    = new FAState("State Name", isInitialState: true, isFinalState: true);

            automata.AddState(state);
            state = new FAState("State Name", isInitialState: true, isFinalState: false);
            Assert.True(automata.UpdateState(state));
        }
Ejemplo n.º 17
0
        public void Should_ReturnTrue_When_IsValid_AutomataIsValidNFA()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata nfaTest = new FiniteAutomata(FiniteAutomataType.NFA, alphabet);

            _ = nfaTest.AddState("A", isInitialState: true); //A
            _ = nfaTest.AddState("B");                       //B
            _ = nfaTest.AddState("C", isFinalState: true);   //C

            _ = nfaTest.AddTransition('0', "A", "A");        // TR: A '0' geçiþiyle A'ya gider.
            _ = nfaTest.AddTransition('1', "A", "B,C");      // TR: A '1' geçiþiyle B ya da C'ye gider.

            _ = nfaTest.AddTransition('0', "B", "A");        // TR: B '0' geçiþiyle B'ye gider.
            _ = nfaTest.AddTransition('1', "B", "A,C");      // TR: B '1' geçiþiyle A ya da C'ye gider.

            _ = nfaTest.AddTransition('0', "C", "A,B");      // TR: C '0' geçiþiyle A ya da B'ye gider.
            _ = nfaTest.AddTransition('1', "C", "C");        // TR: C '1' geçiþiyle C'ye gider.

            Assert.IsTrue(nfaTest.IsValid);
        }
Ejemplo n.º 18
0
        /// <summary>
        ///  Inserts initial state to DFA from NFA.
        /// </summary>
        /// <param name="NFA"></param>
        /// <param name="DFA"></param>
        /// <returns>Updated DFA object.</returns>
        private FiniteAutomata InsertInitalState(FiniteAutomata NFA, FiniteAutomata DFA)
        {
            _ = DFA.AddState(NFA.InitialState);

            // TR: Sadece initial state'in yaptığı geçişleri NFA dan getirilir.
            IEnumerable <FATransition> transitions = NFA.Transitions.Where(x => x.FromState == NFA.InitialState);

            foreach (FATransition transition in transitions)
            {
                if (transition.ToState.Count() == 1)
                {
                    // TR: Hedef state yoksa oluşturulur.
                    FAState toState = transition.ToState.First();
                    _ = DFA.AddState(toState);

                    // TR: 1-1 geçiş olduğundan DFA'ya uyumludur. Geçişi olduğu gibi eklenir.
                    _ = DFA.AddTransition(transition);
                }
                else if (transition.ToState.Count() > 1) // 1 den fazla to state varsa
                {
                    IEnumerable <FAState> toStates = transition.ToState;
                    // TR: Stateleri aralarına ayraç koyarak birleştir.
                    string newStateName = string.Join(STATE_SEPARATOR, toStates);
                    // TR: Herhangi bir state final state ise bu state final olmalıdır.
                    bool isFinalState = toStates.Any(state => state.IsFinalState);

                    // TR: Hedef state yoksa oluşturulur.
                    FAState aState = new FAState(newStateName, isFinalState: isFinalState);
                    _ = DFA.AddState(aState);

                    _ = DFA.AddTransition(transition.TransitionSymbol, transition.FromState.StateName, aState.StateName);
                }
                else // 0 olma durumu
                {
                }
            }

            return(DFA);
        }
Ejemplo n.º 19
0
        private static FiniteAutomata NFABuilder()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata nfaTest = new FiniteAutomata(FiniteAutomataType.NFA, alphabet);

            _ = nfaTest.AddState("A", isInitialState: true); //q0
            _ = nfaTest.AddState("B");                       //q1
            _ = nfaTest.AddState("C", isFinalState: true);   //q2

            _ = nfaTest.AddTransition('0', "A", "A");        // TR: q0 '0' geçişiyle q0'a gider.
            _ = nfaTest.AddTransition('1', "A", "B,C");      // TR: q0 '1' geçişiyle q1 ya da q2'ye gider.

            _ = nfaTest.AddTransition('0', "B", "B");        // TR: q1 '0' geçişiyle q1'e gider.
            _ = nfaTest.AddTransition('1', "B", "A,C");      // TR: q1 '1' geçişiyle q0 ya da q2'ye gider.

            _ = nfaTest.AddTransition('0', "C", "A,B");      // TR: q2 '0' geçişiyle q0 ya da q1'e gider.
            _ = nfaTest.AddTransition('1', "C", "C");        // TR: q2 '1' geçişiyle q2'ye gider.

            return(nfaTest);
        }
Ejemplo n.º 20
0
        private static FiniteAutomata TWDFABuilder2()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata twdfaTest = new FiniteAutomata(FiniteAutomataType.TwoWayDFA, alphabet);

            _ = twdfaTest.AddState("q0", isInitialState: true, isFinalState: true);
            _ = twdfaTest.AddState("q1", isFinalState: true);
            _ = twdfaTest.AddState("q2", isFinalState: true);

            _ = twdfaTest.AddTransition('0', "q0", "q0", 1);
            _ = twdfaTest.AddTransition('1', "q0", "q1", 1);

            _ = twdfaTest.AddTransition('0', "q1", "q1", 1);
            _ = twdfaTest.AddTransition('1', "q1", "q2", 0);

            _ = twdfaTest.AddTransition('0', "q2", "q0", 1);
            _ = twdfaTest.AddTransition('1', "q2", "q2", 0);

            return(twdfaTest);
        }
        public void Should_ReturnsItSelf_When_ConvertNFAToDFA_WithDFAParameter()
        {
            List <char> alphabet = new List <char> {
                'a', 'b', 'c'
            };
            FiniteAutomata dfaTest = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            _ = dfaTest.AddState(true);                 // q0
            _ = dfaTest.AddState();                     // q1
            _ = dfaTest.AddState(isFinalState: true);   // q2

            _ = dfaTest.AddTransition('a', "q0", "q1"); // TR: q0 'a' geçişi ile q1'e gider
            _ = dfaTest.AddTransition('b', "q0", "q2"); // TR: q0 'b' geçişi ile q2'ye gider.
            _ = dfaTest.AddTransition('c', "q0", "q0"); // TR: q0 'c' geçişi ile q0'a gider.

            _ = dfaTest.AddTransition('a', "q1", "q0"); // TR: q1 'a' geçişi ile q0'a gider.
            _ = dfaTest.AddTransition('b', "q1", "q1"); // TR: q1 'b' geçişi ile q1'e gider.
            _ = dfaTest.AddTransition('c', "q1", "q2"); // TR: q1 'c' geçişi ile q2'ye gider.

            _ = dfaTest.AddTransition('a', "q2", "q0"); // TR: q2 'a' geçişi ile q0'a gider.
            _ = dfaTest.AddTransition('b', "q2", "q2"); // TR: q2 'b' geçişi ile q2'ye gider.
            _ = dfaTest.AddTransition('c', "q2", "q2"); // TR: q2 'c' geçişi ile q2'ye gider.

            FiniteAutomataConverter converter    = new FiniteAutomataConverter();
            FiniteAutomata          converterDFA = converter.ConvertNFAToDFA(dfaTest);

            if (converterDFA.InitialState.StateName == dfaTest.InitialState.StateName &&
                converterDFA.FinalState.Count() == dfaTest.FinalState.Count() &&
                converterDFA.States.Count() == dfaTest.States.Count() &&
                converterDFA.Transitions.Count() == dfaTest.Transitions.Count() &&
                converterDFA.States.Count() == dfaTest.States.Count() &&
                converterDFA.Transitions.Last().TransitionSymbol == dfaTest.Transitions.Last().TransitionSymbol)
            {
                Assert.Pass();
            }

            Assert.Fail();
        }
Ejemplo n.º 22
0
        private FiniteAutomata InsertRightDirectionStates(FiniteAutomata TWDFA, FiniteAutomata DFA)
        {
            foreach (FAState state in TWDFA.States)
            {
                _ = DFA.AddState(state);
            }

            foreach (FATransition transition in TWDFA.Transitions)
            {
                if (transition.Direction == true)
                {
                    _ = DFA.AddTransition(transition);
                }
            }

            return(DFA);
        }
Ejemplo n.º 23
0
        public void Should_Equal_When_FinalState_ComparedByFinalState()
        {
            List <char> alphabet = new List <char> {
                '0', '1'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.NFA, alphabet);

            FAState state = new FAState("A", isFinalState: true);

            _ = automata.AddState(state);

            List <FAState> states = new List <FAState>()
            {
                state
            };

            Assert.AreEqual(states, automata.FinalState);
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Converts NFA transitions into the DFA transition, except initial state.
        /// </summary>
        /// <param name="NFA"></param>
        /// <param name="DFA"></param>
        /// <returns>Updated DFA object.</returns>
        private FiniteAutomata Convert(FiniteAutomata NFA, FiniteAutomata DFA)
        {
            Queue <FAState> statesQueue = new Queue <FAState>();

            // TR: Initial state tarafından eklenen tüm stateleri bulup kuyruğa ekler.
            IEnumerable <FAState> states = DFA.States.Where(x => !x.IsInitialState);

            foreach (FAState state in states)
            {
                statesQueue.Enqueue(state);
            }

            // TR: Kuyruktaki tüm nesneler için çalışır.
            do
            {
                // TR: Kuyruktan sıradaki eleman getirilir.
                FAState fromState = statesQueue.Dequeue();

                // TR: DFA olduğu için tüm geçişler kullanılmalıdır.
                foreach (char symbol in DFA.Alphabet)
                {
                    string[] subStateNames = fromState.StateName.Split(STATE_SEPARATOR);

                    List <string> newStateNames = new List <string>();
                    bool          isFinalState  = false;

                    foreach (string subStateName in subStateNames)
                    {
                        FAState      subState      = NFA.States.First(x => x.StateName == subStateName);
                        FATransition subTransition = NFA.Transitions.FirstOrDefault(x => x.FromState == subState && x.TransitionSymbol == symbol);

                        IEnumerable <FAState> subToStates = subTransition.ToState;
                        newStateNames.AddNotExists(subToStates.Select(x => x.StateName));

                        // TR: Herhangi bir state final state ise bu state final olmalıdır.
                        if (!isFinalState)
                        {
                            isFinalState = subToStates.Any(state => state.IsFinalState);
                        }
                    }

                    // TR: Stateleri aralarına ayraç koyarak birleştirir.
                    string newStateName = string.Join(STATE_SEPARATOR, newStateNames.OrderBy(x => x));

                    // TR: Hedef state yoksa oluşturulur.
                    FAState targetState;
                    if (!DFA.States.Any(x => x.StateName == newStateName))
                    {
                        targetState = new FAState(newStateName, isFinalState: isFinalState);
                        _           = DFA.AddState(targetState);

                        // TR: Olmayan item kuyruğa da eklenir.
                        statesQueue.Enqueue(targetState);
                    }
                    else
                    {
                        targetState = DFA.States.First(x => x.StateName == newStateName);
                    }

                    _ = DFA.AddTransition(symbol, fromState.StateName, targetState.StateName);
                }
            } while (statesQueue.Count > 0);

            return(DFA);
        }