Exemplo n.º 1
0
 public void TestAddState()
 {
     fsa.AddState(0);
     fsa.AddState(1);
     fsa.AddState(2);
     Assert.AreEqual(1, fsa.ReturnStateByID(1).ID);
 }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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;
        }
Exemplo n.º 7
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);
        }