2015-01-23 02:12:32 +01:00
|
|
|
package dag
|
2015-01-21 23:20:51 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-09 15:58:52 +01:00
|
|
|
"encoding/json"
|
2015-01-21 23:20:51 +01:00
|
|
|
"fmt"
|
2016-11-10 19:40:27 +01:00
|
|
|
"io"
|
2015-01-21 23:20:51 +01:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Graph is used to represent a dependency graph.
|
|
|
|
type Graph struct {
|
2015-01-30 21:56:03 +01:00
|
|
|
vertices *Set
|
|
|
|
edges *Set
|
2015-10-27 19:58:34 +01:00
|
|
|
downEdges map[interface{}]*Set
|
|
|
|
upEdges map[interface{}]*Set
|
2016-11-10 19:40:27 +01:00
|
|
|
|
|
|
|
// JSON encoder for recording debug information
|
|
|
|
debug *encoder
|
2015-01-21 23:20:51 +01:00
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
// Subgrapher allows a Vertex to be a Graph itself, by returning a Grapher.
|
|
|
|
type Subgrapher interface {
|
|
|
|
Subgraph() Grapher
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Grapher is any type that returns a Grapher, mainly used to identify
|
|
|
|
// dag.Graph and dag.AcyclicGraph. In the case of Graph and AcyclicGraph, they
|
|
|
|
// return themselves.
|
|
|
|
type Grapher interface {
|
|
|
|
DirectedGraph() Grapher
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:01:58 +01:00
|
|
|
// Vertex of the graph.
|
|
|
|
type Vertex interface{}
|
2015-01-21 23:20:51 +01:00
|
|
|
|
2015-01-24 00:01:58 +01:00
|
|
|
// NamedVertex is an optional interface that can be implemented by Vertex
|
|
|
|
// to give it a human-friendly name that is used for outputting the graph.
|
|
|
|
type NamedVertex interface {
|
|
|
|
Vertex
|
2015-01-21 23:20:51 +01:00
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
2016-11-09 22:52:22 +01:00
|
|
|
func (g *Graph) DirectedGraph() Grapher {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:01:58 +01:00
|
|
|
// Vertices returns the list of all the vertices in the graph.
|
|
|
|
func (g *Graph) Vertices() []Vertex {
|
2015-01-30 21:25:49 +01:00
|
|
|
list := g.vertices.List()
|
|
|
|
result := make([]Vertex, len(list))
|
|
|
|
for i, v := range list {
|
|
|
|
result[i] = v.(Vertex)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2015-01-24 00:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Edges returns the list of all the edges in the graph.
|
|
|
|
func (g *Graph) Edges() []Edge {
|
2015-02-07 18:52:34 +01:00
|
|
|
list := g.edges.List()
|
2015-01-30 21:56:03 +01:00
|
|
|
result := make([]Edge, len(list))
|
|
|
|
for i, v := range list {
|
|
|
|
result[i] = v.(Edge)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2015-01-24 00:01:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-21 20:33:47 +02:00
|
|
|
// EdgesFrom returns the list of edges from the given source.
|
|
|
|
func (g *Graph) EdgesFrom(v Vertex) []Edge {
|
|
|
|
var result []Edge
|
|
|
|
from := hashcode(v)
|
|
|
|
for _, e := range g.Edges() {
|
|
|
|
if hashcode(e.Source()) == from {
|
|
|
|
result = append(result, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// EdgesTo returns the list of edges to the given target.
|
|
|
|
func (g *Graph) EdgesTo(v Vertex) []Edge {
|
|
|
|
var result []Edge
|
|
|
|
search := hashcode(v)
|
|
|
|
for _, e := range g.Edges() {
|
|
|
|
if hashcode(e.Target()) == search {
|
|
|
|
result = append(result, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:52:01 +01:00
|
|
|
// HasVertex checks if the given Vertex is present in the graph.
|
|
|
|
func (g *Graph) HasVertex(v Vertex) bool {
|
|
|
|
return g.vertices.Include(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasEdge checks if the given Edge is present in the graph.
|
|
|
|
func (g *Graph) HasEdge(e Edge) bool {
|
|
|
|
return g.edges.Include(e)
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:01:58 +01:00
|
|
|
// Add adds a vertex to the graph. This is safe to call multiple time with
|
|
|
|
// the same Vertex.
|
2015-01-27 05:17:52 +01:00
|
|
|
func (g *Graph) Add(v Vertex) Vertex {
|
2016-11-11 00:05:30 +01:00
|
|
|
g.init()
|
2015-01-30 21:25:49 +01:00
|
|
|
g.vertices.Add(v)
|
2016-11-10 19:40:27 +01:00
|
|
|
g.debug.Add(v)
|
2015-01-27 05:17:52 +01:00
|
|
|
return v
|
2015-01-24 00:01:58 +01:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:56:03 +01:00
|
|
|
// Remove removes a vertex from the graph. This will also remove any
|
|
|
|
// edges with this vertex as a source or target.
|
|
|
|
func (g *Graph) Remove(v Vertex) Vertex {
|
|
|
|
// Delete the vertex itself
|
|
|
|
g.vertices.Delete(v)
|
2016-11-10 19:40:27 +01:00
|
|
|
g.debug.Remove(v)
|
2015-01-30 21:56:03 +01:00
|
|
|
|
|
|
|
// Delete the edges to non-existent things
|
|
|
|
for _, target := range g.DownEdges(v).List() {
|
|
|
|
g.RemoveEdge(BasicEdge(v, target))
|
|
|
|
}
|
|
|
|
for _, source := range g.UpEdges(v).List() {
|
|
|
|
g.RemoveEdge(BasicEdge(source, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-08 00:37:17 +01:00
|
|
|
// Replace replaces the original Vertex with replacement. If the original
|
|
|
|
// does not exist within the graph, then false is returned. Otherwise, true
|
|
|
|
// is returned.
|
|
|
|
func (g *Graph) Replace(original, replacement Vertex) bool {
|
|
|
|
// If we don't have the original, we can't do anything
|
|
|
|
if !g.vertices.Include(original) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:42:52 +01:00
|
|
|
defer g.debug.BeginOperation("Replace", "").End("")
|
2016-11-10 19:40:27 +01:00
|
|
|
|
2015-05-07 20:22:07 +02:00
|
|
|
// If they're the same, then don't do anything
|
|
|
|
if original == replacement {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-02-08 00:37:17 +01:00
|
|
|
// Add our new vertex, then copy all the edges
|
|
|
|
g.Add(replacement)
|
|
|
|
for _, target := range g.DownEdges(original).List() {
|
|
|
|
g.Connect(BasicEdge(replacement, target))
|
|
|
|
}
|
|
|
|
for _, source := range g.UpEdges(original).List() {
|
|
|
|
g.Connect(BasicEdge(source, replacement))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove our old vertex, which will also remove all the edges
|
|
|
|
g.Remove(original)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:56:03 +01:00
|
|
|
// RemoveEdge removes an edge from the graph.
|
|
|
|
func (g *Graph) RemoveEdge(edge Edge) {
|
2016-11-11 00:05:30 +01:00
|
|
|
g.init()
|
2016-11-10 19:40:27 +01:00
|
|
|
g.debug.RemoveEdge(edge)
|
2015-01-30 21:56:03 +01:00
|
|
|
|
|
|
|
// Delete the edge from the set
|
|
|
|
g.edges.Delete(edge)
|
|
|
|
|
|
|
|
// Delete the up/down edges
|
2015-10-27 19:58:34 +01:00
|
|
|
if s, ok := g.downEdges[hashcode(edge.Source())]; ok {
|
2015-01-30 21:56:03 +01:00
|
|
|
s.Delete(edge.Target())
|
|
|
|
}
|
2015-10-27 19:58:34 +01:00
|
|
|
if s, ok := g.upEdges[hashcode(edge.Target())]; ok {
|
2015-01-30 21:56:03 +01:00
|
|
|
s.Delete(edge.Source())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownEdges returns the outward edges from the source Vertex v.
|
|
|
|
func (g *Graph) DownEdges(v Vertex) *Set {
|
2016-11-11 00:05:30 +01:00
|
|
|
g.init()
|
2015-10-27 19:58:34 +01:00
|
|
|
return g.downEdges[hashcode(v)]
|
2015-01-30 21:56:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpEdges returns the inward edges to the destination Vertex v.
|
|
|
|
func (g *Graph) UpEdges(v Vertex) *Set {
|
2016-11-11 00:05:30 +01:00
|
|
|
g.init()
|
2015-10-27 19:58:34 +01:00
|
|
|
return g.upEdges[hashcode(v)]
|
2015-01-30 21:56:03 +01:00
|
|
|
}
|
|
|
|
|
2015-01-24 00:01:58 +01:00
|
|
|
// Connect adds an edge with the given source and target. This is safe to
|
|
|
|
// call multiple times with the same value. Note that the same value is
|
|
|
|
// verified through pointer equality of the vertices, not through the
|
|
|
|
// value of the edge itself.
|
|
|
|
func (g *Graph) Connect(edge Edge) {
|
2016-11-11 00:05:30 +01:00
|
|
|
g.init()
|
2016-11-10 19:40:27 +01:00
|
|
|
g.debug.Connect(edge)
|
2015-01-24 00:01:58 +01:00
|
|
|
|
|
|
|
source := edge.Source()
|
|
|
|
target := edge.Target()
|
2015-10-27 19:58:34 +01:00
|
|
|
sourceCode := hashcode(source)
|
|
|
|
targetCode := hashcode(target)
|
2015-01-24 00:01:58 +01:00
|
|
|
|
|
|
|
// Do we have this already? If so, don't add it again.
|
2015-10-27 19:58:34 +01:00
|
|
|
if s, ok := g.downEdges[sourceCode]; ok && s.Include(target) {
|
2015-01-24 00:01:58 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:56:03 +01:00
|
|
|
// Add the edge to the set
|
|
|
|
g.edges.Add(edge)
|
2015-01-24 00:01:58 +01:00
|
|
|
|
|
|
|
// Add the down edge
|
2015-10-27 19:58:34 +01:00
|
|
|
s, ok := g.downEdges[sourceCode]
|
2015-01-24 00:01:58 +01:00
|
|
|
if !ok {
|
2015-01-30 21:56:03 +01:00
|
|
|
s = new(Set)
|
2015-10-27 19:58:34 +01:00
|
|
|
g.downEdges[sourceCode] = s
|
2015-01-24 00:01:58 +01:00
|
|
|
}
|
|
|
|
s.Add(target)
|
|
|
|
|
|
|
|
// Add the up edge
|
2015-10-27 19:58:34 +01:00
|
|
|
s, ok = g.upEdges[targetCode]
|
2015-01-24 00:01:58 +01:00
|
|
|
if !ok {
|
2015-01-30 21:56:03 +01:00
|
|
|
s = new(Set)
|
2015-10-27 19:58:34 +01:00
|
|
|
g.upEdges[targetCode] = s
|
2015-01-24 00:01:58 +01:00
|
|
|
}
|
|
|
|
s.Add(source)
|
|
|
|
}
|
|
|
|
|
2016-04-13 19:20:18 +02:00
|
|
|
// String outputs some human-friendly output for the graph structure.
|
|
|
|
func (g *Graph) StringWithNodeTypes() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// Build the list of node names and a mapping so that we can more
|
|
|
|
// easily alphabetize the output to remain deterministic.
|
|
|
|
vertices := g.Vertices()
|
|
|
|
names := make([]string, 0, len(vertices))
|
|
|
|
mapping := make(map[string]Vertex, len(vertices))
|
|
|
|
for _, v := range vertices {
|
|
|
|
name := VertexName(v)
|
|
|
|
names = append(names, name)
|
|
|
|
mapping[name] = v
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
// Write each node in order...
|
|
|
|
for _, name := range names {
|
|
|
|
v := mapping[name]
|
|
|
|
targets := g.downEdges[hashcode(v)]
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s - %T\n", name, v))
|
|
|
|
|
|
|
|
// Alphabetize dependencies
|
|
|
|
deps := make([]string, 0, targets.Len())
|
2016-07-14 17:32:47 +02:00
|
|
|
targetNodes := make(map[string]Vertex)
|
2016-04-13 19:20:18 +02:00
|
|
|
for _, target := range targets.List() {
|
2016-07-14 17:32:47 +02:00
|
|
|
dep := VertexName(target)
|
|
|
|
deps = append(deps, dep)
|
|
|
|
targetNodes[dep] = target
|
2016-04-13 19:20:18 +02:00
|
|
|
}
|
|
|
|
sort.Strings(deps)
|
|
|
|
|
|
|
|
// Write dependencies
|
2016-07-14 17:32:47 +02:00
|
|
|
for _, d := range deps {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s - %T\n", d, targetNodes[d]))
|
2016-04-13 19:20:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2015-01-24 00:01:58 +01:00
|
|
|
// String outputs some human-friendly output for the graph structure.
|
2015-01-21 23:20:51 +01:00
|
|
|
func (g *Graph) String() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// Build the list of node names and a mapping so that we can more
|
|
|
|
// easily alphabetize the output to remain deterministic.
|
2015-01-30 21:25:49 +01:00
|
|
|
vertices := g.Vertices()
|
|
|
|
names := make([]string, 0, len(vertices))
|
|
|
|
mapping := make(map[string]Vertex, len(vertices))
|
|
|
|
for _, v := range vertices {
|
2015-01-28 06:48:46 +01:00
|
|
|
name := VertexName(v)
|
2015-01-21 23:20:51 +01:00
|
|
|
names = append(names, name)
|
2015-01-24 00:01:58 +01:00
|
|
|
mapping[name] = v
|
2015-01-21 23:20:51 +01:00
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
|
|
|
|
// Write each node in order...
|
|
|
|
for _, name := range names {
|
2015-01-24 00:01:58 +01:00
|
|
|
v := mapping[name]
|
2015-10-27 19:58:34 +01:00
|
|
|
targets := g.downEdges[hashcode(v)]
|
2015-01-24 00:01:58 +01:00
|
|
|
|
2015-01-21 23:20:51 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%s\n", name))
|
|
|
|
|
|
|
|
// Alphabetize dependencies
|
2015-01-24 00:01:58 +01:00
|
|
|
deps := make([]string, 0, targets.Len())
|
|
|
|
for _, target := range targets.List() {
|
2015-01-28 06:48:46 +01:00
|
|
|
deps = append(deps, VertexName(target))
|
2015-01-21 23:20:51 +01:00
|
|
|
}
|
|
|
|
sort.Strings(deps)
|
|
|
|
|
|
|
|
// Write dependencies
|
|
|
|
for _, d := range deps {
|
|
|
|
buf.WriteString(fmt.Sprintf(" %s\n", d))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
func (g *Graph) init() {
|
2016-11-11 00:05:30 +01:00
|
|
|
if g.vertices == nil {
|
|
|
|
g.vertices = new(Set)
|
|
|
|
}
|
|
|
|
if g.edges == nil {
|
|
|
|
g.edges = new(Set)
|
|
|
|
}
|
|
|
|
if g.downEdges == nil {
|
|
|
|
g.downEdges = make(map[interface{}]*Set)
|
|
|
|
}
|
|
|
|
if g.upEdges == nil {
|
|
|
|
g.upEdges = make(map[interface{}]*Set)
|
|
|
|
}
|
2016-11-10 19:40:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dot returns a dot-formatted representation of the Graph.
|
2016-11-09 15:58:52 +01:00
|
|
|
func (g *Graph) Dot(opts *DotOpts) []byte {
|
|
|
|
return newMarshalGraph("", g).Dot(opts)
|
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
// MarshalJSON returns a JSON representation of the entire Graph.
|
2016-11-09 15:58:52 +01:00
|
|
|
func (g *Graph) MarshalJSON() ([]byte, error) {
|
2016-11-10 19:40:27 +01:00
|
|
|
dg := newMarshalGraph("root", g)
|
2016-11-09 15:58:52 +01:00
|
|
|
return json.MarshalIndent(dg, "", " ")
|
|
|
|
}
|
|
|
|
|
2016-11-10 19:40:27 +01:00
|
|
|
// SetDebugWriter sets the io.Writer where the Graph will record debug
|
|
|
|
// information. After this is set, the graph will immediately encode itself to
|
|
|
|
// the stream, and continue to record all subsequent operations.
|
|
|
|
func (g *Graph) SetDebugWriter(w io.Writer) {
|
2016-11-15 16:15:52 +01:00
|
|
|
g.debug = &encoder{w: w}
|
2016-11-10 19:40:27 +01:00
|
|
|
g.debug.Encode(newMarshalGraph("root", g))
|
2015-01-24 00:01:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-14 14:46:53 +01:00
|
|
|
// DebugVertexInfo encodes arbitrary information about a vertex in the graph
|
|
|
|
// debug logs.
|
|
|
|
func (g *Graph) DebugVertexInfo(v Vertex, info string) {
|
2016-11-15 16:15:52 +01:00
|
|
|
va := newVertexInfo(typeVertexInfo, v, info)
|
2016-11-10 22:54:52 +01:00
|
|
|
g.debug.Encode(va)
|
|
|
|
}
|
|
|
|
|
2016-11-14 14:46:53 +01:00
|
|
|
// DebugEdgeInfo encodes arbitrary information about an edge in the graph debug
|
|
|
|
// logs.
|
|
|
|
func (g *Graph) DebugEdgeInfo(e Edge, info string) {
|
2016-11-15 16:15:52 +01:00
|
|
|
ea := newEdgeInfo(typeEdgeInfo, e, info)
|
2016-11-10 22:54:52 +01:00
|
|
|
g.debug.Encode(ea)
|
|
|
|
}
|
|
|
|
|
2016-11-15 16:15:52 +01:00
|
|
|
// DebugVisitInfo records a visit to a Vertex during a walk operation.
|
|
|
|
func (g *Graph) DebugVisitInfo(v Vertex, info string) {
|
|
|
|
vi := newVertexInfo(typeVisitInfo, v, info)
|
|
|
|
g.debug.Encode(vi)
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:42:52 +01:00
|
|
|
// DebugOperation marks the start of a set of graph transformations in
|
|
|
|
// the debug log, and returns a DebugOperationEnd func, which marks the end of
|
|
|
|
// the operation in the log. Additional information can be added to the log via
|
|
|
|
// the info parameter.
|
|
|
|
//
|
|
|
|
// The returned func's End method allows this method to be called from a single
|
|
|
|
// defer statement:
|
|
|
|
// defer g.DebugOperationBegin("OpName", "operating").End("")
|
|
|
|
//
|
|
|
|
// The returned function must be called to properly close the logical operation
|
|
|
|
// in the logs.
|
|
|
|
func (g *Graph) DebugOperation(operation string, info string) DebugOperationEnd {
|
|
|
|
return g.debug.BeginOperation(operation, info)
|
|
|
|
}
|
|
|
|
|
2015-01-28 06:48:46 +01:00
|
|
|
// VertexName returns the name of a vertex.
|
|
|
|
func VertexName(raw Vertex) string {
|
2015-01-24 00:01:58 +01:00
|
|
|
switch v := raw.(type) {
|
|
|
|
case NamedVertex:
|
2015-01-21 23:20:51 +01:00
|
|
|
return v.Name()
|
2015-01-24 00:01:58 +01:00
|
|
|
case fmt.Stringer:
|
2015-01-21 23:20:51 +01:00
|
|
|
return fmt.Sprintf("%s", v)
|
2015-01-24 00:01:58 +01:00
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%v", v)
|
2015-01-21 23:20:51 +01:00
|
|
|
}
|
|
|
|
}
|