Class RuleContext

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

public final class RuleContext extends Object
Rule Context - Manages Facts and Variables During Rule Execution 规则上下文 - 管理规则执行期间的事实和变量

Provides storage for facts (domain objects) and variables used during rule evaluation and action execution. Also stores rule execution results.

提供在规则评估和动作执行期间使用的事实(领域对象)和变量的存储。同时存储规则执行结果。

Features | 主要功能:

  • Fact management (typed and named) - 事实管理(类型化和命名)
  • Variable storage - 变量存储
  • Result collection - 结果收集
  • Fluent API - 流式API

Usage Examples | 使用示例:

// Create context with facts
RuleContext context = RuleContext.create()
    .put("customerType", "VIP")
    .put("orderAmount", 5000.0)
    .addFact(customer)
    .addFact(order);

// Access values
String type = context.get("customerType");
Double amount = context.get("orderAmount");
Optional<Customer> cust = context.getFact(Customer.class);

// Set results
context.setResult("discount", 0.15);

Security | 安全性:

  • Thread-safe: No (create new instance per execution) - 线程安全: 否(每次执行创建新实例)
Since:
JDK 25, opencode-base-rules V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static RuleContext create()
      Creates an empty rule context 创建空的规则上下文
      Returns:
      new context instance | 新的上下文实例
    • of

      public static RuleContext of(Map<String,Object> values)
      Creates a context from a map of values 从值的Map创建上下文
      Parameters:
      values - the initial values | 初始值
      Returns:
      new context instance | 新的上下文实例
    • of

      public static RuleContext of(Object... keyValues)
      Creates a context from alternating key-value pairs 从交替的键值对创建上下文
      Parameters:
      keyValues - alternating key-value pairs | 交替的键值对
      Returns:
      new context instance | 新的上下文实例
      Throws:
      IllegalArgumentException - if odd number of arguments
    • withFacts

      public static RuleContext withFacts(Object... facts)
      Creates a context with fact objects 使用事实对象创建上下文
      Parameters:
      facts - the fact objects | 事实对象
      Returns:
      new context instance | 新的上下文实例
    • addFact

      public RuleContext addFact(Object fact)
      Adds a fact object to the context 向上下文添加事实对象
      Parameters:
      fact - the fact object | 事实对象
      Returns:
      this context for chaining | 此上下文用于链式调用
    • addFact

      public RuleContext addFact(String name, Object fact)
      Adds a named fact object to the context 向上下文添加命名的事实对象
      Parameters:
      name - the fact name | 事实名称
      fact - the fact object | 事实对象
      Returns:
      this context for chaining | 此上下文用于链式调用
    • put

      public RuleContext put(String key, Object value)
      Sets a variable value in the context 在上下文中设置变量值
      Parameters:
      key - the variable name | 变量名
      value - the variable value | 变量值
      Returns:
      this context for chaining | 此上下文用于链式调用
    • get

      public <T> T get(String key)
      Gets a variable value from the context 从上下文获取变量值
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      key - the variable name | 变量名
      Returns:
      the value, or null if not found | 值,如果未找到则为null
    • get

      public <T> T get(String key, T defaultValue)
      Gets a variable value with a default 获取变量值,带默认值
      Type Parameters:
      T - the value type | 值类型
      Parameters:
      key - the variable name | 变量名
      defaultValue - the default value | 默认值
      Returns:
      the value, or default if not found | 值,如果未找到则为默认值
    • getFact

      public <T> Optional<T> getFact(Class<T> type)
      Gets a fact by type 按类型获取事实
      Type Parameters:
      T - the fact type | 事实类型
      Parameters:
      type - the fact type | 事实类型
      Returns:
      optional containing the fact | 包含事实的Optional
    • getFacts

      public <T> List<T> getFacts(Class<T> type)
      Gets all facts of a specific type 获取特定类型的所有事实
      Type Parameters:
      T - the fact type | 事实类型
      Parameters:
      type - the fact type | 事实类型
      Returns:
      list of facts | 事实列表
    • contains

      public boolean contains(String key)
      Checks if a variable exists 检查变量是否存在
      Parameters:
      key - the variable name | 变量名
      Returns:
      true if exists | 如果存在返回true
    • setResult

      public RuleContext setResult(String key, Object value)
      Sets a result value 设置结果值
      Parameters:
      key - the result key | 结果键
      value - the result value | 结果值
      Returns:
      this context for chaining | 此上下文用于链式调用
    • getResult

      public <T> T getResult(String key)
      Gets a result value 获取结果值
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      key - the result key | 结果键
      Returns:
      the result value | 结果值
    • getResult

      public <T> T getResult(String key, T defaultValue)
      Gets a result value with default 获取结果值,带默认值
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      key - the result key | 结果键
      defaultValue - the default value | 默认值
      Returns:
      the result value or default | 结果值或默认值
    • getResults

      public Map<String,Object> getResults()
      Gets all results as an immutable map 获取所有结果作为不可变Map
      Returns:
      immutable map of results | 结果的不可变Map
    • getVariables

      public Map<String,Object> getVariables()
      Gets all variables as an immutable map 获取所有变量作为不可变Map
      Returns:
      immutable map of variables | 变量的不可变Map
    • facts

      public FactStore facts()
      Gets the fact store 获取事实存储
      Returns:
      the fact store | 事实存储
    • clearResults

      public RuleContext clearResults()
      Clears all results 清除所有结果
      Returns:
      this context for chaining | 此上下文用于链式调用
    • clearVariables

      public RuleContext clearVariables()
      Clears all variables 清除所有变量
      Returns:
      this context for chaining | 此上下文用于链式调用