class ExecutionContext.NodeContext
Provides an execution context for a single given node that can resolve bindings for values stored by ancestor nodes, set its own values, and safely interpolate user-provided strings.
def context = new ExecutionContext(new ExecutionGraph([
["a", "b", "c", "d", "e", "f"],
["x", "d", "y", "f"],
]))
context.ofNode("a")["foo"] = "afoo"
context.ofNode("a")["bar"] = "abar"
context.ofNode("b")["foo"] = "bfoo"
context.ofNode("x")["bar"] = "xbar"
assert context.ofNode("c").binding("a", "foo") == "afoo"
assert context.ofNode("c").binding("a", "bar") == "abar"
assert context.ofNode("c").binding("b", "foo") == "bfoo"
assert context.ofNode("c").binding("x", "bar") == null
assert context.ofNode("e").binding("a", "foo") == "afoo"
assert context.ofNode("e").binding("a", "bar") == "abar"
assert context.ofNode("e").binding("b", "foo") == "bfoo"
assert context.ofNode("e").binding("x", "bar") == "xbar"
assert (context.ofNode("c") % 'w-t-${a.foo} ${b.foo}') == "w-t-afoo bfoo"
assert (context.ofNode("c") % 'w-t-${a.bar}') == "w-t-abar"
assert (context.ofNode("x") % 'w-t-${x.bar}') == 'w-t-${x.bar}'
Constructor and description |
---|
ExecutionContext.NodeContext
(java.lang.Object contextNode, java.lang.Object contextAncestors, java.lang.Object initialBindings = [:]) |
Type Params | Return Type | Name and description |
---|---|---|
|
void |
bind(java.lang.String key, java.lang.Object value) Binds a value to a name in the globals store under the node's namespace. |
|
java.lang.Object |
binding(java.lang.String key) Retrieves a value previously bound using bind() to this node's context. |
|
java.lang.Object |
binding(java.lang.Object ancestorNode, java.lang.String key) Retrieves a value previously bound using bind() under the given ancestor node's namespace and name. |
|
NodeContext |
dive(java.util.Map bindings) Creates and returns a new anonymous NodeContext for use in representing child context frames. |
|
java.lang.Object |
getAll() Returns read-only version of the current bindings for this node only. |
|
java.util.Map |
getAll(java.lang.String key) Returns all objects bound to the given name under any node namespace, as well as the node under which it is found. |
|
java.lang.Object |
getAt(java.lang.String key) Operator alias for binding(java.lang.String) or, if a "namespace.key" is given, binding(def, java.lang.String). |
|
java.lang.Object |
interpolate(java.lang.Object obj) Interpolates the given object by evaluating all list comprehensions and substituting all variable expressions with previously bound values. |
|
java.lang.Object |
mod(java.lang.Object str) Operator alias for interpolate(). |
|
void |
putAt(java.lang.String key, java.lang.Object value) Operator alias for bind(). |
Methods inherited from class | Name |
---|---|
class java.lang.Object |
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Binds a value to a name in the globals store under the node's namespace. The value may later be retrieved by any descendent node's context using binding().
Retrieves a value previously bound using bind() to this node's context. If the given key is not found under this node's namespace, a NameNotFoundException is thrown.
Retrieves a value previously bound using bind() under the given ancestor node's namespace and name.
Creates and returns a new anonymous NodeContext for use in representing child context frames. For example, a list comprehension needs a new context in which to define its bound variable but can't modify its current node's context without potentially overwriting currently bound values.
Returns read-only version of the current bindings for this node only. This is only intended for read-only/reporting purposes.
Returns all objects bound to the given name under any node namespace, as well as the node under which it is found. This should only be used at the end of an execution graph.
Operator alias for binding(java.lang.String) or, if a "namespace.key" is given, binding(def, java.lang.String).
Interpolates the given object by evaluating all list comprehensions and substituting all variable expressions with previously bound values.
context.ofNode("a")["foo"] = "afoo"
context.ofNode("b")["foo"] = "bfoo"
assert (context.ofNode("b") % 'w-t-${.foo}') == "w-t-bfoo"
assert (context.ofNode("b") % 'w-t-${a.foo}') == "w-t-afoo"
context.ofNode("a")["foo"] = ""
assert (context.ofNode("a") % 'w-t-${.foo|frak}') == "w-t-frak"
assert (context.ofNode("a") % 'w-t-${.bar|frak}') == "w-t-frak"
$yield
) is given its own context
namespace containing each token of $in
bound to the name
given as $each
. The default $delimiter
is a
newline but can be any string.
context.ofNode("a")["foo"] = "bar|baz"
def fooList = [
$each: 'item',
$in: '${a.foo}|qux',
$delimiter: '|',
$yield: 'w-t-${.item}',
]
assert context % fooList == [
'w-t-bar',
'w-t-baz',
'w-t-qux',
]
$merge
def fooList = [
$merge: ['foo', 'bar'],
$with: ['baz', 'qux'],
]
assert context % fooList == [
'foo',
'bar',
'baz',
'qux',
]
def fooMap = [
$merge: [foo: 'x', bar: 'y'],
$with: [bar: 'z'],
]
assert context % fooMap == [
foo: 'x',
bar: 'z',
]
Operator alias for interpolate(). Use the modulo (%, percent) operator do variable expansion.
Operator alias for bind().
Groovy Documentation