/// <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); }
/// <summary> /// Sets the <paramref name="inputExpression">input expression</paramref> for the input referenced by <paramref name="inputRef"/> /// </summary> /// <remarks> /// When the expression is null or whitespace the table input will not be used for the rule (will be removed when it was already defined before). /// When the expression is "valid" and the input expression has been defined before, it will override it /// </remarks> /// <param name="inputRef">Reference to decision table input</param> /// <param name="inputExpression">Input evaluation 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="inputRef"/> is null</exception> /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the <paramref name="inputRef"/> is not recognized as the valid table input</exception> private void SetInput(TableInput.Ref inputRef, string inputExpression) { if (IsBuilt) { throw Logger.Error <DmnBuilderException>("Table rule is already built"); } if (inputRef == null) { throw new ArgumentNullException(nameof(inputRef)); } if (!AllTableInputs.ContainsKey(inputRef)) { throw Logger.Error <DmnBuilderException>("Input reference is not valid for current table"); } if (string.IsNullOrWhiteSpace(inputExpression)) { if (InputsInternal.ContainsKey(inputRef)) { InputsInternal.Remove(inputRef); } } else { InputsInternal[inputRef] = inputExpression; } }
/// <summary> /// Adds the expression based table input /// </summary> /// <remarks>The inputs are "indexed" in the order as added to the table definition builder</remarks> /// <param name="expression">Expression to be used as table input</param> /// <param name="inputRef">Reference to added table input that can be used in rule builders</param> /// <param name="allowedValues">Allowed input values</param> /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception> /// <exception cref="ArgumentNullException"> when the <paramref name="expression"/> is not provided</exception> /// <exception cref="ArgumentException"> when the <paramref name="expression"/> is empty or whitespace</exception> public TableDecision WithInput(string expression, out TableInput.Ref inputRef, params string[] allowedValues) { if (IsBuilt) { throw Logger.Error <DmnBuilderException>($"Decision is already built"); } if (expression == null) { throw new ArgumentNullException(nameof(expression)); } if (string.IsNullOrWhiteSpace(expression)) { throw new ArgumentException("Missing expression", nameof(expression)); } var input = new TableInput(Variables, Decisions, InputsInternal.Count).WithExpression(expression); _ = allowedValues != null && allowedValues.Length > 0 ? input.WithAllowedValues(allowedValues) : input.WithoutAllowedValuesConstraint(); inputRef = input.Reference; InputsInternal.Add(input); InputsByRef.Add(inputRef, input); return(this); }
/// <summary> /// Clears input expressions for all inputs /// </summary> /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception> private void ClearInputs() { if (IsBuilt) { throw Logger.Error <DmnBuilderException>("Table rule is already built"); } InputsInternal.Clear(); }