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

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

package com.deliveredtechnologies.rulebook.lang;


import com.deliveredtechnologies.rulebook.NameValueReferableMap;
import com.deliveredtechnologies.rulebook.NameValueReferable;
import com.deliveredtechnologies.rulebook.Result;
import com.deliveredtechnologies.rulebook.NameValueReferableTypeConvertibleMap;
import com.deliveredtechnologies.rulebook.Fact;
import com.deliveredtechnologies.rulebook.model.Rule;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Builds the portion of a Rule that is available in the language on 'given.'
 */
public class GivenRuleBuilder {
  Rule _rule;

  GivenRuleBuilder(Rule rule, NameValueReferableMap facts) {
    _rule = rule;
    given(facts);
  }

  @SafeVarargs
  GivenRuleBuilder(Rule rule, NameValueReferable... facts) {
    _rule = rule;
    given(facts);
  }

  /**
   * Adds a fact to the Rule.
   * @param name  the name of the fact
   * @param value the value of the fact
   * @return      a GivenRuleBuilder
   */
  GivenRuleBuilder given(String name, T value) {
    return given(new Fact(name, value));
  }

  /**
   * Adds one or more facts into the Rule using a {@link NameValueReferableMap}.
   * @param   facts the facts to be added to the Rule
   * @return  the current builder object
   */
  public final GivenRuleBuilder given(NameValueReferableMap facts) {
    _rule.setFacts(facts);
    return this;
  }

  /**
   * Adds one or more facts into the Rule.
   * @param   facts one or more facts
   * @return  the current builder object
   */
  @SafeVarargs
  public final GivenRuleBuilder given(NameValueReferable... facts) {
    _rule.addFacts(facts);
    return this;
  }

  /**
   * Adds the condition for the Rule.
   * @param condition the condition specified for the Rule
   * @return          a builder that allows for the Rule to be built following the condition
   */
  public WhenRuleBuilder when(Predicate> condition) {
    return new WhenRuleBuilder(_rule, condition);
  }

  /**
   * Adds a using constraint in the Rule that restricts the facts supplied to the subsequent 'then' action.
   * @param factNames the fact names to be supplied to the subsequent 'then' action
   * @return          a builder the allows for the Rule to be built following the 'using' statement
   */
  public UsingRuleBuilder using(String... factNames) {
    return new UsingRuleBuilder(_rule, factNames);
  }

  /**
   * Adds a then action into the Rule.
   * @param action  an action that the rule will execute based on the condition
   * @return        a builder that allows for the Rule to be built following the 'then' statement
   */
  public ThenRuleBuilder then(Consumer> action) {
    return new ThenRuleBuilder(_rule, action);
  }

  /**
   * Addds a then action into the Rule.
   * @param action  an action that the rule will execute based on the condition; accepts facts and the result
   * @return        a builder that allows for the Rule to be built following the 'then' statement
   */
  public ThenRuleBuilder then(BiConsumer, Result> action) {
    return new ThenRuleBuilder(_rule, action);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy