/// <summary> /// Prints automaton/transducer to file. /// </summary> /// <param name="fileName">Path to output file.</param> /// <param name="m">Automaton/transducer.</param> /// <param name="format">Format of output.</param> /// <param name="sort">Sort states, moves, etc.?</param> /// <param name="stateSymbolsFileName">Path to state symbols file (optional and only used with FSM format).</param> /// <param name="inputSymbolsFileName">Path to input arc symbols file (optional and only used with FSM format).</param> /// <param name="outputSymbolsFileName">Path to output arc symbols file (optional and only used with a transducer in FSM format).</param> public static void PrintAutomaton( string fileName, ISSAutomaton <SYMBOL> m, PrintFormat format, bool sort = true, string stateSymbolsFileName = null, string inputSymbolsFileName = null, string outputSymbolsFileName = null) { using (var file = new StreamWriter(fileName)) { PrintAutomaton(file, m, format, sort); } }
/* prints automaton, including optional image (temporarily pauses timer) */ internal void PrintAutomaton(ISSAutomaton <SYMBOL> aut, string dir, string fileName, bool normalize = true) { stopwatch.Stop(); string ext; switch (format) { case PrintFormat.TIMBUK: ext = "tmb"; break; case PrintFormat.FSA: ext = "pl"; break; default: ext = format.ToString().ToLower(); break; } string path = Path.Combine(dir, fileName); string textPath = path + "." + ext; if (normalize) { aut = aut.GenericNormalize(); } Printer <SYMBOL> .PrintAutomaton(textPath, aut, format); if (imgExt != "") // print image by executing DOT process { string imgPath = path + "." + imgExt; var process = new Process(); process.StartInfo.FileName = "dot"; if (format == PrintFormat.DOT) // already created DOT file, just read from there { process.StartInfo.Arguments = string.Format("-T{0} {1} -o {2}", imgExt, textPath, imgPath); process.Start(); } else // print automaton as DOT and pipe to process' standard input { process.StartInfo.Arguments = string.Format("-T{0} -o {1}", imgExt, imgPath); process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.Start(); StreamWriter stdin = process.StandardInput; Printer <SYMBOL> .PrintAutomaton(stdin, aut, PrintFormat.DOT); stdin.Close(); } } stopwatch.Start(); }
/// <summary> /// Prints automaton/transducer to standard output. /// </summary> /// <param name="m">Automaton/transducer.</param> /// <param name="format">Format of output.</param> /// <param name="sort">Sort states, moves, etc.?</param> public static void PrintAutomaton(ISSAutomaton <SYMBOL> m, PrintFormat format, bool sort = true) { StreamWriter file = new StreamWriter(Console.OpenStandardOutput()); TextWriter stdout = Console.Out; Console.SetOut(file); try { PrintAutomaton(file, m, format, sort); } finally { file.Close(); Console.SetOut(stdout); } }
/// <summary> /// Prints automaton/transducer to open stream writer. /// </summary> /// <param name="file">Stream writer (caller must close).</param> /// <param name="m">Automaton/transducer.</param> /// <param name="format">Format of output.</param> /// <param name="sort">Sort states, moves, etc.?</param> /// <param name="stateSymbolsFileName">Path to state symbols file (optional and only used with FSM format).</param> /// <param name="inputSymbolsFileName">Path to input arc symbols file (optional and only used with FSM format).</param> /// <param name="outputSymbolsFileName">Path to output arc symbols file (optional and only used with a transducer in FSM format).</param> public static void PrintAutomaton( StreamWriter file, ISSAutomaton <SYMBOL> m, PrintFormat format, bool sort = true, string stateSymbolsFileName = null, string inputSymbolsFileName = null, string outputSymbolsFileName = null) { // make list to enable ordering List <int> states = m.States.ToList(); List <SYMBOL> alphabet = m.Alphabet.ToList(); List <Move <ILabel <SYMBOL> > > moves = m.GenericMoves.Distinct().ToList(); int initialState = m.InitialState; List <int> finalStates = m.FinalStates.ToList(); if (sort) { alphabet.Sort(); if (m.StateNames == null) // sort states by number { states.Sort(); finalStates.Sort(); } else // sort states by name { Comparer <int> comparer = Comparer <int> .Create((s1, s2) => string.Compare(m.StateNames[s1], m.StateNames[s2])); states.Sort(comparer); finalStates.Sort(comparer); } } Dictionary <int, string> stateNames; if (m.StateNames == null) // if no state names, provide defaults { stateNames = new Dictionary <int, string>(states.Count); foreach (int state in states) { stateNames[state] = (format == PrintFormat.DOT) ? string.Format("q_{0}{1}{2}", '{', state, '}') : // DOT can display number as subscript string.Format("q{0}", state); } } else { stateNames = m.StateNames; } Action <StreamWriter, AutomatonType, string, Dictionary <int, string>, PredicateAlgebra <SYMBOL>, List <int>, List <SYMBOL>, List <Move <ILabel <SYMBOL> > >, int, List <int>, string, string, string> print; switch (format) { case PrintFormat.DOT: print = PrintAutomatonDot; break; case PrintFormat.TIMBUK: print = PrintAutomatonTimbuk; break; case PrintFormat.FSA: print = PrintAutomatonFSA; break; case PrintFormat.FSM: print = PrintAutomatonFSM; break; default: throw new ArgumentOutOfRangeException("Invalid print format value"); } // print in given format print( file, m.Type, m.Name, stateNames, new PredicateAlgebra <SYMBOL>(m.Alphabet), states, alphabet, moves, initialState, finalStates, stateSymbolsFileName, inputSymbolsFileName, outputSymbolsFileName ); }