Skip to content

Commit a4bc034

Browse files
refactor: add pretty printing for connection graph utilities
1 parent b295cfe commit a4bc034

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/systems/connectiongraph.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ function Base.:(==)(a::ConnectionVertex, b::ConnectionVertex)
101101
return true
102102
end
103103

104+
function Base.show(io::IO, vert::ConnectionVertex)
105+
for name in @view(vert.name[1:(end - 1)])
106+
print(io, name, ".")
107+
end
108+
print(io, vert.name[end], "::", vert.isouter ? "outer" : "inner")
109+
end
110+
104111
"""
105112
$(TYPEDEF)
106113
@@ -138,6 +145,33 @@ function ConnectionGraph()
138145
return ConnectionGraph(Dict{ConnectionVertex, Int}(), ConnectionVertex[], graph)
139146
end
140147

148+
function Base.show(io::IO, graph::ConnectionGraph)
149+
printstyled(io, get(io, :cgraph_name, "ConnectionGraph"); color = :blue, bold = true)
150+
println(io, " with ", length(graph.labels),
151+
" vertices and ", nsrcs(graph.graph), " hyperedges")
152+
compact = get(io, :compact, false)
153+
for edge_i in 𝑠vertices(graph.graph)
154+
if compact && edge_i > 5
155+
println(io, "")
156+
break
157+
end
158+
edge_idxs = 𝑠neighbors(graph.graph, edge_i)
159+
type = graph.invmap[edge_idxs[1]].type
160+
if type <: Union{InputVar, OutputVar}
161+
type = "Causal"
162+
elseif type == Equality
163+
# otherwise it prints `ModelingToolkit.Equality`
164+
type = "Equality"
165+
end
166+
printstyled(io, " ", type; bold = true, color = :yellow)
167+
print(io, "<")
168+
for vi in @view(edge_idxs[1:(end - 1)])
169+
print(io, graph.invmap[vi], ", ")
170+
end
171+
println(io, graph.invmap[edge_idxs[end]], ">")
172+
end
173+
end
174+
141175
"""
142176
$(TYPEDSIGNATURES)
143177
@@ -208,6 +242,18 @@ Create an empty `ConnectionState` with empty graphs.
208242
"""
209243
ConnectionState() = ConnectionState(ConnectionGraph(), ConnectionGraph())
210244

245+
function Base.show(io::IO, state::AbstractConnectionState)
246+
printstyled(io, typeof(state); bold = true, color = :green)
247+
println(io, " comprising of")
248+
ctx1 = IOContext(io, :cgraph_name => "Connection Network", :compact => true)
249+
show(ctx1, state.connection_graph)
250+
println(io)
251+
println(io, "And")
252+
println(io)
253+
ctx2 = IOContext(io, :cgraph_name => "Domain Network", :compact => true)
254+
show(ctx2, state.domain_connection_graph)
255+
end
256+
211257
"""
212258
$(TYPEDSIGNATURES)
213259

0 commit comments

Comments
 (0)