265
265
266
266
Apply the prefixes in the context `ctx` to the variable name `vn`.
267
267
"""
268
- function prefix (ctx:: PrefixContext{Prefix} , vn:: VarName{Sym} ) where {Prefix,Sym }
269
- return AbstractPPL. prefix (prefix (childcontext (ctx), vn), VarName {Symbol( Prefix) } ())
268
+ function prefix (ctx:: PrefixContext{Prefix} , vn:: VarName ) where {Prefix}
269
+ return AbstractPPL. prefix (prefix (childcontext (ctx), vn), VarName {Prefix} ())
270
270
end
271
271
function prefix (ctx:: AbstractContext , vn:: VarName )
272
272
return prefix (NodeTrait (ctx), ctx, vn)
@@ -351,6 +351,43 @@ NodeTrait(::ConditionContext) = IsParent()
351
351
childcontext (context:: ConditionContext ) = context. context
352
352
setchildcontext (parent:: ConditionContext , child) = ConditionContext (parent. values, child)
353
353
354
+ """
355
+ prefix_conditioned_variables(context::AbstractContext, prefix::VarName)
356
+
357
+ Prefix all the conditioned variables in a given context with `prefix`.
358
+
359
+ ```jldoctest
360
+ julia> using DynamicPPL: prefix_conditioned_variables, ConditionContext
361
+
362
+ julia> c1 = ConditionContext((a=1, ))
363
+ ConditionContext((a = 1,), DefaultContext())
364
+
365
+ julia> prefix_conditioned_variables(c1, @varname(y))
366
+ ConditionContext(Dict(y.a => 1), DefaultContext())
367
+ ```
368
+ """
369
+ function prefix_conditioned_variables (ctx:: ConditionContext , prefix:: VarName )
370
+ # Replace the prefix of the conditioned variables
371
+ vn_dict = to_varname_dict (ctx. values)
372
+ prefixed_vn_dict = Dict (
373
+ AbstractPPL. prefix (vn, prefix) => value for (vn, value) in vn_dict
374
+ )
375
+ # Prefix the child context as well
376
+ prefixed_child_ctx = prefix_conditioned_variables (childcontext (ctx), prefix)
377
+ return ConditionContext (prefixed_vn_dict, prefixed_child_ctx)
378
+ end
379
+ function prefix_conditioned_variables (c:: AbstractContext , prefix:: VarName )
380
+ return prefix_conditioned_variables (
381
+ NodeTrait (prefix_conditioned_variables, c), c, prefix
382
+ )
383
+ end
384
+ prefix_conditioned_variables (:: IsLeaf , context:: AbstractContext , prefix:: VarName ) = context
385
+ function prefix_conditioned_variables (:: IsParent , context:: AbstractContext , prefix:: VarName )
386
+ return setchildcontext (
387
+ context, prefix_conditioned_variables (childcontext (context), prefix)
388
+ )
389
+ end
390
+
354
391
"""
355
392
hasconditioned(context::AbstractContext, vn::VarName)
356
393
@@ -370,7 +407,9 @@ Return value of `vn` in `context`.
370
407
function getconditioned (context:: AbstractContext , vn:: VarName )
371
408
return error (" context $(context) does not contain value for $vn " )
372
409
end
373
- getconditioned (context:: ConditionContext , vn:: VarName ) = getvalue (context. values, vn)
410
+ function getconditioned (context:: ConditionContext , vn:: VarName )
411
+ return getvalue (context. values, vn)
412
+ end
374
413
375
414
"""
376
415
hasconditioned_nested(context, vn)
@@ -387,8 +426,10 @@ hasconditioned_nested(::IsLeaf, context, vn) = hasconditioned(context, vn)
387
426
function hasconditioned_nested (:: IsParent , context, vn)
388
427
return hasconditioned (context, vn) || hasconditioned_nested (childcontext (context), vn)
389
428
end
390
- function hasconditioned_nested (context:: PrefixContext , vn)
391
- return hasconditioned_nested (childcontext (context), prefix (context, vn))
429
+ function hasconditioned_nested (context:: PrefixContext{Prefix} , vn) where {Prefix}
430
+ return hasconditioned_nested (
431
+ prefix_conditioned_variables (childcontext (context), VarName {Prefix} ()), vn
432
+ )
392
433
end
393
434
394
435
"""
405
446
function getconditioned_nested (:: IsLeaf , context, vn)
406
447
return error (" context $(context) does not contain value for $vn " )
407
448
end
408
- function getconditioned_nested (context:: PrefixContext , vn)
409
- return getconditioned_nested (childcontext (context), prefix (context, vn))
449
+ function getconditioned_nested (context:: PrefixContext{Prefix} , vn) where {Prefix}
450
+ return getconditioned_nested (
451
+ prefix_conditioned_variables (childcontext (context), VarName {Prefix} ()), vn
452
+ )
410
453
end
411
454
function getconditioned_nested (:: IsParent , context, vn)
412
455
return if hasconditioned (context, vn)
0 commit comments