Class DecisionTableBuilder

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

public final class DecisionTableBuilder extends Object
Decision Table Builder - Fluent DSL for Decision Table Construction 决策表构建器 - 决策表构建的流式DSL

Provides a fluent API for building decision tables with input columns, output columns, and rows of conditions and values.

提供用于构建决策表的流式API,包含输入列、输出列和条件值行。

Features | 主要功能:

  • Fluent builder API - 流式构建器API
  • Input/output column definition - 输入/输出列定义
  • Row-based and varargs row addition - 基于行和可变参数的行添加
  • Validation on build - 构建时验证

Usage Examples | 使用示例:

DecisionTable table = new DecisionTableBuilder()
    .name("pricing")
    .hitPolicy(HitPolicy.FIRST)
    .input("customerType", String.class)
    .input("amount", Double.class)
    .output("discount", Double.class)
    .output("freeShipping", Boolean.class)
    .row(new Object[]{"VIP", ">= 1000"}, new Object[]{0.15, true})
    .row(new Object[]{"VIP", "-"}, new Object[]{0.10, false})
    .row(new Object[]{"Regular", ">= 500"}, new Object[]{0.05, false})
    .row(new Object[]{"-", "-"}, new Object[]{0.0, false})
    .build();

Condition Syntax | 条件语法:

  • "-" - Wildcard, matches any value | 通配符,匹配任意值
  • ">= 100" - Greater than or equal | 大于等于
  • "<= 100" - Less than or equal | 小于等于
  • "> 100" - Greater than | 大于
  • "< 100" - Less than | 小于
  • "!= value" - Not equal | 不等于
  • "value" - Exact match | 精确匹配

Security | 安全性:

  • Thread-safe: No (builder pattern, single-threaded use) - 线程安全: 否(构建器模式,单线程使用)
  • Null-safe: No (column names and values must not be null) - 空值安全: 否(列名和值不能为null)

Performance | 性能特性:

  • Time complexity: O(1) per rule addition, O(r * c) for build where r = rules, c = conditions - 每次规则添加 O(1), 构建 O(r * c)
  • Space complexity: O(r * c) for decision table - 决策表 O(r * c)
Since:
JDK 25, opencode-base-rules V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • DecisionTableBuilder

      public DecisionTableBuilder()
      Creates a new decision table builder 创建新的决策表构建器
  • Method Details

    • name

      public DecisionTableBuilder name(String name)
      Sets the decision table name 设置决策表名称
      Parameters:
      name - the table name | 表名称
      Returns:
      this builder | 此构建器
    • hitPolicy

      public DecisionTableBuilder hitPolicy(HitPolicy hitPolicy)
      Sets the hit policy 设置命中策略
      Parameters:
      hitPolicy - the hit policy | 命中策略
      Returns:
      this builder | 此构建器
    • input

      public DecisionTableBuilder input(String name, Class<?> type)
      Adds an input column 添加输入列
      Parameters:
      name - the column name | 列名称
      type - the column type | 列类型
      Returns:
      this builder | 此构建器
    • input

      public DecisionTableBuilder input(String name)
      Adds an input column (type defaults to Object) 添加输入列(类型默认为Object)
      Parameters:
      name - the column name | 列名称
      Returns:
      this builder | 此构建器
    • inputs

      public DecisionTableBuilder inputs(String... names)
      Adds multiple input columns 添加多个输入列
      Parameters:
      names - the column names | 列名称
      Returns:
      this builder | 此构建器
    • output

      public DecisionTableBuilder output(String name, Class<?> type)
      Adds an output column 添加输出列
      Parameters:
      name - the column name | 列名称
      type - the column type | 列类型
      Returns:
      this builder | 此构建器
    • output

      public DecisionTableBuilder output(String name)
      Adds an output column (type defaults to Object) 添加输出列(类型默认为Object)
      Parameters:
      name - the column name | 列名称
      Returns:
      this builder | 此构建器
    • outputs

      public DecisionTableBuilder outputs(String... names)
      Adds multiple output columns 添加多个输出列
      Parameters:
      names - the column names | 列名称
      Returns:
      this builder | 此构建器
    • row

      public DecisionTableBuilder row(Object[] conditions, Object[] values)
      Adds a row to the decision table 向决策表添加行
      Parameters:
      conditions - the input conditions | 输入条件
      values - the output values | 输出值
      Returns:
      this builder | 此构建器
      Throws:
      OpenRulesException - if conditions or values don't match column count
    • addRow

      public DecisionTableBuilder addRow(Object... args)
      Adds a row using varargs syntax 使用可变参数语法添加行

      The first half of arguments are conditions, the second half are values.

      参数的前半部分是条件,后半部分是值。

      Parameters:
      args - conditions followed by values | 条件后跟值
      Returns:
      this builder | 此构建器
    • build

      public DecisionTable build()
      Builds the decision table 构建决策表
      Returns:
      the decision table | 决策表
      Throws:
      OpenRulesException - if validation fails
    • getRowCount

      public int getRowCount()
      Gets the current row count 获取当前行数
      Returns:
      the row count | 行数
    • getInputColumnCount

      public int getInputColumnCount()
      Gets the input column count 获取输入列数
      Returns:
      the input column count | 输入列数
    • getOutputColumnCount

      public int getOutputColumnCount()
      Gets the output column count 获取输出列数
      Returns:
      the output column count | 输出列数