From 5f5b110a59624295a25a7a7aff322e74e153a009 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 23 Oct 2014 09:13:39 -0400 Subject: [PATCH] cmd/digraph: digraph: a UNIX-style utility command for common operations on directed graphs in text format. Example: show the transitive closure of imports of the digraph tool itself: % go list -f '{{.ImportPath}}{{.Imports}}' ... | tr '[]' ' ' | digraph forward code.google.com/p/go.tools/cmd/digraph + basic test. LGTM=gri R=gri, sameer CC=golang-codereviews https://golang.org/cl/161760043 --- cmd/digraph/digraph.go | 411 ++++++++++++++++++++++++++++++++++++ cmd/digraph/digraph_test.go | 62 ++++++ 2 files changed, 473 insertions(+) create mode 100644 cmd/digraph/digraph.go create mode 100644 cmd/digraph/digraph_test.go diff --git a/cmd/digraph/digraph.go b/cmd/digraph/digraph.go new file mode 100644 index 00000000..0d3868c4 --- /dev/null +++ b/cmd/digraph/digraph.go @@ -0,0 +1,411 @@ +// The digraph command performs queries over unlabelled directed graphs +// represented in text form. It is intended to integrate nicely with +// typical UNIX command pipelines. +// +// Since directed graphs (import graphs, reference graphs, call graphs, +// etc) often arise during software tool development and debugging, this +// command is included in the go.tools repository. +// +// TODO(adonovan): +// - support input files other than stdin +// - suport alternative formats (AT&T GraphViz, CSV, etc), +// a comment syntax, etc. +// - allow queries to nest, like Blaze query language. +// +package main + +import ( + "bufio" + "flag" + "fmt" + "io" + "os" + "sort" + "strings" +) + +const Usage = `digraph: queries over directed graphs in text form. + +Graph format: + + Each line contains zero or more whitespace-separated fields. + Each field declares a node, and if there are more than one, + an edge from the first to each subsequent one. + The graph is provided on the standard input. + + For instance, the following (acyclic) graph specifies a partial order + among the subtasks of getting dressed: + + % cat clothes.txt + socks shoes + shorts pants + pants belt shoes + shirt tie sweater + sweater jacket + hat + + The line "shirt tie sweater" indicates the two edges shirt -> tie and + shirt -> sweater, not shirt -> tie -> sweater. + +Supported queries: + + nodes + the set of all nodes + degree + the in-degree and out-degree of each node. + preds