/// <summary> /// Creates 3 example NDFA. /// </summary> public static void CreateExampleNDFA() { // With epsilon Automata <string> exmapleNDFA1 = TestAutomata.ReadGraphFile(@".\..\..\graphviz\dotfiles\ndfaExample1"); FileDotEngine.Run(@".\..\..\graphviz\dotfiles\ndfaExample1", "ndfaExample1Pic"); Console.WriteLine($"\nExample NDFA1 is a dfa = {exmapleNDFA1.IsDFA()}"); // EClosure test Console.WriteLine($"NDFA1 eClosure should be = A,B,C,E,F,H,M,O,Q,S From state S\n " + $"Result = {String.Join(",",exmapleNDFA1.EClosure("S").OrderBy(q => q).ToList())}\n"); Console.WriteLine($"NDFA1 eClosure should be = C,E,H From state C\n " + $"Result = {String.Join(",", exmapleNDFA1.EClosure("C").OrderBy(q => q).ToList())}\n"); Automata <string> exmapleNDFA2 = TestAutomata.ReadGraphFile(@".\..\..\graphviz\dotfiles\ndfaExample2"); FileDotEngine.Run(@".\..\..\graphviz\dotfiles\ndfaExample2", "ndfaExample2Pic"); Console.WriteLine($"\nExample NDFA2 is a dfa = {exmapleNDFA2.IsDFA()}"); Automata <string> exmapleNDFA3 = TestAutomata.ReadGraphFile(@".\..\..\graphviz\dotfiles\ndfaExample3"); FileDotEngine.Run(@".\..\..\graphviz\dotfiles\ndfaExample3", "ndfaExample3Pic"); Console.WriteLine($"\nExample NDFA3 is a dfa = {exmapleNDFA3.IsDFA()}"); // With epsilon Automata <string> exmapleNDFA4 = TestAutomata.ReadGraphFile(@".\..\..\graphviz\dotfiles\ndfaExample4"); FileDotEngine.Run(@".\..\..\graphviz\dotfiles\ndfaExample4", "ndfaExample4Pic"); Console.WriteLine($"\nExample NDFA4 is a dfa = {exmapleNDFA4.IsDFA()}"); // EClosure test Console.WriteLine($"NDFA4 eClosure should be = B,C \n " + $"Result = {String.Join(",", exmapleNDFA4.EClosure("B").OrderBy(q => q).ToList())}\n"); }
private static bool GenerateToState(ref string toState, string[] states, char symbol, Automata <string> ndfa) { bool isFinalState = false; SortedSet <string> newStates = new SortedSet <string>(); foreach (string state in states) { // Get the transitions of the NDFA List <Transition <string> > trans = ndfa.GetTransitions(state); // Possible error with getting to states with eClosure foreach (Transition <string> t in trans) { if (t.GetSymbol() == symbol) { // TODO remove for debugging onlu //var ep = ndfa.EClosure(t.GetFromState()); //var ep2 = ndfa.EClosure(t.GetToState()); newStates.UnionWith(ndfa.EClosure(t.GetToState())); } } } foreach (string subState in newStates) { toState += subState + "_"; if (ndfa.endStates.Contains(subState)) { isFinalState = true; } } toState = toState.TrimEnd('_'); return(isFinalState); }
public static Automata <string> ConvertToDFA(Automata <string> ndfa) { SortedSet <char> dfaAlphabet = ndfa.GetAlphabet(); dfaAlphabet.Remove('$'); Automata <string> dfa = new Automata <string>(dfaAlphabet); string combinedStartState = ""; SortedSet <string> completeStartState = new SortedSet <string>(); bool isFinalState = false; // Create list of startstates (startstates + reachable states via EClosure) foreach (string startState in ndfa.startStates) { List <string> reachableStates = ndfa.EClosure(startState); // split each reachable state up in indivual parts if its a combination of states foreach (string s in reachableStates) { string[] states = s.Split('_'); completeStartState.UnionWith(states); } } // Create a the complete startstate by seperating all the reachable "startstates" with a _ foreach (string s in completeStartState) { combinedStartState += s + "_"; if (ndfa.endStates.Contains(s)) // If 1 of these states is a endstate the combined startstate is an endstate { isFinalState = true; } } //trim last "_" off of string combinedStartState = combinedStartState.TrimEnd('_'); // Start if the conversion to DFA ConvertState(combinedStartState, ref dfa, ref ndfa); // Define the only combined startstate dfa.DefineAsStartState(combinedStartState); if (isFinalState) { dfa.DefineAsFinalState(combinedStartState); } // Create all transitions for each symbol to the fuik itself if (dfa.states.Contains("Fuik")) { foreach (char symbol in dfa.GetAlphabet()) { dfa.AddTransition(new Transition <string>("Fuik", symbol, "Fuik")); } } // Do final stuff //return RemoveStateSeperators(dfa); return(RenameStates(RemoveStateSeperators(dfa))); }