Example #1
0
        public void TestRemoveStates()
        {
            Automata automata = new Automata();

            State[] states = new State[] {
                new State("x", false),
                new State("y", false),
                new State("z", true)
            };

            String[] removeStates = new String[] {
                "y", "z"
            };

            automata.AddStates(states);
            automata.RemoveStates(removeStates);

            List <String> expectedStates = states.ToList().Where(
                x => !removeStates.Contains(x.Name)
                ).ToList().Select(
                x => x.Name
                ).ToList();

            Assert.IsTrue(expectedStates.Count == automata.GetStates().Length);

            foreach (var x in expectedStates)
            {
                Assert.IsTrue(automata.ContainsState(x));
            }
        }
Example #2
0
        public void TestMatchAutomata8()
        {
            /*
             *      L = {0}*{1}*{2}* (AFNe)
             */

            Automata automata = new Automata();

            String[] symbols = new String[] {
                "0", "1", "2"
            };

            State[] states = new State[] {
                new State("q0", false),
                new State("q1", false),
                new State("q2", true)
            };

            Transition[] transitions = new Transition[] {
                new Transition(states[0], "0", states[0]),
                new Transition(states[0], "@", states[1]),
                new Transition(states[0], "@", states[0]),
                new Transition(states[1], "1", states[2]),
                new Transition(states[1], "@", states[2]),
                new Transition(states[2], "2", states[2])
            };

            automata.SetAutomataType(AutomataType.AFNe);
            automata.AddSymbols(symbols);
            automata.AddStates(states);
            automata.AddTransitions(transitions);
            automata.SetInitialState(states[0]);

            String[] validInputs = new String[] {
                "1"
            };

            String[] invalidInputs = new String[] {
            };

            AutomataReader reader = new AutomataReader(automata);

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x), $"{x} was supposed to match");
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x), $"{x} was not supposed to match");
            }
        }
