def ntm_table(name): tmachine = Tmachine.query.filter_by(id=name) for i in tmachine: states_d = i.states input_symbols_d = i.input_symbols tape_symbols_d = i.tape_symbols left_end_d = i.left_end transitions_d = i.transitions initial_state_d = i.initial_state blank_symbol_d = i.blank_symbol reject_state_d = i.reject_state final_states_d = i.final_states ntm = NTM(states=literal_eval(states_d), input_symbols=literal_eval(input_symbols_d), tape_symbols=literal_eval(tape_symbols_d), left_end=left_end_d, transitions=literal_eval(transitions_d), initial_state=initial_state_d, blank_symbol=blank_symbol_d, reject_state=reject_state_d, final_states=final_states_d) dc = ntm.transitions df = pd.DataFrame.from_dict(data=dc, orient='index') df.fillna("( -, -, - )", inplace=True) df = df.applymap(lambda x: change_ntm(x)) rows, columns = df.shape reject_state = "( " + ntm.reject_state + ", -, - )" final_state = "( " + ntm.final_states + ", -, - )" z1 = [] z2 = [] for i in ntm.tape_symbols: z2.append(reject_state) z1.append(final_state) df1 = pd.DataFrame([z1], index=[ntm.final_states], columns=list(df.columns.values)) df2 = pd.DataFrame([z2], index=[ntm.reject_state], columns=list(df.columns.values)) df = df.append(df1) df = df.append(df2) return df, ntm.input_symbols
def ntm(): form = MyForm() if form.validate_on_submit(): tm = False states = parser1.dict_parse(form.states.data) states_dict = literal_eval(states) input_s = parser1.dict_parse(form.input_symbols.data) input_symbols_dict = literal_eval(input_s) tape_s = parser1.dict_parse(form.tape_symbols.data) tape_symbols_dict = literal_eval(tape_s) initial_state_d = form.initial_state.data if form.left_end.data: left_end_d = form.left_end.data else: left_end_d = '>' try: transitions_d = literal_eval(form.prechody.data) except: transitions_d = {} success_message = ("Neočakávaná chyba v prechodovej funkcii") flash(success_message) if form.blank_symbol.data: blank_symbol_d = form.blank_symbol.data else: blank_symbol_d = '#' reject_state_d = form.reject_state.data final_states_d = form.final_states.data try: ntm = NTM(states=states_dict, input_symbols=input_symbols_dict, tape_symbols=tape_symbols_dict, left_end=left_end_d, transitions=transitions_d, initial_state=initial_state_d, blank_symbol=blank_symbol_d, reject_state=reject_state_d, final_states=final_states_d) if ntm: tm = True except (exceptions.InvalidStateError, exceptions.InvalidSymbolError, exceptions.MissingStateError, exceptions.MissingSymbolError, exceptions.InitialStateError, exceptions.FinalStateError, exceptions.RejectStateError, exceptions.LeftEndError, exceptions.RejectionError, exceptions.InvalidDirectionError) as err: tm = False success_message = (err) flash(success_message) if tm: try: tmachine = Tmachine(definicia=form.funkcia.data, tm_d_n_x='ntm') db.session.add(tmachine) db.session.commit() except exc.IntegrityError: db.session().rollback() tm = False success_message = ( "Definícia/Názov TM už existuje prosím zvolte iný názov") flash(success_message) if tm: db.session.add(tmachine) db.session.commit() name = str(tmachine.id) with open("tm{}.py".format(name), "w") as text_file: text_file.write( "from turing_machine.ntm import NTM \nntm = NTM(" "\nstates = {0}" ",\ninput_symbols = {1}," "\ntape_symbols = {2},\nleft_end = '{3}'," "\ntransitions = {4}," "\ninitial_state = '{5}',\nblank_symbol = '{6}'," "\nreject_state = '{7}', \nfinal_states = '{8}' \n)". format(states_dict, input_symbols_dict, tape_symbols_dict, left_end_d, transitions_d, initial_state_d, blank_symbol_d, reject_state_d, final_states_d)) success_message = ('Nový NTM: ' + form.funkcia.data + ' je vytvorený') flash(success_message) return redirect(url_for('index')) return render_template('ntm.html', title='NTM', form=form)
ntm = NTM(states={'rj', 'q1', 'qi', 'q3', 'q0', 'q2', 'q4', 'q5'}, input_symbols={'0', '1'}, tape_symbols={'.', '0', 'y', '>', 'x', '1'}, left_end='>', transitions={ 'qi': { '>': {('q0', '>', 'R')} }, 'q0': { '0': {('q1', 'x', 'R'), ('q5', '0', 'R'), ('rj', '0', 'R')}, 'y': {('q3', 'y', 'R')} }, 'q1': { '0': {('q1', '0', 'R')}, '1': {('rj', '1', 'R'), ('q2', 'y', 'L')}, 'y': {('q1', 'y', 'R')} }, 'q2': { '0': {('q2', '0', 'L')}, 'x': {('q0', 'x', 'R')}, 'y': {('q2', 'y', 'L')} }, 'q3': { 'y': {('q3', 'y', 'R')}, '.': {('q4', '.', 'R')} }, 'q5': { '0': {('q5', '0', 'R')}, '.': {('rj', '0', 'R')} } }, initial_state='qi', blank_symbol='.', reject_state='rj', final_states='q4')
def ntm(): form = MyForm() if form.validate_on_submit(): tm = False states = parser1.dict_parse(form.states.data) states_dict = literal_eval(states) input_s = parser1.dict_parse(form.input_symbols.data) input_symbols_dict = literal_eval(input_s) tape_s = parser1.dict_parse(form.tape_symbols.data) tape_symbols_dict = literal_eval(tape_s) initial_state_d = form.initial_state.data if form.left_end.data: left_end_d = form.left_end.data else: left_end_d = '>' try: transitions_d = literal_eval(form.prechody.data) except: transitions_d = {} success_message = ("Neočakávaná chyba v prechodovej funkcii") flash(success_message) if form.blank_symbol.data: blank_symbol_d = form.blank_symbol.data else: blank_symbol_d = '#' reject_state_d = form.reject_state.data final_states_d = form.final_states.data try: ntm = NTM( states = states_dict, input_symbols= input_symbols_dict, tape_symbols= tape_symbols_dict, left_end = left_end_d, transitions = transitions_d, initial_state = initial_state_d, blank_symbol = blank_symbol_d, reject_state = reject_state_d, final_states = final_states_d ) if ntm: tm = True except(exceptions.InvalidStateError,exceptions.InvalidSymbolError,exceptions.MissingStateError,exceptions.MissingSymbolError, exceptions.InitialStateError,exceptions.FinalStateError,exceptions.RejectStateError,exceptions.LeftEndError, exceptions.RejectionError,exceptions.InvalidDirectionError) as err: tm = False success_message = (err) flash(success_message) if tm: try: tmachine = Tmachine(definicia= form.funkcia.data,tm_d_n_x='ntm', states =states,input_symbols = input_s, tape_symbols = tape_s,left_end = left_end_d, transitions = form.prechody.data, initial_state = initial_state_d, blank_symbol = blank_symbol_d,reject_state = reject_state_d, final_states = final_states_d) db.session.add(tmachine) db.session.commit() success_message = ('Nový NTM: '+form.funkcia.data+' je vytvorený') flash(success_message) return redirect(url_for('main.index')) except exc.IntegrityError: db.session().rollback() tm = False success_message = ("Definícia/Názov TM už existuje prosím zvolte iný názov") flash(success_message) return render_template('ntm.html', title='NTM', form=form)
def upload(file): name = (file.readline()[:-2:]).decode("utf-8") druh = (file.readline()[:-2:]).decode("utf-8") states_u = (file.readline()[:-3:]).decode("utf-8") input_symbols_u = (file.readline()[:-3:]).decode("utf-8") tape_symbols_u = (file.readline()[:-3:]).decode("utf-8") left_end_u = (file.readline()[:-3:]).decode("utf-8") transitions_u = (file.readline()[:-3:]).decode("utf-8") initial_state_u = (file.readline()[:-3:]).decode("utf-8") blank_symbol_u = (file.readline()[:-3:]).decode("utf-8") reject_state_u = (file.readline()[:-3:]).decode("utf-8") final_states_u = (file.readline()[:-2:]).decode("utf-8") if druh == 'DTM': form = MyForm() druh = 'dtm' tm = False try: dtm = DTM(states=literal_eval(states_u), input_symbols=literal_eval(input_symbols_u), tape_symbols=literal_eval(tape_symbols_u), left_end=literal_eval(left_end_u), transitions=literal_eval(transitions_u), initial_state=literal_eval(initial_state_u), blank_symbol=literal_eval(blank_symbol_u), reject_state=literal_eval(reject_state_u), final_states=literal_eval(final_states_u)) if dtm: tm = True except (exceptions.InvalidStateError, exceptions.InvalidSymbolError, exceptions.MissingStateError, exceptions.MissingSymbolError, exceptions.InitialStateError, exceptions.FinalStateError, exceptions.RejectStateError, exceptions.LeftEndError, exceptions.RejectionError, exceptions.InvalidDirectionError) as err: tm = False success_message = (err) return success_message, druh if tm: try: tmachine = Tmachine( definicia=name, tm_d_n_x='dtm', states=states_u, input_symbols=input_symbols_u, tape_symbols=tape_symbols_u, left_end=literal_eval(left_end_u), transitions=transitions_u, initial_state=literal_eval(initial_state_u), blank_symbol=literal_eval(blank_symbol_u), reject_state=literal_eval(reject_state_u), final_states=literal_eval(final_states_u)) db.session.add(tmachine) db.session.commit() success_message = True return success_message, druh except exc.IntegrityError: db.session().rollback() tm = False success_message = ( "Názov turingovho stroja už existuje prosím zvolte iný názov" ) return success_message, druh elif druh == 'NTM': tm = False try: ntm = NTM(states=literal_eval(states_u), input_symbols=literal_eval(input_symbols_u), tape_symbols=literal_eval(tape_symbols_u), left_end=literal_eval(left_end_u), transitions=literal_eval(transitions_u), initial_state=literal_eval(initial_state_u), blank_symbol=literal_eval(blank_symbol_u), reject_state=literal_eval(reject_state_u), final_states=literal_eval(final_states_u)) if ntm: tm = True except (exceptions.InvalidStateError, exceptions.InvalidSymbolError, exceptions.MissingStateError, exceptions.MissingSymbolError, exceptions.InitialStateError, exceptions.FinalStateError, exceptions.RejectStateError, exceptions.LeftEndError, exceptions.RejectionError, exceptions.InvalidDirectionError) as err: tm = False success_message = (err) return success_message if tm: try: tmachine = Tmachine( definicia=name, tm_d_n_x='ntm', states=states_u, input_symbols=input_symbols_u, tape_symbols=tape_symbols_u, left_end=literal_eval(left_end_u), transitions=transitions_u, initial_state=literal_eval(initial_state_u), blank_symbol=literal_eval(blank_symbol_u), reject_state=literal_eval(reject_state_u), final_states=literal_eval(final_states_u)) db.session.add(tmachine) db.session.commit() success_message = True return success_message, druh except exc.IntegrityError: db.session().rollback() tm = False success_message = ( "Názov turingovho stroja už existuje prosím zvolte iný názov" ) return success_message, druh elif druh == 'XDTM': tm = False try: xdtm = XDTM(states=literal_eval(states_u), input_symbols=literal_eval(input_symbols_u), tape_symbols=literal_eval(tape_symbols_u), left_end=literal_eval(left_end_u), transitions=literal_eval(transitions_u), initial_state=literal_eval(initial_state_u), blank_symbol=literal_eval(blank_symbol_u), reject_state=literal_eval(reject_state_u), final_states=literal_eval(final_states_u)) if xdtm: tm = True except (exceptions.InvalidStateError, exceptions.InvalidSymbolError, exceptions.MissingStateError, exceptions.MissingSymbolError, exceptions.InitialStateError, exceptions.FinalStateError, exceptions.RejectStateError, exceptions.LeftEndError, exceptions.RejectionError, exceptions.InvalidDirectionError) as err: tm = False success_message = (err) return success_message if tm: try: tmachine = Tmachine( definicia=name, tm_d_n_x='xtm', states=states_u, input_symbols=input_symbols_u, tape_symbols=tape_symbols_u, left_end=literal_eval(left_end_u), transitions=transitions_u, initial_state=literal_eval(initial_state_u), blank_symbol=literal_eval(blank_symbol_u), reject_state=literal_eval(reject_state_u), final_states=literal_eval(final_states_u)) db.session.add(tmachine) db.session.commit() success_message = True return success_message, druh except exc.IntegrityError: db.session().rollback() tm = False success_message = ( "Názov turingovho stroja už existuje prosím zvolte iný názov" ) return success_message, druh elif druh == 'XNTM': pass #### VIACPASKOVY NEDETERMINISCKY TM else: success_message = "Turingov stroj nebolo možné načítať" druh = False return success_message, druh
def ntm_table_none_final(name, vstup, final): tmachine = Tmachine.query.filter_by(id=name) for i in tmachine: states_d = i.states input_symbols_d = i.input_symbols tape_symbols_d = i.tape_symbols left_end_d = i.left_end transitions_d = i.transitions initial_state_d = i.initial_state blank_symbol_d = i.blank_symbol reject_state_d = i.reject_state final_states_d = i.final_states ntm = NTM(states=literal_eval(states_d), input_symbols=literal_eval(input_symbols_d), tape_symbols=literal_eval(tape_symbols_d), left_end=left_end_d, transitions=literal_eval(transitions_d), initial_state=initial_state_d, blank_symbol=blank_symbol_d, reject_state=reject_state_d, final_states=final_states_d) dc = ntm.transitions df = pd.DataFrame.from_dict(data=dc, orient='index') df.fillna("( -, -, - )", inplace=True) df = df.applymap(lambda x: change_ntm(x)) rows, columns = df.shape reject_state = "( " + ntm.reject_state + ", -, - )" final_state = "( " + ntm.final_states + ", -, - )" z1 = [] z2 = [] for i in ntm.tape_symbols: z2.append("( -, -, - )") z1.append("( -, -, - )") df1 = pd.DataFrame([z1], index=[ntm.final_states], columns=list(df.columns.values)) df2 = pd.DataFrame([z2], index=[ntm.reject_state], columns=list(df.columns.values)) df = df.append(df1) df = df.append(df2) generator = ntm.validate_input_ntm_none_final(vstup, final, step=True) list_of_table = [] list_of_tape = [] tape_index = [] tape_table = [] tape_index.append(ntm.left_end) tape_table.append("") for i in vstup: tape_index.append(i) tape_table.append("") tape_table[0] = "^" dl = pd.DataFrame(data=tape_table, index=tape_index) dl.columns = [""] dl = dl.T list_of_tape.append(dl) stroj = False counter = 0 for s, t, c, tape_symbol, current_state, tape, final, cycle, cross, crossnumber in generator: if ntm.final_states == current_state: stroj = True list_of_table.append( df.style.applymap(color_red, subset=pd.IndexSlice[current_state, [tape_symbol]])) tape_index = [] tape_table = [] if c == 'R': counter += 1 if c == 'L': counter -= 1 for i in tape: tape_index.append(i) tape_table.append("") tape_index.append(ntm.blank_symbol) tape_table.append("") if len(tape) - 1 < counter: tape_index.append(ntm.blank_symbol) tape_table.append("") tape_table[counter] = "^" dl = pd.DataFrame(data=tape_table, index=tape_index) dl.columns = [""] dl = dl.T list_of_tape.append(dl) return df, list_of_table, stroj, list_of_tape, cycle, final, cross, crossnumber