Skip to content

Commit c0fb433

Browse files
authored
Merge pull request #220 from ReactiveBayes/new-print-of-constraints
New print of constraints
2 parents 59ec7ac + b5d7ea7 commit c0fb433

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/plugins/meta/meta_engine.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ struct MetaSpecification
3939
submodel_meta::Vector
4040
end
4141

42+
function Base.show(io::IO, c::MetaSpecification)
43+
indent = get(io, :indent, 1)
44+
head = get(io, :head, true)
45+
if head
46+
print(io, "Meta: \n")
47+
else
48+
print(io, "\n")
49+
end
50+
for meta in getmetaobjects(c)
51+
print(io, " "^indent)
52+
print(io, meta)
53+
print(io, "\n")
54+
end
55+
for submodel in getsubmodelmeta(c)
56+
print(io, " "^indent)
57+
print(io, submodel)
58+
print(io, "\n")
59+
end
60+
end
61+
4262
getmetaobjects(m::MetaSpecification) = m.meta_objects
4363
getsubmodelmeta(m::MetaSpecification) = m.submodel_meta
4464
getspecificsubmodelmeta(m::MetaSpecification) = filter(m -> is_specificsubmodelmeta(m), getsubmodelmeta(m))
@@ -76,6 +96,16 @@ getkey(m::GeneralSubModelMeta) = getsubmodel(m)
7696

7797
const SubModelMeta = Union{GeneralSubModelMeta, SpecificSubModelMeta}
7898

99+
function Base.show(io::IO, constraint::SubModelMeta)
100+
print(
101+
IOContext(io, (:indent => get(io, :indent, 0) + 2), (:head => false)),
102+
"Meta for submodel ",
103+
getsubmodel(constraint),
104+
" = ",
105+
getmetaobjects(constraint)
106+
)
107+
end
108+
79109
MetaSpecification() = MetaSpecification(Vector{MetaObject}(), Vector{SubModelMeta}())
80110

81111
Base.push!(m::MetaSpecification, o::MetaObject) = push!(m.meta_objects, o)

src/plugins/variational_constraints/variational_constraints_engine.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ function Base.show(io::IO, constraint::FactorizationConstraint{V, F} where {V, F
214214
print(io, join(getconstraint(constraint), ""))
215215
end
216216

217+
function Base.show(io::IO, constraint::FactorizationConstraint{V, F} where {V, F})
218+
print(io, "q(")
219+
print(io, join(getvariables(constraint), ", "))
220+
print(io, ") = ")
221+
print(io, getconstraint(constraint))
222+
end
223+
217224
"""
218225
A `MarginalFormConstraint` represents a single functional form constraint in a variational marginal constraint specification. We use type parametrization
219226
to dispatch on different types of constraints, for example `q(x, y) :: MvNormal` should be treated different from `q(x) :: Normal`.
@@ -263,7 +270,6 @@ end
263270
GeneralSubModelConstraints(fform::Function) = GeneralSubModelConstraints(fform, Constraints())
264271

265272
fform(c::GeneralSubModelConstraints) = c.fform
266-
Base.show(io::IO, constraint::GeneralSubModelConstraints) = print(io, "q(", getsubmodel(constraint), ") :: ", getconstraint(constraint))
267273

268274
getsubmodel(c::GeneralSubModelConstraints) = c.fform
269275
getconstraint(c::GeneralSubModelConstraints) = c.constraints
@@ -282,7 +288,15 @@ end
282288

283289
SpecificSubModelConstraints(submodel::FactorID) = SpecificSubModelConstraints(submodel, Constraints())
284290

285-
Base.show(io::IO, constraint::SpecificSubModelConstraints) = print(io, "q(", getsubmodel(constraint), ") :: ", getconstraint(constraint))
291+
function Base.show(io::IO, constraint::Union{SpecificSubModelConstraints, GeneralSubModelConstraints})
292+
print(
293+
IOContext(io, (:indent => get(io, :indent, 0) + 2), (:head => false)),
294+
"q(",
295+
getsubmodel(constraint),
296+
") = ",
297+
getconstraint(constraint)
298+
)
299+
end
286300

287301
getsubmodel(c::SpecificSubModelConstraints) = c.submodel
288302
getconstraint(c::SpecificSubModelConstraints) = c.constraints
@@ -325,9 +339,15 @@ Constraints(constraints::Vector) = begin
325339
end
326340

327341
function Base.show(io::IO, c::Constraints)
328-
print(io, "Constraints: \n")
342+
indent = get(io, :indent, 1)
343+
head = get(io, :head, true)
344+
if head
345+
print(io, "Constraints: \n")
346+
else
347+
print(io, "\n")
348+
end
329349
for constraint in getconstraints(c)
330-
print(io, " ")
350+
print(io, " "^indent)
331351
print(io, constraint)
332352
print(io, "\n")
333353
end

0 commit comments

Comments
 (0)