コード例 #1
0
        [DataRow(8, 8, 0, 0)]       //no hit
        public void CollectCountStrHitPolicyTest(int?a, int?b, int?c, int hitsCount)
        {
            var dir  = AppDomain.CurrentDomain.BaseDirectory;
            var file = Path.Combine(dir, "dmn/dmn1.3/hitpolicy_Collect_CountStr.dmn");
            var ctx  = DmnExecutionContextFactory.CreateExecutionContext(DmnParser.Parse13(file));

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Collect"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (hitsCount <= 0)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);

            result.IsSingleResult.Should().Be(true);
            result.SingleResult.Should().NotBeNull();
            result.SingleResult.Should().HaveCount(1);

            result.Results[0].Variables.Should().HaveCount(1);
            var output = result.Results[0].Variables[0];

            output.Should().NotBeNull();
            output.Name.Should().Be("o");
            if (c != null)
            {
                output.Value.Should().Be(c.ToString()).And.BeOfType <string>();
                output.Type.Should().Be(typeof(string));
            }
            else
            {
                output.Value.Should().BeNull();
                output.Type.Should().Be(typeof(string));
            }
        }
コード例 #2
0
        [DataRow(8, 8, 0, 0)]       //no hit
        public void CollectCountStrHitPolicyTest(int?a, int?b, int?c, int hitsCount)
        {
            var ctx = CTX("hitpolicy_Collect_CountStr.dmn");

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Collect"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (hitsCount <= 0)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(1);
            result.HasResult.Should().Be(true);

            result.IsSingleResult.Should().Be(true);
            result.FirstResultVariables.Should().NotBeNull();
            result.FirstResultVariables.Should().HaveCount(1);

            result.Results[0].Variables.Should().HaveCount(1);
            result.Results[0].HitRules.Should().HaveCount(hitsCount);
            var output = result.Results[0].Variables[0];

            output.Should().NotBeNull();
            output.Name.Should().Be("o");
            if (c != null)
            {
                output.Value.Should().Be(c.ToString()).And.BeOfType <string>();
                output.Type.Should().Be(typeof(string));
            }
            else
            {
                output.Value.Should().BeNull();
                output.Type.Should().Be(typeof(string));
            }
        }
コード例 #3
0
        [DataRow(8, 8, null, 0, 0)]       //no hit
        public void PriorityHitPolicyTest(int?a, int?b, string c, int hitsCount, int outVariableCount)
        {
            var dir  = AppDomain.CurrentDomain.BaseDirectory;
            var file = Path.Combine(dir, "dmn/hitpolicy_Priority.dmn");
            var ctx  = DmnExecutionContextFactory.CreateExecutionContext(DmnParser.Parse(file));

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Priority"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (hitsCount <= 0)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);
            if (hitsCount == 1)
            {
                result.IsSingleResult.Should().Be(true);
                result.SingleResult.Should().NotBeNull();
                result.SingleResult.Should().HaveCount(outVariableCount);
            }

            if (outVariableCount <= 0)
            {
                return;
            }

            var output = result.SingleResult[0];

            output.Should().NotBeNull();
            output.Name.Should().Be("o");
            output.Value.Should().Be(c).And.BeOfType <string>();
            output.Type.Should().Be(typeof(string));
        }
コード例 #4
0
        [DataRow(8, 8, null, 0, 0)]       //no hit
        public void PriorityHitPolicyTest(int?a, int?b, string c, int hitsCount, int outVariableCount)
        {
            var ctx = CTX("hitpolicy_Priority.dmn");

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Priority"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (hitsCount <= 0)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);
            if (hitsCount == 1)
            {
                result.IsSingleResult.Should().Be(true);
                result.FirstResultVariables.Should().NotBeNull();
                result.FirstResultVariables.Should().HaveCount(outVariableCount);
                result.Results[0].HitRules.Should().HaveCount(1);
            }

            if (outVariableCount <= 0)
            {
                return;
            }

            var output = result.FirstResultVariables[0];

            output.Should().NotBeNull();
            output.Name.Should().Be("o");
            output.Value.Should().Be(c).And.BeOfType <string>();
            output.Type.Should().Be(typeof(string));
        }
