Пример #1
0
func (sg *SubGraph) Dotty(labels *digraph.Labels, highlightVertices, highlightEdges map[int]bool) string {
	V := make([]string, 0, len(sg.V))
	E := make([]string, 0, len(sg.E))
	for vidx, v := range sg.V {
		highlight := ""
		if highlightVertices[vidx] {
			highlight = " color=red"
		}
		V = append(V, fmt.Sprintf(
			"n%v [label=\"%v\"%v];",
			vidx,
			labels.Label(v.Color),
			highlight,
		))
	}
	for eidx, e := range sg.E {
		highlight := ""
		if highlightEdges[eidx] {
			highlight = " color=red"
		}
		E = append(E, fmt.Sprintf(
			"n%v->n%v [label=\"%v\"%v]",
			e.Src,
			e.Targ,
			labels.Label(e.Color),
			highlight,
		))
	}
	return fmt.Sprintf("digraph{\n%v\n%v\n}", strings.Join(V, "\n"), strings.Join(E, "\n"))
}
Пример #2
0
func (emb *Embedding) Dotty(labels *digraph.Labels, attrs map[int]map[string]interface{}) string {
	V := make([]string, 0, len(emb.SG.V))
	E := make([]string, 0, len(emb.SG.E))
	// TODO: Replace this with strconv.Quote
	safeStr := func(i interface{}) string {
		s := fmt.Sprint(i)
		s = strings.Replace(s, "\n", "\\n", -1)
		s = strings.Replace(s, "\"", "\\\"", -1)
		return s
	}
	renderAttrs := func(color, id int) string {
		a := attrs[id]
		label := labels.Label(color)
		strs := make([]string, 0, len(a)+1)
		strs = append(strs, fmt.Sprintf(`idx="%v"`, id))
		if line, has := a["start_line"]; has {
			strs = append(strs, fmt.Sprintf(`label="%v\n[line: %v]"`, safeStr(label), safeStr(line)))
		} else {
			strs = append(strs, fmt.Sprintf(`label="%v"`, safeStr(label)))
		}
		for name, value := range a {
			if name == "label" || name == "id" {
				continue
			}
			strs = append(strs, fmt.Sprintf("%v=\"%v\"", name, safeStr(value)))
		}
		return strings.Join(strs, ",")
	}
	for idx, id := range emb.Ids {
		V = append(V, fmt.Sprintf(
			"%v [%v];",
			id,
			renderAttrs(emb.SG.V[idx].Color, id),
		))
	}
	for idx := range emb.SG.E {
		e := &emb.SG.E[idx]
		E = append(E, fmt.Sprintf(
			"%v -> %v [label=\"%v\"];",
			emb.Ids[e.Src],
			emb.Ids[e.Targ],
			safeStr(labels.Label(e.Color)),
		))
	}
	return fmt.Sprintf(
		`digraph {
    %v
    %v
}
`, strings.Join(V, "\n    "), strings.Join(E, "\n    "))
}
Пример #3
0
func (sg *SubGraph) Pretty(labels *digraph.Labels) string {
	V := make([]string, 0, len(sg.V))
	E := make([]string, 0, len(sg.E))
	for _, v := range sg.V {
		V = append(V, fmt.Sprintf(
			"(%v)",
			labels.Label(v.Color),
		))
	}
	for _, e := range sg.E {
		E = append(E, fmt.Sprintf(
			"[%v->%v:%v]",
			e.Src,
			e.Targ,
			labels.Label(e.Color),
		))
	}
	return fmt.Sprintf("{%v:%v}%v%v", len(sg.E), len(sg.V), strings.Join(V, ""), strings.Join(E, ""))
}