Ejemplo n.º 1
0
        private static Automaat <string> RetrieveDfaFromPartitions(SortedSet <Partition> partitions,
                                                                   SortedSet <char> symbols)
        {
            Automaat <string> automaat = new Automaat <string>(symbols);

            foreach (Partition p in partitions)
            {
                if (p.IsFinal)
                {
                    automaat.DefineAsFinalState(p.Identifier.ToString());
                }
                if (p.IsStart)
                {
                    automaat.DefineAsStartState(p.Identifier.ToString());
                }
                foreach (KeyValuePair <string, Row> row in p.Rows)
                {
                    foreach (KeyValuePair <char, string> innerRow in row.Value.InnerRows)
                    {
                        string toState = "";
                        foreach (Partition p2 in partitions)
                        {
                            if (p2.ContainsState(innerRow.Value))
                            {
                                toState = p2.Identifier.ToString();
                                break;
                            }
                        }
                        automaat.AddTransition(new Transition <string>(p.Identifier.ToString(), innerRow.Key, toState));
                    }
                }
            }

            return(automaat);
        }
Ejemplo n.º 2
0
        private static void contains(DfaGenerateValue param, ref Automaat <string> dfa)
        {
            char[] chars        = param.Parameter.ToCharArray();
            int    stateCounter = 0;

            dfa.DefineAsStartState(stateCounter.ToString());
            foreach (char c in chars)
            {
                dfa.AddTransition(new Transition <string>(stateCounter.ToString(), c,
                                                          (stateCounter + 1).ToString()));

                stateCounter = dfa.States.Count - 1;
            }

            dfa.DefineAsFinalState(stateCounter.ToString());

            //Hardcopy states
            List <string> ogStates = new List <string>();

            foreach (string state in dfa.States)
            {
                ogStates.Add(state);
            }

            for (int i = 0; i < ogStates.Count; i++)
            {
                string state = ogStates[i];

                List <Transition <string> > trans = dfa.GetTransition(state);

                SortedSet <char> routesPresent = new SortedSet <char>();
                foreach (Transition <string> t in trans)
                {
                    routesPresent.Add(t.Symbol);
                }

                foreach (char letter in dfa.Symbols)
                {
                    if (!routesPresent.Contains(letter) && !dfa.FinalStates.Contains(state))
                    {
                        int stateToReturnTo = backTrackForWorkingRoute(chars, letter);

                        dfa.AddTransition(new Transition <string>(state, letter, ogStates[stateToReturnTo]));
                    }
                }
            }

            foreach (char c in dfa.Symbols)
            {
                foreach (string finalstate in dfa.FinalStates)
                {
                    dfa.AddTransition(new Transition <string>(stateCounter.ToString(), c, stateCounter.ToString()));
                }
            }
        }
Ejemplo n.º 3
0
        private static void beginsWith(DfaGenerateValue param, ref Automaat <string> dfa)
        {
            char[] chars        = param.Parameter.ToCharArray();
            int    stateCounter = 0;

            dfa.DefineAsStartState(stateCounter.ToString());
            foreach (char c in chars)
            {
                dfa.AddTransition(new Transition <string>(stateCounter.ToString(), c,
                                                          (stateCounter + 1).ToString()));

                stateCounter = dfa.States.Count - 1;
            }

            dfa.DefineAsFinalState(stateCounter.ToString());
            foreach (char c in dfa.Symbols)
            {
                dfa.AddTransition(new Transition <string>(stateCounter.ToString(), c, stateCounter.ToString()));
            }

            //Hardcopy states
            SortedSet <string> ogStates = new SortedSet <string>();

            foreach (string state in dfa.States)
            {
                ogStates.Add(state);
            }

            foreach (string state in ogStates)
            {
                List <Transition <string> > trans = dfa.GetTransition(state);

                SortedSet <char> routesPresent = new SortedSet <char>();
                foreach (Transition <string> t in trans)
                {
                    routesPresent.Add(t.Symbol);
                }

                foreach (char letter in dfa.Symbols)
                {
                    if (!routesPresent.Contains(letter))
                    {
                        dfa.AddTransition(new Transition <string>(state, letter, "F"));
                    }
                }
            }

            if (dfa.States.Contains("F"))
            {
                foreach (char letter in dfa.Symbols)
                {
                    dfa.AddTransition(new Transition <string>("F", letter, "F"));
                }
            }
        }
        public static Automaat <string> ConvertRegExp(RegExp regExp)
        {
            Automaat <string> automaat = new Automaat <string>();

            automaat.DefineAsStartState("0");
            automaat.DefineAsFinalState("1");
            int stateCounter = 2;

            Convert(regExp, ref automaat, ref stateCounter, 0, 1);
            automaat.Symbols = new SortedSet <char>(automaat.Transitions.Distinct().Select(e => e.Symbol).ToList());
            //Epsilon should not be in alphabet
            automaat.Symbols.Remove('$');
            return(automaat);
        }
