Example #1
0
        private static void TestNDFAClass()
        {
            Automata <String> NDFA1 = TestAutomata.getExampleSlide8Lesson2();
            Automata <String> NDFA2 = TestAutomata.getExampleSlide14Lesson2();

            NDFA1.PrintTransitions();
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("NDFA1 is dfa? " + NDFA1.IsDfa());
            Console.WriteLine("-----------------------------------------");
            foreach (string state in NDFA1.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");
            foreach (string state in NDFA1.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("String {0} is a {1} string for Auto1.", VALID_STRING_1, NDFA1.IsStringAcceptable(VALID_STRING_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto1.", INVALID_STRING_0, NDFA1.IsStringAcceptable(INVALID_STRING_0) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto1.", VALID_STRING_2, NDFA1.IsStringAcceptable(VALID_STRING_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto1.", INVALID_STRING_1, NDFA1.IsStringAcceptable(INVALID_STRING_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto1.", INVALID_STRING_2, NDFA1.IsStringAcceptable(INVALID_STRING_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto1.", INVALID_STRING_3, NDFA1.IsStringAcceptable(INVALID_STRING_3) ? "valid" : "invalid");
            Console.WriteLine("-----------------------------------------");
            Console.WriteLine("Generate a string of length 9. Resulting string = " + NDFA1.GenerateLanguageOfGivenLength(9));
            Console.WriteLine("-----------------------------------------");

            NDFA2.PrintTransitions();
            Console.WriteLine("NDFA2 is dfa? " + NDFA2.IsDfa());
            Console.WriteLine("-----------------------------------------");
        }
        /// <summary>
        /// Turns a dfa into a form in which the original is not accepted.
        /// </summary>
        /// <param name="originalDfa">The dfa to make a reverse of</param>
        /// <returns>A dfa that is a reverse of the original dfa</returns>
        public Automata <T> ReverseDfa(Automata <T> originalDfa)
        {
            if (!originalDfa.IsDfa())
            {
                throw new ArgumentException("Given automata is not a DFA.");
            }

            Automata <T> reversedDfa = new Automata <T>(originalDfa.Symbols);

            //Every transition is swapped as follows: new transition(original end state, symbol, original start state)
            foreach (Transition <T> transition in originalDfa.Transitions)
            {
                reversedDfa.AddTransition(new Transition <T>(transition.ToState, transition.FromState, transition.Identifier));
            }
            //All start states become end states
            foreach (T startState in originalDfa.StartStates)
            {
                reversedDfa.DefineAsFinalState(startState);
            }
            //All end states become start states
            foreach (T endState in originalDfa.FinalStates)
            {
                reversedDfa.DefineAsStartState(endState);
            }

            return(reversedDfa);
        }
        /// <summary>
        /// Turns a dfa into a form in which the original is not accepted.
        /// </summary>
        /// <param name="originalDfa">The dfa to "invert"</param>
        /// <returns>A dfa that is !the original dfa</returns>
        public Automata <T> NotDfa(Automata <T> originalDfa)
        {
            if (!originalDfa.IsDfa())
            {
                throw new ArgumentException("Given automata is not a DFA.");
            }

            Automata <T> notDfa = new Automata <T>(originalDfa.Symbols);
            //Alles wat geen end state is word een end, en omgekeerd.
            //Filter all end states from all the states
            IEnumerable <T> newEndStates = originalDfa.States.Except(originalDfa.FinalStates);

            //Set everthing in the new dfa
            foreach (Transition <T> transition in originalDfa.Transitions)
            {
                notDfa.AddTransition(transition);
            }
            foreach (T startState in originalDfa.StartStates)
            {
                notDfa.DefineAsStartState(startState);
            }
            foreach (T finalState in newEndStates)
            {
                notDfa.DefineAsFinalState(finalState);
            }

            return(notDfa);
        }
        /// <summary>
        /// Combines two <see cref="Automata{T}"/> into a single automata if they are both dfa's
        /// </summary>
        /// <param name="firstDfa">The first dfa to use</param>
        /// <param name="secondDfa">The other dfa to use</param>
        /// <returns>A dfa that consists that takes the conditions for both dfa's into account</returns>
        public Automata <T> CombineAutomataAnd(Automata <T> firstDfa, Automata <T> secondDfa)
        {
            if (!firstDfa.IsDfa() || !secondDfa.IsDfa())
            {
                throw new ArgumentException("One of the given automata is not a dfa");
            }

            IEnumerable <char> combinedAlphabet = firstDfa.Symbols.Union(secondDfa.Symbols);
            Automata <T>       combinedDfa      = new Automata <T>(combinedAlphabet.ToArray());

            startStatesForDfa.Clear();
            combinedTransitionsMap.Clear();

            //Read the starting states
            SetStartStatesForAndOrOr(firstDfa, secondDfa);

            //Gather all states that follow
            MapReachableStatesForGivenDFA(firstDfa, secondDfa);

            //Generate the dfa, starting with the start states (no really sherlock)
            GenerateCombinedDfa(combinedDfa, startStatesForDfa);

            //Set start states
            foreach (T startState in startStatesForDfa)
            {
                combinedDfa.DefineAsStartState(startState);
            }

            //And end state, for the and it is a combination. So if is is a F and E, G then the endstates should contain F and either E or G.
            //So generate the end state by combining them in the same way as the start states, and then check if the dfa has the state.
            List <T> endStates = new List <T>();

            foreach (T firstEnd in firstDfa.FinalStates)
            {
                string firstAsString = firstEnd.ToString();
                foreach (T secondEnd in secondDfa.FinalStates)
                {
                    string secondAsString = secondEnd.ToString();
                    string newState       = firstAsString + "-" + secondAsString;
                    T      newStateT      = (T)Convert.ChangeType(newState, typeof(T));
                    endStates.Add(newStateT);
                }
            }

            foreach (T endState in endStates)
            {
                foreach (T state in combinedDfa.States)
                {
                    if (state.Equals(endState))
                    {
                        combinedDfa.DefineAsFinalState(endState);
                    }
                }
            }

            return(combinedDfa);
        }
Example #5
0
        private static void TestRegExAndThompson()
        {
            RegExpTest ret = new RegExpTest();

            ret.testLanguage();
            ret.testToString();

            RegExp regExp1 = new RegExp("baa");
            RegExp regExp2 = new RegExp("aba");
            RegExp regExp3 = new RegExp("bb");

            RegExp oneOrTwo           = regExp1.Or(regExp2);
            RegExp orStar             = oneOrTwo.Star();
            RegExp orStarDotThree     = orStar.Dot(regExp3);
            RegExp orStarDotThreePlus = orStarDotThree.Plus();

            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("And conversion by use of the thompson construction:");
            Console.WriteLine("-----------------------------------------");

            ThompsonConstruction thompson     = new ThompsonConstruction();
            Automata <string>    thompsonNDFA = thompson.GenerateNDFA(orStarDotThreePlus);

            Console.WriteLine("thompsonNDFA is dfa? " + thompsonNDFA.IsDfa());
            Console.WriteLine("-----------------------------------------");

            thompsonNDFA.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in thompsonNDFA.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in thompsonNDFA.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("String {0} is a {1} string for Auto3.", THOMPSON_TEST_1, thompsonNDFA.IsStringAcceptable(THOMPSON_TEST_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto3.", THOMPSON_TEST_2, thompsonNDFA.IsStringAcceptable(THOMPSON_TEST_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for Auto3.", THOMPSON_TEST_3, thompsonNDFA.IsStringAcceptable(THOMPSON_TEST_3) ? "valid" : "invalid");
            Console.WriteLine("-----------------------------------------");
        }
Example #6
0
        /// <summary>
        /// Turns the given <see cref="Automata{T}"/> ndfa into it's dfa.
        /// </summary>
        /// <param name="ndfaToTransform">the <see cref="Automata{T}"/> representing the ndfa</param>
        /// <returns>The <see cref="Automata{T}"/> dfa that can be build from the given ndfa</returns>
        public Automata <T> TransformNdfaIntoDfa(Automata <T> ndfaToTransform)
        {
            if (ndfaToTransform.IsDfa())
            {
                Console.WriteLine("Given NDFA is actually a DFA."); return(ndfaToTransform);
            }

            ndfa = ndfaToTransform;

            //Determine the states to go to with a and b, including empty transitions.
            reachableStates = GetAllReachableStatesByFromState();

            //Build the automata
            GenerateDFA();

            //See if we actually have a DFA
            if (!dfa.IsDfa())
            {
                Console.WriteLine("Diagram isn't a DFA."); return(new Automata <T>());
            }

            return(dfa);
        }
Example #7
0
        private static void TestMutationAndMinimalization()
        {
            Automata <string> L1 = TestAutomata.dfaMutationTestL1();
            Automata <string> L4 = TestAutomata.dfaMutationTestL4();

            Console.WriteLine("L1 is dfa? " + L1.IsDfa());
            Console.WriteLine("-----------------------------------------");

            L1.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("L4 is dfa? " + L4.IsDfa());
            Console.WriteLine("-----------------------------------------");

            L4.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L4.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L4.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            //Not L1
            DfaMutation <string> dfaMutator = new DfaMutation <string>();
            Automata <string>    notL1      = dfaMutator.NotDfa(L1);

            foreach (string state in notL1.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in notL1.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            //Reverted L4
            Automata <string> revertedL4 = dfaMutator.ReverseDfa(L4);

            Console.WriteLine("revertedL4 is dfa? " + revertedL4.IsDfa());
            Console.WriteLine("-----------------------------------------");

            revertedL4.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in revertedL4.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in revertedL4.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            Automata <string> L1AndL4 = dfaMutator.CombineAutomataAnd(L1, L4);

            Console.WriteLine("L1AndL4 is dfa? " + L1AndL4.IsDfa());
            Console.WriteLine("-----------------------------------------");

            L1AndL4.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1AndL4.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1AndL4.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            Automata <string> L1OrL4 = dfaMutator.CombinaAutomataOr(L1, L4);

            Console.WriteLine("L1OrL4 is dfa? " + L1OrL4.IsDfa());
            Console.WriteLine("-----------------------------------------");

            L1OrL4.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1OrL4.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1OrL4.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            Minimizer <string> minimizer      = new Minimizer <string>();
            Automata <string>  L1AndL4Minimal = minimizer.MinimizeUsingBrzozowski(L1AndL4);

            Console.WriteLine("L1AndL4Minimal is dfa? " + L1AndL4Minimal.IsDfa());
            Console.WriteLine("-----------------------------------------");

            L1AndL4Minimal.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1AndL4Minimal.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in L1AndL4Minimal.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");
        }
Example #8
0
        private static void TestNdfaToDfa()
        {
            Automata <string> testNdfa = TestAutomata.ndfaToDfaTest();

            Console.WriteLine("testNdfa is dfa? " + testNdfa.IsDfa());
            Console.WriteLine("-----------------------------------------");

            testNdfa.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("StartStatesIn NDFA: ");
            foreach (string startState in testNdfa.StartStates)
            {
                Console.WriteLine(startState);
            }
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("EndStates NDFA: ");
            foreach (string endState in testNdfa.FinalStates)
            {
                Console.WriteLine(endState);
            }
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_VALID_1, testNdfa.IsStringAcceptable(NDFA_TO_DFA_VALID_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_VALID_2, testNdfa.IsStringAcceptable(NDFA_TO_DFA_VALID_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_VALID_3, testNdfa.IsStringAcceptable(NDFA_TO_DFA_VALID_3) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_VALID_4, testNdfa.IsStringAcceptable(NDFA_TO_DFA_VALID_4) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_INVALID_1, testNdfa.IsStringAcceptable(NDFA_TO_DFA_INVALID_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_INVALID_2, testNdfa.IsStringAcceptable(NDFA_TO_DFA_INVALID_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfa.", NDFA_TO_DFA_INVALID_3, testNdfa.IsStringAcceptable(NDFA_TO_DFA_INVALID_3) ? "valid" : "invalid");
            Console.WriteLine("-----------------------------------------");

            NdfaToDfa <string> converter   = new NdfaToDfa <string>();
            Automata <string>  testNdfaDFA = converter.TransformNdfaIntoDfa(testNdfa);

            Console.WriteLine("testNdfaDFA is dfa? " + testNdfaDFA.IsDfa());
            Console.WriteLine("-----------------------------------------");

            testNdfaDFA.PrintTransitions();
            Console.WriteLine("-----------------------------------------");

            foreach (string state in testNdfaDFA.StartStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            foreach (string state in testNdfaDFA.FinalStates)
            {
                Console.WriteLine(state);
            }
            Console.WriteLine("-----------------------------------------");

            Console.WriteLine("And the same strings as befor in the DFA:");
            Console.WriteLine();
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_VALID_1, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_VALID_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_VALID_2, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_VALID_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_VALID_3, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_VALID_3) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_VALID_4, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_VALID_4) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_INVALID_1, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_INVALID_1) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_INVALID_2, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_INVALID_2) ? "valid" : "invalid");
            Console.WriteLine("String {0} is a {1} string for testNdfaDFA.", NDFA_TO_DFA_INVALID_3, testNdfaDFA.IsStringAcceptable(NDFA_TO_DFA_INVALID_3) ? "valid" : "invalid");
            Console.WriteLine("-----------------------------------------");
        }
        /// <summary>
        /// Combines two <see cref="Automata{T}"/> into a single automata if they are both dfa's
        /// </summary>
        /// <param name="firstDfa">The first dfa to use</param>
        /// <param name="secondDfa">The other dfa to use</param>
        /// <returns>A dfa that consists that takes the conditions for both dfa's into account</returns>
        public Automata <T> CombinaAutomataOr(Automata <T> firstDfa, Automata <T> secondDfa)
        {
            if (!firstDfa.IsDfa() || !secondDfa.IsDfa())
            {
                throw new ArgumentException("One of the given automata is not a dfa");
            }

            IEnumerable <char> combinedAlphabet = firstDfa.Symbols.Union(secondDfa.Symbols);
            Automata <T>       combinedDfa      = new Automata <T>(combinedAlphabet.ToArray());

            startStatesForDfa.Clear();
            combinedTransitionsMap.Clear();

            //Read the starting states
            SetStartStatesForAndOrOr(firstDfa, secondDfa);

            //Gather all states that follow
            MapReachableStatesForGivenDFA(firstDfa, secondDfa);

            //Generate the dfa, starting with the start states (no really sherlock)
            GenerateCombinedDfa(combinedDfa, startStatesForDfa);

            //Set start states
            foreach (T startState in startStatesForDfa)
            {
                combinedDfa.DefineAsStartState(startState);
            }

            //And end state, for the or it should contain one. So if is is a F and E, G then any state with a E, F or G is an end state.
            List <T> endStates = new List <T>();

            endStates.AddRange(firstDfa.FinalStates);
            foreach (T state in secondDfa.FinalStates)
            {
                foreach (T endState in endStates)
                {
                    if (!StatesAreEqual(state, endState))
                    {
                        endStates.Add(state); break;
                    }
                }
            }

            foreach (T state in combinedDfa.States)
            {
                string[] splitStates     = state.ToString().Split('-');
                List <T> separateStatesT = new List <T>();
                foreach (string splitState in splitStates)
                {
                    separateStatesT.Add((T)Convert.ChangeType(splitState, typeof(T)));
                }

                //Now check if any of the seperates states are in endStates (so if intersect leaves atleast one element)
                int elementsLeft = separateStatesT.Intersect(endStates).ToList().Count;

                if (elementsLeft > 0)
                {
                    combinedDfa.DefineAsFinalState(state);
                }
            }

            return(combinedDfa);
        }