Beispiel #1
0
        public static Automata <string> ExampleSlide8Lesson2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <string> m        = new Automata <string>(alphabet);

            m.AddTransition(new Transition <string>("q0", 'a', "q1"));
            m.AddTransition(new Transition <string>("q0", 'b', "q4"));

            m.AddTransition(new Transition <string>("q1", 'a', "q4"));
            m.AddTransition(new Transition <string>("q1", 'b', "q2"));

            m.AddTransition(new Transition <string>("q2", 'a', "q3"));
            m.AddTransition(new Transition <string>("q2", 'b', "q4"));

            m.AddTransition(new Transition <string>("q3", 'a', "q1"));
            m.AddTransition(new Transition <string>("q3", 'b', "q2"));

            // the error state, loops for a and b:
            m.AddTransition(new Transition <string>("q4", 'a'));
            m.AddTransition(new Transition <string>("q4", 'b'));

            // only on start state in a dfa:
            m.DefineAsStartState("q0");

            // two final states:
            m.DefineAsFinalState("q2");
            m.DefineAsFinalState("q3");

            return(m);
        }
Beispiel #2
0
        public static Automata <string> Example2Week1()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <string> m        = new Automata <string>(alphabet);

            m.AddTransition(new Transition <string>("A", 'b', "B"));
            m.AddTransition(new Transition <string>("B", 'a', "A"));

            m.DefineAsStartState("A");

            m.DefineAsFinalState("A");

            return(m);
        }
        public static Automata Parse(this Automata automata, IEnumerable <string> automataData)
        {
            // valida se o parser pode ser começado
            automata.ParseAutomataDataScopeValidate(automataData);

            var states   = automataData.ElementAt(1).Split(',');
            var alphabet = automataData.ElementAt(2).Split(',');
            var q0       = automataData.ElementAt(3);
            var f        = automataData.ElementAt(4).Split(',');

            automata.States   = states.Select(StringFormat).ToList();
            automata.Alphabet = alphabet.Select(StringFormat).ToList();
            automata.Q0       = q0.StringFormat();
            automata.F        = f.Select(StringFormat).ToList();

            var transitions = automataData.Skip(5);

            foreach (var transition in transitions)
            {
                if (transition.Contains("####"))
                {
                    break;
                }
                else
                {
                    automata.AddTransition(new Transition().Parse(transition));
                }
            }

            return(automata);
        }
Beispiel #4
0
        public static Automata <string> DfaTEST3()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <string> m        = new Automata <string>(alphabet);

            m.AddTransition(new Transition <string>("A", 'a', "B"));
            //Toggle test for missing symbol
            m.AddTransition(new Transition <string>("A", 'b', "C"));
            //Toggle test for multiple of the same symbol
            //m.AddTransition(new Transition<string>("A", 'b', "A"));


            m.AddTransition(new Transition <string>("B", 'b', "C"));
            m.AddTransition(new Transition <string>("B", 'a', "A"));

            m.AddTransition(new Transition <string>("C", 'a', "C"));
            m.AddTransition(new Transition <string>("C", 'b', "C"));

            m.DefineAsStartState("A");


            m.DefineAsFinalState("C");

            return(m);
        }
Beispiel #5
0
        public void CreateBasicSetup()
        {
            previousstates = new string[] { "BEGIN", "MIDDLE", null };

            nextstates = new string[] { "BROKEN", "CANCELED", null };



            automata = new Automata <Foo>();
            automata.AddTransition("NEW", "VERIFIED", null);
            automata.AddTransition("VERIFIED", "READY", new FooValidator());
            automata.AddTransition(previousstates, nextstates, new FooValidator());

            ohterContainer             = new Foo();
            ohterContainer.IntProp     = 50;
            ohterContainer.DecimalProp = 2.71M;
            ohterContainer.StringProp  = "This is false";

            container             = new Foo();
            container.IntProp     = 42;
            container.DecimalProp = 3.14M;
            container.StringProp  = "This is a test";
        }
