当前位置: 首页>>代码示例>>C#>>正文


C# Parser.AddFunction方法代码示例

本文整理汇总了C#中Parser.AddFunction方法的典型用法代码示例。如果您正苦于以下问题:C# Parser.AddFunction方法的具体用法?C# Parser.AddFunction怎么用?C# Parser.AddFunction使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Parser的用法示例。


在下文中一共展示了Parser.AddFunction方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Iterative

        public static string Iterative(string sfx, string sfdx, double a, double b, out int count)
        {
            Parser fx = new Parser(sfx);
            fx.AddFunction<Algorithms>("log");
            fx.Compile();
            Parser fdx = new Parser(sfdx);
            fdx.AddFunction<Algorithms>("log");
            fdx.Compile();

            double x0 = (a + b)/2;
            if (x0 == 0)
            {
                x0 = (b + x0)/2;
            }
            fdx.Variables.Add("x", x0);

            double lambda = -1 / fdx.Calculate();

            count = 0;
            fx.Variables.Add("x", x0);
            double xn = x0 + lambda*fx.Calculate();

            while ((xn - x0) > eps)
            {
                x0 = xn;
                fx.Variables["x"] = xn;
                xn = xn + lambda * fx.Calculate();
                ++count;
            }

            return xn.ToString();
        }
开发者ID:osst,项目名称:University,代码行数:32,代码来源:Form1.cs

示例2: Parse

 public static double Parse(string input)
 {
     Parser OMG = new Parser(input);
      OMG.AddFunction<Agent>("F");
      OMG.Compile();
      OMG.Variables.Add("x",2);
      OMG.Variables.Add("y", 2);
      OMG.Variables.Add("z", 2);
      return OMG.Calculate();
 }
开发者ID:osst,项目名称:University,代码行数:10,代码来源:Tests.cs

示例3: Chord

        public static string Chord(string sfx, string sfdx, double a, double b, out int count)
        {
            Parser fx = new Parser(sfx);
            fx.AddFunction<Algorithms>("log");
            fx.Compile();
            Parser fdx = new Parser(sfdx);
            fdx.AddFunction<Algorithms>("log");
            fdx.Compile();

            fx.Variables["x"] = a;
            fdx.Variables["x"] = a;
            double a_fpt = fx.Calculate()*fdx.Calculate();

            fx.Variables["x"] = b;
            fdx.Variables["x"] = b;
            double b_fpt = fx.Calculate()*fdx.Calculate();

            double fixedPoint = a_fpt > 0 ? a : b;

            double xnm1 = (a + b) / 2;
            fx.Variables["x"] = xnm1;
            double fx_r = fx.Calculate();
            fx.Variables["x"] = fixedPoint;
            double fx0_r = fx.Calculate();
            double xn = xnm1 - (xnm1 - fixedPoint)/(fx_r - fx0_r)*fx_r;

            count = 0;

            while ((xn - xnm1) > eps)
            {
                xnm1 = xn;

                fx.Variables["x"] = xn;
                fx_r = fx.Calculate();

                xn = xn - (xn - fixedPoint) / (fx_r - fx0_r) * fx_r;
                ++count;
            }

            return xn.ToString();
        }
开发者ID:osst,项目名称:University,代码行数:41,代码来源:Form1.cs

