def _saveFA(aut, io): if isinstance(aut, DFA): io.write("@DFA ") NFAp = False elif isinstance(aut, NFA): io.write("@NFA ") NFAp = True else: raise common.DFAerror() if not NFAp and aut.Initial != 0: foo = {0: aut.Initial, aut.Initial: 0} aut.reorder(foo) for sf in aut.Final: io.write("{0:>s} ".format(statePP(aut.States[sf]))) if NFAp: io.write(" * ") for sf in aut.Initial: io.write("{0:>s} ".format(statePP(aut.States[sf]))) io.write("\n") for s in xrange(len(aut.States)): if s in aut.delta: for a in aut.delta[s].keys(): if isinstance(aut.delta[s][a], set): for s1 in aut.delta[s][a]: io.write("{0:>s} {1:>s} {2:>}\n".format( statePP(aut.States[s]), str(a), statePP(aut.States[s1]))) else: io.write("{0:>s} {1:>s} {2:>s}\n".format( statePP(aut.States[s]), str(a), statePP(aut.States[aut.delta[s][a]]))) else: io.write("{0:>s} \n".format(statePP(aut.States[s])))
def saveToJson(FileName, aut, mode="w"): """ Saves a finite automata definition to a file using the JSON format """ try: f = open(FileName, mode) except IOError: raise common.DFAerror() f.write(toJson(aut)) f.close()
def exportToGrail(fileName, fa): """ Saves a finite automatom definition to a file using Grail format :arg fileName: file name :type fileName: string :arg fa: the FA :type fa: FA""" try: f = open(fileName, "w") except IOError: raise common.DFAerror() FAToGrail(f, fa) f.close()
def _exportToTeX(FileName, fa): """ Saves a finite automatom definition to a latex tabular. Saves a finite automata definition to a file using the input format .. versionchanged:: 0.9.4 :param str FileName: file name :param FA fa: the FA :raises DFAerror: if a file error occurs""" try: f = open(FileName, "w") except IOError: raise common.DFAerror() # initial is the first one if fa.Initial: foo = {0: fa.Initial, fa.Initial: 0} fa.reorder(foo) f.write("$$\\begin{array}{r|") for i in xrange(len(fa.Sigma)): f.write("|c") f.write("}\n") for c in fa.Sigma: f.write("&{0:>s}".format(str(c))) f.write(" \\\\\hline\n") for s in xrange(len(fa.States)): if s in fa.delta: if fa.Initial == s: f.write("\\rightarrow") if s in fa.Final: f.write("\\star") f.write("{0:>s}".format(str(s))) for a in fa.delta[s].keys(): if isinstance(fa.delta[s][a], set): f.write("&\{") for s1 in fa.delta[s][a]: f.write("{0:>s} ".format(str(s1))) f.write("\}") else: s1 = fa.delta[s][a] f.write("&{0:>s}".format(str(s1))) f.write("\\\\\n") f.write("\end{array}$$") f.close()
def saveToFile(FileName, fa, mode="a"): """ Saves a list finite automata definition to a file using the input format .. versionchanged:: 0.9.5 .. versionchanged:: 0.9.6 .. versionchanged:: 0.9.7 New format with quotes and alphabet :param str FileName: file name :param fa: the FA :type fa: list of FA :param str mode: writing mode""" # TODO: write the complete information into file according with the new format try: f = open(FileName, mode) except IOError: raise common.DFAerror() if type(fa) == list: for d in fa: f.write(saveToString(d)) else: f.write(saveToString(fa)) f.close()