All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.deliveredtechnologies.rulebook.lang.RuleBookAddRuleBuilder Maven / Gradle / Ivy

There is a newer version: 0.12
Show newest version
package com.deliveredtechnologies.rulebook.lang;

import com.deliveredtechnologies.rulebook.model.Rule;
import com.deliveredtechnologies.rulebook.model.RuleBook;

import java.util.function.Consumer;

/**
 * Builds the portion of the RuleBook that adds rules.
 */
public class RuleBookAddRuleBuilder implements TerminatingRuleBookBuilder {

  private RuleBook _ruleBook;

  RuleBookAddRuleBuilder(RuleBook ruleBook) {
    _ruleBook = ruleBook;
  }

  RuleBookAddRuleBuilder(RuleBook ruleBook, Consumer> consumer) {
    this(ruleBook);
    addRule(consumer);
  }

   RuleBookAddRuleBuilder(RuleBook ruleBook, Rule rule) {
    this(ruleBook);
    addRule(rule);
  }

  /**
   * Adds a rule to the RuleBook.
   * @param consumer  functional interface that supplies a RuleBookRuleBuilder for building a Rule
   * @return          a builder with the added Rule
   */
  public RuleBookAddRuleBuilder addRule(Consumer> consumer) {
    consumer.accept(new RuleBookRuleBuilder<>(_ruleBook));
    return this;
  }

  /**
   * Adds a rule to the RuleBook.
   * @param rule  Rule to be added into the RuleBook
   * @param    the fact type of the Rule
   * @return      RuleBookBuilder with the added Rule
   */
  public  RuleBookAddRuleBuilder addRule(Rule rule) {
    _ruleBook.addRule(rule);
    return this;
  }

  @Override
  public RuleBook build() {
    return _ruleBook;
  }
}