Skip to content

Commit 106ae9b

Browse files
authored
Merge pull request #495 from ReactiveBayes/show-meta-suggestions
update rule error printing, adds meta spec
2 parents 4798769 + 9268c46 commit 106ae9b

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

src/rule.jl

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,23 +1167,36 @@ function Base.showerror(io::IO, error::RuleMethodError)
11671167
node_rules = filter(m -> ReactiveMP.get_node_from_rule_method(m) == spec_fform, methods(ReactiveMP.rule))
11681168
println(io, "Alternatively, consider re-specifying model using an existing rule:\n")
11691169

1170-
node_message_names = filter(x -> x != ["Nothing"], get_message_names_from_rule_method.(node_rules))
1171-
node_message_types = filter(!isempty, get_message_types_from_rule_method.(node_rules))
1172-
for (m_name, m_type) in zip(node_message_names, node_message_types)
1173-
message_input = [string("m_", n, "::", t) for (n, t) in zip(m_name, m_type)]
1174-
println(io, spec_fform, "(", join(message_input, ", "), ")")
1175-
end
1170+
for node_rule in node_rules
1171+
node_message_names = filter(x -> x != ["Nothing"], get_message_names_from_rule_method(node_rule))
1172+
node_message_types = filter(!isempty, get_message_types_from_rule_method(node_rule))
11761173

1177-
node_marginal_names = filter(x -> x != ["Nothing"], get_marginal_names_from_rule_method.(node_rules))
1178-
node_marginal_types = filter(!isempty, get_marginal_types_from_rule_method.(node_rules))
1179-
for (m_name, m_type) in zip(node_marginal_names, node_marginal_types)
1180-
marginal_input = [string("q_", n, "::", t) for (n, t) in zip(m_name, m_type)]
1181-
println(io, spec_fform, "(", join(marginal_input, ", "), ")")
1182-
end
1183-
if !isempty(node_marginal_names)
1184-
println(io, "\nNote that for marginal rules (i.e., involving q_*), the order of input types matters.")
1174+
node_marginal_names = filter(x -> x != ["Nothing"], get_marginal_names_from_rule_method(node_rule))
1175+
node_marginal_types = filter(!isempty, get_marginal_types_from_rule_method(node_rule))
1176+
1177+
node_meta = get_meta_from_rule_method(node_rule)
1178+
1179+
node_message_input = [string("m_", n, "::", t) for (n, t) in zip(node_message_names, node_message_types)]
1180+
node_marginal_input = [string("q_", n, "::", t) for (n, t) in zip(node_marginal_names, node_marginal_types)]
1181+
1182+
print(io, spec_fform)
1183+
print(io, "(")
1184+
join(io, node_message_input, ", ")
1185+
if !isempty(node_marginal_input)
1186+
print(io, ", ")
1187+
end
1188+
join(io, node_marginal_input, ", ")
1189+
print(io, ", ")
1190+
1191+
if node_meta !== "Nothing"
1192+
print(io, "meta::", node_meta)
1193+
end
1194+
1195+
print(io, ")")
1196+
println(io)
11851197
end
11861198

1199+
println(io, "\nNote that for rules involving `q_*`, the order of input types matters.")
11871200
else
11881201
println(io, "\n\n[WARN]: Non-standard rule layout found! Possible fix, define rule with the following arguments:\n")
11891202
println(io, "rule.fform: ", error.fform)

test/rule_tests.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,30 @@
607607
@test occursin("m_a::BayesBase.PointMass", output)
608608
@test occursin("q_a::BayesBase.PointMass", output)
609609
end
610+
611+
let
612+
struct MyCustomNode end
613+
614+
@node MyCustomNode Stochastic [out, a]
615+
616+
@rule MyCustomNode(:out, Marginalisation) (m_a::PointMass, q_a::PointMass, meta::Float64) = begin
617+
return 1
618+
end
619+
620+
err = ReactiveMP.RuleMethodError(
621+
MyCustomNode, Val{:out}(), Marginalisation(), nothing, nothing, Val{(:a,)}(), (Marginal(PointMass, false, false, nothing),), "meta", nothing, nothing
622+
)
623+
624+
io = IOBuffer()
625+
showerror(io, err)
626+
output = String(take!(io))
627+
628+
@test occursin("Alternatively, consider re-specifying model using an existing rule:", output)
629+
@test occursin("MyCustomNode", output)
630+
@test occursin("q_a::BayesBase.PointMass", output)
631+
@test occursin("MyCustomNode(m_a::BayesBase.PointMass, q_a::BayesBase.PointMass, meta::Float64)", output)
632+
@test occursin("meta::Float64", output)
633+
end
610634
end
611635

612636
@testset "marginalrule_method_error" begin

0 commit comments

Comments
 (0)