Skip to content

Commit 6780e9c

Browse files
committed
add show methods for componets with callbacks
1 parent 366f928 commit 6780e9c

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/show.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ function Base.show(io::IO, ::MIME"text/plain", @nospecialize(nw::Network))
2323
print(io, nw.layer.aggregator)
2424
# print(io, "\n ├─ vertex output dimension: $(nw.im.vdepth)")
2525
# print(io, "\n └─ edge output dimension: $(nw.im.edepth)")
26+
27+
Ncb = length(collect_callbackbatches(nw))
28+
if Ncb 1
29+
nvert = mapreduce(has_callback, +, nw.im.vertexm)
30+
nedge = mapreduce(has_callback, +, nw.im.edgem)
31+
_, setword = maybe_plural(Ncb, "callback set")
32+
_, vertword = maybe_plural(nvert, "vertex", "vertices")
33+
_, edgeword = maybe_plural(nedge, "edge")
34+
print(io, "\n$Ncb $setword across $nvert $vertword and $nedge $edgeword")
35+
end
2636
end
2737
end
2838

@@ -78,6 +88,17 @@ function print_states_params(io, @nospecialize(c::ComponentModel), styling)
7888
push!(info, styled"$num &ext in: &&$arr")
7989
end
8090

91+
if has_callback(c)
92+
cbs = get_callbacks(c)
93+
num, word = maybe_plural(length(cbs), "callback")
94+
str = ""
95+
str *= styled"$num &$word: &&$(shortrepr(first(cbs)))"
96+
for cb in Base.tail(cbs)
97+
str *= styled"\n&&&$(shortrepr(cb))"
98+
end
99+
push!(info, str)
100+
end
101+
81102
print_treelike(io, align_strings(info))
82103
end
83104
function _inout_string(@nospecialize(c::VertexModel), f, name)
@@ -329,3 +350,46 @@ function str_significant(x; sigdigits, phantom_minus=false)
329350
end
330351
formatted
331352
end
353+
354+
function Base.show(io::IO, ::MIME"text/plain", @nospecialize(cb::ComponentCallback))
355+
basename = readuntil(IOBuffer(repr(cb)), '{')
356+
print(io, basename)
357+
print(io, "(")
358+
_print_condsyms(io, cb)
359+
print(io, " affecting ")
360+
_print_affectsyms(io, cb)
361+
if cb isa VectorContinousComponentCallback
362+
print(io, ", len=", cb.len)
363+
end
364+
print(io, ")")
365+
end
366+
367+
function shortrepr(cb::ComponentCallback)
368+
io = IOBuffer()
369+
_print_condsyms(io, cb)
370+
print(io, " affecting ")
371+
_print_affectsyms(io, cb)
372+
String(take!(io))
373+
end
374+
375+
function _print_condsyms(io, @nospecialize(cb::ComponentCallback))
376+
print(io, "(")
377+
_print_syms(io, cb.condition.sym, true)
378+
_print_syms(io, cb.condition.psym, isempty(cb.condition.sym))
379+
print(io, ")")
380+
end
381+
function _print_affectsyms(io, @nospecialize(cb::ComponentCallback))
382+
print(io, "(")
383+
_print_syms(io, cb.affect.sym, true)
384+
_print_syms(io, cb.affect.psym, isempty(cb.affect.sym))
385+
print(io, ")")
386+
end
387+
function _print_syms(io, syms::Tuple, isfirst)
388+
for s in syms
389+
if !isfirst
390+
print(io, ", ")
391+
end
392+
print(io, ':', s)
393+
isfirst = false
394+
end
395+
end

test/callbacks_test.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ end
9494
@test maximum(abs.(tript - tref)) < 1e-5
9595
end
9696

97+
@testest "show functions for callbacks" begin
98+
nw = basenetwork()
99+
v = nw.im.vertexm[1]
100+
101+
cond = ComponentCondition(identity, [, ], [])
102+
affect = ComponentAffect(identity, [, ],[])
103+
cb = VectorContinousComponentCallback(cond, affect, 2)
104+
add_callback!(v, cb)
105+
display(v)
106+
cond = ComponentCondition(identity, [], [])
107+
affect = ComponentAffect(identity, [],[:M])
108+
cb2 = ContinousComponentCallback(cond, affect)
109+
add_callback!(v, cb2)
110+
display(v)
111+
display(nw)
112+
end
113+
97114
@testset "vector callbacks" begin
98115
nw = basenetwork()
99116
u0 = zeros(dim(nw))

0 commit comments

Comments
 (0)