From a289a5b76b27ef1bcd7e241e26a7007cdafabf27 Mon Sep 17 00:00:00 2001 From: epatters Date: Fri, 2 Aug 2019 22:16:05 -0400 Subject: [PATCH] CLEANUP: Eliminate Nullables in TikZ wiring diagrams (#21). --- src/graphics/TikZ.jl | 42 ++++++++++++++---------------- src/graphics/TikZWiringDiagrams.jl | 25 +++++++++--------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/graphics/TikZ.jl b/src/graphics/TikZ.jl index 687fb0445..db4482cfb 100644 --- a/src/graphics/TikZ.jl +++ b/src/graphics/TikZ.jl @@ -20,7 +20,6 @@ export Expression, Statement, GraphStatement, Coordinate, Property, GraphNode, GraphEdge, MatrixNode, pprint using AutoHashEquals -using Nullables # AST ##### @@ -39,10 +38,9 @@ end @auto_hash_equals struct Property <: Expression key::AbstractString - value::Nullable{AbstractString} + value::Union{AbstractString,Nothing} - Property(key::AbstractString) = new(key, Nullable()) - Property(key::AbstractString, value::AbstractString) = new(key, Nullable(value)) + Property(key::AbstractString, value=nothing) = new(key, value) end @auto_hash_equals struct PathOperation <: Expression @@ -70,19 +68,19 @@ end # FIXME: Name is optional, according to TikZ manual. name::AbstractString props::Vector{Property} - coord::Nullable{Coordinate} + coord::Union{Coordinate,Nothing} # Allow nested pictures even though TikZ does not "officially" support them. content::Union{AbstractString,Picture} - Node(name::AbstractString; props=Property[], coord=Nullable(), content="") = + Node(name::AbstractString; props=Property[], coord=nothing, content="") = new(name, props, coord, content) end @auto_hash_equals struct EdgeNode <: Expression props::Vector{Property} - content::Nullable{AbstractString} + content::Union{AbstractString,Nothing} - EdgeNode(; props=Property[], content=Nullable()) = new(props, content) + EdgeNode(; props=Property[], content=nothing) = new(props, content) end @auto_hash_equals struct Edge <: Statement @@ -90,10 +88,10 @@ end tgt::AbstractString op::PathOperation props::Vector{Property} - node::Nullable{EdgeNode} + node::Union{EdgeNode,Nothing} Edge(src::AbstractString, tgt::AbstractString; - op=PathOperation("to"), props=Property[], node=Nullable()) = + op=PathOperation("to"), props=Property[], node=nothing) = new(src, tgt, op, props, node) end @@ -116,9 +114,9 @@ end @auto_hash_equals struct GraphNode <: GraphStatement name::AbstractString props::Vector{Property} - content::Nullable{AbstractString} + content::Union{AbstractString,Nothing} - GraphNode(name::AbstractString; props=Property[], content=Nullable()) = + GraphNode(name::AbstractString; props=Property[], content=nothing) = new(name, props, content) end @@ -177,9 +175,9 @@ function pprint(io::IO, node::Node, n::Int) print(io, "\\node") pprint(io, node.props) print(io, " ($(node.name))") - if !isnull(node.coord) + if !isnothing(node.coord) print(io, " at ") - pprint(io, get(node.coord)) + pprint(io, node.coord) end if isa(node.content, Picture) println(io, " {") @@ -199,9 +197,9 @@ function pprint(io::IO, edge::Edge, n::Int) pprint(io, edge.props) print(io, " ($(edge.src)) ") pprint(io, edge.op) - if !isnull(edge.node) + if !isnothing(edge.node) print(io, " ") - pprint(io, get(edge.node)) + pprint(io, edge.node) end print(io, " ($(edge.tgt));") end @@ -209,8 +207,8 @@ end function pprint(io::IO, node::EdgeNode, n::Int) print(io, "node") pprint(io, node.props) - if !isnull(node.content) - print(io, " {$(get(node.content))}") + if !isnothing(node.content) + print(io, " {$(node.content)}") end end @@ -243,8 +241,8 @@ end function pprint(io::IO, node::GraphNode, n::Int) indent(io, n) print(io, node.name) - if !isnull(node.content) - print(io, "/\"$(get(node.content))\"") + if !isnothing(node.content) + print(io, "/\"$(node.content)\"") end if !isempty(node.props) print(io, " ") @@ -292,9 +290,9 @@ end function pprint(io::IO, prop::Property, n::Int) print(io, prop.key) - if !isnull(prop.value) + if !isnothing(prop.value) print(io, "=") - print(io, get(prop.value)) + print(io, prop.value) end end diff --git a/src/graphics/TikZWiringDiagrams.jl b/src/graphics/TikZWiringDiagrams.jl index 925cfb71c..ea54e264d 100644 --- a/src/graphics/TikZWiringDiagrams.jl +++ b/src/graphics/TikZWiringDiagrams.jl @@ -4,10 +4,9 @@ module TikZWiringDiagrams export to_tikz using Match -using Nullables -import ...Doctrines: ObExpr, HomExpr, dom, codom, head, args, compose, id -import ...Syntax: GATExpr, show_latex +using ...Doctrines: ObExpr, HomExpr, dom, codom, head, args, compose, id +using ...Syntax: GATExpr, show_latex using ...WiringDiagrams import ..TikZ @@ -22,7 +21,9 @@ struct Wire Wire(label::String; reverse::Bool=false) = new(label, reverse) end -""" Object in a TikZ wiring diagram. +""" A bundle of wires in a TikZ wiring diagram. + +A graphical representation of an object. """ const Wires = Vector{Wire} @@ -37,15 +38,16 @@ struct Port new(wire, anchor, angle, show_label) end -""" Morphism in a TikZ wiring diagram. +""" A box in a TikZ wiring diagram. + +A `Box` is a graphical representation of a morphism, and need not be rendered +as a geometric box (rectangle). """ struct Box node::TikZ.Node inputs::Vector{Port} outputs::Vector{Port} end -dom(box::Box)::Wires = [ port.label for port in box.inputs ] -codom(box::Box)::Wires = [ port.label for port in box.outputs ] # Wiring diagrams ################# @@ -96,8 +98,7 @@ function to_tikz(f::HomExpr; TikZ.Property("font", "{\\fontsize{$font_size}{$(round(1.2*font_size;digits=2))}}"), TikZ.Property("container/.style", "{inner sep=0}"), - TikZ.Property("every path/.style", - "{solid, line width=$line_width}"), + TikZ.Property("every path/.style", "{solid, line width=$line_width}"), ] if !isempty(arrowtip) decoration = "{markings, mark=at position 0.5 with {\\arrow{$arrowtip}}}" @@ -254,10 +255,10 @@ function sequence(name::String, homs::Vector)::Box # Create edge node for label. We use the source port, not the target port # (see also `GraphvizWiring`). wire = src_port.wire - if (style[:labels] && src_port.show_label && tgt_port.show_label) - node = TikZ.EdgeNode(content=wire.label, props=edge_node_props) + node = if (style[:labels] && src_port.show_label && tgt_port.show_label) + TikZ.EdgeNode(content=wire.label, props=edge_node_props) else - node = Nullable() + nothing end # Create path operation and draw edge.