Evaluate() public method

Execute the evaluation of this parser instance with external symbols.
public Evaluate ( String input, Value>.Dictionary values ) : Value
input String The input to evaluate.
values Value>.Dictionary /// The values in an Hashtable containing string (name), Value (value) pairs. ///
return Value
Beispiel #1
0
        static void AdditionBenchmark()
        {
            var n = 10000;
            var parser = new Parser();
            Console.WriteLine("Running addition benchmarks ...");

            var sw = Stopwatch.StartNew();

            // Result ~85 ms with dynamic Operator Binding (using PerformOverFind())
            // Result ~77 ms without dynamic Operating Binding (using cast and + operator)

            for (var i = 0; i < n; i++)
            {
                parser.Evaluate("2+3");
            }

            sw.Stop();

            Console.WriteLine("Time for n = {0} runs : {1} ms", n, sw.ElapsedMilliseconds);
            Console.WriteLine("Finished !");
        }
Beispiel #2
0
        /// <summary>
        /// Execute the evaluation of this parser instance with external symbols.
        /// </summary>
        /// <param name="parser">The parser to extend.</param>
        /// <param name="input">The input to evaluate.</param>
        /// <param name="values">
        /// The values in an anonymous object - containing name - value pairs.
        /// </param>
        /// <returns>The value from the evaluation.</returns>
        public static Value Evaluate(this Parser parser, String input, Object values)
        {
            var symbols = values.ToDictionary();

            return(parser.Evaluate(input, symbols));
        }
Beispiel #3
0
 /// <summary>
 /// Execute the evaluation of this parser instance without any external symbols.
 /// </summary>
 /// <param name="parser">The parser to extend.</param>
 /// <param name="input">The input to evaluate.</param>
 /// <returns>The value from the evaluation.</returns>
 public static Value Evaluate(this Parser parser, String input)
 {
     return(parser.Evaluate(input, new Dictionary <String, Value>()));
 }
Beispiel #4
0
        static void RunBenchmark(BenchmarkKind which)
        {
            var yamp = new YAMP.Parser();
            var mpparser = new MathParser.Parser();
            var mptk = new MathParserTK_NET.MathParserTK();
            var mpnet = new MathParserNet.Parser();
            var mfmp = new MathFunctions.MathParser();
            var llmp = new MathParserDataStructures.MathObj();
            var calcEngine = new CalcEngine.CalcEngine();
            calcEngine.CacheExpressions = false;

            var lines = new string[0];

            switch (which)
            {
                case BenchmarkKind.Standard:
                    //         UB
                    //YAMP  : 154 ms
                    //LLMP  : 108 ms
                    //MP    : 4134 ms
                    //MPTK  : 375 ms
                    //MPNET : 3054 ms
                    //MFP   : 88 ms
                    //CALEN : 33 ms
                    //NCALC : 420 ms
                    lines = MakeTenK("2-3*5+7/2-8*2");
                    break;

                case BenchmarkKind.File:
                    //         UB
                    //YAMP  : 2084 ms
                    //LLMP  : 1072 ms
                    //MP    : 372847 ms
                    //MPTK  : ---
                    //MPNET : ---
                    //MFP   : ---
                    //CALEN : 271 ms
                    //NCALC : ---
                    if (!File.Exists(BMK_FILE))
                        GenerateBenchmarks();

                    lines = File.ReadAllLines(BMK_FILE);
                    break;

                case BenchmarkKind.Little:
                    //         UB
                    //YAMP  : 71 ms
                    //LLMP  : 59 ms
                    //MP    : 1840 ms
                    //MPTK  : 87 ms
                    //MPNET : 3232 ms
                    //MFP   : 37 ms
                    //CALEN : 23 ms
                    //NCALC : 247 ms
                    lines = MakeTenK("2+3");
                    break;

                case BenchmarkKind.Thomson:
                    //         UB
                    //YAMP  : 193 ms
                    //LLMP  : 138 ms
                    //MP    : 11508 ms
                    //MPTK  : 647 ms
                    //MPNET : 3827 ms
                    //MFP   : ---
                    //CALEN : 41 ms
                    //NCALC : ---
                    lines = MakeTenK("2-(3*5)^2+7/(2-8)*2");
                    break;
            }

            Console.WriteLine("Starting benchmarks ...");
            Console.WriteLine("----------");

            // The implementation here... YAMP
            Benchmark("YAMP", lines, query => yamp.Evaluate(query));

            //http://www.codeproject.com/Articles/53001/LL-Mathematical-Parser
            Benchmark("LLMathParser", lines, query => llmp.Evaluate(query, new char[0], new double[0]));

            //http://www.codeproject.com/Articles/11164/Math-Parser
            Benchmark("MathParser", lines, query => mpparser.Evaluate(query));

            //http://www.codeproject.com/Tips/381509/Math-Parser-NET-Csharp
            Benchmark("MathParserTK", lines, query => mptk.Parse(query, false));

            //http://www.codeproject.com/Articles/274093/Math-Parser-NET
            Benchmark("MathParserNet", lines, query => mpnet.Simplify(query));

            //http://www.codeproject.com/Articles/23061/MathParser-Math-Formula-Parser
            Benchmark("MathFormulaParser", lines, query => mfmp.Calculate(query));

            //http://www.codeproject.com/Articles/246374/A-Calculation-Engine-for-NET
            Benchmark("CalcEngine", lines, query => calcEngine.Evaluate(query));

            //http://ncalc.codeplex.com/
            //Benchmark("NCalc", lines, query => new NCalc.Expression(query, NCalc.EvaluateOptions.NoCache).Evaluate());
        }
