Class OpenRules

java.lang.Object
cloud.opencode.base.rules.OpenRules

public final class OpenRules extends Object
OpenRules Facade - Unified Entry Point for Rules Engine OpenRules门面 - 规则引擎统一入口

Provides factory methods and utilities for creating rules, rule engines, decision tables, and related components.

提供创建规则、规则引擎、决策表和相关组件的工厂方法和工具。

Features | 主要功能:

  • Rule creation via fluent DSL - 通过流式DSL创建规则
  • Rule engine configuration - 规则引擎配置
  • Decision table building - 决策表构建
  • Conflict resolution strategies - 冲突解决策略
  • Rule execution listeners - 规则执行监听器

Usage Examples | 使用示例:

// Create a simple rule
Rule discountRule = OpenRules.rule("discount-rule")
    .description("Apply discount for VIP customers")
    .priority(100)
    .when(ctx -> "VIP".equals(ctx.get("customerType")))
    .then(ctx -> ctx.put("discount", 0.15))
    .build();

// Create and configure rule engine
RuleEngine engine = OpenRules.engine()
    .register(discountRule)
    .setConflictResolver(OpenRules.priorityResolver())
    .addListener(OpenRules.loggingListener())
    .build();

// Create and evaluate decision table
DecisionTable table = OpenRules.decisionTable()
    .name("pricing")
    .hitPolicy(HitPolicy.FIRST)
    .input("customerType", String.class)
    .input("amount", Double.class)
    .output("discount", Double.class)
    .row(new Object[]{"VIP", ">= 1000"}, new Object[]{0.15})
    .row(new Object[]{"VIP", "-"}, new Object[]{0.10})
    .row(new Object[]{"-", "-"}, new Object[]{0.0})
    .build();

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (arguments must not be null) - 空值安全: 否(参数不能为null)
Since:
JDK 25, opencode-base-rules V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • rule

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

      public static RuleBuilder rule()
      Creates a new rule builder 创建新的规则构建器
      Returns:
      the rule builder | 规则构建器
    • group

      public static RuleGroupBuilder group(String name)
      Creates a new rule group builder with the given name 使用给定名称创建新的规则组构建器
      Parameters:
      name - the group name | 组名称
      Returns:
      the rule group builder | 规则组构建器
    • engine

      public static RuleEngineBuilder engine()
      Creates a new rule engine builder 创建新的规则引擎构建器
      Returns:
      the rule engine builder | 规则引擎构建器
    • defaultEngine

      public static RuleEngine defaultEngine()
      Creates a new default rule engine 创建新的默认规则引擎
      Returns:
      the rule engine | 规则引擎
    • engineWith

      public static RuleEngine engineWith(Rule... rules)
      Creates a rule engine with the given rules 使用给定规则创建规则引擎
      Parameters:
      rules - the rules to register | 要注册的规则
      Returns:
      the rule engine | 规则引擎
    • engineWith

      public static RuleEngine engineWith(RuleGroup group)
      Creates a rule engine with the given rule group 使用给定规则组创建规则引擎
      Parameters:
      group - the rule group to register | 要注册的规则组
      Returns:
      the rule engine | 规则引擎
    • decisionTable

      public static DecisionTableBuilder decisionTable()
      Creates a new decision table builder 创建新的决策表构建器
      Returns:
      the decision table builder | 决策表构建器
    • decisionTable

      public static DecisionTableBuilder decisionTable(String name)
      Creates a new decision table builder with the given name 使用给定名称创建新的决策表构建器
      Parameters:
      name - the table name | 表名称
      Returns:
      the decision table builder | 决策表构建器
    • context

      public static RuleContext context()
      Creates a new empty rule context 创建新的空规则上下文
      Returns:
      the rule context | 规则上下文
    • contextOf

      public static RuleContext contextOf(Object... keyValues)
      Creates a rule context with initial facts 使用初始事实创建规则上下文
      Parameters:
      keyValues - alternating key-value pairs | 交替的键值对
      Returns:
      the rule context | 规则上下文
    • condition

      public static Condition condition(Predicate<RuleContext> predicate)
      Creates a condition from a predicate 从谓词创建条件
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      the condition | 条件
    • alwaysTrue

      public static Condition alwaysTrue()
      Creates an always-true condition 创建始终为真的条件
      Returns:
      the condition | 条件
    • alwaysFalse

      public static Condition alwaysFalse()
      Creates an always-false condition 创建始终为假的条件
      Returns:
      the condition | 条件
    • action

      public static Action action(Consumer<RuleContext> consumer)
      Creates an action from a consumer 从消费者创建动作
      Parameters:
      consumer - the consumer | 消费者
      Returns:
      the action | 动作
    • noOp

      public static Action noOp()
      Creates a no-op action 创建无操作动作
      Returns:
      the action | 动作
    • priorityResolver

      public static ConflictResolver priorityResolver()
      Gets the priority-based conflict resolver 获取基于优先级的冲突解决器

      Rules are ordered by priority (lower number = higher priority).

      规则按优先级排序(数字越小 = 优先级越高)。

      Returns:
      the priority conflict resolver | 优先级冲突解决器
    • orderResolver

      public static ConflictResolver orderResolver()
      Gets the order-based conflict resolver 获取基于顺序的冲突解决器

      Rules are kept in registration order.

      规则保持注册顺序。

      Returns:
      the order conflict resolver | 顺序冲突解决器
    • loggingListener

      public static RuleListener loggingListener()
      Creates a logging rule listener 创建日志规则监听器
      Returns:
      the logging listener | 日志监听器
    • key

      public static <T> TypedKey<T> key(String name, Class<T> type)
      Creates a typed key for type-safe fact access 创建类型化键用于类型安全的事实访问
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      name - the key name | 键名称
      type - the value type | 值类型
      Returns:
      the typed key | 类型化键
    • tracingListener

      public static TracingRuleListener tracingListener()
      Creates a tracing listener for execution tracing 创建追踪监听器用于执行追踪
      Returns:
      the tracing listener | 追踪监听器
    • metrics

      public static RuleMetrics metrics()
      Creates a new rule metrics collector 创建新的规则指标收集器
      Returns:
      the rule metrics | 规则指标
    • metricsListener

      public static MetricsListener metricsListener(RuleMetrics metrics)
      Creates a metrics listener backed by the given metrics collector 创建由指定指标收集器支持的指标监听器
      Parameters:
      metrics - the metrics collector | 指标收集器
      Returns:
      the metrics listener | 指标监听器
    • metricsListener

      public static MetricsListener metricsListener()
      Creates a metrics listener with its own internal metrics collector 创建带有内部指标收集器的指标监听器
      Returns:
      the metrics listener | 指标监听器
    • validate

      public static ValidationReport validate(Collection<Rule> rules)
      Validates a collection of rules 验证规则集合
      Parameters:
      rules - the rules to validate | 要验证的规则
      Returns:
      the validation report | 验证报告
    • version

      public static String version()
      Gets the library version 获取库版本
      Returns:
      the version string | 版本字符串
    • info

      public static String info()
      Gets the library information 获取库信息
      Returns:
      the library info | 库信息