Property definitions

dot $ DotGraph :: defaultinit
# A Graph representation suited for dot format.
#
# Creating a new graph
# ~~~
# var graph = new DotGraph("G", "digraph")
# graph["rankdir"] = "BT"
# graph["ranksep"] = 0.3
# graph["nodesep"] = 0.3
# graph.nodes_attrs["fontname"] = "helvetica"
# graph.edges_attrs["color"] = "gray"
# ~~~
#
# Creating subgraphs
# ~~~
# var sub = new DotGraph("cluster_sub", "subgraph")
# sub["label"] = "process #1"
#
# var a0 = sub.add_node("a0")
# var a1 = sub.add_node("a1")
# sub.add_edge(a0, a1)
#
# graph.add sub
# ~~~
class DotGraph
	super DotElement

	# Graph kind like `graph`, `digraph`, `subgraph`...
	var kind: String is writable

	# Nodes attributes
	var nodes_attrs = new AttributeMap

	# Edges attributes
	var edges_attrs = new AttributeMap

	# Node list by id
	var nodes = new HashMap[String, DotElement]

	# Add a node to the graph
	#
	# If the graph already contains a node with that ID, it will be replaced.
	fun add(element: DotElement) do
		nodes[element.id] = element
	end

	# Edge list
	#
	# There can be multiple edges between the same couple of nodes.
	var edges = new Array[DotEdge]

	# Add a new node to the graph
	fun add_node(id: String): DotNode do
		var node = new DotNode(id)
		add node
		return node
	end

	# Add an edge to the graph
	fun add_edge(from, to: DotElement): DotEdge do
		var edge = new DotEdge(from, to)
		add edge
		return edge
	end

	redef fun to_dot do
		var dot = new Buffer
		dot.append "{kind} \"{id}\" \{\n"
		if attrs.not_empty then dot.append attrs.to_dot(";\n")
		if nodes_attrs.not_empty then dot.append "node[{nodes_attrs.to_dot(",")}];\n"
		if edges_attrs.not_empty then dot.append "edge[{edges_attrs.to_dot(",")}];\n"
		for id, node in nodes do
			dot.append "{node.to_dot};\n"
		end
		for edge in edges do
			dot.append("{edge.to_dot};\n")
		end
		dot.append("\}")
		return dot
	end

	# Render `self` as an SVG image
	fun to_svg: Text do
		var proc = new ProcessDuplex("dot", "-Tsvg")
		var svg = proc.write_and_read(to_dot)
		proc.close
		proc.wait
		return svg
	end

	# Show dot in graphviz (blocking)
	fun show do
		var f = new ProcessWriter("dot", "-Txlib")
		f.write to_dot
		f.close
		f.wait
	end
end
lib/dot/dot.nit:75,1--171,3