Class RuleBuilder

java.lang.Object
cloud.opencode.base.rules.dsl.RuleBuilder

public final class RuleBuilder extends Object
Rule Builder - Fluent DSL for Rule Construction 规则构建器 - 规则构建的流式DSL

Provides a fluent API for constructing rules with conditions and actions.

提供用于构建带条件和动作的规则的流式API。

Features | 主要功能:

  • Fluent API - 流式API
  • Multiple conditions (AND) - 多个条件(AND)
  • Multiple actions - 多个动作
  • Priority and grouping - 优先级和分组

Usage Examples | 使用示例:

Rule rule = RuleBuilder.rule("vip-discount")
    .description("VIP customer discount rule")
    .priority(1)
    .group("discounts")
    .when(ctx -> ctx.get("customerType").equals("VIP"))
    .and(ctx -> ctx.<Double>get("amount") > 1000)
    .then(ctx -> ctx.setResult("discount", 0.15))
    .build();

Security | 安全性:

  • Thread-safe: No (builder pattern, single-threaded use) - 线程安全: 否(构建器模式,单线程使用)
  • Null-safe: No (conditions and actions must not be null) - 空值安全: 否(条件和动作不能为null)

Performance | 性能特性:

  • Time complexity: O(1) per condition/action addition - 每次条件/动作添加 O(1)
  • Space complexity: O(c + a) where c = conditions, a = actions - O(c + a), c为条件数, a为动作数
Since:
JDK 25, opencode-base-rules V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • RuleBuilder

      public RuleBuilder(String name)
      Creates a rule builder with the given name 使用给定名称创建规则构建器
      Parameters:
      name - the rule name | 规则名称
    • RuleBuilder

      public RuleBuilder()
      Creates a rule builder with a default name 使用默认名称创建规则构建器
  • Method Details

    • rule

      public static RuleBuilder rule(String name)
      Creates a rule builder with the given name 使用给定名称创建规则构建器
      Parameters:
      name - the rule name | 规则名称
      Returns:
      the builder | 构建器
    • description

      public RuleBuilder description(String description)
      Sets the rule description 设置规则描述
      Parameters:
      description - the description | 描述
      Returns:
      this builder | 此构建器
    • priority

      public RuleBuilder priority(int priority)
      Sets the rule priority (lower value = higher priority) 设置规则优先级(值越小优先级越高)
      Parameters:
      priority - the priority | 优先级
      Returns:
      this builder | 此构建器
    • group

      public RuleBuilder group(String group)
      Sets the rule group 设置规则分组
      Parameters:
      group - the group name | 组名
      Returns:
      this builder | 此构建器
    • enabled

      public RuleBuilder enabled(boolean enabled)
      Sets whether the rule is enabled 设置规则是否启用
      Parameters:
      enabled - whether enabled | 是否启用
      Returns:
      this builder | 此构建器
    • terminal

      public RuleBuilder terminal()
      Marks the rule as terminal (stops engine execution when fired) 将规则标记为终止规则(触发时停止引擎执行)
      Returns:
      this builder | 此构建器
    • terminal

      public RuleBuilder terminal(boolean terminal)
      Sets whether the rule is terminal 设置规则是否为终止规则
      Parameters:
      terminal - whether terminal | 是否为终止规则
      Returns:
      this builder | 此构建器
    • when

      public RuleBuilder when(Predicate<RuleContext> predicate)
      Adds a condition using a predicate 使用谓词添加条件
      Parameters:
      predicate - the condition predicate | 条件谓词
      Returns:
      this builder | 此构建器
    • when

      public RuleBuilder when(Condition condition)
      Adds a condition object 添加条件对象
      Parameters:
      condition - the condition | 条件
      Returns:
      this builder | 此构建器
    • and

      public RuleBuilder and(Predicate<RuleContext> predicate)
      Adds an additional AND condition 添加额外的AND条件
      Parameters:
      predicate - the condition predicate | 条件谓词
      Returns:
      this builder | 此构建器
    • and

      public RuleBuilder and(Condition condition)
      Adds an additional AND condition object 添加额外的AND条件对象
      Parameters:
      condition - the condition | 条件
      Returns:
      this builder | 此构建器
    • then

      public RuleBuilder then(Consumer<RuleContext> action)
      Adds an action using a consumer 使用消费者添加动作
      Parameters:
      action - the action consumer | 动作消费者
      Returns:
      this builder | 此构建器
    • then

      public RuleBuilder then(Action action)
      Adds an action object 添加动作对象
      Parameters:
      action - the action | 动作
      Returns:
      this builder | 此构建器
    • andThen

      public RuleBuilder andThen(Consumer<RuleContext> action)
      Adds an additional action 添加额外的动作
      Parameters:
      action - the action consumer | 动作消费者
      Returns:
      this builder | 此构建器
    • andThen

      public RuleBuilder andThen(Action action)
      Adds an additional action object 添加额外的动作对象
      Parameters:
      action - the action | 动作
      Returns:
      this builder | 此构建器
    • build

      public Rule build()
      Builds the rule 构建规则
      Returns:
      the constructed rule | 构建的规则
      Throws:
      IllegalStateException - if no condition or action is set | 如果未设置条件或动作则抛出