Beispiel #1
0
        /// <summary>
        /// Builds the decision table definition
        /// </summary>
        /// <returns>Table decision definition built</returns>
        /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception>
        /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when there is no input, no output or no rule defined in builder</exception>
        protected internal override IDmnDecision Build()
        {
            if (IsBuilt)
            {
                throw Logger.Error <DmnBuilderException>("Decision is already built");
            }
            if (InputsInternal.Count < 1)
            {
                throw Logger.Error <DmnBuilderException>($"At least one input must be defined for decision table {Name}");
            }
            if (OutputsInternal.Count < 1)
            {
                throw Logger.Error <DmnBuilderException>($"At least one output must be defined for decision table {Name}");
            }
            if (RulesInternal.Count < 1)
            {
                throw Logger.Error <DmnBuilderException>($"At least one rule must be defined for decision table {Name}");
            }

            //create
            var tableDecision = new DmnDecisionTable(
                Name,
                HitPolicy,
                Aggregation,
                InputsInternal.Select(i => i.GetResultOrBuild()).ToArray(),
                OutputsInternal.Select(o => o.GetResultOrBuild()).ToArray(),
                RulesInternal.Select(r => r.GetResultOrBuild()).ToArray(),
                RequiredInputs.Select(i => Variables[i].GetResultOrBuild()).ToArray(),
                RequiredDecisions.Select(d => Decisions[d].GetResultOrBuild()).ToArray());

            ResultInternal = tableDecision;
            return(tableDecision);
        }
Beispiel #2
0
        /// <summary>
        /// Sets the <paramref name="outputExpression">output expression</paramref> for the output referenced by <paramref name="outputRef"/>
        /// </summary>
        /// <remarks>
        /// When the expression is null or whitespace the table output will not be used for the rule (will be removed when it was already defined before).
        /// When the expression is "valid" and the output expression has been defined before, it will override it
        /// </remarks>
        /// <param name="outputRef">Reference to decision table output</param>
        /// <param name="outputExpression">Output calculation expression</param>
        /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception>
        /// <exception cref="ArgumentNullException">Throws <see cref="ArgumentNullException"/> when the <paramref name="outputRef"/> is null</exception>
        /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the <paramref name="outputRef"/> is not recognized as the valid table output</exception>
        private void SetOutput(TableOutput.Ref outputRef, string outputExpression)
        {
            if (IsBuilt)
            {
                throw Logger.Error <DmnBuilderException>("Table rule is already built");
            }

            if (outputRef == null)
            {
                throw new ArgumentNullException(nameof(outputRef));
            }
            if (!AllTableOutputs.ContainsKey(outputRef))
            {
                throw Logger.Error <DmnBuilderException>("Output reference is not valid for current table");
            }

            if (string.IsNullOrWhiteSpace(outputExpression))
            {
                if (OutputsInternal.ContainsKey(outputRef))
                {
                    OutputsInternal.Remove(outputRef);
                }
            }
            else
            {
                OutputsInternal[outputRef] = outputExpression;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Clears the output calculation expressions for all outputs
        /// </summary>
        /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception>
        private void ClearOutputs()
        {
            if (IsBuilt)
            {
                throw Logger.Error <DmnBuilderException>("Table rule is already built");
            }

            OutputsInternal.Clear();
        }
Beispiel #4
0
        /// <summary>
        /// Adds the table output with reference to the variable to store the output value to
        /// </summary>
        /// <remarks>The outputs are "indexed" in the order as added to the table definition builder</remarks>
        /// <param name="variableRef">Reference to variable used to store the table output to</param>
        /// <param name="outputRef">Reference to added table output that can be used in rule builders</param>
        /// <param name="allowedValues">Allowed output values</param>
        /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception>
        /// <exception cref="ArgumentNullException"> when the <paramref name="variableRef"/> is not provided</exception>
        public TableDecision WithOutput(Variable.Ref variableRef, out TableOutput.Ref outputRef, params string[] allowedValues)
        {
            if (IsBuilt)
            {
                throw Logger.Error <DmnBuilderException>($"Decision is already built");
            }
            if (variableRef == null)
            {
                throw new ArgumentNullException(nameof(variableRef));
            }

            var output = new TableOutput(Variables, Decisions, OutputsInternal.Count).WithVariable(variableRef);

            _ = allowedValues != null && allowedValues.Length > 0
                ? output.WithAllowedValues(allowedValues)
                : output.WithoutAllowedValuesConstraint();

            outputRef = output.Reference;
            OutputsInternal.Add(output);
            OutputsByRef.Add(outputRef, output);
            return(this);
        }