Class DefaultRule

java.lang.Object
cloud.opencode.base.rules.engine.DefaultRule
All Implemented Interfaces:
Rule, Comparable<Rule>

public class DefaultRule extends Object implements Rule
Default Rule Implementation 默认规则实现

Standard implementation of the Rule interface with condition and action.

带条件和动作的规则接口标准实现。

Features | 主要功能:

  • Immutable rule configuration - 不可变规则配置
  • Priority and group support - 优先级和分组支持
  • Enable/disable toggle - 启用/禁用开关

Usage Examples | 使用示例:

Rule rule = new DefaultRule("discount", "VIP discount", 1, "pricing",
    true,
    ctx -> "VIP".equals(ctx.get("type")),
    ctx -> ctx.setResult("discount", 0.15));

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-rules V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • DefaultRule

      public DefaultRule(String name, String description, int priority, String group, boolean enabled, Condition condition, Action action)
      Creates a default rule 创建默认规则
      Parameters:
      name - the rule name | 规则名称
      description - the description | 描述
      priority - the priority | 优先级
      group - the group | 分组
      enabled - whether enabled | 是否启用
      condition - the condition | 条件
      action - the action | 动作
  • Method Details

    • getName

      public String getName()
      Description copied from interface: Rule
      Gets the unique name of this rule 获取此规则的唯一名称
      Specified by:
      getName in interface Rule
      Returns:
      the rule name | 规则名称
    • getDescription

      public String getDescription()
      Description copied from interface: Rule
      Gets the description of this rule 获取此规则的描述
      Specified by:
      getDescription in interface Rule
      Returns:
      the rule description, may be null | 规则描述,可能为null
    • getPriority

      public int getPriority()
      Description copied from interface: Rule
      Gets the priority of this rule (lower value = higher priority) 获取此规则的优先级(值越小优先级越高)
      Specified by:
      getPriority in interface Rule
      Returns:
      the priority value | 优先级值
    • getGroup

      public String getGroup()
      Description copied from interface: Rule
      Gets the group this rule belongs to 获取此规则所属的分组
      Specified by:
      getGroup in interface Rule
      Returns:
      the group name, or null if not grouped | 分组名称,如果未分组则为null
    • isEnabled

      public boolean isEnabled()
      Description copied from interface: Rule
      Checks if this rule is enabled 检查此规则是否启用
      Specified by:
      isEnabled in interface Rule
      Returns:
      true if the rule is enabled | 如果规则启用返回true
    • evaluate

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

      public void execute(RuleContext context)
      Description copied from interface: Rule
      Executes the rule action with the given context 使用给定上下文执行规则动作
      Specified by:
      execute in interface Rule
      Parameters:
      context - the rule context | 规则上下文
    • getCondition

      public Condition getCondition()
      Gets the condition 获取条件
      Returns:
      the condition | 条件
    • getAction

      public Action getAction()
      Gets the action 获取动作
      Returns:
      the action | 动作
    • toString

      public String toString()
      Overrides:
      toString in class Object