コード例 #1
0
        public static void GenericDataContext_Tests()
        {
            CalculationEngine.DefaultFunctions.Add(new FunctionDefinition("GetParent", 0, (calculationEngine, parms) =>
            {
                var personCalculationEngine = calculationEngine as TestPersonCalculationEngine;
                return(personCalculationEngine.DataContext.Name);
            }));

            var testPersonCalculationEngine = new TestPersonCalculationEngine();

            var p = TestPerson.CreateTestPerson();

            p.Parent = TestPerson.CreateTestPerson();
            testPersonCalculationEngine.DataContext = p;
            testPersonCalculationEngine.Test("GetParent()", p.Name);
        }
コード例 #2
0
        public void Units_Test()
        {
            CalculationEngine engine = new CalculationEngine(new CalculationEngineOptions
            {
                ServiceProvider = new ServiceProvider()
            });

            var p = new UnitModel
            {
                This = TestPerson.CreateTestPerson()
            };

            engine.DataContext = p;

            var expression = engine.Parse("This.Age*Y*5.0000");

            Console.WriteLine(expression.Evaluate());
        }
コード例 #3
0
ファイル: TestPerson.cs プロジェクト: maleet/ZCalcEngine
        public static TestPerson CreateTestPerson()
        {
            var p = new TestPerson();

            p.Name     = "Test Person";
            p.Code     = "Code";
            p.Nullable = null;
            p.Id       = Guid.Parse("96C5888C-6C75-43DD-A372-2A3398DAE038");
            p.Birth    = DateTime.Today.AddYears(-30);
            p.Number   = 55;
            p.Male     = true;
            for (int i = 0; i < 5; i++)
            {
                var c = new TestPerson();
                c.Name   = "Test Child " + i.ToString();
                c.Id     = Guid.Parse(i + "6C5888C-6C75-43DD-A372-2A3398DAE038");
                c.Birth  = DateTime.Today.AddYears(-i);
                c.Male   = i % 2 == 0;
                c.Number = i;
                p.Children.Add(c);
            }
            return(p);
        }
