Esempio n. 1
0
        static void Main2()
        {
            const int iterations = 100000;
            var       p          = new RegexParser();

            string regexp = @"b(ab)*bbc?";
            string text   = "bbbc";

            p.TryParse(regexp, out Expr expr);
            Console.WriteLine(expr.CollectTree(
                                  e => e.GetItems(),
                                  e => e.GetType().Name + ": " + e.ToString()
                                  ));

            //var fsm = ExprFsmBuilder.BuildFsm(expr, null);
            //fsm.SaveGraphToFile(@"c:\temp\outFSM.dgml");
            //var fsmWithoutEmpty = fsm.RemoveEmptyTransitions().Optimize();
            //var fsmWithoutEmptyAndOptimized = fsm.RemoveEmptyTransitions().Optimize();
            //fsmWithoutEmpty.SaveGraphToFile(@"c:\temp\outFSMwithoutEmptyAndOptimized.dgml");
            //fsmWithoutEmptyAndOptimized.SaveGraphToFile(@"c:\temp\outFSMwithoutEmpty.dgml");
            //var fsmDFA = fsmWithoutEmptyAndOptimized.MakeDFA();
            //fsmDFA.SaveGraphToFile(@"c:\temp\outDFA.dgml");
            //var m2 = fsmDFA.MinimizeDFA();
            //m2.SaveGraphToFile(@"c:\temp\outDFA2.dgml");

            var nfaRunner = Fsms.MakeFsmRunner(expr, FsmRunnerMode.NFA);
            var nfaAns    = nfaRunner.IsMatch(text);
            var nfaT      = Measure(iterations, () => nfaRunner.IsMatch(text));

            var dfaRunner = Fsms.MakeFsmRunner(expr, FsmRunnerMode.DFA);
            var dfaAns    = dfaRunner.IsMatch(text);
            var dfaT      = Measure(iterations, () => dfaRunner.IsMatch(text));

            var mdfaRunner = Fsms.MakeFsmRunner(expr, FsmRunnerMode.DFA);
            var mdfaAns    = mdfaRunner.IsMatch(text);
            var mdfaT      = Measure(iterations, () => mdfaRunner.IsMatch(text));

            var exprAns = false;
            var exprT   = Measure(iterations, () =>
            {
                int position = 0;
                exprAns      = expr.Match(text, ref position) && position == text.Length;
            });

            ExprTreeRunner treeRunner = new ExprTreeRunner(expr);
            var            treeAns    = treeRunner.IsMatch(text);
            var            treeT      = Measure(iterations, () => treeRunner.IsMatch(text));

            treeRunner.LastState.SaveStatesLogToFile(@"c:\temp\treematch.dgml");

            Console.WriteLine("\tResult {0} for \"{1}\"", treeAns, text);
            Console.WriteLine(exprT + "\t" + exprAns + "\t recursive tree");  //just parsed expr
            Console.WriteLine(treeT + "\t" + treeAns + "\t stackless tree");
            Console.WriteLine(nfaT + "\t" + nfaAns + "\t NFA");
            Console.WriteLine(dfaT + "\t" + dfaAns + "\t DFA");
            Console.WriteLine(dfaT + "\t" + mdfaAns + "\t MDFA");
            return;
        }
Esempio n. 2
0
 /// <summary>
 /// 删除状态
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public bool RemoveState(string name)
 {
     if (IsContainState(name))
     {
         Fsms.Remove(name);
         return(true);
     }
     return(false);
 }
Esempio n. 3
0
 /// <summary>
 /// 注册状态
 /// </summary>
 /// <param name="name"></param>
 /// <param name="fsm"></param>
 /// <returns></returns>
 public bool RegisterState(FSMBase fsm)
 {
     if (IsContainState(fsm.Name))
     {
         return(false);
     }
     Fsms.Add(fsm.Name, fsm);
     fsm.ownerMgr = this;
     return(true);
 }
Esempio n. 4
0
 public bool IsContainState(string name)
 {
     return(Fsms.ContainsKey(name));
 }