/// <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> /// Adds the table rule. /// </summary> /// <param name="name">Name of the table rule</param> /// <param name="description">Description of th rule</param> /// <param name="builder">Builder used to create the rule definition (see <see cref="TableRule"/> for details)</param> /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the definition has already been built</exception> /// <exception cref="ArgumentNullException"> when the <paramref name="name"/> is not provided</exception> /// <exception cref="ArgumentException"> when the <paramref name="name"/> is empty or whitespace</exception> /// <exception cref="ArgumentNullException"> when the <paramref name="builder"/> is not provided</exception> /// <exception cref="DmnBuilderException">Throws <see cref="DmnBuilderException"/> when the rule with given <paramref name="name"/> already exists</exception> public TableDecision WithRule(string name, string description, Func <TableRule.TableRuleInputBuilder, TableRule.TableRuleOutputBuilder> builder) { if (IsBuilt) { throw Logger.Error <DmnBuilderException>($"Decision is already built"); } if (name == null) { throw new ArgumentNullException(nameof(name)); } if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Missing rule name", nameof(name)); } if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (Rules.Any(r => r.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))) { throw Logger.Error <DmnBuilderException>($"Rule {name} already exists"); } var rule = new TableRule(Variables, Decisions, InputsByRef, OutputsByRef, RulesInternal.Count, name, description); builder.Invoke(new TableRule.TableRuleInputBuilder(rule)); RulesInternal.Add(rule); return(this); }