Skip to content

Commit

Permalink
CLEANUP: Eliminate Nullables in TikZ wiring diagrams (#21).
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters committed Aug 3, 2019
1 parent c78b810 commit a289a5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
42 changes: 20 additions & 22 deletions src/graphics/TikZ.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export Expression, Statement, GraphStatement, Coordinate, Property,
GraphNode, GraphEdge, MatrixNode, pprint

using AutoHashEquals
using Nullables

# AST
#####
Expand All @@ -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
Expand Down Expand Up @@ -70,30 +68,30 @@ 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
src::AbstractString
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

Expand All @@ -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

Expand Down Expand Up @@ -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, " {")
Expand All @@ -199,18 +197,18 @@ 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

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

Expand Down Expand Up @@ -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, " ")
Expand Down Expand Up @@ -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

Expand Down
25 changes: 13 additions & 12 deletions src/graphics/TikZWiringDiagrams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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}

Expand All @@ -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
#################
Expand Down Expand Up @@ -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}}}"
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a289a5b

Please sign in to comment.