Class ChainedContext

java.lang.Object
cloud.opencode.base.expression.context.ChainedContext
All Implemented Interfaces:
EvaluationContext

public class ChainedContext extends Object implements 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:
  • Constructor Details

    • ChainedContext

      public ChainedContext(EvaluationContext parent)
      Create chained context with parent 使用父上下文创建链式上下文
      Parameters:
      parent - the parent context | 父上下文
    • ChainedContext

      public ChainedContext(EvaluationContext parent, Object rootObject)
      Create chained context with parent and root object 使用父上下文和根对象创建链式上下文
      Parameters:
      parent - the parent context | 父上下文
      rootObject - the root object | 根对象
  • Method Details

    • getRootObject

      public Object getRootObject()
      Description copied from interface: EvaluationContext
      Get the root object 获取根对象
      Specified by:
      getRootObject in interface EvaluationContext
      Returns:
      the root object | 根对象
    • setRootObject

      public void setRootObject(Object root)
      Description copied from interface: EvaluationContext
      Set the root object 设置根对象
      Specified by:
      setRootObject in interface EvaluationContext
      Parameters:
      root - the root object | 根对象
    • getVariable

      public Object getVariable(String name)
      Description copied from interface: EvaluationContext
      Get variable value 获取变量值
      Specified by:
      getVariable in interface EvaluationContext
      Parameters:
      name - the variable name | 变量名
      Returns:
      the variable value | 变量值
    • setVariable

      public void setVariable(String name, Object value)
      Description copied from interface: EvaluationContext
      Set variable value 设置变量值
      Specified by:
      setVariable in interface EvaluationContext
      Parameters:
      name - the variable name | 变量名
      value - the variable value | 变量值
    • hasVariable

      public boolean hasVariable(String name)
      Description copied from interface: EvaluationContext
      Check if variable exists 检查变量是否存在
      Specified by:
      hasVariable in interface EvaluationContext
      Parameters:
      name - the variable name | 变量名
      Returns:
      true if exists | 如果存在返回true
    • getVariables

      public Map<String,Object> getVariables()
      Description copied from interface: EvaluationContext
      Get all variables 获取所有变量
      Specified by:
      getVariables in interface EvaluationContext
      Returns:
      the variable map | 变量映射
    • getLocalVariables

      public Map<String,Object> getLocalVariables()
      Get local variables only (not including parent) 仅获取本地变量(不包括父上下文)
      Returns:
      the local variable map | 本地变量映射
    • getFunctionRegistry

      public FunctionRegistry getFunctionRegistry()
      Description copied from interface: EvaluationContext
      Get function registry 获取函数注册表
      Specified by:
      getFunctionRegistry in interface EvaluationContext
      Returns:
      the function registry | 函数注册表
    • getPropertyAccessors

      public List<PropertyAccessor> getPropertyAccessors()
      Description copied from interface: EvaluationContext
      Get property accessors 获取属性访问器
      Specified by:
      getPropertyAccessors in interface EvaluationContext
      Returns:
      the property accessor list | 属性访问器列表
    • getTypeConverter

      public TypeConverter getTypeConverter()
      Description copied from interface: EvaluationContext
      Get type converter 获取类型转换器
      Specified by:
      getTypeConverter in interface EvaluationContext
      Returns:
      the type converter | 类型转换器
    • getSandbox

      public Sandbox getSandbox()
      Description copied from interface: EvaluationContext
      Get security sandbox 获取安全沙箱
      Specified by:
      getSandbox in interface EvaluationContext
      Returns:
      the sandbox, or null if not configured | 沙箱,如果未配置则返回null
    • createChild

      public EvaluationContext createChild()
      Description copied from interface: EvaluationContext
      Create child context 创建子上下文

      Creates a child context that inherits from this context but can have its own variable bindings. Used for collection operations.

      创建继承自此上下文但可以有自己变量绑定的子上下文。用于集合操作。

      Specified by:
      createChild in interface EvaluationContext
      Returns:
      the child context | 子上下文
    • getParent

      public EvaluationContext 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

      public static ChainedContext of(EvaluationContext parent)
      Create chained context from parent 从父上下文创建链式上下文
      Parameters:
      parent - the parent context | 父上下文
      Returns:
      the chained context | 链式上下文
    • of

      public static ChainedContext of(EvaluationContext parent, Object rootObject)
      Create chained context from parent with root object 从父上下文和根对象创建链式上下文
      Parameters:
      parent - the parent context | 父上下文
      rootObject - the root object | 根对象
      Returns:
      the chained context | 链式上下文
    • builder

      public static ChainedContext.Builder builder(EvaluationContext parent)
      Create a builder for ChainedContext 创建 ChainedContext 的构建器
      Parameters:
      parent - the parent context | 父上下文
      Returns:
      the builder | 构建器