Пример #1
0
        /// <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();
        }
Пример #2
0
        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);
                        }
                    }
                }
            }
        }