def mostrar(self): f = open("Token" + str(self.N) + ".dot", "w") f.write( "digraph{\nfake [style=invisible]\nfake -> s0 [style=bold]\n\n") print("Alfabeto que acepta el automata") print(self.Sigma) print("Estado de inicio del automata") print(self.S.__str__()) print("Conjunto de Estados(Objeto) no vacios") for e in self.K: print(e.__str__()) f.write(e.__str__()) f.write("\n") f.write("\n") print("Conjunto de Estados(Objeto) de aceptacion") print(self.Z.__str__()) print("Conjunto de Transiciones(Objeto)") for e in self.M: print(e.__str__()) f.write(e.__str__() + "\n") f.write("\n}") f.close() afn = automata_IO.nfa_dot_importer("Token" + str(self.N) + ".dot") automata_IO.nfa_to_dot(afn, "Token" + str(self.N), ".")
def nfa_tb_to_diagram(json_filename, output_filename): with open(json_filename) as json_file: nfa_tb = json.load(json_file) # construct nfa parameters alphabet = nfa_tb["alphabet"] states = nfa_tb["states"] nfa = dict() nfa["alphabet"] = set(alphabet) nfa["states"] = set(states) nfa["initial_states"] = set(nfa_tb["initial_states"]) nfa["accepting_states"] = set(nfa_tb["accepting_states"]) # construct nfa transitions from transition table transition_table = nfa_tb["transition_table"] if len(transition_table) != len(states): raise Exception( "number of rows in transition table not equal to number of states") if len(transition_table[0]) != len(alphabet): raise Exception( "number of columns in transition table not equal to number of elements in alphabet" ) transitions = dict() for i in range(len(states)): for j in range(len(alphabet)): if len(transition_table[i][j]) > 0: transitions[(states[i], alphabet[j])] = set(transition_table[i][j]) nfa["transitions"] = transitions print(nfa) automata_IO.nfa_to_dot(nfa, output_filename)
def main(): automataDict = automata_IO.nfa_json_importer("./resources/AFN.json") alphabet = automataDict["alphabet"] initialState = "" for state in automataDict["initial_states"]: initialState = state break transitions = automataDict["transitions"] acceptingStates = automataDict["accepting_states"] automata_IO.nfa_to_dot(automataDict, "AFN", "./resources") automata = AFNAutomata(alphabet, initialState, transitions, acceptingStates) AFNProcessScreen(automata)
def test_nfa_graphviz_intersection_render(self): """ Tests rendering through graphviz library a nfa derived from an intersection """ automata_IO.nfa_to_dot(self.nfa_test_02, 'graphviz_nfa_intersection', 'tests/outputs')
def test_nfa_to_dot(self): """ Tests a simple nfa rendering thorough graphviz library""" automata_IO.nfa_to_dot(self.nfa_test_01, 'graphviz_nfa_simple', 'tests/outputs')
if __name__ == "__main__": automata = Automata() ''' In order to run this program you must to create a file called, i.e., "RE.txt" which has to contain the Regular expresion and the alphabet, such as: 0*.1.0*.1.0*.1.0* 0 1 Where the first line is the RE and the next ones are the alphabet ''' automata.readFile("RE.txt") automata.convertREToPostfix() automata.convertREToNFA() automata.createTransitionMatrix() nfa_example = automata_IO.nfa_json_importer('quintuple_NFA.json') automata_IO.nfa_to_dot(nfa_example, 'graphic_NFA', './') automata.NFA_to_DFA() dfa_example = automata_IO.dfa_json_importer('quintuple_DFA.json') automata_IO.dfa_to_dot(dfa_example, 'graphic_DFA', './') automata.validateStrings() print( "The quintuple for the NFA is in 'quintuple_NFA.json', the graphic is in 'graphic_NFA.dot.svg'" ) print( "The quintuple for the DFA is in 'quintuple_DFA.json', the graphic is in 'graphic_DFA.dot.svg'" )
nfa_example = { 'alphabet': {'0', '1'}, 'states': {'q0', 'q1', 'q2', 'q3'}, 'initial_states': {'q0'}, 'accepting_states': {'q3'}, 'transitions': { ('q0', '0'): {'q0', 'q1'}, ('q0', '1'): {'q0', 'q2'}, ('q1', '0'): {'q3'}, ('q1', '1'): {'q3'}, ('q2', '0'): {'q3'} } } automata_IO.nfa_to_dot(nfa_example, 'nfa_example') tb_to_diagram.nfa_tb_to_diagram("nfa_transition_table.json", "nfa_transition_table") tb_to_diagram.dfa_tb_to_diagram("dfa_transition_table.json", "dfa_transition_table") tb_to_diagram.nfa_tb_to_diagram("example1.json", "example1") tb_to_diagram.dfa_tb_to_diagram("example2.json", "example2") tb_to_diagram.dfa_tb_to_diagram("example3.json", "example3")
def test_rename_nfa_states(self): """ Tests a correct NFA states renaming """ automata_IO.nfa_to_dot(NFA.rename_nfa_states(self.nfa_1, 'TOP_'), 'nfa_renamed_1', 'tests/outputs')
from PySimpleAutomata import NFA, automata_IO nfa_example = automata_IO.nfa_dot_importer('Input.dot') automata_IO.nfa_to_dot(nfa_example, 'Input', '.') import webbrowser import os new = 2 url = 'file://' + os.path.realpath("input.dot.svg") webbrowser.open(url, new=new)
instance.read_and_store_accepting_states() instance.read_and_store_dfa_transitions() else: instance.read_and_store_initial_states() instance.read_and_store_accepting_states() instance.read_and_store_nfa_transitions() with open("Part1.json", "w") as file: json.dump(instance.getData(), file) if is_dfa: dfa = automata_IO.dfa_json_importer('Part1.json') automata_IO.dfa_to_dot(dfa, 'dfa') else: nfa = automata_IO.nfa_json_importer('Part1.json') automata_IO.nfa_to_dot(nfa, 'nfa') is_dfa_nfa_selection_exit = True is_exit = True if choice in ["2", 2]: instance = COMP5361() instance.read_and_store_alphabets() instance.read_and_store_states() instance.read_and_store_initial_states() instance.read_and_store_accepting_states() instance.read_and_store_nfa_transitions() instance.nfa_to_dfa_conversion() instance.build_Data() instance.display_nfa_to_dfa_transition_table()