// GraphAsText is the standard implementation of Grapher func (g *defaultGrapher) GraphAsText(dotText []byte) (string, error) { graphAst, err := parser.ParseBytes(dotText) if err != nil { return "", err } dag := ggv.NewGraph() // A directed acyclic graph ggv.Analyse(graphAst, dag) if len(dag.Edges.Edges) == 0 { return "", errNoActivity } nodeToOutEdges := g.collectionGetter.generateNodeToOutEdges(dag) inDegreeZeroNodes := g.collectionGetter.getInDegreeZeroNodes(dag) nameToNodes := dag.Nodes.Lookup buffer := new(bytes.Buffer) statusMap := make(map[string]discoveryStatus) for _, root := range inDegreeZeroNodes { g.searcher.dfs(searchArgs{ root: root, path: nil, nodeToOutEdges: nodeToOutEdges, nameToNodes: nameToNodes, buffer: buffer, statusMap: statusMap, }) } return buffer.String(), nil }
func (this *DotSerializer) Deserialize(data []byte) (*DAG, error) { ast, err := dotparser.ParseBytes(data) if err != nil { return nil, err } graph := dot.NewGraph() dot.Analyse(ast, graph) p := NewDAG(graph.Name) for src, dests := range graph.Edges.SrcToDsts { for dest, _ := range dests { if orig, ok := p.Jobs[src]; !ok { n := dotNameToDAGJob(graph, src) n.Post = append(n.Post, dest) p.Jobs[src] = n } else { orig.Post = append(orig.Post, dest) } if orig, ok := p.Jobs[dest]; !ok { n := dotNameToDAGJob(graph, dest) n.Prev = append(n.Prev, src) p.Jobs[dest] = n } else { orig.Prev = append(orig.Prev, src) } if _, ok := p.InDegrees[src]; !ok { p.InDegrees[src] = 0 } if orig, ok := p.InDegrees[dest]; !ok { p.InDegrees[dest] = 1 } else { p.InDegrees[dest] = orig + 1 } } } for _, edge := range graph.Edges.Edges { if v, ok := edge.Attrs["nonstrict"]; ok { nonstrict, err := util.StringToBool(strings.Trim(v, "\"")) if err != nil { log.Error(err) return nil, err } if len(edge.Src) != 0 && len(edge.Dst) != 0 { r := NewRelation() r.NonStrict = nonstrict if _, ok := p.Relations[edge.Src]; !ok { p.Relations[edge.Src] = make(map[string]*Relation) } p.Relations[edge.Src][edge.Dst] = r } } } return p, nil }
//Parses the buffer into a abstract syntax tree representing the graph. func Parse(buf []byte) (*ast.Graph, error) { return parser.ParseBytes(buf) }