/// <summary>
        /// CTOR
        /// </summary>
        /// <param name="name">Decision table name</param>
        /// <param name="hitPolicy">Decision table hit policy</param>
        /// <param name="aggregation">Aggregation type of Collect hit policy</param>
        /// <param name="inputs">Decision table inputs</param>
        /// <param name="outputs">Decision table outputs</param>
        /// <param name="rules">Decision table rules</param>
        /// <param name="requiredInputs">Inputs the decision table depends on.</param>
        /// <param name="requiredDecisions">Decisions that needs to be evaluated before the decision table (provide the inputs to the table).</param>
        /// <exception cref="ArgumentNullException"><paramref name="inputs"/>, <paramref name="outputs"/> or <paramref name="rules"/> is null</exception>
        /// <exception cref="ArgumentException"><paramref name="inputs"/>, <paramref name="outputs"/> or <paramref name="rules"/> is empty</exception>
        public DmnDecisionTable(
            string name,
            HitPolicyEnum hitPolicy, CollectHitPolicyAggregationEnum aggregation,
            List <DmnDecisionTableInput> inputs,
            List <DmnDecisionTableOutput> outputs,
            List <DmnDecisionTableRule> rules,
            List <DmnVariableDefinition> requiredInputs, List <IDmnDecision> requiredDecisions)
            : base(name, requiredInputs, requiredDecisions)
        {
            HitPolicy   = hitPolicy;
            Aggregation = aggregation;
            Inputs      = inputs ?? throw Logger.Fatal <ArgumentNullException>($"{nameof(inputs)} is null");
            Outputs     = outputs ?? throw Logger.Fatal <ArgumentNullException>($"{nameof(outputs)} is null");
            Rules       = rules ?? throw Logger.Fatal <ArgumentNullException>($"{nameof(rules)} is null");

            if (inputs.Count < 1)
            {
                throw Logger.Fatal <ArgumentException>("No inputs for the table");
            }
            if (outputs.Count < 1)
            {
                throw Logger.Fatal <ArgumentException>("No outputs for the table");
            }
            if (rules.Count < 1)
            {
                throw Logger.Fatal <ArgumentException>("No rules for the table");
            }
        }
Example #2
0
        /// <summary>
        /// CTOR
        /// </summary>
        /// <param name="name">Decision table name</param>
        /// <param name="hitPolicy">Decision table hit policy</param>
        /// <param name="aggregation">Aggregation type of Collect hit policy</param>
        /// <param name="inputs">Decision table inputs</param>
        /// <param name="outputs">Decision table outputs</param>
        /// <param name="rules">Decision table rules</param>
        /// <param name="requiredInputs">Inputs the decision table depends on.</param>
        /// <param name="requiredDecisions">Decisions that needs to be evaluated before the decision table (provide the inputs to the table).</param>
        /// <exception cref="ArgumentNullException"><paramref name="inputs"/>, <paramref name="outputs"/> or <paramref name="rules"/> is null</exception>
        /// <exception cref="ArgumentException"><paramref name="inputs"/>, <paramref name="outputs"/> or <paramref name="rules"/> is empty</exception>
        public DmnDecisionTable(
            string name,
            HitPolicyEnum hitPolicy, CollectHitPolicyAggregationEnum aggregation,
            DmnDecisionTableInput[] inputs,
            DmnDecisionTableOutput[] outputs,
            DmnDecisionTableRule[] rules,
            IReadOnlyCollection <IDmnVariable> requiredInputs,
            IReadOnlyCollection <IDmnDecision> requiredDecisions)
            : base(name, requiredInputs, requiredDecisions)
        {
            HitPolicy   = hitPolicy;
            Aggregation = aggregation;
            Inputs      = inputs ?? throw Logger.Fatal <ArgumentNullException>($"{nameof(inputs)} is null");
            Outputs     = outputs ?? throw Logger.Fatal <ArgumentNullException>($"{nameof(outputs)} is null");
            Rules       = rules ?? throw Logger.Fatal <ArgumentNullException>($"{nameof(rules)} is null");

            if (inputs.Length < 1)
            {
                throw Logger.Fatal <ArgumentException>("No inputs for the table");
            }
            if (outputs.Length < 1)
            {
                throw Logger.Fatal <ArgumentException>("No outputs for the table");
            }
            if (rules.Length < 1)
            {
                throw Logger.Fatal <ArgumentException>("No rules for the table");
            }
        }
Example #3
0
 /// <summary>
 /// Sets the hit policy aggregation type of the decision table
 /// </summary>
 /// <param name="aggregation">Hit policy aggregation</param>
 /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception>
 public TableDecision WithAggregation(CollectHitPolicyAggregationEnum aggregation)
 {
     if (IsBuilt)
     {
         throw Logger.Error <DmnBuilderException>($"Decision is already built");
     }
     Aggregation = aggregation;
     return(this);
 }
        private void CollectAggregateHitPolicyTest(CollectHitPolicyAggregationEnum aggregate, int?a, int?b, string[] hits)
        {
            var def = new DmnDefinitionBuilder()
                      .WithInput <int>("a", out var aRef)
                      .WithInput <int>("b", out var bRef)
                      .WithVariable <long>("o", out var oRef)

                      .WithTableDecision("Collect", table =>
                                         table
                                         .WithInput(aRef, out var inputARef)
                                         .WithInput(bRef, out var inputBRef)
                                         .WithOutput(oRef, out var outputRef)
                                         .WithHitPolicy(HitPolicyEnum.Collect)
                                         .WithAggregation(aggregate)

                                         .WithRule("r1", r => r
                                                   .When(inputARef, "< 5").And(inputBRef, "1").Then(outputRef, "5"))
                                         .WithRule("r2", r => r
                                                   .When(inputARef, "<= 5").And(inputBRef, "2").Then(outputRef, "3"))
                                         .WithRule("r3", r => r
                                                   .When(inputARef, "5").And(inputBRef, "1").Then(outputRef, "4"))
                                         .WithRule("r4", r => r
                                                   .When(inputARef, "5").Then(outputRef, "8"))
                                         .WithRule("r5", r => r
                                                   .When(inputARef, "6").Then(outputRef, "2"))
                                         .WithRule("r6", r => r
                                                   .When(inputARef, ">= 6").And(inputBRef, "1").Then(outputRef, "1"))
                                         .WithRule("rEmpty", r => r
                                                   .When(inputARef, ">= 6").And(inputBRef, "2").Then(outputRef, ""))
                                         .WithRule("r7", r => r
                                                   .When(inputBRef, "3").Then(outputRef, "2"))
                                         .WithRule("r8", r => r
                                                   .When(inputARef, "9").And(inputBRef, "1").Then(outputRef, "3"))
                                         .Requires(aRef).Requires(bRef))
                      .Build();

            var ctx = DmnExecutionContextFactory
                      .CreateExecutionContext(def)
                      .WithInputParameter("a", a)
                      .WithInputParameter("b", b);

            var result = ctx.ExecuteDecision("Collect");

            var hitsCount = hits?.Length ?? 0;

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

            if (hitsCount == 0)
            {
                return;
            }

            result.Results.Should().HaveCount(1);//single result, but might have multiple hits aggregated
            result.IsSingleResult.Should().Be(true);
            result.Results[0].HitRules.Should().HaveCount(hitsCount);

            for (var i = 0; i < hitsCount; i++)
            {
                // ReSharper disable once PossibleNullReferenceException
                result.Results[0].HitRules[i].Name.Should().Be(hits[i]);
            }
        }