Example #3
0
        public void TestAddStates()
        {
            Automata automata = new Automata();

            State[] states = new State[] {
                new State("x", false),
                new State("y", false),
                new State("z", true)
            };

            String  extraState        = "k";
            Boolean extraStateIsFinal = true;

            automata.AddStates(states);
            automata.AddState(extraState, extraStateIsFinal);

            Assert.IsTrue((states.Length + 1) == (automata.GetStates().Length));

            foreach (var x in states)
            {
                Assert.IsTrue(automata.ContainsState(x));
            }
            Assert.IsTrue(automata.ContainsState(extraState));

            List <State> finalStates = states.ToList().Where(
                x => x.IsFinal
                ).ToList();

            Assert.AreEqual((finalStates.Count + 1), (automata.GetFinalStates().Length));

            foreach (var x in finalStates)
            {
                Assert.IsTrue(automata.GetFinalStates().Contains(x));
                Assert.IsTrue(automata.GetStateLike(x).IsFinal);
            }
            Assert.IsTrue(automata.GetStateLike(extraState).IsFinal);
        }
        public void TestConversion_NDFAe_To_DFA_0()
        {
            /*
             *      L = {a,b}*{a}{a,b}{a,b} (AFNe)
             */

            Automata automata = new Automata();

            String[] symbols = new String[] {
                "a", "b"
            };

            State[] states = new State[] {
                new State("q1", false),
                new State("q2", false),
                new State("q3", false),
                new State("q4", false),
                new State("q5", true)
            };

            Transition[] transitions = new Transition[] {
                new Transition(states[0], "a", states[0]),
                new Transition(states[0], "b", states[0]),
                new Transition(states[0], "a", states[1]),
                new Transition(states[1], "a", states[2]),
                new Transition(states[1], "b", states[2]),
                new Transition(states[2], "a", states[3]),
                new Transition(states[2], "b", states[3]),
                new Transition(states[3], "@", states[4])
            };

            automata.SetAutomataType(AutomataType.AFNe);
            automata.AddSymbols(symbols);
            automata.AddStates(states);
            automata.AddTransitions(transitions);
            automata.SetInitialState(states[0]);

            String[] validInputs = new String[] {
                "aabb",
                "aaabb",
                "ababababababababababb",
                "ababababaaaaaaaaaaabb",
                "aaaaaaaaaaaaaaaaaaaaa",
                "bbbbbbbbbbbbbbbbbbabb",
                "aaaaaaaaaaaaaabbabaab",
                "ababababababababababa"
            };

            String[] invalidInputs = new String[] {
                "abababbbbbbbbbbbbbbbbb",
                "ababababababbababaabba",
                "Xababababababababaaabb",
                "ababababbabababababXab",
                "ab",
                "bba",
                "a",
                "b",
                ""
            };

            AutomataReader reader = new AutomataReader(automata);

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x), $"{x} was supposed to match");
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x), $"{x} was not supposed to match");
            }

            Automata convertedAutomata = AutomataConverter.ToDFA(automata);

            reader = new AutomataReader(convertedAutomata);

            Assert.AreEqual(AutomataType.AFD, convertedAutomata.GetAutomataType());

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x), $"{x} was supposed to match");
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x), $"{x} was not supposed to match");
            }
        }
        public void TestConversion_NDFAe_To_DFA_3()
        {
            Automata automata = new Automata();

            String[] symbols = new String[] {
                "a", "b"
            };

            State[] states = new State[] {
                new State("q0", false),
                new State("q1", false),
                new State("q2", false),
                new State("q3", false),
                new State("q4", false),
                new State("q5", false),
                new State("q6", false),
                new State("q7", false),
                new State("q8", false),
                new State("q9", false),
                new State("q10", false),
                new State("q11", false),
                new State("q12", false),
                new State("q13", false),
                new State("q14", false),
                new State("q15", false),
                new State("q16", false),
                new State("q17", false),
                new State("q18", false),
                new State("q19", true)
            };

            Transition[] transitions = new Transition[] {
                new Transition(states[0], "@", states[1]),
                new Transition(states[1], "@", states[2]),
                new Transition(states[1], "@", states[4]),
                new Transition(states[2], "a", states[3]),
                new Transition(states[3], "@", states[6]),
                new Transition(states[4], "@", states[5]),
                new Transition(states[5], "@", states[6]),
                new Transition(states[6], "@", states[7]),
                new Transition(states[7], "@", states[8]),
                new Transition(states[7], "@", states[18]),
                new Transition(states[8], "@", states[9]),
                new Transition(states[9], "@", states[10]),
                new Transition(states[9], "@", states[12]),
                new Transition(states[10], "b", states[11]),
                new Transition(states[11], "@", states[16]),
                new Transition(states[12], "b", states[13]),
                new Transition(states[13], "@", states[14]),
                new Transition(states[14], "a", states[15]),
                new Transition(states[15], "@", states[16]),
                new Transition(states[16], "@", states[17]),
                new Transition(states[17], "@", states[18]),
                new Transition(states[17], "@", states[8]),
                new Transition(states[18], "@", states[19])
            };

            automata.SetAutomataType(AutomataType.AFNe);
            automata.AddSymbols(symbols);
            automata.AddStates(states);
            automata.AddTransitions(transitions);
            automata.SetInitialState(states[0]);

            String[] validInputs = new String[] {
                "",
                "a",
                "b",
                "bbbbb",
                "aba",
                "abab",
                "ba",
                "bab",
                "bbbbbbbbbbbbbbb",
                "bbbbbbbbbbbbabbbbb"
            };

            String[] invalidInputs = new String[] {
                "aa",
                "aab",
                "abaa",
                "ababbbbbbbbaa",
                "bbbbbbbbbbbbbbaa",
                "abbbbbX",
                "Xbbbbbaaabbbb",
                "ababababababababababaaab",
                "babababaabab"
            };

            AutomataReader reader = new AutomataReader(automata);

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x), $"{x} was supposed to match");
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x), $"{x} was not supposed to match");
            }

            Automata convertedAutomata = AutomataConverter.ToDFA(automata);

            reader = new AutomataReader(convertedAutomata);

            Assert.AreEqual(AutomataType.AFD, convertedAutomata.GetAutomataType());

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x), $"{x} was supposed to match");
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x), $"{x} was not supposed to match");
            }
        }
Example #6
0
        public void TestMatchAutomata1()
        {
            /*
             *      L = {0,1}*{00} (AFN)
             */

            Automata automata = new Automata();

            String[] symbols = new String[] {
                "0", "1"
            };

            State[] states = new State[] {
                new State("q0", false),
                new State("q1", false),
                new State("q2", true)
            };

            Transition[] transitions = new Transition[] {
                new Transition(states[0], "1", states[0]),
                new Transition(states[0], "0", states[0]),
                new Transition(states[0], "0", states[1]),
                new Transition(states[1], "0", states[2])
            };

            automata.AddSymbols(symbols);
            automata.AddStates(states);
            automata.AddTransitions(transitions);
            automata.SetAutomataType(AutomataType.AFN);
            automata.SetInitialState(states[0]);

            String[] validInputs = new String[] {
                "101001011011011011010101010000011111111110000000000001100",
                "101010100001111111010101000000000000000000000000000001100",
                "000000000000000000000000000000000000000000000000000000000",
                "111111111111111111111111110011111111111111111111111100000",
                "100101010101010101010101001101010101011111111111000011100",
                "111111111111111111111111111111110000000000000000000000000"
            };

            String[] invalidInputs = new String[] {
                "111111111111111111111111111120000000000000000000000000000",
                "222222222220000000000000000000000000000000000000000000000",
                "111111111111111111111111111111111111111111111111111111110",
                "000000000000000000000000000000000000000000000000000000001",
                "111111111111111111111111111111111111111111111111111111111",
                "111111111111111111111111111111111111111111111111111111001"
            };

            AutomataReader reader = new AutomataReader(automata);

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x));
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x));
            }
        }
