Skip to content

Commit e2243f2

Browse files
committed
graph viz
1 parent 71fccf6 commit e2243f2

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
build/**
22
.vscode/launch.json
3+
.ipynb_checkpoints/
4+
__pycache__

scripts/graphviz.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import matplotlib.cm
2+
import random
3+
import matplotlib
4+
import json
5+
import pydot
6+
7+
8+
with open('data/default_rules.json')as fp:
9+
rules = json.load(fp)
10+
11+
def parse_tree(rules, mark_required = False):
12+
tree = dict()
13+
for r in rules:
14+
pt = r['pointer']
15+
pt = pt.replace('/*','')
16+
17+
path = pt.split('/')
18+
if pt == '/':
19+
path = ['']
20+
21+
subtree = tree
22+
for k in path:
23+
if k not in subtree:
24+
subtree[k] = dict()
25+
subtree = subtree[k]
26+
req, opt = (getter(r,'required'), getter(r, 'optional'))
27+
for k in req:
28+
if k not in subtree:
29+
subtree[k] = {}
30+
if mark_required:
31+
subtree[k]['R'] = '0'
32+
for k in opt:
33+
if k not in subtree:
34+
subtree[k] = {}
35+
return tree
36+
37+
def rename(node):
38+
for k in list(node.keys()):
39+
v = node[k]
40+
if k == 'R':
41+
continue
42+
if k in rename.used_names:
43+
rename.used_names[k] += 1
44+
node[k+' '*(used_names[k])] = v
45+
node.pop(k)
46+
else:
47+
rename.used_names[k] = 0
48+
rename(v)
49+
rename.used_names = dict()
50+
51+
52+
53+
54+
def draw(parent_name, child_name, req = False):
55+
c = matplotlib.colors.rgb2hex(matplotlib.cm.tab10(random.random()))
56+
if req:
57+
args = dict(color='black')
58+
else:
59+
args = dict(color=c, style='dashed')
60+
edge = pydot.Edge(parent_name, child_name, **args)
61+
graph.add_edge(edge)
62+
63+
def visit(node, parent=None):
64+
for k, v in node.items():
65+
if k == 'R':
66+
continue
67+
if isinstance(v, dict):
68+
if parent:
69+
draw(parent, k, 'R' in v)
70+
visit(v, k)
71+
else:
72+
draw(parent, k)
73+
74+
75+
tree = parse_tree(rules, True)
76+
rename(tree)
77+
graph = pydot.Dot(graph_type='graph',rankdir='LR')
78+
visit(tree[''], 'PolyFEM')
79+
graph.write_png('example1_graph.png')

0 commit comments

Comments
 (0)