Class ChainedContext
java.lang.Object
cloud.opencode.base.expression.context.ChainedContext
- All Implemented Interfaces:
EvaluationContext
Chained Evaluation Context
链式求值上下文
A context that chains multiple contexts together, allowing hierarchical variable lookup. Variables are searched in order from this context to parent contexts.
一个将多个上下文链接在一起的上下文,允许分层变量查找。变量按从当前上下文到父上下文的顺序搜索。
Usage | 用法
EvaluationContext parent = new StandardContext();
parent.setVariable("x", 10);
ChainedContext child = ChainedContext.of(parent);
child.setVariable("y", 20);
// child has access to both x (from parent) and y (local)
Object result = OpenExpression.eval("x + y", child); // 30
Features | 主要功能:
- Hierarchical variable lookup (local then parent) - 层次化变量查找(本地然后父级)
- Inherits function registry and sandbox from parent - 继承父级的函数注册表和沙箱
- Chain depth tracking - 链深度跟踪
- Builder pattern for construction - 构建器模式用于构造
Security | 安全性:
- Thread-safe: Yes, ConcurrentHashMap for local variables - 线程安全: 是,本地变量使用ConcurrentHashMap
- Null-safe: Yes, null name returns null/false - 空值安全: 是,null名称返回null/false
Usage Examples | 使用示例:
EvaluationContext parent = new StandardContext();
parent.setVariable("x", 10);
ChainedContext child = ChainedContext.of(parent);
child.setVariable("y", 20);
Object result = OpenExpression.eval("x + y", child); // 30
- Since:
- JDK 25, opencode-base-expression V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for ChainedContext ChainedContext 构建器 -
Constructor Summary
ConstructorsConstructorDescriptionChainedContext(EvaluationContext parent) Create chained context with parent 使用父上下文创建链式上下文ChainedContext(EvaluationContext parent, Object rootObject) Create chained context with parent and root object 使用父上下文和根对象创建链式上下文 -
Method Summary
Modifier and TypeMethodDescriptionstatic ChainedContext.Builderbuilder(EvaluationContext parent) Create a builder for ChainedContext 创建 ChainedContext 的构建器Create child context 创建子上下文intgetDepth()Get the chain depth (number of parent contexts + 1) 获取链深度(父上下文数量 + 1)Get function registry 获取函数注册表Get local variables only (not including parent) 仅获取本地变量(不包括父上下文)Get the parent context 获取父上下文Get property accessors 获取属性访问器Get the root object 获取根对象Get security sandbox 获取安全沙箱Get type converter 获取类型转换器getVariable(String name) Get variable value 获取变量值Get all variables 获取所有变量booleanhasVariable(String name) Check if variable exists 检查变量是否存在static ChainedContextof(EvaluationContext parent) Create chained context from parent 从父上下文创建链式上下文static ChainedContextof(EvaluationContext parent, Object rootObject) Create chained context from parent with root object 从父上下文和根对象创建链式上下文voidsetRootObject(Object root) Set the root object 设置根对象voidsetVariable(String name, Object value) Set variable value 设置变量值
-
Constructor Details
-
ChainedContext
Create chained context with parent 使用父上下文创建链式上下文- Parameters:
parent- the parent context | 父上下文
-
ChainedContext
Create chained context with parent and root object 使用父上下文和根对象创建链式上下文- Parameters:
parent- the parent context | 父上下文rootObject- the root object | 根对象
-
-
Method Details
-
getRootObject
Description copied from interface:EvaluationContextGet the root object 获取根对象- Specified by:
getRootObjectin interfaceEvaluationContext- Returns:
- the root object | 根对象
-
setRootObject
Description copied from interface:EvaluationContextSet the root object 设置根对象- Specified by:
setRootObjectin interfaceEvaluationContext- Parameters:
root- the root object | 根对象
-
getVariable
Description copied from interface:EvaluationContextGet variable value 获取变量值- Specified by:
getVariablein interfaceEvaluationContext- Parameters:
name- the variable name | 变量名- Returns:
- the variable value | 变量值
-
setVariable
Description copied from interface:EvaluationContextSet variable value 设置变量值- Specified by:
setVariablein interfaceEvaluationContext- Parameters:
name- the variable name | 变量名value- the variable value | 变量值
-
hasVariable
Description copied from interface:EvaluationContextCheck if variable exists 检查变量是否存在- Specified by:
hasVariablein interfaceEvaluationContext- Parameters:
name- the variable name | 变量名- Returns:
- true if exists | 如果存在返回true
-
getVariables
Description copied from interface:EvaluationContextGet all variables 获取所有变量- Specified by:
getVariablesin interfaceEvaluationContext- Returns:
- the variable map | 变量映射
-
getLocalVariables
-
getFunctionRegistry
Description copied from interface:EvaluationContextGet function registry 获取函数注册表- Specified by:
getFunctionRegistryin interfaceEvaluationContext- Returns:
- the function registry | 函数注册表
-
getPropertyAccessors
Description copied from interface:EvaluationContextGet property accessors 获取属性访问器- Specified by:
getPropertyAccessorsin interfaceEvaluationContext- Returns:
- the property accessor list | 属性访问器列表
-
getTypeConverter
Description copied from interface:EvaluationContextGet type converter 获取类型转换器- Specified by:
getTypeConverterin interfaceEvaluationContext- Returns:
- the type converter | 类型转换器
-
getSandbox
Description copied from interface:EvaluationContextGet security sandbox 获取安全沙箱- Specified by:
getSandboxin interfaceEvaluationContext- Returns:
- the sandbox, or null if not configured | 沙箱,如果未配置则返回null
-
createChild
Description copied from interface:EvaluationContextCreate child context 创建子上下文Creates a child context that inherits from this context but can have its own variable bindings. Used for collection operations.
创建继承自此上下文但可以有自己变量绑定的子上下文。用于集合操作。
- Specified by:
createChildin interfaceEvaluationContext- Returns:
- the child context | 子上下文
-
getParent
Get the parent context 获取父上下文- Returns:
- the parent context | 父上下文
-
getDepth
public int getDepth()Get the chain depth (number of parent contexts + 1) 获取链深度(父上下文数量 + 1)- Returns:
- the chain depth | 链深度
-
of
Create chained context from parent 从父上下文创建链式上下文- Parameters:
parent- the parent context | 父上下文- Returns:
- the chained context | 链式上下文
-
of
Create chained context from parent with root object 从父上下文和根对象创建链式上下文- Parameters:
parent- the parent context | 父上下文rootObject- the root object | 根对象- Returns:
- the chained context | 链式上下文
-
builder
Create a builder for ChainedContext 创建 ChainedContext 的构建器- Parameters:
parent- the parent context | 父上下文- Returns:
- the builder | 构建器
-