コード例 #5
0
        public void AllowedValuesTest(string a, string b, string c, bool validIn, bool validOut)
        {
            var ctx = CTX("allowedValues.dmn");

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Test"); };

            if (!validIn && validOut)
            {
                act.Should().NotThrow();
                result.HasResult.Should().BeFalse();
                //act.Should().Throw<DmnExecutorException>().WithMessage("Decision table Test, *Input value * is not in allowed values list *");
                return;
            }

            if (!validOut)
            {
                act.Should().Throw <DmnExecutorException>().WithMessage("Decision table Test,* Output value * is not in allowed values list *");
                return;
            }

            act.Invoke();

            result.Should().NotBeNull();
            result.IsSingleResult.Should().Be(true);
            result.FirstResultVariables.Should().NotBeNull();
            result.FirstResultVariables.Should().HaveCount(1);
            result.Results[0].HitRules.Should().HaveCount(1);

            var output = result.FirstResultVariables[0];

            output.Should().NotBeNull();
            output.Name.Should().Be("c");
            output.Value.Should().Be(c).And.BeOfType <string>();
            output.Type.Should().Be(typeof(string));
        }
コード例 #6
0
        public void AllowedValuesTest(string a, string b, string c, bool validIn, bool validOut)
        {
            var dir  = AppDomain.CurrentDomain.BaseDirectory;
            var file = Path.Combine(dir, "dmn/dmn1.3/allowedValues.dmn");
            var ctx  = DmnExecutionContextFactory.CreateExecutionContext(DmnParser.Parse(file, DmnParser.DmnVersionEnum.V1_3));

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Test"); };

            if (!validIn && validOut)
            {
                act.Should().Throw <DmnExecutorException>().WithMessage("Decision table Test,* Input value * is not in allowed values list *");
                return;
            }

            if (!validOut)
            {
                act.Should().Throw <DmnExecutorException>().WithMessage("Decision table Test,* Output value * is not in allowed values list *");
                return;
            }

            act.Invoke();

            result.Should().NotBeNull();
            result.IsSingleResult.Should().Be(true);
            result.SingleResult.Should().NotBeNull();
            result.SingleResult.Should().HaveCount(1);

            var output = result.SingleResult[0];

            output.Should().NotBeNull();
            output.Name.Should().Be("c");
            output.Value.Should().Be(c).And.BeOfType <string>();
            output.Type.Should().Be(typeof(string));
        }
コード例 #7
0
        [DataRow(null, null, new[] { "" }, new[] { -1 }, new[] { "" }, false)]            //null -> 0
        public void RuleOrderHitPolicyMultiOutTest(int?a, int?b, string[] o1, int[] o2, string[] o3, bool hasHit)
        {
            var dir  = AppDomain.CurrentDomain.BaseDirectory;
            var file = Path.Combine(dir, "dmn/dmn1.3/hitpolicy_MultiOut_RuleOrder.dmn");
            var ctx  = DmnExecutionContextFactory.CreateExecutionContext(DmnParser.Parse13(file));

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("RuleOrder"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (!hasHit)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            var hitsCount = o1.Length;

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);

            if (hitsCount == 1)
            {
                result.IsSingleResult.Should().Be(true);
                result.SingleResult.Should().NotBeNull();
            }

            result.Results.Should().HaveCount(hitsCount);
            var idx = 0;

            foreach (var resultHit in result.Results)
            {
                var o1Result  = resultHit.Variables?.FirstOrDefault(v => v.Name == "o1");
                var o2Result  = resultHit.Variables?.FirstOrDefault(v => v.Name == "o2");
                var o3Result  = resultHit.Variables?.FirstOrDefault(v => v.Name == "o3");
                var o1Asserrt = o1[idx];
                var o2Asserrt = o2[idx];
                var o3Asserrt = o3[idx];

                if (o1Result == null)
                {
                    o1Asserrt.Should().BeNull();
                }
                else
                {
                    o1Result.Should().NotBeNull();
                    o1Result.Value.Should().NotBeNull();
                    o1Result.Value.Should().Be(o1Asserrt).And.BeOfType <string>();
                    o1Result.Type.Should().Be(typeof(string));
                }
                if (o2Result == null)
                {
                    o2Asserrt.Should().Be(-1); //-1 means null
                }
                else
                {
                    o2Result.Should().NotBeNull();
                    o2Result.Value.Should().NotBeNull();
                    o2Result.Value.Should().Be(o2Asserrt).And.BeOfType <int>();
                    o2Result.Type.Should().Be(typeof(int));
                }
                if (o3Result == null)
                {
                    o3Asserrt.Should().BeNull();
                }
                else
                {
                    o3Result.Should().NotBeNull();
                    o3Result.Value.Should().NotBeNull();
                    o3Result.Value.Should().Be(o3Asserrt).And.BeOfType <string>();
                    o3Result.Type.Should().Be(typeof(string));
                }
                idx++;
            }
        }
