-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrp2dot.ml
More file actions
102 lines (91 loc) · 2.41 KB
/
grp2dot.ml
File metadata and controls
102 lines (91 loc) · 2.41 KB
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
(* grp2dot. 2021.10.28 *)
(*
converter from .grp data to .dot data
Usage: grp2dot aaa.grp
- output: stdout in the dot format
- In grp, each edge can have multiple weights
-- An edge with multiple weights converted to multiple edges of same nodes with single weight
-- 1st edge: black + solid
-- 2nd edge: black + dotted
-- 3rd edge: blue + solid
-- 4th edge: blue + dotted
-- 5th edge: red + solid
-- 6th edge: red + dotted
*)
open Grpdata
;;
let string_of_list sep to_string ls =
List.fold_left (fun str x -> if str = "" then to_string x else str ^ sep ^ (to_string x)) "" ls
;;
(* read from file *)
let inputstr_stdin () =
let x = ref "" in
try
while true do
x := !x ^ (input_line stdin) ^ "\n"
done ;
"" (* dummy *)
with End_of_file -> !x ;;
let readfile strp filename =
let ic = open_in filename in
try
while true do
strp := !strp ^ (input_line ic) ^ "\n"
done
with End_of_file -> close_in ic
;;
(* parser *)
let parse str =
Gparser.main Glexer.token
(Lexing.from_string str)
;;
let speclist = []
;;
let msgUsage = "USAGE: grp2dot <grpfile>"
;;
let usage_and_exit () =
Arg.usage speclist msgUsage;
exit 0
;;
let to_dot gtype gbody : string =
let name = "mygraph" in
let gtypeS = match gtype with DIRECTED -> "digraph" | UNDIRECTED -> "graph" in
let edgeS = match gtype with DIRECTED -> "->" | UNDIRECTED -> "--" in
let gbodyS =
List.fold_left
(fun s (n1,n2,ww) ->
let n1S = string_of_int n1 in
let n2S = string_of_int n2 in
let edgeHeaderS = n1S ^ " " ^ edgeS ^ " " ^ n2S in
let edges = ref "" in
for i = 0 to List.length ww - 1 do
let w = List.nth ww i in
let styleS =
match i mod 2 with
| 0 -> ""
| _ -> " style=dotted"
in
let colorS =
match (i / 2) mod 3 with
| 0 -> ""
| 1 -> " color=blue"
| _ -> " color=red"
in
let labelS = "label = \"" ^ (string_of_int w) ^ "\"" in
let attriv = labelS ^ colorS ^ styleS in
edges := !edges ^ (edgeHeaderS ^ " [" ^ attriv ^ "];")
done;
s ^ "\n " ^ !edges
)
""
gbody
in
gtypeS ^ " " ^ name ^ "{" ^ gbodyS ^ "\n}"
;;
let () =
let strp = ref "" in
Arg.parse speclist (readfile strp) msgUsage;
let (gtype,gbody) = parse !strp in
let dotfile = to_dot gtype gbody in
print_endline dotfile
;;