-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.go
103 lines (91 loc) · 1.9 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gdi
import (
"fmt"
"strings"
"sync"
)
//
//var g *graph
//
//func init() {
// g = &graph{
// nodes: make(map[string]*node),
// }
//}
type node struct {
name string
fields []*nodeItem
edges []*edge
//fieldMap map[string]struct{}
//fields map[string]string
//edges map[string]string
}
type edge struct {
from string
to string
}
type nodeItem struct {
fieldName string
fieldType string
}
type graph struct {
lock sync.Mutex
nodes map[string]*node
}
func (n *node) addFiled(f *nodeItem) {
//if n.fields ==nil {
// n.fields =make(map[string]string)
//}
//n.fields[f.fieldType]=f.fieldName
n.fields = append(n.fields, f)
}
func (n *node) addEdge(e *edge) {
//if n.edges==nil {
// n.edges=make(map[string]string)
//}
//n.edges[e.from]=e.to
n.edges = append(n.edges, e)
}
func (gdi *GDIPool) addNode(n *node) {
gdi.g.lock.Lock()
defer gdi.g.lock.Unlock()
if _, ok := gdi.g.nodes[n.name]; !ok {
gdi.g.nodes[n.name] = n
}
}
func (gdi *GDIPool) Graph() string {
var gs []string
/*
"node0" [
label = "<f0> 0x10ba8| <f1>"
shape = "record"
];
*/
nodeTpl := `
"%v" [
label = <%v>
shape = "none"
]
`
gTpl := `
digraph {
rankdir=LR;
%v
}
`
for _, n := range gdi.g.nodes {
var fields []string
fields = append(fields, fmt.Sprintf(`<table BORDER="1" CELLBORDER="1" CELLSPACING="0"><tr><td PORT="f100"><font POINT-SIZE="18"><b>struct %v</b></font></td></tr>`, n.name))
for _, field := range n.fields {
ns := strings.Split(field.fieldName, "#")
fields = append(fields, fmt.Sprintf(`<tr><td PORT="%v">%v %v</td></tr>`, ns[0], ns[1], field.fieldType))
}
gs = append(gs, fmt.Sprintf(nodeTpl, n.name, strings.Join(fields, "")+"</table>"))
var edges []string
for _, e := range n.edges {
edges = append(edges, fmt.Sprintf(`%v->"%v":f100;`, e.from, e.to))
}
gs = append(gs, strings.Join(edges, "\n"))
}
return fmt.Sprintf(gTpl, strings.Join(gs, "\n"))
}