|
| 1 | +module MTKDeepDiffsExt |
| 2 | + |
| 3 | +using DeepDiffs, ModelingToolkit |
| 4 | +using ModelingToolkit.BipartiteGraphs: Label, BipartiteAdjacencyList, unassigned |
| 5 | +using ModelingToolkit.SystemStructures: SystemStructure, MatchedSystemStructure, |
| 6 | + SystemStructurePrintMatrix, HighlightInt |
| 7 | + |
| 8 | +struct HighlightIntDiff |
| 9 | + new::HighlightInt |
| 10 | + old::HighlightInt |
| 11 | +end |
| 12 | + |
| 13 | +function Base.show(io::IO, d::HighlightIntDiff) |
| 14 | + p_color = d.new.highlight |
| 15 | + (d.new.match && !d.old.match) && (p_color = :light_green) |
| 16 | + (!d.new.match && d.old.match) && (p_color = :light_red) |
| 17 | + |
| 18 | + (d.new.match || d.old.match) && printstyled(io, "(", color = p_color) |
| 19 | + if d.new.i != d.old.i |
| 20 | + Base.show(io, HighlightInt(d.old.i, :light_red, d.old.match)) |
| 21 | + print(io, " ") |
| 22 | + Base.show(io, HighlightInt(d.new.i, :light_green, d.new.match)) |
| 23 | + else |
| 24 | + Base.show(io, HighlightInt(d.new.i, d.new.highlight, false)) |
| 25 | + end |
| 26 | + (d.new.match || d.old.match) && printstyled(io, ")", color = p_color) |
| 27 | +end |
| 28 | + |
| 29 | +struct BipartiteAdjacencyListDiff |
| 30 | + new::BipartiteAdjacencyList |
| 31 | + old::BipartiteAdjacencyList |
| 32 | +end |
| 33 | + |
| 34 | +function Base.show(io::IO, l::BipartiteAdjacencyListDiff) |
| 35 | + print(io, |
| 36 | + LabelDiff(Label(l.new.match === true ? "∫ " : ""), |
| 37 | + Label(l.old.match === true ? "∫ " : ""))) |
| 38 | + (l.new.match !== true && l.old.match !== true) && print(io, " ") |
| 39 | + |
| 40 | + new_nonempty = isnothing(l.new.u) ? nothing : !isempty(l.new.u) |
| 41 | + old_nonempty = isnothing(l.old.u) ? nothing : !isempty(l.old.u) |
| 42 | + if new_nonempty === true && old_nonempty === true |
| 43 | + if (!isempty(setdiff(l.new.highligh_u, l.new.u)) || |
| 44 | + !isempty(setdiff(l.old.highligh_u, l.old.u))) |
| 45 | + throw(ArgumentError("The provided `highligh_u` must be a sub-graph of `u`.")) |
| 46 | + end |
| 47 | + |
| 48 | + new_items = Dict(i => HighlightInt(i, :nothing, i === l.new.match) for i in l.new.u) |
| 49 | + old_items = Dict(i => HighlightInt(i, :nothing, i === l.old.match) for i in l.old.u) |
| 50 | + |
| 51 | + highlighted = union(map(intersect(l.new.u, l.old.u)) do i |
| 52 | + HighlightIntDiff(new_items[i], old_items[i]) |
| 53 | + end, |
| 54 | + map(setdiff(l.new.u, l.old.u)) do i |
| 55 | + HighlightInt(new_items[i].i, :light_green, |
| 56 | + new_items[i].match) |
| 57 | + end, |
| 58 | + map(setdiff(l.old.u, l.new.u)) do i |
| 59 | + HighlightInt(old_items[i].i, :light_red, |
| 60 | + old_items[i].match) |
| 61 | + end) |
| 62 | + print(IOContext(io, :typeinfo => typeof(highlighted)), highlighted) |
| 63 | + elseif new_nonempty === true |
| 64 | + printstyled(io, map(l.new.u) do i |
| 65 | + HighlightInt(i, :nothing, i === l.new.match) |
| 66 | + end, color = :light_green) |
| 67 | + elseif old_nonempty === true |
| 68 | + printstyled(io, map(l.old.u) do i |
| 69 | + HighlightInt(i, :nothing, i === l.old.match) |
| 70 | + end, color = :light_red) |
| 71 | + elseif old_nonempty !== nothing || new_nonempty !== nothing |
| 72 | + print(io, |
| 73 | + LabelDiff(Label(new_nonempty === false ? "∅" : "", :light_black), |
| 74 | + Label(old_nonempty === false ? "∅" : "", :light_black))) |
| 75 | + else |
| 76 | + printstyled(io, '⋅', color = :light_black) |
| 77 | + end |
| 78 | +end |
| 79 | + |
| 80 | +struct LabelDiff |
| 81 | + new::Label |
| 82 | + old::Label |
| 83 | +end |
| 84 | +function Base.show(io::IO, l::LabelDiff) |
| 85 | + if l.new != l.old |
| 86 | + printstyled(io, l.old.s, color = :light_red) |
| 87 | + length(l.new.s) != 0 && length(l.old.s) != 0 && print(io, " ") |
| 88 | + printstyled(io, l.new.s, color = :light_green) |
| 89 | + else |
| 90 | + print(io, l.new) |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +struct SystemStructureDiffPrintMatrix <: |
| 95 | + AbstractMatrix{Union{LabelDiff, BipartiteAdjacencyListDiff}} |
| 96 | + new::SystemStructurePrintMatrix |
| 97 | + old::SystemStructurePrintMatrix |
| 98 | +end |
| 99 | + |
| 100 | +function DeepDiffs.deepdiff(old::Union{MatchedSystemStructure, SystemStructure}, |
| 101 | + new::Union{MatchedSystemStructure, SystemStructure}) |
| 102 | + new_sspm = SystemStructurePrintMatrix(new) |
| 103 | + old_sspm = SystemStructurePrintMatrix(old) |
| 104 | + Base.print_matrix(stdout, SystemStructureDiffPrintMatrix(new_sspm, old_sspm)) |
| 105 | +end |
| 106 | + |
| 107 | +function Base.size(ssdpm::SystemStructureDiffPrintMatrix) |
| 108 | + max.(Base.size(ssdpm.new), Base.size(ssdpm.old)) |
| 109 | +end |
| 110 | + |
| 111 | +function Base.getindex(ssdpm::SystemStructureDiffPrintMatrix, i::Integer, j::Integer) |
| 112 | + checkbounds(ssdpm, i, j) |
| 113 | + if i > 1 && (j == 3 || j == 7) |
| 114 | + old = new = BipartiteAdjacencyList(nothing, nothing, unassigned) |
| 115 | + (i <= size(ssdpm.new, 1)) && (new = ssdpm.new[i, j]) |
| 116 | + (i <= size(ssdpm.old, 1)) && (old = ssdpm.old[i, j]) |
| 117 | + BipartiteAdjacencyListDiff(new, old) |
| 118 | + else |
| 119 | + old = new = Label("") |
| 120 | + (i <= size(ssdpm.new, 1)) && (new = ssdpm.new[i, j]) |
| 121 | + (i <= size(ssdpm.old, 1)) && (old = ssdpm.old[i, j]) |
| 122 | + LabelDiff(new, old) |
| 123 | + end |
| 124 | +end |
| 125 | + |
| 126 | +end # module |
0 commit comments