Interface Rule

All Superinterfaces:
Comparable<Rule>
All Known Subinterfaces:
ScoringRule
All Known Implementing Classes:
DefaultRule

public interface Rule extends Comparable<Rule>
Rule Interface - Core Abstraction of Business Rule 规则接口 - 业务规则的核心抽象

Defines the contract for a business rule with condition evaluation and action execution.

定义业务规则的契约,包括条件评估和动作执行。

Features | 主要功能:

  • Condition evaluation - 条件评估
  • Action execution - 动作执行
  • Priority-based ordering - 基于优先级排序
  • Group support - 分组支持

Usage Examples | 使用示例:

Rule rule = OpenRules.rule("discount-rule")
    .when(ctx -> ctx.get("amount") > 1000)
    .then(ctx -> ctx.setResult("discount", 0.1))
    .build();

if (rule.evaluate(context)) {
    rule.execute(context);
}

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
Since:
JDK 25, opencode-base-rules V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default priority value | 默认优先级值
  • Method Summary

    Modifier and Type
    Method
    Description
    default int
    compareTo(Rule other)
    Compares rules by priority (lower value = higher priority) 按优先级比较规则(值越小优先级越高)
    boolean
    Evaluates the rule condition against the given context 根据给定上下文评估规则条件
    void
    Executes the rule action with the given context 使用给定上下文执行规则动作
    Gets the description of this rule 获取此规则的描述
    default String
    Gets the group this rule belongs to 获取此规则所属的分组
    Gets the unique name of this rule 获取此规则的唯一名称
    int
    Gets the priority of this rule (lower value = higher priority) 获取此规则的优先级(值越小优先级越高)
    default boolean
    Checks if this rule is enabled 检查此规则是否启用
    default boolean
    Checks if this rule is terminal (stops engine execution when fired) 检查此规则是否是终止规则(触发时停止引擎执行)
  • Field Details

    • DEFAULT_PRIORITY

      static final int DEFAULT_PRIORITY
      Default priority value | 默认优先级值
      See Also:
  • Method Details

    • getName

      String getName()
      Gets the unique name of this rule 获取此规则的唯一名称
      Returns:
      the rule name | 规则名称
    • getDescription

      String getDescription()
      Gets the description of this rule 获取此规则的描述
      Returns:
      the rule description, may be null | 规则描述,可能为null
    • getPriority

      int getPriority()
      Gets the priority of this rule (lower value = higher priority) 获取此规则的优先级(值越小优先级越高)
      Returns:
      the priority value | 优先级值
    • evaluate

      boolean evaluate(RuleContext context)
      Evaluates the rule condition against the given context 根据给定上下文评估规则条件
      Parameters:
      context - the rule context containing facts | 包含事实的规则上下文
      Returns:
      true if the condition is satisfied | 如果条件满足返回true
    • execute

      void execute(RuleContext context)
      Executes the rule action with the given context 使用给定上下文执行规则动作
      Parameters:
      context - the rule context | 规则上下文
    • getGroup

      default String getGroup()
      Gets the group this rule belongs to 获取此规则所属的分组
      Returns:
      the group name, or null if not grouped | 分组名称,如果未分组则为null
    • isEnabled

      default boolean isEnabled()
      Checks if this rule is enabled 检查此规则是否启用
      Returns:
      true if the rule is enabled | 如果规则启用返回true
    • isTerminal

      default boolean isTerminal()
      Checks if this rule is terminal (stops engine execution when fired) 检查此规则是否是终止规则(触发时停止引擎执行)
      Returns:
      true if the rule is terminal | 如果规则是终止规则返回true
      Since:
      JDK 25, opencode-base-rules V1.0.3
    • compareTo

      default int compareTo(Rule other)
      Compares rules by priority (lower value = higher priority) 按优先级比较规则(值越小优先级越高)
      Specified by:
      compareTo in interface Comparable<Rule>
      Parameters:
      other - the other rule to compare | 要比较的另一个规则
      Returns:
      comparison result | 比较结果