コード例 #8
0
        public void AnyHitPolicyMultiOutTest(int?a, int?b, string o1, int?o2, bool hasHit, bool isErr)
        {
            var dir  = AppDomain.CurrentDomain.BaseDirectory;
            var file = Path.Combine(dir, "dmn/dmn1.3/hitpolicy_MultiOut_Any.dmn");
            var ctx  = DmnExecutionContextFactory.CreateExecutionContext(DmnParser.Parse13(file));

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Any"); };

            if (isErr)
            {
                act.Should().Throw <DmnExecutorException>().WithMessage("ANY hit policy violation - the outputs don't match");
                return;
            }
            act.Invoke();


            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (!hasHit)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(1);
            result.HasResult.Should().Be(true);

            result.IsSingleResult.Should().Be(true);
            result.SingleResult.Should().NotBeNull();
            var outCount = 2;
            var o1Index  = 0;
            var o2Index  = 1;

            if (o1 == null)
            {
                outCount--;
                o1Index = -1;
                o2Index--;
            }
            if (o2 == null)
            {
                outCount--;
                o2Index = -1;
            }

            result.SingleResult.Should().HaveCount(outCount);

            if (o1Index >= 0)
            {
                var output1 = result.SingleResult[o1Index];
                output1.Should().NotBeNull();
                output1.Name.Should().Be("o1");
                output1.Value.Should().Be(o1).And.BeOfType <string>();
                output1.Type.Should().Be(typeof(string));
            }

            // ReSharper disable once InvertIf
            if (o2Index >= 0)
            {
                var output2 = result.SingleResult[o2Index];
                output2.Should().NotBeNull();
                output2.Name.Should().Be("o2");
                output2.Value.Should().Be(o2).And.BeOfType <int>();
                output2.Type.Should().Be(typeof(int));
            }
        }
コード例 #9
0
        [DataRow(8, 8, new int[] { }, 0, new[] { 0 })]       //no hit
        public void CollectListHitPolicyTest(int?a, int?b, int[] cIn, int hitsCount, int[] outVariableCount)
        {
            var c = new int?[cIn.Length];

            for (var i = 0; i < cIn.Length; i++)
            {
                if (cIn[i] >= 0)
                {
                    c[i] = cIn[i];
                }
                else
                {
                    c[i] = null;
                }
            }
            var dir  = AppDomain.CurrentDomain.BaseDirectory;
            var file = Path.Combine(dir, "dmn/dmn1.3/hitpolicy_Collect_List.dmn");
            var ctx  = DmnExecutionContextFactory.CreateExecutionContext(DmnParser.Parse13(file));

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Collect"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (hitsCount <= 0)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);
            if (hitsCount == 1)
            {
                result.IsSingleResult.Should().Be(true);
                result.SingleResult.Should().NotBeNull();
                result.SingleResult.Should().HaveCount(outVariableCount[0]);
            }

            for (var i = 0; i < hitsCount; i++)
            {
                if (outVariableCount[i] <= 0)
                {
                    result.Results[i].Variables.Should().HaveCount(0);
                    continue;
                }

                result.Results[i].Variables.Should().HaveCount(1);
                var output = result.Results[i].Variables[0];
                output.Should().NotBeNull();
                output.Name.Should().Be("o");
                output.Value.Should().Be(c[i]).And.BeOfType <long>();
                output.Type.Should().Be(typeof(long));
            }
        }
コード例 #10
0
        [DataRow(8, 8, new int[] { }, 0, new[] { 0 })]       //no hit
        public void CollectListHitPolicyTest(int?a, int?b, int[] cIn, int hitsCount, int[] outVariableCount)
        {
            var c = new int?[cIn.Length];

            for (var i = 0; i < cIn.Length; i++)
            {
                if (cIn[i] >= 0)
                {
                    c[i] = cIn[i];
                }
                else
                {
                    c[i] = null;
                }
            }
            var ctx = CTX("hitpolicy_Collect_List.dmn");

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Collect"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (hitsCount <= 0)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);
            if (hitsCount == 1)
            {
                result.IsSingleResult.Should().Be(true);
                result.FirstResultVariables.Should().NotBeNull();
                result.FirstResultVariables.Should().HaveCount(outVariableCount[0]);
            }

            for (var i = 0; i < hitsCount; i++)
            {
                if (outVariableCount[i] <= 0)
                {
                    result.Results[i].Variables.Should().HaveCount(0);
                    continue;
                }

                result.Results[i].Variables.Should().HaveCount(1);
                result.Results[i].HitRules.Should().HaveCount(1);
                var output = result.Results[i].Variables[0];
                output.Should().NotBeNull();
                output.Name.Should().Be("o");
                output.Value.Should().Be(c[i]).And.BeOfType <long>();
                output.Type.Should().Be(typeof(long));
            }
        }
