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

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

            automata.AddSymbols(symbols);
            automata.AddSymbol(extraSymbol);

            Assert.IsTrue((symbols.Length + 1) == (automata.GetSymbols().Length));

            foreach (var x in symbols)
            {
                Assert.IsTrue(automata.ContainsSymbol(x));
            }
            Assert.IsTrue(automata.ContainsSymbol(extraSymbol));
        }
Example #2
0
        public static Automata Deserialize(String plainText, Boolean displayDebug = false)
        {
            Boolean abandon = false;
            // Char newLine = '\n';
            // Char commaSeparator = ',';
            var badTextLines = plainText.Split(NEWLINE_CHAR).ToList();
            var textLines    = new List <String>();

            badTextLines.ForEach(
                x => {
                String input = RemoveBadChars(x);
                if (x.Contains(EOF_STR))
                {
                    textLines.Add(input.Trim());
                }
                else
                {
                    textLines.Add(input);
                }
            }
                );
            Automata deserializedAutomata = new Automata();

            if (textLines.Count >= 7)
            {
                String        type         = textLines[0].Trim();
                List <String> states       = textLines[1].Split(COMMA_SEPARATOR_CHAR).ToList();
                List <String> symbols      = textLines[2].Split(COMMA_SEPARATOR_CHAR).ToList();
                String        initialState = textLines[3].Trim();
                List <String> finalStates  = textLines[4].Split(COMMA_SEPARATOR_CHAR).ToList();
                List <String> transitions  = new List <String>();

                var EOFIndex = textLines.ToList().IndexOf(EOF_STR);

                for (Int32 i = 5; i < textLines.Count - 1; i++)
                {
                    if (!String.IsNullOrWhiteSpace(textLines[i]) && i < EOFIndex)
                    {
                        transitions.Add(textLines[i]);
                    }
                }

                deserializedAutomata.SetAutomataType(DeserializeType(type));

                symbols.ForEach(
                    x => {
                    try {
                        deserializedAutomata.AddSymbol(x);
                        if (displayDebug)
                        {
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine($"Symbol '{x}' loaded.");
                            Console.ResetColor();
                        }
                    } catch (Exception e) {
                        abandon = !HandleException(e, x, "Symbol");
                        if (abandon)
                        {
                            throw new AutomataSerializerException("Serialization was abandoned.");
                        }
                    }
                }
                    );

                states.ForEach(
                    x => {
                    try {
                        deserializedAutomata.AddState(DeserializeState(x, finalStates.ToArray()));
                        if (displayDebug)
                        {
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine($"State '{x}' loaded.");
                            Console.ResetColor();
                        }
                    } catch (Exception e) {
                        abandon = !HandleException(e, x, "State");
                        if (abandon)
                        {
                            throw new AutomataSerializerException("Serialization was abandoned.");
                        }
                    }
                }
                    );

                transitions.ForEach(
                    x => {
                    try {
                        deserializedAutomata.AddTransition(DeserializeTransition(x, deserializedAutomata));
                        if (displayDebug)
                        {
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine($"Transition '{x}' loaded.");
                            Console.ResetColor();
                        }
                    } catch (Exception e) {
                        abandon = !HandleException(e, x, "Transition");
                        if (abandon)
                        {
                            throw new AutomataSerializerException("Serialization was abandoned.");
                        }
                    }
                }
                    );

                if (deserializedAutomata.ContainsState(initialState))
                {
                    deserializedAutomata.SetInitialState(
                        deserializedAutomata.GetStates().ToList().Find(
                            x => x.Name == initialState
                            )
                        );
                    if (displayDebug)
                    {
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine($"State '{initialState}' set as initializer.");
                        Console.ResetColor();
                    }
                }
                else
                {
                    throw new AutomataSerializerException($"Initial state '{initialState}' is invalid.");
                }
            }
            else
            {
                throw new AutomataSerializerException("Not enough arguments.");
            }

            return(deserializedAutomata);
        }