/// <summary> /// Read and analize an input strings by NFM /// </summary> /// <param name="nfm">Used NFM</param> static void useNFM(NFM <string> nfm) { Console.Write(Notes.useMachineUseNote); nfm.InputString = Console.ReadLine().Select(x => "" + x).ToList(); nfm.processString(); // Compute allowing of input string Console.WriteLine("Input string is{0} acceping. \n", nfm.IsFinal?"":" not"); nfm.ReInitialize(); List <List <string> > substrs = nfm.GetAcceptingSubstrFromInput().ToList(); if (substrs.Count == 0) { Console.WriteLine("Input string isn't contain any accepting substring."); } else { foreach (var item in substrs) { Console.Write("The follow substring was accepting: "); Console.WriteLine(item.Aggregate(" ", (acum, x) => acum + x)); } } // Back all parameters of the machine to default to future use nfm.ReInitialize(); }
/// <summary> /// save the NFM to file /// </summary> /// <param name="machine">NFM to be saved</param> /// <param name="filename">Output file </param> static void SaveNFMMachine(NFM <string> machine, string filename) { try { using (StreamWriter outputFile = new StreamWriter(new FileStream(filename, FileMode.OpenOrCreate))) { outputFile.WriteLine("# Alphabet"); outputFile.WriteLine(machine.Alphabet.Literals.Aggregate("", (acum, x) => acum + x.ToString() + ' ')); outputFile.WriteLine("# Count of states"); outputFile.WriteLine(machine.StateTable.Count); #region save states outputFile.WriteLine("# States"); var states = machine.StateTable.States; states.Reverse(); // Need for conservation input order foreach (State <string> state in states) { //saving states string str = state.Caption; if (state.IsStartingState) { str += " 1"; } else { str += " 0"; } if (state.IsFinalState) { str += " 1"; } else { str += " 0"; } foreach (Pair <string, State <string> > pair in state.NextStates) { str += ' ' + pair.first + ' ' + pair.second.Id; } outputFile.WriteLine(str); } #endregion outputFile.WriteLine("# Name"); outputFile.WriteLine(machine.Name); } Console.WriteLine("Machine {0} succesfull saved", machine.Name); } catch (System.Exception e) { //something was wrong :( Console.WriteLine("Error! Machine haven't saved. Error message is {0} ", e.Message); } }
/// <summary> /// Load NFM from the input file /// </summary> /// <param name="filename">Name of file contains the NFM</param> /// <returns>Loaded NFM</returns> static NFM <string> loadNFM(string filename) { try { using (StreamReader inputFile = new StreamReader(new FileStream(filename, FileMode.Open))) { string inputStr; // This block need to skip the comments #region skip comment inputStr = inputFile.ReadLine(); while (inputStr[0] == '#') { inputStr = inputFile.ReadLine(); } #endregion Alphabet <string> alphabet = new Alphabet <string>(inputStr.Split(' ')); #region skip comment inputStr = inputFile.ReadLine(); while (inputStr[0] == '#') { inputStr = inputFile.ReadLine(); } #endregion int countOfStates = int.Parse(inputStr); #region skip comment inputStr = inputFile.ReadLine(); while (inputStr[0] == '#') { inputStr = inputFile.ReadLine(); } #endregion List <List <Pair <string, int> > > nextStatesForAll = new List <List <Pair <string, int> > >(countOfStates); List <State <string> > states = new List <State <string> >(countOfStates); //collecting state-input strings and make state without next states #region input states for (int i = 1; i <= countOfStates; i++) { List <string> stateString = inputStr.Split(' ').ToList(); inputStr = inputFile.ReadLine(); string caption = stateString[0]; bool isStarting = stateString[1] == "1"; bool isFinal = stateString[2] == "1"; int countOfNextStates = (stateString.Count - 3) / 2; List <Pair <string, int> > nextStates = new List <Pair <string, int> > (countOfNextStates); for (int j = 0; j < countOfNextStates; j++) { nextStates.Add(new Pair <string, int>(stateString[2 + j * 2 + 1], int.Parse(stateString[2 + j * 2 + 2]))); } nextStatesForAll.Add(nextStates); states.Add(new State <string>(alphabet, (uint)i, isStarting, isFinal, caption)); } // adding the next states to states for (int i = 0; i < countOfStates; i++) { states[i].NextStates = nextStatesForAll[i]. Select(x => new Pair <string, State <string> >(x.first, states[x.second - 1])).ToList(); } #endregion #region skip comment inputStr = inputFile.ReadLine(); while (inputStr[0] == '#') { inputStr = inputFile.ReadLine(); } #endregion string name = inputStr; // make the machine StateTable <string> stateTable = new StateTable <string>(states); NFM <string> nfm = new NFM <string>(alphabet, stateTable); nfm.Name = name; Console.WriteLine("Machine {0} succesfull have loaded", nfm.Name); return(nfm); } } catch (System.Exception e) { //something was wronk :( Console.WriteLine("Error. Machine haven't loaded. Exception message:{0} ", e.Message); return(null); } }
public void Test(uint countLevel = 100, uint countCycle = 100) { #region makingMachine // Alphabet<string> alphabet = new Alphabet<string>("* % a".Split(' ').ToList()); // List<State<string>> states = new List<State<string>>(7); // states.Add(new State<string>(alphabet, 1, true, false, "A")); // states.Add(new State<string>(alphabet, 2, false, false, "Q1")); // states.Add(new State<string>(alphabet, 3, false, false, "Q2")); // states.Add(new State<string>(alphabet, 4, false, false, "Q3")); // states.Add(new State<string>(alphabet, 5, false, false, "Q4")); // states.Add(new State<string>(alphabet, 6, false, false, "Q5")); // states.Add(new State<string>(alphabet, 7, false, true, "Q6")); // states[0].NextStates = new List<Pair<string, State<string>>>() { // new Pair<string, State<string>>("%", states[1])}; // states[1].NextStates = new List<Pair<string, State<string>>>() { // new Pair<string, State<string>>("*", states[2])}; // states[2].NextStates = new List<Pair<string, State<string>>>() { // new Pair<string, State<string>>("a", states[3])}; // states[3].NextStates = new List<Pair<string, State<string>>>() { // new Pair<string, State<string>>("*", states[4])}; // states[4].NextStates = new List<Pair<string, State<string>>>(){ // new Pair<string, State<string>>("*", states[1]), // new Pair<string, State<string>>("*", states[5])}; // states[5].NextStates = new List<Pair<string, State<string>>>(){ // new Pair<string, State<string>>("*", states[4]), // new Pair<string, State<string>>("%", states[6])}; // StateTable<string> table = new StateTable<string>(states); // NFM<string> machine = new NFM<string>(alphabet, table) ; #endregion Regex verifer = new Regex("^%(\\*a\\*\\*(\\*\\*)*)+%$"); NFM <string> machine = loadNFM("MyTask.txt"); using (StreamWriter output = new StreamWriter(new FileStream(filename, FileMode.OpenOrCreate))) { // string str = "*a**%"; // machine.InputString = str.Select(x=>"" + x).ToList(); // machine.processString(); // bool answVer = verifer.IsMatch(str) ; // Console.WriteLine(machine.IsFinal); // Console.WriteLine(machine.IsBroken); // foreach (var item in machine.StateTable.States) // { // Console.Write("\n{0} :",item.Id); // foreach (var item1 in item.NextStates) // { // Console.Write("{0} : {1} ; ",item1.first, item1.second.Id); // } // } // bool answMach = machine.IsFinal && !machine.IsBroken; // string answStr = ""; // machine.ReInitialize(); // List<List<string>> substrs = machine.GetAcceptingSubstrFromInput().ToList(); // if (answVer == answMach) // { // answStr = "PASS"; // } // else // { // answStr = "FAILED"; // } // string answSubstrs = ""; // if (VerifySubstr(verifer, substrs)) // { // answSubstrs = "PASS"; // } // else // { // answSubstrs = "FAILED"; // } // output.WriteLine(" main test {0} ; substr test {4} sequence = {1} ; Verifer answer = {2} ; Machine answer = {3}; substrs:\n ", // answStr, str, answVer, answMach, answSubstrs); // foreach (var item in substrs) // { // string substr = item.Aggregate("",(acum, x)=> acum + x); // // Console.WriteLine(substr); // output.WriteLine(substr); // }} for (int level = 5; level <= countLevel; level++) { for (int i = 0; i < countCycle; i++) { string str = GetRandomString(level, true).Aggregate("", (acum, x) => (acum + x)); machine.InputString = str.Select(x => "" + x).ToList(); machine.processString(); bool answVer = verifer.IsMatch(str); bool answMach = machine.IsFinal && !machine.IsBroken; string answStr = ""; machine.ReInitialize(); List <List <string> > substrs = machine.GetAcceptingSubstrFromInput().ToList(); if (answVer == answMach) { answStr = "PASS"; } else { answStr = "FAILED"; } string answSubstrs = ""; if (VerifySubstr(verifer, substrs)) { answSubstrs = "PASS"; } else { answSubstrs = "FAILED"; } output.WriteLine(" main test {0} ; substr test {4} sequence = {1} ; Verifer answer = {2} ; Machine answer = {3}; substrs:\n ", answStr, str, answVer, answMach, answSubstrs); foreach (var item in substrs) { string substr = item.Aggregate("", (acum, x) => acum + x); // Console.WriteLine(substr); output.WriteLine(substr); } } } } }
static void Main(string[] args) { JusticePortalContext jpc = new JusticePortalContext(); DB.DBFuncs db = new DB.DBFuncs(jpc); while (true) { Console.WriteLine("Choose:"); Console.WriteLine("1. SEBRA MIN"); Console.WriteLine("2. ZPKONPI MIN"); Console.WriteLine("3. CAREERS MIN"); Console.WriteLine("4. NEWS MIN"); Console.WriteLine("5. NFM MIN"); Console.WriteLine("6. OPDU MIN"); Console.WriteLine("7. BUDGET MIN"); Console.WriteLine("8. OP MIN"); Console.WriteLine("9. CONSULT MIN"); Console.WriteLine("10. CRAWL PAGE"); Console.WriteLine("11. CHILD PROTECTION COLLECTION"); Console.WriteLine("12. ESPCH COLLECTION"); Console.WriteLine("13. CRAWL NLAB PAGE"); Console.WriteLine("14. JUR REG"); Console.WriteLine("15. NLAB NEWS"); Console.WriteLine("16. GDIN NEWS"); Console.WriteLine("17. GDIN KONPI"); Console.WriteLine("18. GDIN Careers"); Console.WriteLine("19. PKGdinCrawler"); Console.WriteLine("20. Anticorr"); Console.WriteLine("21.PKMinCrawler"); Console.WriteLine("22.PKGDOCrawler"); Console.WriteLine("23.GDOCraw"); Console.WriteLine("24.ZPKONPI GDO"); Console.WriteLine("25.AV TEXT"); Console.WriteLine("26.AV Careers"); Console.WriteLine("27.AV PK"); Console.WriteLine("28.AV News"); Console.WriteLine("29.AV CHL12"); Console.WriteLine("30. AV ZPKONPI"); Console.WriteLine("300. AV FULL"); Console.WriteLine("301. MinProjects"); Console.WriteLine("302. GDIN Page"); Console.WriteLine("303. MinPKArchiveCrawler"); Console.WriteLine("304. CitizenshipProtoPage"); Console.WriteLine("305. MinInterviews"); var choice = Console.ReadLine(); switch (choice) { case "1": var sebra = new Sebra(db); sebra.Download(); break; case "2": var zpkonpi = new Zpkonpimin(db); zpkonpi.Download(); break; case "3": var careers = new Careers(db); careers.Download(); break; case "4": var newsMin = new MinNews(db); newsMin.Download(); break; case "5": var nfm = new NFM(db); nfm.Download(); break; case "6": var opdu = new OPDU(db); opdu.Download(); break; case "7": var minFin = new MinFin(db); minFin.Download(); break; case "8": var minOP = new MinOP(db); minOP.Download(); break; case "9": var minConsult = new MinConsult(db); minConsult.Download(); break; case "10": var crawlPage = new CrawlPage(db); crawlPage.Download(); break; case "11": var ChildProtectionCollection = new ChildProtectionCollection(db); ChildProtectionCollection.Download(); break; case "12": var ESPCHCollection = new ESPCHCollection(db); ESPCHCollection.Download(); break; case "13": var CrawlNLABPage = new CrawlNLABPage(db); CrawlNLABPage.Download(); break; case "14": var JurReg = new JurReg(db); JurReg.Download(); break; case "15": var NLABNews = new NLABNews(db); NLABNews.Download(); break; case "16": var GdinNews = new GdinNews(db); GdinNews.Download(); break; case "17": var ZpkonpiGdin = new ZpkonpiGdin(db); ZpkonpiGdin.Download(); break; case "18": var CareersGdin = new CareersGdin(db); CareersGdin.Download(); break; case "19": var PKGdinCrawler = new PKGdinCrawler(db); PKGdinCrawler.Download(); break; case "20": var Anticorr = new Anticorr(db); Anticorr.Download(); break; case "21": var PKMinCrawler = new PKMinCrawler(db); PKMinCrawler.Download(); break; case "22": var GDOPKCrawler = new GDOPKCrawler(db); GDOPKCrawler.Download(); break; case "23": var GDOCraw = new GDOCraw(db); GDOCraw.Download(); break; case "24": var ZpkonpiGDO = new ZpkonpiGDO(db); ZpkonpiGDO.Download(); break; case "25": var AVText = new AVText(db); AVText.Download(); break; case "26": var AVCareers = new AVCareers(db); AVCareers.Download(); break; case "27": var AVPK = new AVPK(db); AVPK.Download(); break; case "28": var AVNews = new AVNews(db); AVNews.Download(); break; case "29": var AVCHL12 = new AVCHL12(db); AVCHL12.Download(); break; case "30": var AVZPKONPI = new AVZPKONPI(db); AVZPKONPI.Download(); break; case "300": var AVText1 = new AVText(db); AVText1.Download(); var AVCareers1 = new AVCareers(db); AVCareers1.Download(); var AVPK1 = new AVPK(db); AVPK1.Download(); var AVNews1 = new AVNews(db); AVNews1.Download(); break; case "301": var MinProjects = new MinProjects(db); MinProjects.Download(); break; case "302": var GdinPage = new GdinPage(db); GdinPage.Download(); break; case "303": var MinPKArchiveCrawler = new MinPKArchiveCrawler(db); MinPKArchiveCrawler.Download(); break; case "304": var CitizenshipProtoPage = new CitizenshipProtoPage(db); CitizenshipProtoPage.Download(); break; case "305": var MinInterview = new MinInterview(db); MinInterview.Download(); break; } Console.WriteLine("Press key"); Console.ReadKey(); } }
static void Main(string[] args) { List <NFM <string> > machines = new List <NFM <string> >(); while (true) { Console.Write(Notes.startNote); switch (Console.ReadLine()) { case "1": { #region use mode Console.Write(Notes.useMachineSelectModeNote); switch (Console.ReadLine()) { case "1": { #region load from file Console.Write("Please, enter file name: "); string name = Console.ReadLine(); NFM <string> machine = loadNFM(name); if (machine != null) { machines.Add(machine); } #endregion } break; case "2": { #region use already exists machine while (true) { Console.Write(Notes.useInSessionMachineSelect); for (int i = 0; i < machines.Count; i++) { Console.WriteLine("machines number {0} : {1}; ", i + 1, machines[i].Name); } Console.Write("Enter num selected machine. Q to back to the main menu: "); string answ = Console.ReadLine(); if (answ.ToLower() == "q") { break; } try { int num = int.Parse(answ) - 1; useNFM(machines[num]); } catch (System.Exception) { Console.WriteLine("Input Error."); } } #endregion } break; default: continue; } #endregion } break; case "2": { #region construct mode machines.Add(ConstrucktNFM()); // Console.Write(Notes.selectMachineNote); // switch (Console.ReadLine()) // { // case "1": // {//construct NFM mode // machines.Add(ConstrucktNFM()); // }break; // default: continue; // } #endregion } break; case "3": { #region quiet request string saveDir = "./lastSessionMachines/"; Directory.CreateDirectory(saveDir); foreach (var x in machines) { SaveNFMMachine(x, saveDir + x.Name + ".txt"); } return; #endregion } default: continue; } } }
/// <summary> /// Construct new NFM by parameters, that will be read from Console input /// </summary> /// <returns>NFM</returns> static NFM <string> ConstrucktNFM() { Console.Write(Notes.contructingAlphabetStartNote); #region collecting Alphabet Console.Write(Notes.constructingAlphabetEnteringNote); string literals = Console.ReadLine(); Alphabet <string> alphabet = new Alphabet <string>(literals.Split(' ')); Console.Write(Notes.constructingAlphabetEndNote); #endregion #region collecting states Console.Write(Notes.constructingTableStateBeginNote); int countOfStates = int.Parse(Console.ReadLine()); List <State <string> > states = new List <State <string> >(countOfStates); Console.Write(Notes.constructingTableStateEnteringStateNote); List <List <Pair <string, int> > > nextStatesForAll = new List <List <Pair <string, int> > >(countOfStates); for (int i = 1; i <= countOfStates; i++) { Console.Write("Enter state by id {0}: ", i); List <string> stateString = Console.ReadLine().Split(' ').ToList(); string caption = stateString[0]; bool isStarting = stateString[1] == "1"; bool isFinal = stateString[2] == "1"; int countOfNextStates = (stateString.Count - 3) / 2; List <Pair <string, int> > nextStates = new List <Pair <string, int> > (countOfNextStates); for (int j = 0; j < countOfNextStates; j++) { nextStates.Add(new Pair <string, int>(stateString[2 + j * 2 + 1], int.Parse(stateString[2 + j * 2 + 2]))); } nextStatesForAll.Add(nextStates); states.Add(new State <string>(alphabet, (uint)i, isStarting, isFinal, caption)); } for (int i = 0; i < countOfStates; i++) { states[i].NextStates = nextStatesForAll[i]. Select(x => new Pair <string, State <string> >(x.first, states[x.second - 1])).ToList(); } #endregion //make machine StateTable <string> stateTable = new StateTable <string>(states); NFM <string> nfm = new NFM <string>(alphabet, stateTable); Console.Write(Notes.machineIsReadyNote); nfm.Name = Console.ReadLine(); Console.Write(Notes.constructingTableStateEndNote); return(nfm); }