コード例 #11
0
        [DataRow(null, null, new[] { "" }, new[] { -1 }, new[] { "" }, false)]            //null -> 0
        public void RuleOrderHitPolicyMultiOutTest(int?a, int?b, string[] o1, int[] o2, string[] o3, bool hasHit)
        {
            var ctx = CTX("hitpolicy_MultiOut_RuleOrder.dmn");

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("RuleOrder"); };

            act.Invoke();

            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (!hasHit)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            var hitsCount = o1.Length;

            result.Results.Should().HaveCount(hitsCount);
            result.HasResult.Should().Be(true);

            if (hitsCount == 1)
            {
                result.IsSingleResult.Should().Be(true);
                result.FirstResultVariables.Should().NotBeNull();
            }

            result.Results.Should().HaveCount(hitsCount);
            var idx = 0;

            foreach (var resultHit in result.Results)
            {
                resultHit.HitRules.Should().HaveCount(1);
                var o1Result = resultHit.Variables?.FirstOrDefault(v => v.Name == "o1");
                var o2Result = resultHit.Variables?.FirstOrDefault(v => v.Name == "o2");
                var o3Result = resultHit.Variables?.FirstOrDefault(v => v.Name == "o3");
                var o1Assert = o1[idx];
                var o2Assert = o2[idx];
                var o3Assert = o3[idx];

                if (o1Result == null)
                {
                    o1Assert.Should().BeNull();
                }
                else
                {
                    o1Result.Should().NotBeNull();
                    o1Result.Value.Should().NotBeNull();
                    o1Result.Value.Should().Be(o1Assert).And.BeOfType <string>();
                    o1Result.Type.Should().Be(typeof(string));
                }
                if (o2Result == null)
                {
                    o2Assert.Should().Be(-1); //-1 means null
                }
                else
                {
                    o2Result.Should().NotBeNull();
                    o2Result.Value.Should().NotBeNull();
                    o2Result.Value.Should().Be(o2Assert).And.BeOfType <int>();
                    o2Result.Type.Should().Be(typeof(int));
                }
                if (o3Result == null)
                {
                    o3Assert.Should().BeNull();
                }
                else
                {
                    o3Result.Should().NotBeNull();
                    o3Result.Value.Should().NotBeNull();
                    o3Result.Value.Should().Be(o3Assert).And.BeOfType <string>();
                    o3Result.Type.Should().Be(typeof(string));
                }
                idx++;
            }
        }
コード例 #12
0
        public void AnyHitPolicyMultiOutTest(int?a, int?b, string o1, int?o2, bool hasHit, bool isErr)
        {
            var ctx = CTX("hitpolicy_MultiOut_Any.dmn");

            ctx.WithInputParameter("a", a);
            ctx.WithInputParameter("b", b);

            DmnDecisionResult result = null;
            Action            act    = () => { result = ctx.ExecuteDecision("Any"); };

            if (isErr)
            {
                act.Should().Throw <DmnExecutorException>().WithMessage("ANY hit policy violation - the outputs don't match");
                return;
            }
            act.Invoke();


            result.Should().NotBeNull();
            result.Results.Should().NotBeNull();

            if (!hasHit)
            {
                result.Results.Should().HaveCount(0);
                result.HasResult.Should().Be(false);
                result.IsSingleResult.Should().Be(false);
                return;
            }

            result.Results.Should().HaveCount(1);
            result.HasResult.Should().Be(true);
            result.Results[0].HitRules.Should().HaveCount(1);

            result.IsSingleResult.Should().Be(true);
            result.FirstResultVariables.Should().NotBeNull();
            var outCount = 2;
            var o1Index  = 0;
            var o2Index  = 1;

            if (o1 == null)
            {
                outCount--;
                o1Index = -1;
                o2Index--;
            }
            if (o2 == null)
            {
                outCount--;
                o2Index = -1;
            }

            result.FirstResultVariables.Should().HaveCount(outCount);

            if (o1Index >= 0)
            {
                var output1 = result.FirstResultVariables[o1Index];
                output1.Should().NotBeNull();
                output1.Name.Should().Be("o1");
                output1.Value.Should().Be(o1).And.BeOfType <string>();
                output1.Type.Should().Be(typeof(string));
            }

            // ReSharper disable once InvertIf
            if (o2Index >= 0)
            {
                var output2 = result.FirstResultVariables[o2Index];
                output2.Should().NotBeNull();
                output2.Name.Should().Be("o2");
                output2.Value.Should().Be(o2).And.BeOfType <int>();
                output2.Type.Should().Be(typeof(int));
            }
        }