Beispiel #5
0
        static void RunBenchmark(BenchmarkKind which)
        {
            var yamp       = new YAMP.Parser();
            var mpparser   = new MathParser.Parser();
            var mptk       = new MathParserTK_NET.MathParserTK();
            var mpnet      = new MathParserNet.Parser();
            var mfmp       = new MathFunctions.MathParser();
            var llmp       = new MathParserDataStructures.MathObj();
            var calcEngine = new CalcEngine.CalcEngine();

            calcEngine.CacheExpressions = false;

            var lines = new string[0];

            switch (which)
            {
            case BenchmarkKind.Standard:
                //         UB
                //YAMP  : 154 ms
                //LLMP  : 108 ms
                //MP    : 4134 ms
                //MPTK  : 375 ms
                //MPNET : 3054 ms
                //MFP   : 88 ms
                //CALEN : 33 ms
                //NCALC : 420 ms
                lines = MakeTenK("2-3*5+7/2-8*2");
                break;

            case BenchmarkKind.File:
                //         UB
                //YAMP  : 2084 ms
                //LLMP  : 1072 ms
                //MP    : 372847 ms
                //MPTK  : ---
                //MPNET : ---
                //MFP   : ---
                //CALEN : 271 ms
                //NCALC : ---
                if (!File.Exists(BMK_FILE))
                {
                    GenerateBenchmarks();
                }

                lines = File.ReadAllLines(BMK_FILE);
                break;

            case BenchmarkKind.Little:
                //         UB
                //YAMP  : 71 ms
                //LLMP  : 59 ms
                //MP    : 1840 ms
                //MPTK  : 87 ms
                //MPNET : 3232 ms
                //MFP   : 37 ms
                //CALEN : 23 ms
                //NCALC : 247 ms
                lines = MakeTenK("2+3");
                break;

            case BenchmarkKind.Thomson:
                //         UB
                //YAMP  : 193 ms
                //LLMP  : 138 ms
                //MP    : 11508 ms
                //MPTK  : 647 ms
                //MPNET : 3827 ms
                //MFP   : ---
                //CALEN : 41 ms
                //NCALC : ---
                lines = MakeTenK("2-(3*5)^2+7/(2-8)*2");
                break;
            }

            Console.WriteLine("Starting benchmarks ...");
            Console.WriteLine("----------");

            // The implementation here... YAMP
            Benchmark("YAMP", lines, query => yamp.Evaluate(query));

            //http://www.codeproject.com/Articles/53001/LL-Mathematical-Parser
            Benchmark("LLMathParser", lines, query => llmp.Evaluate(query, new char[0], new double[0]));

            //http://www.codeproject.com/Articles/11164/Math-Parser
            Benchmark("MathParser", lines, query => mpparser.Evaluate(query));

            //http://www.codeproject.com/Tips/381509/Math-Parser-NET-Csharp
            Benchmark("MathParserTK", lines, query => mptk.Parse(query, false));

            //http://www.codeproject.com/Articles/274093/Math-Parser-NET
            Benchmark("MathParserNet", lines, query => mpnet.Simplify(query));

            //http://www.codeproject.com/Articles/23061/MathParser-Math-Formula-Parser
            Benchmark("MathFormulaParser", lines, query => mfmp.Calculate(query));

            //http://www.codeproject.com/Articles/246374/A-Calculation-Engine-for-NET
            Benchmark("CalcEngine", lines, query => calcEngine.Evaluate(query));

            //http://ncalc.codeplex.com/
            //Benchmark("NCalc", lines, query => new NCalc.Expression(query, NCalc.EvaluateOptions.NoCache).Evaluate());
        }
Beispiel #6
0
        public static void Run()
        {
            var query = String.Empty;
            var buffer = new StringBuilder();
            var parser = new Parser();
            parser.LoadPlugin(typeof(PhysicsPlugin).Assembly);
            parser.LoadPlugin(typeof(SensorsPlugin).Assembly);
            parser.LoadPlugin(typeof(IoPlugin).Assembly);
            parser.LoadPlugin(typeof(SetsPlugin).Assembly);
            var exit = false;

            parser.InteractiveMode = true;
            parser.UseScripting = true;

            parser.AddCustomFunction("G", v => new ScalarValue(((ScalarValue)v).Re * Math.PI));
            parser.AddCustomConstant("R", 2.53);

            parser.NotificationReceived += OnNotified;
            parser.UserInputRequired += OnUserPrompt;
            parser.PauseDemanded += OnPauseDemanded;

            while (!exit)
            {
                buffer.Remove(0, buffer.Length);
                Console.Write("> ");

                while(true)
                {
                    query = Console.ReadLine();

                    if (query.Length == 0 || query[query.Length - 1] != '\\')
                    {
                        buffer.Append(query);
                        break;
                    }

                    buffer.Append(query.Substring(0, query.Length - 1)).Append("\n");
                }

                query = buffer.ToString();
                exit = query.Equals("exit");

                if (!exit)
                {
                    try
                    {
                        var value = parser.Evaluate(query);

                        if (value != null)
                        {
                            var result = value.ToString(parser.Context);
                            Console.WriteLine(result);
                        }
                    }
                    catch (YAMPParseException parseex)
                    {
                        Console.WriteLine(parseex.Message);
                        Console.WriteLine("---");
                        Console.Write(parseex.ToString());
                        Console.WriteLine("---");
                    }
                    catch (YAMPRuntimeException runex)
                    {
                        Console.WriteLine("An exception during runtime occured:");
                        Console.WriteLine(runex.Message);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
            }
        }