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

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

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

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

/**
 * Chains building of a Rule onto a RuleBookBuilder.
 * @param  the type of the Result
 */
public class RuleBookRuleBuilder {
  private RuleBook _ruleBook;
  private Class _ruleClass = GoldenRule.class;

  RuleBookRuleBuilder(RuleBook ruleBook) {
    _ruleBook = ruleBook;
  }

  /**
   * Specifies the Rule type.
   * @param ruleType  the type (class) of Rule to build
   * @return          RuleBookRuleBuilder using the Rule type specified
   */
  public RuleBookRuleBuilder withRuleType(Class ruleType) {
    _ruleClass = ruleType;
    return this;
  }

  public RuleBookAuditableRuleBuilder withName(String ruleName) {
    return new RuleBookAuditableRuleBuilder<>(_ruleBook, _ruleClass, ruleName);
  }

  /**
   * Specifies the fact type
   * @param factType  the type of object that facts are restricted to in the Rule.
   * @param        the generic fact type
   * @return          RuleBookRuleWithFactTypeBuilder for building RuleBook Rules with a specific fact type
   */
  @SuppressWarnings("unchecked")
  public  RuleBookRuleWithFactTypeBuilder withFactType(Class factType) {
    Rule rule = (Rule)RuleBuilder.create(_ruleClass).withFactType(factType).build();
    _ruleBook.addRule(rule);
    return new RuleBookRuleWithFactTypeBuilder<>(rule);
  }

  /**
   * Specifies no fact type.
   * A generic Object type is used for the Rule's fact type.
   * @return RuleBookRuleWithFactTypeBuilder for building RuleBook Rules with a specific fact type
   */
  public RuleBookRuleWithFactTypeBuilder withNoSpecifiedFactType() {
    return withFactType(Object.class);
  }
}