Ejemplo n.º 5
0
        public static Automaat <string> Convert(Automaat <string> ndfa)
        {
            Automaat <string>  dfa = new Automaat <string>(ndfa.Symbols);
            string             combinedStartState = "";
            SortedSet <string> completeStartState = new SortedSet <string>();

            bool isFinalState = false;

            // Loop through all the available start states from the ndfa and create a list with them + their epsilon-linked states
            foreach (string startState in ndfa.StartStates)
            {
                RetrieveEpsilonIncludedState(startState, ndfa, ref completeStartState);
            }

            //Turn sortedset into a string with all its states
            foreach (string s in completeStartState)
            {
                combinedStartState += s + "_";
                if (ndfa.FinalStates.Contains(s))
                {
                    isFinalState = true;
                }
            }


            //trim last "_" off of string
            combinedStartState = combinedStartState.TrimEnd('_');
            //Start conversion
            ConvertState(combinedStartState, ref dfa, ref ndfa);
            // Define combinedStartState as one and only start state in dfa
            dfa.DefineAsStartState(combinedStartState);
            if (isFinalState)
            {
                dfa.DefineAsFinalState(combinedStartState);
            }

            // Add a symbol loop to the failstate if one is created during conversion.
            if (dfa.States.Contains("F"))
            {
                foreach (char route in dfa.Symbols)
                {
                    dfa.AddTransition(new Transition <string>("F", route, "F"));
                }
            }
            return(finaliseConversion(dfa));
        }
Ejemplo n.º 6
0
        private static Automaat <string> finaliseMerge(Automaat <string> merged)
        {
            Automaat <string> finalisedMerge = new Automaat <string>(merged.Symbols);

            foreach (Transition <string> t in merged.Transitions)
            {
                finalisedMerge.AddTransition(new Transition <string>(t.FromState.Replace("_", String.Empty), t.Symbol, t.ToState.Replace("_", String.Empty)));
            }

            foreach (string startState in merged.StartStates)
            {
                finalisedMerge.DefineAsStartState(startState.Replace("_", String.Empty));
            }

            foreach (string finalState in merged.FinalStates)
            {
                finalisedMerge.DefineAsFinalState(finalState.Replace("_", String.Empty));
            }


            return(finalisedMerge);
        }
Ejemplo n.º 7
0
        private static void AddMergedState(Dictionary <char, string> prevMergedState, ref Automaat <string> merged, Automaat <string> dfaA, Automaat <string> dfaB, MergeType type)
        {
            // string[] states = prevMergedState.Split('_');
            // Add prev
            int    countFinal = 0;
            int    countStart = 0;
            string completePrevMergedState = "";

            foreach (KeyValuePair <char, string> entry in prevMergedState)
            {
                completePrevMergedState += entry.Value + "_";

                if (entry.Key == 'A')
                {
                    if (dfaA.FinalStates.Contains(entry.Value))
                    {
                        countFinal++;
                    }
                    if (dfaA.StartStates.Contains(entry.Value))
                    {
                        countStart++;
                    }
                }

                else if (entry.Key == 'B')
                {
                    if (dfaB.FinalStates.Contains(entry.Value))
                    {
                        countFinal++;
                    }
                    if (dfaB.StartStates.Contains(entry.Value))
                    {
                        countStart++;
                    }
                }
            }

            completePrevMergedState = completePrevMergedState.TrimEnd('_');

            if (type == MergeType.Union && countFinal == prevMergedState.Count)
            {
                merged.DefineAsFinalState(completePrevMergedState);
            }
            else if (type == MergeType.Concatenation && countFinal >= 1)
            {
                merged.DefineAsFinalState(completePrevMergedState);
            }

            if (type == MergeType.Union && countStart == prevMergedState.Count)
            {
                merged.DefineAsStartState(completePrevMergedState);
            }
            else if (type == MergeType.Concatenation && countStart >= 1)
            {
                merged.DefineAsStartState(completePrevMergedState);
            }


            if (merged.GetTransition(completePrevMergedState).Count == merged.Symbols.Count)
            {
                return;
            }

            foreach (char symbol in merged.Symbols)
            {
                Dictionary <char, string> newMergedState = new Dictionary <char, string>();
                // This could break though
                if (CheckExistingRouteForChar(completePrevMergedState, symbol, merged))
                {
                    return;
                }

                foreach (KeyValuePair <char, string> entry in prevMergedState)
                {
                    if (entry.Key == 'A')
                    {
                        CollectRoutesFromDfa(entry, symbol, dfaA, ref newMergedState);
                    }
                    else if (entry.Key == 'B')
                    {
                        CollectRoutesFromDfa(entry, symbol, dfaB, ref newMergedState);
                    }
                }

                string completeNewMergedState = "";
                foreach (KeyValuePair <char, string> entry in newMergedState)
                {
                    completeNewMergedState += entry.Value + "_";
                }
                completeNewMergedState = completeNewMergedState.TrimEnd('_');
                merged.AddTransition(new Transition <string>(completePrevMergedState, symbol, completeNewMergedState));


                AddMergedState(newMergedState, ref merged, dfaA, dfaB, type);
            }
        }