Class RuleContext
java.lang.Object
cloud.opencode.base.rules.RuleContext
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 Summary
Modifier and TypeMethodDescriptionAdds a fact object to the context 向上下文添加事实对象Adds a named fact object to the context 向上下文添加命名的事实对象Clears all results 清除所有结果Clears all variables 清除所有变量<T> booleanChecks if a typed key exists in variables or facts 检查类型化键是否存在于变量或事实中booleanChecks if a variable exists 检查变量是否存在static RuleContextcreate()Creates an empty rule context 创建空的规则上下文facts()Gets the fact store 获取事实存储<T> TGets a typed value by typed key (checks variables first, then facts) 通过类型化键获取类型化值(先检查变量,再检查事实)<T> TGets a typed value by typed key with a default 通过类型化键获取类型化值,带默认值<T> TGets a variable value from the context 从上下文获取变量值<T> TGets a variable value with a default 获取变量值,带默认值<T> Optional<T> Gets a fact by type 按类型获取事实<T> List<T> Gets all facts of a specific type 获取特定类型的所有事实<T> TGets a result value 获取结果值<T> TGets a result value with default 获取结果值,带默认值Gets all results as an immutable map 获取所有结果作为不可变MapGets all variables as an immutable map 获取所有变量作为不可变Mapstatic RuleContextCreates a context from alternating key-value pairs 从交替的键值对创建上下文static RuleContextCreates a context from a map of values 从值的Map创建上下文<T> RuleContextPuts a typed value by typed key into variables 通过类型化键将类型化值放入变量中Sets a variable value in the context 在上下文中设置变量值Sets a result value 设置结果值static RuleContextCreates a context with fact objects 使用事实对象创建上下文
-
Method Details
-
create
Creates an empty rule context 创建空的规则上下文- Returns:
- new context instance | 新的上下文实例
-
of
Creates a context from a map of values 从值的Map创建上下文- Parameters:
values- the initial values | 初始值- Returns:
- new context instance | 新的上下文实例
-
of
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
Creates a context with fact objects 使用事实对象创建上下文- Parameters:
facts- the fact objects | 事实对象- Returns:
- new context instance | 新的上下文实例
-
addFact
Adds a fact object to the context 向上下文添加事实对象- Parameters:
fact- the fact object | 事实对象- Returns:
- this context for chaining | 此上下文用于链式调用
-
addFact
Adds a named fact object to the context 向上下文添加命名的事实对象- Parameters:
name- the fact name | 事实名称fact- the fact object | 事实对象- Returns:
- this context for chaining | 此上下文用于链式调用
-
put
Sets a variable value in the context 在上下文中设置变量值- Parameters:
key- the variable name | 变量名value- the variable value | 变量值- Returns:
- this context for chaining | 此上下文用于链式调用
-
get
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
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
-
getFacts
-
contains
Checks if a variable exists 检查变量是否存在- Parameters:
key- the variable name | 变量名- Returns:
- true if exists | 如果存在返回true
-
setResult
Sets a result value 设置结果值- Parameters:
key- the result key | 结果键value- the result value | 结果值- Returns:
- this context for chaining | 此上下文用于链式调用
-
getResult
Gets a result value 获取结果值- Type Parameters:
T- the result type | 结果类型- Parameters:
key- the result key | 结果键- Returns:
- the result value | 结果值
-
getResult
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
-
getVariables
-
facts
-
get
Gets a typed value by typed key (checks variables first, then facts) 通过类型化键获取类型化值(先检查变量,再检查事实)- Type Parameters:
T- the value type | 值类型- Parameters:
key- the typed key | 类型化键- Returns:
- the typed value, or null if not found | 类型化值,如果未找到则为null
- Since:
- JDK 25, opencode-base-rules V1.0.3
-
get
Gets a typed value by typed key with a default 通过类型化键获取类型化值,带默认值- Type Parameters:
T- the value type | 值类型- Parameters:
key- the typed key | 类型化键defaultValue- the default value | 默认值- Returns:
- the typed value, or default if not found | 类型化值,如果未找到则为默认值
- Since:
- JDK 25, opencode-base-rules V1.0.3
-
put
Puts a typed value by typed key into variables 通过类型化键将类型化值放入变量中- Type Parameters:
T- the value type | 值类型- Parameters:
key- the typed key | 类型化键value- the value | 值- Returns:
- this context for chaining | 此上下文用于链式调用
- Since:
- JDK 25, opencode-base-rules V1.0.3
-
contains
Checks if a typed key exists in variables or facts 检查类型化键是否存在于变量或事实中- Type Parameters:
T- the value type | 值类型- Parameters:
key- the typed key | 类型化键- Returns:
- true if exists | 如果存在返回true
- Since:
- JDK 25, opencode-base-rules V1.0.3
-
clearResults
Clears all results 清除所有结果- Returns:
- this context for chaining | 此上下文用于链式调用
-
clearVariables
Clears all variables 清除所有变量- Returns:
- this context for chaining | 此上下文用于链式调用
-