コード例 #4
0
        public static void DataContext_Tests()
        {
            CalculationEngine engine = new CalculationEngine(new CalculationEngineOptions
            {
                ServiceProvider = new ServiceProvider()
            });

            // adjust culture
            var cultureInfo = engine.CultureInfo;

            engine.CultureInfo = CultureInfo.InvariantCulture;

            var p = TestPerson.CreateTestPerson();

            p.Parent           = TestPerson.CreateTestPerson();
            engine.DataContext = p;

            engine.Variables.Add("sp", 1);

            var s = @"3600 / (1sp * XLOOKUP( CONCATENATE(Specs('jaty'), '-', IF( Number(Specs('jawd')) >= 0068 && Number(Specs('jawd')) < 0110, '68/110', IF(Number(Specs('jawd')) >= 0110 && Number(Specs('jawd')) < 0150, '110/150', Specs('jawd') ) ), '-', IF(Contains('JH', Specs('jaap')), 'JH', 'JL'), '-', IF(Contains('PK;PH', Specs('jspe')), 'OV', 'VOL')), Array( '04-68/110-JL-VOL', '04-68/110-JL-OV', '04-68/110-JH-VOL', '04-68/110-JH-OV', '14-68/110-JL-VOL', '14-68/110-JL-OV', '14-68/110-JH-VOL', '14-68/110-JH-OV', '29-68/110-JL-VOL', '29-68/110-JL-OV', '29-68/110-JH-VOL', '29-68/110-JH-OV', '49-68/110-JL-VOL', '49-68/110-JL-OV', '49-68/110-JH-VOL', '49-68/110-JH-OV', '28-68/110-JL-VOL', '28-68/110-JL-OV', '28-68/110-JH-VOL', '28-68/110-JH-OV', '38-68/110-JL-VOL', '38-68/110-JL-OV', '38-68/110-JH-VOL', '38-68/110-JH-OV', '05-68/110-JL-VOL', '05-68/110-JL-OV', '05-68/110-JH-VOL', '05-68/110-JH-OV', '06-68/110-JL-VOL', '06-68/110-JL-OV', '06-68/110-JH-VOL', '06-68/110-JH-OV', '06-110/150-JL-VOL', '06-110/150-JL-OV', '06-110/150-JH-VOL', '06-110/150-JH-OV', '32-68/110-JL-VOL', '32-68/110-JL-OV', '32-68/110-JH-VOL', '32-68/110-JH-OV', '36-68/110-JL-VOL', '36-68/110-JL-OV', '36-68/110-JH-VOL', '36-68/110-JH-OV', '36-110/150-JL-VOL', '36-110/150-JL-OV', '36-110/150-JH-VOL', '36-110/150-JH-OV', '86-68/110-JL-VOL', '86-68/110-JL-OV', '86-68/110-JH-VOL', '86-68/110-JH-OV', 'C7-68/110-JL-VOL', 'C7-68/110-JL-OV', 'C7-68/110-JH-VOL', 'C7-68/110-JH-OV', 'C7-110/150-JL-VOL', 'C7-110/150-JL-OV', 'C7-110/150-JH-VOL', 'C7-110/150-JH-OV', '26-110/150-JL-VOL', '26-110/150-JL-OV', '26-110/150-JH-VOL', '26-110/150-JH-OV', '46-110/150-JL-VOL', '46-110/150-JL-OV', '46-110/150-JH-VOL', '46-110/150-JH-OV', '55-68/110-JL-VOL', '55-68/110-JL-OV', '55-68/110-JH-VOL', '55-68/110-JH-OV' ), Array( 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 39, 42, 35, 38, 39, 42, 35, 38, 37, 37, 41, 38, 39, 42, 35, 38, 39, 42, 35, 38, 37, 37, 41, 38, 39, 42, 35, 38, 39, 42, 35, 38, 37, 37, 41, 38, 37, 37, 41, 38, 37, 37, 41, 38, 39, 42, 35, 22 ), 38))";

            p.Specs.Add("jaty", "04");
            p.Specs.Add("jawd", "0092");
            p.Specs.Add("jspe", "00");
            p.Specs.Add("jaap", "JH");

            engine.Test(s, 76.5957446808511M);
            Assert.AreEqual("(3600 / 1 * 1 * XLOOKUP(CONCATENATE('04', '-', IF('68/110', IF('110/150', Specs('jawd'))/*{}*/)/*{'68/110'}*/, '-', IF(CONTAINS('JH', 'JH')/*{True}*/, 'JH', 'JL')/*{'JH'}*/, '-', IF(CONTAINS('PK;PH', '00')/*{False}*/, 'OV', 'VOL')/*{'VOL'}*/)/*{'04-68/110-JH-VOL'}*/, ['04-68/110-JL-VOL', '04-68/110-JL-OV', '04-68/110-JH-VOL', '04-68/110-JH-OV', '14-68/110-JL-VOL', '14-68/110-JL-OV', '14-68/110-JH-VOL', '14-68/110-JH-OV', '29-68/110-JL-VOL', '29-68/110-JL-OV', '29-68/110-JH-VOL', '29-68/110-JH-OV', '49-68/110-JL-VOL', '49-68/110-JL-OV', '49-68/110-JH-VOL', '49-68/110-JH-OV', '28-68/110-JL-VOL', '28-68/110-JL-OV', '28-68/110-JH-VOL', '28-68/110-JH-OV', '38-68/110-JL-VOL', '38-68/110-JL-OV', '38-68/110-JH-VOL', '38-68/110-JH-OV', '05-68/110-JL-VOL', '05-68/110-JL-OV', '05-68/110-JH-VOL', '05-68/110-JH-OV', '06-68/110-JL-VOL', '06-68/110-JL-OV', '06-68/110-JH-VOL', '06-68/110-JH-OV', '06-110/150-JL-VOL', '06-110/150-JL-OV', '06-110/150-JH-VOL', '06-110/150-JH-OV', '32-68/110-JL-VOL', '32-68/110-JL-OV', '32-68/110-JH-VOL', '32-68/110-JH-OV', '36-68/110-JL-VOL', '36-68/110-JL-OV', '36-68/110-JH-VOL', '36-68/110-JH-OV', '36-110/150-JL-VOL', '36-110/150-JL-OV', '36-110/150-JH-VOL', '36-110/150-JH-OV', '86-68/110-JL-VOL', '86-68/110-JL-OV', '86-68/110-JH-VOL', '86-68/110-JH-OV', 'C7-68/110-JL-VOL', 'C7-68/110-JL-OV', 'C7-68/110-JH-VOL', 'C7-68/110-JH-OV', 'C7-110/150-JL-VOL', 'C7-110/150-JL-OV', 'C7-110/150-JH-VOL', 'C7-110/150-JH-OV', '26-110/150-JL-VOL', '26-110/150-JL-OV', '26-110/150-JH-VOL', '26-110/150-JH-OV', '46-110/150-JL-VOL', '46-110/150-JL-OV', '46-110/150-JH-VOL', '46-110/150-JH-OV', '55-68/110-JL-VOL', '55-68/110-JL-OV', '55-68/110-JH-VOL', '55-68/110-JH-OV'], [38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 38, 38, 47, 47, 39, 42, 35, 38, 39, 42, 35, 38, 37, 37, 41, 38, 39, 42, 35, 38, 39, 42, 35, 38, 37, 37, 41, 38, 39, 42, 35, 38, 39, 42, 35, 38, 37, 37, 41, 38, 37, 37, 41, 38, 37, 37, 41, 38, 39, 42, 35, 22], 38)/*{47}*/)/*{76.5957446808511}*/", engine.ParsedExpression);

            engine.Test("Name", "Test Person");
            engine.Test("1h", 1D / 60);

            engine.Test("1mp*ChildrenDct('Test Child 2').Age", p.ChildrenDct["Test Child 2"].Age.Value);

            engine.Variables.Add("am", 1);
            engine.Test("HASH(Name)", "53A4E9EC08910DE9BB6EDAA99F8C867C");
            engine.Test("HASH('Name')", "49EE3087348E8D44E1FEDA1917443987");
            engine.Test("CONCATENATE(am, HASH('Name'))", "149EE3087348E8D44E1FEDA1917443987");
            engine.Functions.Remove("CODE");
            Assert.IsTrue(engine.Validate <bool>("Code='Code'"));
            engine.Test("Parent.Name", "Test Person");
            engine.Test("Name.Length * 2", p.Name.Length * 2);
            engine.Test("Children.Count", p.Children.Count);
            engine.Test("Children(2).Name", p.Children[2].Name);

            engine.Test("15*ChildrenDct(\"Test Child 2\").Age+14", (15 * p.ChildrenDct["Test Child 2"].Age + 14).Value);
            engine.Test("ChildrenDct('Test Child 2').Name", p.ChildrenDct["Test Child 2"].Name);
            engine.Test("ValueOr(ChildrenDct('Test Child 2').Nullable, 0) <> 0", false);
            engine.Test("ValueOr(ChildrenDct('Test Child 2').Nullable, 0) < 6", true);
            engine.Test("ValueOr(ChildrenDct('Test Child 2').Number, 0) <> 0", true);
            engine.ThrowOnInvalidBindingExpression = false;
            engine.Test("ValueOr(ChildrenDct('NotExist').Number, 3) == 3", true);
            engine.ThrowOnInvalidBindingExpression = true;
            Assert.Throws <CalcEngineBindingException>(() => { engine.Test("ChildrenDct('NotExist').Number", true); });

            var ex = Assert.Throws <CalcEngineBindingException>(() =>
            {
                var d = engine.Evaluate <double>("ChildrenAgeDct('Test Child 10') * 2");
            });

            Assert.That(ex.Message, Is.EqualTo("'ChildrenAgeDct' of 'Zirpl.CalcEngine.Tests.TestPerson' (TestPerson) don't have key(s) 'Test Child 10'"));

            Assert.AreEqual(0, engine.Evaluate <double>("ChildrenAgeDct('Test Child 10') * 2", false));
            Assert.AreEqual(2, engine.Evaluate <double>("ChildrenAgeDct('Test Child 10') + 2", false));

            engine.Test("ChildrenIdDct('16C5888C-6C75-43DD-A372-2A3398DAE038').Name", p.ChildrenDct["Test Child 1"].Name);
            engine.Test("ChildrenDct.Count", p.ChildrenDct.Count);

            // DataContext functions
            engine.RegisterFunction("GetParent", 0, (calculationEngine, parms) =>
            {
                var testPerson = calculationEngine.DataContext as TestPerson;
                return(testPerson.Name);
            });
            engine.Test("GetParent()", p.Name);

            engine.InValidation = true;
            Assert.That(new double[] { }, Is.EquivalentTo(engine.Evaluate("LessThan(Range(0, 3), ChildrenAgeDct('Test Child 2'))") as IEnumerable));
            Assert.That(new double[] { }, Is.EquivalentTo(engine.Evaluate("LessThan(Range(0, 3), ChildrenWeightDct('Test Child 2'))") as IEnumerable));
            Assert.That(new double[] { }, Is.EquivalentTo(engine.Evaluate("LessThan(Range(0, 3), ChildrenSalaryDct('Test Child 2'))") as IEnumerable));

            engine.InValidation = false;
            Assert.That(new double[] { 0, 1 }, Is.EquivalentTo(engine.Evaluate("LessThan(Range(0, 3), ChildrenAgeDct('Test Child 2'))") as IEnumerable));
            Assert.That(new double[] { 0, 1, 2, 3 }, Is.EquivalentTo(engine.Evaluate("LessThan(Range(0, 3), ChildrenWeightDct('Test Child 2'))") as IEnumerable));
            Assert.That(new double[] { 0, 1, 2, 3 }, Is.EquivalentTo(engine.Evaluate("LessThan(Range(0, 3), ChildrenSalaryDct('Test Child 2'))") as IEnumerable));
        }