Example #7
0
        public void TestMatchAutomata7()
        {
            /*
             *      L = {@,+,-}{0,...,9}*{.}{0,...,9}* (AFD)
             */

            Automata automata = new Automata();

            String[] symbols = new String[] {
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "+", "-", "."
            };

            State[] originalStates = new State[] {
                new State("q0", false),
                new State("q1", false),
                new State("q2", false),
                new State("q3", false),
                new State("q4", false),
                new State("q5", true)
            };

            State[] states = new State[] {
                new GroupedState(new List <State> {
                    originalStates[0],
                    originalStates[1]
                }),
                originalStates[1],
                originalStates[2],
                new GroupedState(new List <State> {
                    originalStates[1],
                    originalStates[4]
                }),
                new GroupedState(new List <State> {
                    originalStates[2],
                    originalStates[3],
                    originalStates[5]
                }),
                new GroupedState(new List <State> {
                    originalStates[3],
                    originalStates[5]
                }),
            };

            Transition[] transitions = new Transition[] {
                new Transition(states[0], "+", states[1]),
                new Transition(states[0], "-", states[1]),
                new Transition(states[0], ".", states[2]),
                new Transition(states[0], "0", states[3]),
                new Transition(states[0], "1", states[3]),
                new Transition(states[0], "2", states[3]),
                new Transition(states[0], "3", states[3]),
                new Transition(states[0], "4", states[3]),
                new Transition(states[0], "5", states[3]),
                new Transition(states[0], "6", states[3]),
                new Transition(states[0], "7", states[3]),
                new Transition(states[0], "8", states[3]),
                new Transition(states[0], "9", states[3]),
                new Transition(states[1], "0", states[3]),
                new Transition(states[1], "1", states[3]),
                new Transition(states[1], "2", states[3]),
                new Transition(states[1], "3", states[3]),
                new Transition(states[1], "4", states[3]),
                new Transition(states[1], "5", states[3]),
                new Transition(states[1], "6", states[3]),
                new Transition(states[1], "7", states[3]),
                new Transition(states[1], "8", states[3]),
                new Transition(states[1], "9", states[3]),
                new Transition(states[1], ".", states[2]),
                new Transition(states[2], "0", states[5]),
                new Transition(states[2], "1", states[5]),
                new Transition(states[2], "2", states[5]),
                new Transition(states[2], "3", states[5]),
                new Transition(states[2], "4", states[5]),
                new Transition(states[2], "5", states[5]),
                new Transition(states[2], "6", states[5]),
                new Transition(states[2], "7", states[5]),
                new Transition(states[2], "8", states[5]),
                new Transition(states[2], "9", states[5]),
                new Transition(states[3], "0", states[3]),
                new Transition(states[3], "1", states[3]),
                new Transition(states[3], "2", states[3]),
                new Transition(states[3], "3", states[3]),
                new Transition(states[3], "4", states[3]),
                new Transition(states[3], "5", states[3]),
                new Transition(states[3], "6", states[3]),
                new Transition(states[3], "7", states[3]),
                new Transition(states[3], "8", states[3]),
                new Transition(states[3], "9", states[3]),
                new Transition(states[3], ".", states[4]),
                new Transition(states[4], "0", states[5]),
                new Transition(states[4], "1", states[5]),
                new Transition(states[4], "2", states[5]),
                new Transition(states[4], "3", states[5]),
                new Transition(states[4], "4", states[5]),
                new Transition(states[4], "5", states[5]),
                new Transition(states[4], "6", states[5]),
                new Transition(states[4], "7", states[5]),
                new Transition(states[4], "8", states[5]),
                new Transition(states[4], "9", states[5]),
                new Transition(states[5], "0", states[5]),
                new Transition(states[5], "1", states[5]),
                new Transition(states[5], "2", states[5]),
                new Transition(states[5], "3", states[5]),
                new Transition(states[5], "4", states[5]),
                new Transition(states[5], "5", states[5]),
                new Transition(states[5], "6", states[5]),
                new Transition(states[5], "7", states[5]),
                new Transition(states[5], "8", states[5]),
                new Transition(states[5], "9", states[5])
            };

            automata.AddSymbols(symbols);
            automata.AddStates(states);
            automata.AddTransitions(transitions);
            automata.SetAutomataType(AutomataType.AFNe);
            automata.SetInitialState(states[0]);

            String[] validInputs = new String[] {
                "+.020165451210",
                ".451541321531",
                "58146514.561456",
                "-854654.",
                "-585.5126513",
                "-.4754648",
                "256.2662562",
            };

            String[] invalidInputs = new String[] {
                "egsrdhjdtsjsgfnsgbn",
                "5641684651",
                "6846515143.6456145654.561465123",
                "-.",
                ".",
                "+98465.8645a"
            };

            AutomataReader reader = new AutomataReader(automata);

            foreach (var x in validInputs)
            {
                Assert.IsTrue(reader.Matches(x), $"{x} was supposed to match");
            }

            foreach (var x in invalidInputs)
            {
                Assert.IsFalse(reader.Matches(x), $"{x} was not supposed to match");
            }
        }