Beispiel #6
0
        public static Automata DeepCloneAutomata(Automata automata)
        {
            Automata clonedAutomata = new Automata();

            clonedAutomata.SetAutomataType(automata.GetAutomataType());
            clonedAutomata.SetSymbols(automata.GetSymbols());
            automata.GetStates().ToList().ForEach(
                x => clonedAutomata.AddState(DeepCloneState(x))
                );
            automata.GetTransitions().ToList().ForEach(
                x => clonedAutomata.AddTransition(DeepCloneTransition(clonedAutomata, x))
                );
            clonedAutomata.SetInitialState(clonedAutomata.GetStateLike(automata.GetInitialState()));
            return(clonedAutomata);
        }
Beispiel #7
0
        public static Automata BuildAutomata(ConfigurationBase initStep)
        {
            Dictionary <string, FAState> visited = new Dictionary <string, FAState>();
            Stack <ConfigurationBase>    working = new Stack <ConfigurationBase>(1024);

            working.Push(initStep);
            Automata auto = new Automata();
            FAState  init = auto.AddState();

            auto.SetInitialState(init);
            visited.Add(initStep.GetID(), init);

            do
            {
                ConfigurationBase current      = working.Pop();
                FAState           currentState = visited[current.GetID()];

                IEnumerable <ConfigurationBase> list = current.MakeOneMove();

                //for (int i = 0; i < list.Length; i++)
                foreach (ConfigurationBase step in list)
                {
                    //ConfigurationBase step = list[i];
                    FAState target;
                    string  nextID = step.GetID();
                    if (visited.ContainsKey(nextID))
                    {
                        target = visited[nextID];
                    }
                    else
                    {
                        target = auto.AddState();
                        working.Push(step);
                        visited.Add(nextID, target);
                    }

                    auto.AddTransition(currentState, step.Event, target);
                }
            }while (working.Count > 0);

            return(auto);
        }
Beispiel #8
0
        public static Automata <string> NDFAToConvert()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <string> m        = new Automata <string>(alphabet);

            m.AddTransition(new Transition <string>("1", 'a', "2"));
            m.AddTransition(new Transition <string>("1", 'b', "2"));
            m.AddTransition(new Transition <string>("1", 'b', "3"));

            m.AddTransition(new Transition <string>("2", 'a', "2"));
            m.AddTransition(new Transition <string>("2", 'a', "4"));

            // m.AddTransition(new Transition<string>("3", '$', "3"));

            m.AddTransition(new Transition <string>("4", 'b', "3"));
            m.AddTransition(new Transition <string>("4", 'b', "1"));

            m.DefineAsStartState("1");
            m.DefineAsFinalState("1");

            m.DefineAsFinalState("4");

            return(m);
        }
Beispiel #9
0
        public static Automata <string> DfaTEST2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <string> m        = new Automata <string>(alphabet);

            m.AddTransition(new Transition <string>("A", 'a', "B"));
            m.AddTransition(new Transition <string>("A", 'b', "C"));
            //Toggle test
            //m.AddTransition(new Transition<string>("A", '$', "C"));

            m.AddTransition(new Transition <string>("B", 'b', "C"));
            m.AddTransition(new Transition <string>("B", 'a', "A"));

            m.AddTransition(new Transition <string>("C", 'a', "C"));
            m.AddTransition(new Transition <string>("C", 'b', "C"));

            m.DefineAsStartState("A");


            m.DefineAsFinalState("C");

            return(m);
        }
Beispiel #10
0
        private static Automata BuildAutomataWithRefusalsAndDiv(ConfigurationBase InitSpecStep)
        {
            Dictionary <string, FAState> visited = new Dictionary <string, FAState>();
            Stack <ConfigurationBase>    working = new Stack <ConfigurationBase>(1024);

            working.Push(InitSpecStep);
            Automata auto = new Automata();
            FAState  init = auto.AddState();

            auto.SetInitialState(init);
            visited.Add(InitSpecStep.GetID(), init);

            do
            {
                ConfigurationBase current      = working.Pop();
                FAState           currentState = visited[current.GetID()];

                if (current.IsDivergent())
                {
                    currentState.IsDiv = true;
                }
                else
                {
                    IEnumerable <ConfigurationBase> list = current.MakeOneMove();
                    List <string> negateRefusal          = new List <string>();
                    bool          hasTau = false;

                    //for (int i = 0; i < list.Length; i++)
                    foreach (ConfigurationBase step in list)
                    {
                        //ConfigurationBase step = list[i];

                        if (step.Event == Constants.TAU)
                        {
                            hasTau = true;
                        }
                        else
                        {
                            negateRefusal.Add(step.Event);
                        }

                        FAState target;
                        string  nextID = step.GetID();
                        if (visited.ContainsKey(nextID))
                        {
                            target = visited[nextID];
                        }
                        else
                        {
                            target = auto.AddState();
                            working.Push(step);
                            visited.Add(nextID, target);
                        }

                        auto.AddTransition(currentState, step.Event, target);
                    }

                    if (hasTau)
                    {
                        currentState.NegatedRefusal = null;
                    }
                    else
                    {
                        currentState.NegatedRefusal = negateRefusal;
                    }
                }
            }while (working.Count > 0);

            return(auto);
        }
