Exemplo n.º 1
0
        private static FiniteAutomata TWDFABuilder3()
        {
            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");
            _ = twdfaTest.AddState("q4", isFinalState: true);

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

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

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

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

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

            return(twdfaTest);
        }
        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();
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
        public void Should_ReturnFalse_When_IsValid_AutomataIsInvalidDFA()
        {
            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('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('c', "q0", "q1"); // TR: q0 'c' geçiþi ile q1'e 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.IsFalse(dfaTest.IsValid);
        }
Exemplo n.º 5
0
        public void Should_ReturnTrue_When_IsValid_AutomataIsValid2DFA()
        {
            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);

            Assert.True(twdfaTest.IsValid);
        }
        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();
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
0
        public void Should_ReturnFalse_When_AddTransition_WithNullFATransition()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            FATransition transition = null;

            Assert.False(automata.AddTransition(transition));
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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"));
        }
Exemplo n.º 11
0
        public void Should_ReturnTrue_When_AddTransition_WithFATranstion()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

            FAState      state1     = new FAState("q1", isInitialState: true, isFinalState: true);
            FATransition transition = new FATransition('a', state1, state1);

            Assert.True(automata.AddTransition(transition));
        }
Exemplo n.º 12
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"));
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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);
        }
        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();
        }
Exemplo n.º 16
0
        public void Should_ReturnTrue_When_AddTransition_MultipleToStatesInNFA()
        {
            List <char> alphabet = new List <char>()
            {
                'a'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.NFA, alphabet);

            FAState state1 = new FAState("q1", isInitialState: true, isFinalState: true);
            FAState state2 = new FAState("q2");

            List <FAState> states = new List <FAState>()
            {
                state1, state2
            };

            FATransition transition = new FATransition('a', state1, states);

            Assert.True(automata.AddTransition(transition));
        }
Exemplo n.º 17
0
        private FiniteAutomata InsertLeftDirectionStates(FiniteAutomata TWDFA, FiniteAutomata DFA)
        {
            IEnumerable <FATransition> leftTransitions     = TWDFA.Transitions.Where(x => !x.Direction);
            Queue <FATransition>       leftTransitionQueue = new Queue <FATransition>(leftTransitions);

            do
            {
                FATransition transition = leftTransitionQueue.Dequeue();
                FAState      toState    = transition.ToState.First();
                IEnumerable <FATransition> l0Transitions = TWDFA.Transitions.Where(x => x.FromState == toState);
                FAState targetState = null;
                if (l0Transitions.Any(x => !x.Direction))
                {
                    IEnumerable <FATransition> l1Transitions = l0Transitions.Where(x => !x.Direction);
                    if (l1Transitions.Count() > 1)
                    {
                        leftTransitionQueue.Enqueue(transition);
                    }
                    else
                    {
                        FATransition l1Transition = l1Transitions.First();
                        FAState      l1State      = l1Transition.FromState;

                        targetState = GetOptimalState(l1State, l0Transitions, DFA);
                    }
                }
                else
                {
                    targetState = GetOptimalState(toState, l0Transitions, DFA);
                }

                FATransition newTransition = new FATransition(transition.TransitionSymbol, transition.FromState, targetState);
                _ = DFA.AddTransition(newTransition);
            } while (leftTransitionQueue.Count > 0);

            return(DFA);
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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);
        }
Exemplo n.º 21
0
        public void Should_ReturnFalse_When_Run_DFAWithABCABCABC()
        {
            List <char> alphabet = new List <char>()
            {
                'a', 'b', 'c'
            };
            FiniteAutomata automata = new FiniteAutomata(FiniteAutomataType.DFA, alphabet);

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

            _ = automata.AddTransition('a', "q0", "q1");
            _ = automata.AddTransition('b', "q0", "q1");
            _ = automata.AddTransition('c', "q0", "q1");

            _ = automata.AddTransition('a', "q1", "q2");
            _ = automata.AddTransition('b', "q1", "q2");
            _ = automata.AddTransition('c', "q1", "q2");

            _ = automata.AddTransition('a', "q2", "q3");
            _ = automata.AddTransition('b', "q2", "q3");
            _ = automata.AddTransition('c', "q2", "q3");

            _ = automata.AddTransition('a', "q3", "q0");
            _ = automata.AddTransition('b', "q3", "q0");
            _ = automata.AddTransition('c', "q3", "q0");

            Assert.False(automata.Run("abcabcabc"));
        }