|
| 1 | +module MTKDeepDiffsExt |
| 2 | + |
| 3 | +using DeepDiffs, ModelingToolkit |
| 4 | +using ModelingToolkit.BipartiteGraphs: Label, BipartiteAdjacencyList, unassigned, |
| 5 | + HighlightInt |
| 6 | +using ModelingToolkit.SystemStructures: SystemStructure, MatchedSystemStructure, |
| 7 | + SystemStructurePrintMatrix |
| 8 | + |
| 9 | +""" |
| 10 | +A utility struct for displaying the difference between two HighlightInts. |
| 11 | +
|
| 12 | +# Example |
| 13 | +```julia |
| 14 | +using ModelingToolkit, DeepDiffs |
| 15 | +
|
| 16 | +old_i = HighlightInt(1, :default, true) |
| 17 | +new_i = HighlightInt(2, :default, false) |
| 18 | +diff = HighlightIntDiff(new_i, old_i) |
| 19 | +
|
| 20 | +show(diff) |
| 21 | +``` |
| 22 | +""" |
| 23 | +struct HighlightIntDiff |
| 24 | + new::HighlightInt |
| 25 | + old::HighlightInt |
| 26 | +end |
| 27 | + |
| 28 | +function Base.show(io::IO, d::HighlightIntDiff) |
| 29 | + p_color = d.new.highlight |
| 30 | + (d.new.match && !d.old.match) && (p_color = :light_green) |
| 31 | + (!d.new.match && d.old.match) && (p_color = :light_red) |
| 32 | + |
| 33 | + (d.new.match || d.old.match) && printstyled(io, "(", color = p_color) |
| 34 | + if d.new.i != d.old.i |
| 35 | + Base.show(io, HighlightInt(d.old.i, :light_red, d.old.match)) |
| 36 | + print(io, " ") |
| 37 | + Base.show(io, HighlightInt(d.new.i, :light_green, d.new.match)) |
| 38 | + else |
| 39 | + Base.show(io, HighlightInt(d.new.i, d.new.highlight, false)) |
| 40 | + end |
| 41 | + (d.new.match || d.old.match) && printstyled(io, ")", color = p_color) |
| 42 | +end |
| 43 | + |
| 44 | +""" |
| 45 | +A utility struct for displaying the difference between two |
| 46 | +BipartiteAdjacencyList's. |
| 47 | +
|
| 48 | +# Example |
| 49 | +```julia |
| 50 | +using ModelingToolkit, DeepDiffs |
| 51 | +
|
| 52 | +old = BipartiteAdjacencyList(...) |
| 53 | +new = BipartiteAdjacencyList(...) |
| 54 | +diff = BipartiteAdjacencyListDiff(new, old) |
| 55 | +
|
| 56 | +show(diff) |
| 57 | +``` |
| 58 | +""" |
| 59 | +struct BipartiteAdjacencyListDiff |
| 60 | + new::BipartiteAdjacencyList |
| 61 | + old::BipartiteAdjacencyList |
| 62 | +end |
| 63 | + |
| 64 | +function Base.show(io::IO, l::BipartiteAdjacencyListDiff) |
| 65 | + print(io, |
| 66 | + LabelDiff(Label(l.new.match === true ? "∫ " : ""), |
| 67 | + Label(l.old.match === true ? "∫ " : ""))) |
| 68 | + (l.new.match !== true && l.old.match !== true) && print(io, " ") |
| 69 | + |
| 70 | + new_nonempty = isnothing(l.new.u) ? nothing : !isempty(l.new.u) |
| 71 | + old_nonempty = isnothing(l.old.u) ? nothing : !isempty(l.old.u) |
| 72 | + if new_nonempty === true && old_nonempty === true |
| 73 | + if (!isempty(setdiff(l.new.highligh_u, l.new.u)) || |
| 74 | + !isempty(setdiff(l.old.highligh_u, l.old.u))) |
| 75 | + throw(ArgumentError("The provided `highligh_u` must be a sub-graph of `u`.")) |
| 76 | + end |
| 77 | + |
| 78 | + new_items = Dict(i => HighlightInt(i, :nothing, i === l.new.match) for i in l.new.u) |
| 79 | + old_items = Dict(i => HighlightInt(i, :nothing, i === l.old.match) for i in l.old.u) |
| 80 | + |
| 81 | + highlighted = union(map(intersect(l.new.u, l.old.u)) do i |
| 82 | + HighlightIntDiff(new_items[i], old_items[i]) |
| 83 | + end, |
| 84 | + map(setdiff(l.new.u, l.old.u)) do i |
| 85 | + HighlightInt(new_items[i].i, :light_green, |
| 86 | + new_items[i].match) |
| 87 | + end, |
| 88 | + map(setdiff(l.old.u, l.new.u)) do i |
| 89 | + HighlightInt(old_items[i].i, :light_red, |
| 90 | + old_items[i].match) |
| 91 | + end) |
| 92 | + print(IOContext(io, :typeinfo => typeof(highlighted)), highlighted) |
| 93 | + elseif new_nonempty === true |
| 94 | + printstyled(io, map(l.new.u) do i |
| 95 | + HighlightInt(i, :nothing, i === l.new.match) |
| 96 | + end, color = :light_green) |
| 97 | + elseif old_nonempty === true |
| 98 | + printstyled(io, map(l.old.u) do i |
| 99 | + HighlightInt(i, :nothing, i === l.old.match) |
| 100 | + end, color = :light_red) |
| 101 | + elseif old_nonempty !== nothing || new_nonempty !== nothing |
| 102 | + print(io, |
| 103 | + LabelDiff(Label(new_nonempty === false ? "∅" : "", :light_black), |
| 104 | + Label(old_nonempty === false ? "∅" : "", :light_black))) |
| 105 | + else |
| 106 | + printstyled(io, '⋅', color = :light_black) |
| 107 | + end |
| 108 | +end |
| 109 | + |
| 110 | +""" |
| 111 | +A utility struct for displaying the difference between two Labels |
| 112 | +in git-style red/green highlighting. |
| 113 | +
|
| 114 | +# Example |
| 115 | +```julia |
| 116 | +using ModelingToolkit, DeepDiffs |
| 117 | +
|
| 118 | +old = Label("before") |
| 119 | +new = Label("after") |
| 120 | +diff = LabelDiff(new, old) |
| 121 | +
|
| 122 | +show(diff) |
| 123 | +``` |
| 124 | +""" |
| 125 | +struct LabelDiff |
| 126 | + new::Label |
| 127 | + old::Label |
| 128 | +end |
| 129 | +function Base.show(io::IO, l::LabelDiff) |
| 130 | + if l.new != l.old |
| 131 | + printstyled(io, l.old.s, color = :light_red) |
| 132 | + length(l.new.s) != 0 && length(l.old.s) != 0 && print(io, " ") |
| 133 | + printstyled(io, l.new.s, color = :light_green) |
| 134 | + else |
| 135 | + print(io, l.new) |
| 136 | + end |
| 137 | +end |
| 138 | + |
| 139 | +""" |
| 140 | +A utility struct for displaying the difference between two |
| 141 | +(Matched)SystemStructure's in git-style red/green highlighting. |
| 142 | +
|
| 143 | +# Example |
| 144 | +```julia |
| 145 | +using ModelingToolkit, DeepDiffs |
| 146 | +
|
| 147 | +old = SystemStructurePrintMatrix(...) |
| 148 | +new = SystemStructurePrintMatrix(...) |
| 149 | +diff = SystemStructureDiffPrintMatrix(new, old) |
| 150 | +
|
| 151 | +show(diff) |
| 152 | +``` |
| 153 | +""" |
| 154 | +struct SystemStructureDiffPrintMatrix <: |
| 155 | + AbstractMatrix{Union{LabelDiff, BipartiteAdjacencyListDiff}} |
| 156 | + new::SystemStructurePrintMatrix |
| 157 | + old::SystemStructurePrintMatrix |
| 158 | +end |
| 159 | + |
| 160 | +function Base.size(ssdpm::SystemStructureDiffPrintMatrix) |
| 161 | + max.(Base.size(ssdpm.new), Base.size(ssdpm.old)) |
| 162 | +end |
| 163 | + |
| 164 | +function Base.getindex(ssdpm::SystemStructureDiffPrintMatrix, i::Integer, j::Integer) |
| 165 | + checkbounds(ssdpm, i, j) |
| 166 | + if i > 1 && (j == 4 || j == 9) |
| 167 | + old = new = BipartiteAdjacencyList(nothing, nothing, unassigned) |
| 168 | + (i <= size(ssdpm.new, 1)) && (new = ssdpm.new[i, j]) |
| 169 | + (i <= size(ssdpm.old, 1)) && (old = ssdpm.old[i, j]) |
| 170 | + BipartiteAdjacencyListDiff(new, old) |
| 171 | + else |
| 172 | + old = new = Label("") |
| 173 | + (i <= size(ssdpm.new, 1)) && (new = ssdpm.new[i, j]) |
| 174 | + (i <= size(ssdpm.old, 1)) && (old = ssdpm.old[i, j]) |
| 175 | + LabelDiff(new, old) |
| 176 | + end |
| 177 | +end |
| 178 | + |
| 179 | +function DeepDiffs.deepdiff(old::Union{MatchedSystemStructure, SystemStructure}, |
| 180 | + new::Union{MatchedSystemStructure, SystemStructure}) |
| 181 | + new_sspm = SystemStructurePrintMatrix(new) |
| 182 | + old_sspm = SystemStructurePrintMatrix(old) |
| 183 | + Base.print_matrix(stdout, SystemStructureDiffPrintMatrix(new_sspm, old_sspm)) |
| 184 | +end |
| 185 | + |
| 186 | +end # module |
0 commit comments