2014-05-24 14:47:04 -05:00
|
|
|
package digraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateDot is used to emit a GraphViz compatible definition
|
|
|
|
// for a directed graph. It can be used to dump a .dot file.
|
|
|
|
func GenerateDot(nodes []Node, w io.Writer) {
|
|
|
|
w.Write([]byte("digraph {\n"))
|
|
|
|
defer w.Write([]byte("}\n"))
|
|
|
|
for _, n := range nodes {
|
2014-07-01 11:54:40 -05:00
|
|
|
w.Write([]byte(fmt.Sprintf("\t\"%s\";\n", n)))
|
2014-05-24 14:47:04 -05:00
|
|
|
for _, edge := range n.Edges() {
|
|
|
|
target := edge.Tail()
|
2014-07-01 11:54:40 -05:00
|
|
|
line := fmt.Sprintf("\t\"%s\" -> \"%s\" [label=\"%s\"];\n",
|
2014-05-24 14:47:04 -05:00
|
|
|
n, target, edge)
|
|
|
|
w.Write([]byte(line))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|