示例4: verify_various_parsing_errors

        public void verify_various_parsing_errors()
        {
            var parser = new Parser();
            parser.RegisterMethods();
            parser.AddFunction<int, int, int>("Max", (x, y) => Math.Max(x, y));

            try
            {
                parser.Parse<object>("1++ +1==2").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 2:
            ... ++ +1==2 ...
            ^--- Unexpected token: '++'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("true # false").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... # false ...
            ^--- Invalid token.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("'abc' - 'abc'").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 7:
            ... - 'abc' ...
            ^--- Operator '-' cannot be applied to operands of type 'System.String' and 'System.String'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("0 + null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 3:
            ... + null ...
            ^--- Operator '+' cannot be applied to operands of type 'System.Int32' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("0 / null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 3:
            ... / null ...
            ^--- Operator '/' cannot be applied to operands of type 'System.Int32' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("true && null").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 6:
            ... && null ...
            ^--- Operator '&&' cannot be applied to operands of type 'System.Boolean' and 'null'.",
                    e.Message);
            }

            try
            {
                parser.Parse<object>("true || null").Invoke(null);
                Assert.Fail();
//.........这里部分代码省略.........
开发者ID:revocengiz,项目名称:ExpressiveAnnotations,代码行数:101,代码来源:ParserTest.cs

示例5: verify_short_circuit_evaluation

        public void verify_short_circuit_evaluation()
        {
            var parser = new Parser();
            parser.AddFunction<object, bool>("CastToBool", obj => (bool) obj);

            try
            {
                parser.Parse<object>("CastToBool(null)").Invoke(null);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is NullReferenceException);
            }

            // below, the exception should not be thrown as above
            // reason? - first argument is suffient to determine the value of the expression so the second one is not going to be evaluated
            Assert.IsFalse(parser.Parse<object>("false && CastToBool(null)").Invoke(null));
            Assert.IsTrue(parser.Parse<object>("true || CastToBool(null)").Invoke(null));
        }
开发者ID:revocengiz,项目名称:ExpressiveAnnotations,代码行数:20,代码来源:ParserTest.cs

示例6: verify_methods_overriding

        public void verify_methods_overriding()
        {
            var parser = new Parser();

            // register utility methods
            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction<int, string>("Whoami", i => string.Format("utility method {0}", i));

            var model = new ModelWithMethods();

            // redefined model methods take precedence
            Assert.IsTrue(parser.Parse<ModelWithMethods>("Whoami() == 'model method'").Invoke(model));
            Assert.IsTrue(parser.Parse<ModelWithMethods>("Whoami(1) == 'model method 1'").Invoke(model));
        }
开发者ID:revocengiz,项目名称:ExpressiveAnnotations,代码行数:14,代码来源:ParserTest.cs

示例7: verify_methods_overloading

        public void verify_methods_overloading()
        {
            // methods overloading is based on the number of arguments
            var parser = new Parser();
            parser.AddFunction("Whoami", () => "utility method");
            parser.AddFunction<int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction<int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse<object>("Whoami() == 'utility method'").Invoke(null));
            Assert.IsTrue(parser.Parse<object>("Whoami(1) == 'utility method 1'").Invoke(null));
            Assert.IsTrue(parser.Parse<object>("Whoami(2, 'final') == 'utility method 2 - final'").Invoke(null));
        }
开发者ID:revocengiz,项目名称:ExpressiveAnnotations,代码行数:12,代码来源:ParserTest.cs

示例8: verify_methods_ambiguity

        public void verify_methods_ambiguity()
        {
            // since arguments types are not taken under consideration for methods overloading, following logic should fail
            var parser = new Parser();
            parser.AddFunction<int, string>("Whoami", i => string.Format("utility method {0}", i));
            parser.AddFunction<string, string>("Whoami", s => string.Format("utility method {0}", s));

            parser.AddFunction<string, string, string>("Glue", (s1, s2) => string.Concat(s1, s2));
            parser.AddFunction<int, int, string>("Glue", (i1, i2) => string.Concat(i1, i2));

            try
            {
                Assert.IsTrue(parser.Parse<object>("Whoami(0) == 'utility method 0'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Whoami(0) == 'utility method 0' ...
            ^--- Function 'Whoami' accepting 1 argument is ambiguous.",
                    e.Message);
            }

            try
            {
                Assert.IsTrue(parser.Parse<object>("Glue('a', 'b') == 'ab'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Glue('a', 'b') == 'ab' ...
            ^--- Function 'Glue' accepting 2 arguments is ambiguous.",
                    e.Message);
            }

            // not only built-in, but also context extracted methods are subjected to the same rules
            parser = new Parser();
            var model = new ModelWithAmbiguousMethods();

            try
            {
                Assert.IsTrue(parser.Parse<ModelWithAmbiguousMethods>("Whoami(0) == 'model method 0'").Invoke(model));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.AreEqual(
                    @"Parse error on line 1, column 1:
            ... Whoami(0) == 'model method 0' ...
            ^--- Function 'Whoami' accepting 1 argument is ambiguous.",
                    e.Message);
            }
        }
开发者ID:revocengiz,项目名称:ExpressiveAnnotations,代码行数:58,代码来源:ParserTest.cs

示例9: verify_implicit_type_conversion

        public void verify_implicit_type_conversion()
        {
            var parser = new Parser();
            parser.AddFunction<object, string>("Whoami", o => string.Format("utility method {0}", o));
            parser.AddFunction<int, string, string>("Whoami", (i, s) => string.Format("utility method {0} - {1}", i, s));

            Assert.IsTrue(parser.Parse<object>("Whoami('0') == 'utility method 0'").Invoke(null)); // successful conversion from String to Object
            Assert.IsTrue(parser.Parse<object>("Whoami(1, '2') == 'utility method 1 - 2'").Invoke(null)); // types matched, no conversion needed

            try
            {
                Assert.IsTrue(parser.Parse<object>("Whoami('1', '2') == 'utility method 1 - 2'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 8:
            ... '1', '2') == 'utility method 1 - 2' ...
            ^--- Function 'Whoami' 1st argument implicit conversion from 'System.String' to expected 'System.Int32' failed.",
                    e.Message);
            }

            try
            {
                Assert.IsTrue(parser.Parse<object>("Whoami(1, 2) == 'utility method 1 - 2'").Invoke(null));
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(
                    @"Parse error on line 1, column 11:
            ... 2) == 'utility method 1 - 2' ...
            ^--- Function 'Whoami' 2nd argument implicit conversion from 'System.Int32' to expected 'System.String' failed.",
                    e.Message);
            }
        }
开发者ID:revocengiz,项目名称:ExpressiveAnnotations,代码行数:39,代码来源:ParserTest.cs

示例10: Newton

        public static string Newton(string sfx, string sfdx, string sfddx, double a, double b, out int count)
        {
            Parser fx = new Parser(sfx);
            fx.AddFunction<Algorithms>("log");
            fx.Compile();
            Parser fdx = new Parser(sfdx);
            fdx.AddFunction<Algorithms>("log");
            fdx.Compile();
            Parser fddx = new Parser(sfddx);
            fddx.AddFunction<Algorithms>("log");
            fdx.Compile();

            fx.Variables["x"] = a;
            fddx.Variables["x"] = a;
            double a_fpt = fx.Calculate() * fddx.Calculate();

            fx.Variables["x"] = b;
            fddx.Variables["x"] = b;
            fx.Compile();
            double b_fpt = fx.Calculate() * fddx.Calculate();

            double fixedPoint = a_fpt > 0 ? a : b;

            fx.Variables["x"] = fixedPoint;
            fdx.Variables["x"] = fixedPoint;
            double xn = fixedPoint - fx.Calculate()/fdx.Calculate();
            count = 0;
            while ((xn - fixedPoint) > eps)
            {
                fixedPoint = xn;

                fx.Variables["x"] = xn;
                fdx.Variables["x"] = xn;

                xn = fixedPoint - fx.Calculate() / fdx.Calculate();
                ++count;
            }

            return xn.ToString();
        }
开发者ID:osst,项目名称:University,代码行数:40,代码来源:Form1.cs


注:本文中的Parser.AddFunction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。