public Automata <String> inverseAutomata(Automata <String> automaton) { Automata <String> invertedAutomaton = new Automata <string>(automaton.getAlphabet()); foreach (String state in automaton.getFinalStates()) { invertedAutomaton.defineAsStartState(state); } foreach (Transition <String> transition in automaton.getTransitions()) { invertedAutomaton.addTransition(transition); } foreach (String state in automaton.getStates()) { if (!automaton.getFinalStates().Contains(state)) { invertedAutomaton.defineAsFinalState(state); } } foreach (String state in automaton.getFinalStates()) { if (state != invertedAutomaton.getStartStates().First()) { invertedAutomaton.addTransition(new Transition <string>(invertedAutomaton.getStartStates().First(), state)); } } return(invertedAutomaton); }
public Automata <String> renameStates(Automata <String> automaton) { Automata <String> renamedAutomaton = new Automata <string>(automaton.getAlphabet()); Dictionary <String, String> referencer = new Dictionary <string, string>(); int counter = 0; foreach (String state in automaton.getStates()) { referencer.Add(state, "q" + counter++); Console.WriteLine("q" + counter); } if (automaton.getStates().Contains("Ø")) { referencer["Ø"] = "Ø"; } foreach (Transition <String> transition in automaton.getTransitions()) { renamedAutomaton.addTransition(new Transition <string>(referencer[transition.getFromState()], transition.getSymbol(), referencer[transition.getToState()])); } foreach (String state in automaton.getFinalStates()) { renamedAutomaton.defineAsFinalState(referencer[state]); } foreach (String state in automaton.getStartStates()) { renamedAutomaton.defineAsStartState(referencer[state]); } return(renamedAutomaton); }
// public Automata<String> reverseAutomata(Automata<String> automaton) // { // Automata<String> reversedAutomaton = new Automata<String>(automaton.getAlphabet()); // // foreach (Transition<String> transition in automaton.getTransitions()) // { // transition.reverseFromToState(); // reversedAutomaton.addTransition(transition); // } // // foreach (String state in automaton.getStartStates()) { // reversedAutomaton.defineAsStartState(state); // } // // foreach (String state in automaton.getFinalStates()) { // reversedAutomaton.defineAsFinalState(state); // } // reversedAutomaton.printTransitions(); // // reversedAutomaton = inverseAutomata(reversedAutomaton); // // reversedAutomaton.printTransitions(); // return reversedAutomaton; // } public Automata <String> reverseAutomata(Automata <String> automaton) { Automata <String> reversedAutomaton = new Automata <string>(automaton.getAlphabet()); foreach (Transition <String> transition in automaton.getTransitions()) { reversedAutomaton.addTransition(new Transition <String> (transition.getToState(), transition.getSymbol(), transition.getFromState())); } foreach (String state in automaton.getFinalStates()) { reversedAutomaton.defineAsStartState(state); } foreach (String state in automaton.getStartStates()) { reversedAutomaton.defineAsFinalState(state); } return(reversedAutomaton); }
public Automata <String> NDFAToDFA(Automata <String> Automaton) { SortedSet <char> alphabet = Automaton.getAlphabet(); Automata <String> dfa = new Automata <String>(alphabet); Dictionary <Transition <String>, SortedSet <String> > transitionsDict = new Dictionary <Transition <String>, SortedSet <String> >(); dfa.defineAsStartState(prettyPrint(findStartState(Automaton))); //Set startstates for dfa foreach (char letter in alphabet) //Check all states after the startstate with all the letters in the alphabet { if (findMultipleAccessible(Automaton, letter, findStartState(Automaton)).Count() == 0) { dfa.addTransition(new Transition <string>(prettyPrint(findStartState(Automaton)), letter, "Ø")); } else { SortedSet <String> toStates = findMultipleAccessible(Automaton, letter, findStartState(Automaton)); Transition <String> newTransition = new Transition <string>(prettyPrint(findStartState(Automaton)), letter, prettyPrint(toStates)); transitionsDict.Add(newTransition, toStates); dfa.addTransition(newTransition); } } int tempTransitionsNumber = 0; Dictionary <Transition <String>, SortedSet <String> > tempTransitionsDict = new Dictionary <Transition <String>, SortedSet <String> >(); while (dfa.getTransistionsNumber() != tempTransitionsNumber) { tempTransitionsNumber = dfa.getTransistionsNumber(); foreach (KeyValuePair <Transition <String>, SortedSet <String> > pair in transitionsDict) { foreach (char letter in alphabet) { if (findMultipleAccessible(Automaton, letter, pair.Value).Count() == 0) { if (dfa.getTransitions().Contains(new Transition <string>(pair.Key.getToState(), letter, "Ø")) == false) { dfa.addTransition(new Transition <string>(pair.Key.getToState(), letter, "Ø")); tempTransitionsDict.Add(new Transition <string>(pair.Key.getToState(), letter, "Ø"), findMultipleAccessible(Automaton, letter, pair.Value)); } } else { SortedSet <String> toStates = findMultipleAccessible(Automaton, letter, pair.Value); Transition <String> newTransition = new Transition <string>(pair.Key.getToState(), letter, prettyPrint(toStates)); if (dfa.getTransitions().Contains(new Transition <string>(pair.Key.getToState(), letter, prettyPrint(toStates))) == false) { tempTransitionsDict.Add(newTransition, toStates); dfa.addTransition(newTransition); } } } } transitionsDict = transitionsDict.Union(tempTransitionsDict).ToDictionary(k => k.Key, v => v.Value); } SortedSet <String> tempListFinal = new SortedSet <String>(); foreach (String stat in dfa.getStates()) { foreach (String endState in Automaton.getFinalStates()) { if (stat.Contains(endState)) { tempListFinal.Add(stat); } } } foreach (String finState in tempListFinal) { dfa.defineAsFinalState(finState); } return(dfa); }