Skip to content

Commit

Permalink
Merge pull request #220 from ReactiveBayes/new-print-of-constraints
Browse files Browse the repository at this point in the history
New print of constraints
  • Loading branch information
wouterwln authored Apr 12, 2024
2 parents 59ec7ac + b5d7ea7 commit c0fb433
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
30 changes: 30 additions & 0 deletions src/plugins/meta/meta_engine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ struct MetaSpecification
submodel_meta::Vector
end

function Base.show(io::IO, c::MetaSpecification)
indent = get(io, :indent, 1)
head = get(io, :head, true)
if head
print(io, "Meta: \n")
else
print(io, "\n")
end
for meta in getmetaobjects(c)
print(io, " "^indent)
print(io, meta)
print(io, "\n")
end
for submodel in getsubmodelmeta(c)
print(io, " "^indent)
print(io, submodel)
print(io, "\n")
end
end

getmetaobjects(m::MetaSpecification) = m.meta_objects
getsubmodelmeta(m::MetaSpecification) = m.submodel_meta
getspecificsubmodelmeta(m::MetaSpecification) = filter(m -> is_specificsubmodelmeta(m), getsubmodelmeta(m))
Expand Down Expand Up @@ -76,6 +96,16 @@ getkey(m::GeneralSubModelMeta) = getsubmodel(m)

const SubModelMeta = Union{GeneralSubModelMeta, SpecificSubModelMeta}

function Base.show(io::IO, constraint::SubModelMeta)
print(
IOContext(io, (:indent => get(io, :indent, 0) + 2), (:head => false)),
"Meta for submodel ",
getsubmodel(constraint),
" = ",
getmetaobjects(constraint)
)
end

MetaSpecification() = MetaSpecification(Vector{MetaObject}(), Vector{SubModelMeta}())

Base.push!(m::MetaSpecification, o::MetaObject) = push!(m.meta_objects, o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ function Base.show(io::IO, constraint::FactorizationConstraint{V, F} where {V, F
print(io, join(getconstraint(constraint), ""))
end

function Base.show(io::IO, constraint::FactorizationConstraint{V, F} where {V, F})
print(io, "q(")
print(io, join(getvariables(constraint), ", "))
print(io, ") = ")
print(io, getconstraint(constraint))
end

"""
A `MarginalFormConstraint` represents a single functional form constraint in a variational marginal constraint specification. We use type parametrization
to dispatch on different types of constraints, for example `q(x, y) :: MvNormal` should be treated different from `q(x) :: Normal`.
Expand Down Expand Up @@ -263,7 +270,6 @@ end
GeneralSubModelConstraints(fform::Function) = GeneralSubModelConstraints(fform, Constraints())

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

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

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

Base.show(io::IO, constraint::SpecificSubModelConstraints) = print(io, "q(", getsubmodel(constraint), ") :: ", getconstraint(constraint))
function Base.show(io::IO, constraint::Union{SpecificSubModelConstraints, GeneralSubModelConstraints})
print(
IOContext(io, (:indent => get(io, :indent, 0) + 2), (:head => false)),
"q(",
getsubmodel(constraint),
") = ",
getconstraint(constraint)
)
end

getsubmodel(c::SpecificSubModelConstraints) = c.submodel
getconstraint(c::SpecificSubModelConstraints) = c.constraints
Expand Down Expand Up @@ -325,9 +339,15 @@ Constraints(constraints::Vector) = begin
end

function Base.show(io::IO, c::Constraints)
print(io, "Constraints: \n")
indent = get(io, :indent, 1)
head = get(io, :head, true)
if head
print(io, "Constraints: \n")
else
print(io, "\n")
end
for constraint in getconstraints(c)
print(io, " ")
print(io, " "^indent)
print(io, constraint)
print(io, "\n")
end
Expand Down

2 comments on commit c0fb433

@bvdmitri
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/104788

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.0.0 -m "<description of version>" c0fb43342dff4ddf8003a45e8a39cde01b1477ac
git push origin v4.0.0

Please sign in to comment.