Beispiel #11
0
        public static Automata <string> ExampleSlide14Lesson2()
        {
            char[]            alphabet = { 'a', 'b' };
            Automata <string> m        = new Automata <string>(alphabet);

            m.AddTransition(new Transition <string>("A", 'a', "C"));
            m.AddTransition(new Transition <string>("A", 'b', "B"));
            m.AddTransition(new Transition <string>("A", 'b', "C"));

            m.AddTransition(new Transition <string>("B", 'b', "C"));
            m.AddTransition(new Transition <string>("B", "C"));

            m.AddTransition(new Transition <string>("C", 'a', "D"));
            m.AddTransition(new Transition <string>("C", 'a', "E"));
            m.AddTransition(new Transition <string>("C", 'b', "D"));

            m.AddTransition(new Transition <string>("D", 'a', "B"));
            m.AddTransition(new Transition <string>("D", 'a', "C"));

            m.AddTransition(new Transition <string>("E", 'a'));
            m.AddTransition(new Transition <string>("E", "D"));

            // only on start state in a dfa:
            m.DefineAsStartState("A");

            // two final states:
            m.DefineAsFinalState("C");
            m.DefineAsFinalState("E");

            return(m);
        }
        private static Automata BuildAutomataWithRefusalsAndDiv(ConfigurationBase InitSpecStep)
        {
            Dictionary<string, FAState> visited = new Dictionary<string, FAState>();
            Stack<ConfigurationBase> working = new Stack<ConfigurationBase>(1024);
            working.Push(InitSpecStep);
            Automata auto = new Automata();
            FAState init = auto.AddState();
            auto.SetInitialState(init);
            visited.Add(InitSpecStep.GetID(), init);

            do
            {
                ConfigurationBase current = working.Pop();
                FAState currentState = visited[current.GetID()];

                if (current.IsDivergent())
                {
                    currentState.IsDiv = true;
                }
                else
                {
                    IEnumerable<ConfigurationBase> list = current.MakeOneMove();
                    List<string> negateRefusal = new List<string>();
                    bool hasTau = false;

                    //for (int i = 0; i < list.Length; i++)
                    foreach (ConfigurationBase step in list)
                    {
                        //ConfigurationBase step = list[i];

                        if (step.Event == Constants.TAU)
                        {
                            hasTau = true;
                        }
                        else
                        {
                            negateRefusal.Add(step.Event);
                        }

                        FAState target;
                        string nextID = step.GetID();
                        if (visited.ContainsKey(nextID))
                        {
                            target = visited[nextID];
                        }
                        else
                        {
                            target = auto.AddState();
                            working.Push(step);
                            visited.Add(nextID, target);
                        }

                        auto.AddTransition(currentState, step.Event, target);
                    }

                    if (hasTau)
                    {
                        currentState.NegatedRefusal = null;
                    }
                    else
                    {
                        currentState.NegatedRefusal = negateRefusal;
                    }
                }
            }
            while (working.Count > 0);

            return auto;
        }
Beispiel #13
0
 public void WhenDuplicateTransitionAddedInDictionaryAndValidatorIsEqualThenAddTransitionShouldThrowException()
 {
     //Act/Assert
     Assert.ThrowsException <ArgumentException>(() => automata.AddTransition("VERIFIED", "READY", new FooValidator()));
 }
Beispiel #14
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);
        }