@@ -375,66 +375,6 @@ NodeTrait(::ConditionContext) = IsParent()
375375childcontext (context:: ConditionContext ) = context. context
376376setchildcontext (parent:: ConditionContext , child) = ConditionContext (parent. values, child)
377377
378- """
379- collapse_prefix_and_condition(context::AbstractContext)
380-
381- Apply `PrefixContext`s to any conditioned values inside them, and remove
382- the `PrefixContext`s from the context stack.
383-
384- ```jldoctest
385- julia> using DynamicPPL: collapse_prefix_and_condition
386-
387- julia> c1 = PrefixContext({:a}(ConditionContext((x=1, )))
388- ```
389- """
390- function collapse_prefix_and_condition (context:: PrefixContext{Prefix} ) where {Prefix}
391- # Collapse the child context (thus applying any inner prefixes first)
392- collapsed = collapse_prefix_and_condition (childcontext (context))
393- # Prefix any conditioned variables with the current prefix
394- # Note: prefix_conditioned_variables is O(N) in the depth of the context stack.
395- # So is this function. In the worst case scenario, this is O(N^2) in the
396- # depth of the context stack.
397- return prefix_conditioned_variables (collapsed, VarName {Prefix} ())
398- end
399- collapse_prefix_and_condition (context:: AbstractContext ) = context
400-
401- """
402- prefix_conditioned_variables(context::AbstractContext, prefix::VarName)
403-
404- Prefix all the conditioned variables in a given context with `prefix`.
405-
406- ```jldoctest
407- julia> using DynamicPPL: prefix_conditioned_variables, ConditionContext
408-
409- julia> c1 = ConditionContext((a=1, ))
410- ConditionContext((a = 1,), DefaultContext())
411-
412- julia> prefix_conditioned_variables(c1, @varname(y))
413- ConditionContext(Dict(y.a => 1), DefaultContext())
414- ```
415- """
416- function prefix_conditioned_variables (ctx:: ConditionContext , prefix:: VarName )
417- # Replace the prefix of the conditioned variables
418- vn_dict = to_varname_dict (ctx. values)
419- prefixed_vn_dict = Dict (
420- AbstractPPL. prefix (vn, prefix) => value for (vn, value) in vn_dict
421- )
422- # Prefix the child context as well
423- prefixed_child_ctx = prefix_conditioned_variables (childcontext (ctx), prefix)
424- return ConditionContext (prefixed_vn_dict, prefixed_child_ctx)
425- end
426- function prefix_conditioned_variables (c:: AbstractContext , prefix:: VarName )
427- return prefix_conditioned_variables (
428- NodeTrait (prefix_conditioned_variables, c), c, prefix
429- )
430- end
431- prefix_conditioned_variables (:: IsLeaf , context:: AbstractContext , prefix:: VarName ) = context
432- function prefix_conditioned_variables (:: IsParent , context:: AbstractContext , prefix:: VarName )
433- return setchildcontext (
434- context, prefix_conditioned_variables (childcontext (context), prefix)
435- )
436- end
437-
438378"""
439379 hasconditioned(context::AbstractContext, vn::VarName)
440380
@@ -474,7 +414,7 @@ function hasconditioned_nested(::IsParent, context, vn)
474414 return hasconditioned (context, vn) || hasconditioned_nested (childcontext (context), vn)
475415end
476416function hasconditioned_nested (context:: PrefixContext{Prefix} , vn) where {Prefix}
477- return hasconditioned_nested (collapse_prefix_and_condition (context), vn)
417+ return hasconditioned_nested (collapse_prefix_stack (context), vn)
478418end
479419
480420"""
@@ -492,7 +432,7 @@ function getconditioned_nested(::IsLeaf, context, vn)
492432 return error (" context $(context) does not contain value for $vn " )
493433end
494434function getconditioned_nested (context:: PrefixContext{Prefix} , vn) where {Prefix}
495- return getconditioned_nested (collapse_prefix_and_condition (context), vn)
435+ return getconditioned_nested (collapse_prefix_stack (context), vn)
496436end
497437function getconditioned_nested (:: IsParent , context, vn)
498438 return if hasconditioned (context, vn)
@@ -563,9 +503,7 @@ function conditioned(context::ConditionContext)
563503 return _merge (context. values, conditioned (childcontext (context)))
564504end
565505function conditioned (context:: PrefixContext{Prefix} ) where {Prefix}
566- return conditioned (
567- prefix_conditioned_variables (childcontext (context), VarName {Prefix} ())
568- )
506+ return conditioned (collapse_prefix_stack (context))
569507end
570508
571509struct FixedContext{Values,Ctx<: AbstractContext } <: AbstractContext
@@ -630,7 +568,7 @@ function hasfixed_nested(::IsParent, context, vn)
630568 return hasfixed (context, vn) || hasfixed_nested (childcontext (context), vn)
631569end
632570function hasfixed_nested (context:: PrefixContext , vn)
633- return hasfixed_nested (childcontext (context), prefix (context, vn) )
571+ return hasfixed_nested (collapse_prefix_stack (context), vn )
634572end
635573
636574"""
@@ -648,7 +586,7 @@ function getfixed_nested(::IsLeaf, context, vn)
648586 return error (" context $(context) does not contain value for $vn " )
649587end
650588function getfixed_nested (context:: PrefixContext , vn)
651- return getfixed_nested (childcontext (context), prefix (context, vn) )
589+ return getfixed_nested (collapse_prefix_stack (context), vn )
652590end
653591function getfixed_nested (:: IsParent , context, vn)
654592 return if hasfixed (context, vn)
@@ -743,3 +681,80 @@ function fixed(context::FixedContext)
743681 # precedence over decendants of `context`.
744682 return _merge (context. values, fixed (childcontext (context)))
745683end
684+
685+ """
686+ collapse_prefix_stack(context::AbstractContext)
687+
688+ Apply `PrefixContext`s to any conditioned or fixed values inside them, and remove
689+ the `PrefixContext`s from the context stack.
690+
691+ ```jldoctest
692+ julia> using DynamicPPL: collapse_prefix_stack
693+
694+ julia> c1 = PrefixContext({:a}(ConditionContext((x=1, )))
695+ ```
696+ """
697+ function collapse_prefix_stack (context:: PrefixContext{Prefix} ) where {Prefix}
698+ # Collapse the child context (thus applying any inner prefixes first)
699+ collapsed = collapse_prefix_stack (childcontext (context))
700+ # Prefix any conditioned variables with the current prefix
701+ # Note: prefix_conditioned_variables is O(N) in the depth of the context stack.
702+ # So is this function. In the worst case scenario, this is O(N^2) in the
703+ # depth of the context stack.
704+ return prefix_cond_and_fixed_variables (collapsed, VarName {Prefix} ())
705+ end
706+ collapse_prefix_stack (context:: AbstractContext ) = context
707+
708+ """
709+ prefix_cond_and_fixed_variables(context::AbstractContext, prefix::VarName)
710+
711+ Prefix all the conditioned and fixed variables in a given context with a single
712+ `prefix`.
713+
714+ ```jldoctest
715+ julia> using DynamicPPL: prefix_cond_and_fixed_variables, ConditionContext
716+
717+ julia> c1 = ConditionContext((a=1, ))
718+ ConditionContext((a = 1,), DefaultContext())
719+
720+ julia> prefix_cond_and_fixed_variables(c1, @varname(y))
721+ ConditionContext(Dict(y.a => 1), DefaultContext())
722+ ```
723+ """
724+ function prefix_cond_and_fixed_variables (ctx:: ConditionContext , prefix:: VarName )
725+ # Replace the prefix of the conditioned variables
726+ vn_dict = to_varname_dict (ctx. values)
727+ prefixed_vn_dict = Dict (
728+ AbstractPPL. prefix (vn, prefix) => value for (vn, value) in vn_dict
729+ )
730+ # Prefix the child context as well
731+ prefixed_child_ctx = prefix_cond_and_fixed_variables (childcontext (ctx), prefix)
732+ return ConditionContext (prefixed_vn_dict, prefixed_child_ctx)
733+ end
734+ function prefix_cond_and_fixed_variables (ctx:: FixedContext , prefix:: VarName )
735+ # Replace the prefix of the conditioned variables
736+ vn_dict = to_varname_dict (ctx. values)
737+ prefixed_vn_dict = Dict (
738+ AbstractPPL. prefix (vn, prefix) => value for (vn, value) in vn_dict
739+ )
740+ # Prefix the child context as well
741+ prefixed_child_ctx = prefix_cond_and_fixed_variables (childcontext (ctx), prefix)
742+ return FixedContext (prefixed_vn_dict, prefixed_child_ctx)
743+ end
744+ function prefix_cond_and_fixed_variables (c:: AbstractContext , prefix:: VarName )
745+ return prefix_cond_and_fixed_variables (
746+ NodeTrait (prefix_cond_and_fixed_variables, c), c, prefix
747+ )
748+ end
749+ function prefix_cond_and_fixed_variables (
750+ :: IsLeaf , context:: AbstractContext , prefix:: VarName
751+ )
752+ return context
753+ end
754+ function prefix_cond_and_fixed_variables (
755+ :: IsParent , context:: AbstractContext , prefix:: VarName
756+ )
757+ return setchildcontext (
758+ context, prefix_cond_and_fixed_variables (childcontext (context), prefix)
759+ )
760+ end
0 commit comments