static public MachineFileInfo Read(ITextFile textFile) { MachineFileReader reader = new MachineFileReader(); reader.ReadFile(textFile); MachineFileInfo info = new MachineFileInfo(reader.Name, reader.History, reader.NextLineId); return(info); }
static void Main(string[] args) { Console.WriteLine($"\n1) Defaults\n2) From file"); bool defaults = Convert.ToInt32(Console.ReadLine()) == 1; if (defaults) { Console.WriteLine($"\n1) Mealy Machine Example 1\n2) Mealy Machine Example 2\n3) Moore Machine Example 1\n4) Mealy Machine Minimization Example\n5) Moore Machine Minimization Example"); Machines selection = (Machines)Convert.ToInt32(Console.ReadLine()); string fileName = ""; bool shouldMinimize = false; bool isMachineMealy = true; switch (selection) { case Machines.MealyMachineExample: fileName = "../../../mealy.txt"; break; case Machines.MealyMachineExample2: fileName = "../../../mealy2.txt"; break; case Machines.MooreMachineExample: fileName = "../../../words.txt"; isMachineMealy = false; break; case Machines.MealyMachineMinimizationExample: fileName = "../../../mealyMinimize.txt"; shouldMinimize = true; break; case Machines.MooreMachineMinimizationExample: fileName = "../../../mooreMinimize.txt"; shouldMinimize = true; isMachineMealy = false; break; } Machine machine = MachineFileReader.ReadMachineFromFile(fileName); machine.Show(); if (shouldMinimize) { Console.WriteLine($"\nMinimizing...\n"); machine.Minimize(); machine.Show(); } RunMachine(isMachineMealy, machine); } else { Console.Write("\n1) Mealy Machine\n2) Moore Machine\n"); bool isMachineMealy = Convert.ToInt32(Console.ReadLine()) == 1; Console.Write("\nEnter filename: "); string fileName = Console.ReadLine(); Machine machine = MachineFileReader.ReadMachineFromFile(fileName); machine.Show(); Console.Write("\nDo you want to minimize the machine ? (y/n): "); bool shouldMinimize = Console.ReadKey().KeyChar == 'y'; if (shouldMinimize) { machine.Minimize(); machine.Show(); } RunMachine(isMachineMealy, machine); } }
public void WriteHistory(string name) { List <string> lines = new List <string> { NameCommand(name) }; // As the history tree could be very deep, keep a "stack" of history events in order to avoid recursive calls. List <HistoryEvent> historyEvents = new List <HistoryEvent>(); historyEvents.AddRange(History.RootEvent.Children); HistoryEvent previousEvent = null; while (historyEvents.Count > 0) { HistoryEvent currentEvent = historyEvents[0]; if (previousEvent != currentEvent.Parent && previousEvent != null) { lines.Add(CurrentCommand(currentEvent.Parent.Id)); } GetLines(currentEvent, lines); historyEvents.RemoveAt(0); previousEvent = currentEvent; // Place the current event's children at the top of the "stack". This effectively means we're doing a depth-first traversion of the history tree. historyEvents.InsertRange(0, currentEvent.Children); } lines.Add(CurrentCommand(History.CurrentEvent.Id)); // If we have any "arg" commands, stick them in a "args" command and put them at the start of the file. // Putting them in a single command should allow them to be better compressed than individually. Dictionary <int, string> args = new Dictionary <int, string>(); int i = 0; while (i < lines.Count) { string[] tokens = lines[i].Split(':'); if (tokens[0] == _idArg) { lines.RemoveAt(i); MachineFileReader.ReadArgCommand(tokens[1], args); } else { i++; } } // Write the file. if (args.Count > 0) { lines.Insert(0, ArgsCommand(args, true)); } foreach (string line in lines) { _textFile.WriteLine(line); } }