forked from jsemric/ahofa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_nfa.py
executable file
·72 lines (56 loc) · 1.88 KB
/
draw_nfa.py
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
#!/usr/bin/env python3
# Jakub Semric 2018
import argparse
import subprocess
import math
from nfa import Nfa
def write_output(fname, buf):
with open(fname, 'w') as f:
for i in buf:
f.write(i)
def get_freq(fname):
ret = {}
with open(fname, 'r') as f:
for line in f:
line = line.split('#')[0]
if line:
state, freq, *_ = line.split()
ret[int(state)] = int(freq)
return ret
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-o','--output', type=str, metavar='FILE', default="Aut.dot",
help='output file')
parser.add_argument('input', metavar='NFA', type=str)
parser.add_argument('-f', '--freq', type=str, help='heat map')
parser.add_argument(
'-t', '--trans', action='store_true',
help='show transition labels')
parser.add_argument(
'-r', '--rules', type=int, help='number of rules to show')
parser.add_argument(
'-d', '--depth', type=int, help='maximal depth of a state to display')
args = parser.parse_args()
aut = Nfa.parse(args.input)
freq = None
if args.freq:
freq = get_freq(args.freq)
states = set(aut.states)
if args.rules:
rules = list(aut.fin_pred().values())[0:args.rules]
states = set([s for subl in rules for s in subl])
if args.depth:
depth = aut.state_depth
states = set(filter(lambda x: depth[x] < args.depth, states))
out = aut.write_dot(
show_trans=args.trans, freq=freq, states=states,
freq_scale=lambda x: math.log(x + 2), show_diff=0)
write_output(args.output, out)
image = args.output.split('.dot')[0] + '.jpg'
prog = 'dot -Tjpg ' + args.output + ' -o ' + image
subprocess.call(prog.split())
prog = 'xdg-open ' + image
subprocess.call(prog.split())
if __name__ == "__main__":
main()