forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.ml
More file actions
339 lines (310 loc) · 9.14 KB
/
Copy pathgraph.ml
File metadata and controls
339 lines (310 loc) · 9.14 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
open Stdune
module File_format = struct
type t =
| Gexf
| Dot
| Summary
let equal a b =
match a, b with
| Gexf, Gexf | Dot, Dot | Summary, Summary -> true
| _, _ -> false
;;
let conv =
( (function
| "gexf" -> Ok Gexf
| "dot" -> Ok Dot
| "summary" -> Ok Summary
| s -> Error (`Msg (Format.sprintf "%s is not a valid graph format" s)))
, fun fmt t ->
Format.pp_print_string
fmt
(match t with
| Gexf -> "gexf"
| Dot -> "dot"
| Summary -> "summary") )
;;
end
module Attribute = struct
type t =
| String of string
| Int of int
| Float of float
| Boolean of bool
let to_dyn = function
| String s -> Dyn.Variant ("String", [ Dyn.String s ])
| Int i -> Dyn.Variant ("Int", [ Dyn.Int i ])
| Float f -> Dyn.Variant ("Float", [ Dyn.Float f ])
| Boolean b -> Dyn.Variant ("Boolean", [ Dyn.Bool b ])
;;
let to_string = function
| String s -> s
| Int i -> Int.to_string i
| Float f -> Float.to_string f
| Boolean b -> Bool.to_string b
;;
let to_kind_string = function
| String _ -> "string"
| Int _ -> "int"
| Float _ -> "float"
| Boolean _ -> "boolean"
;;
end
module Node = struct
type t =
{ label : string option
; attributes : Attribute.t Int.Map.t
}
let to_dyn t =
Dyn.Record
[ "label", Dyn.Option (Option.map t.label ~f:(fun s -> Dyn.String s))
; "attributes", Int.Map.to_dyn Attribute.to_dyn t.attributes
]
;;
end
module Edge = struct
module T = struct
type t =
{ src_id : int
; dst_id : int
}
let to_dyn t = Dyn.Record [ "src_id", Dyn.Int t.src_id; "dst_id", Dyn.Int t.dst_id ]
let compare { src_id; dst_id } t =
let open Ordering.O in
let= () = Int.compare src_id t.src_id in
Int.compare dst_id t.dst_id
;;
end
include T
include Comparable.Make (T)
end
type t =
{ nodes : Node.t Int.Map.t
; edges : Edge.Set.t
; attributes : (int * string) String.Map.t
; attribute_count : int
}
let empty =
{ nodes = Int.Map.empty
; edges = Edge.Set.empty
; attributes = String.Map.empty
; attribute_count = 0
}
;;
let to_dyn t =
Dyn.Record
[ "nodes", Int.Map.to_dyn Node.to_dyn t.nodes; "edges", Edge.Set.to_dyn t.edges ]
;;
let add_node ?label t ~id ~attributes =
(* Map attributes, and possibly create a new entry *)
let attributes, attribute_count, node_attributes =
String.Map.foldi
attributes
~init:(t.attributes, t.attribute_count, Int.Map.empty)
~f:(fun attr_name value (attributes, attribute_count, node_attributes) ->
let value_kind = Attribute.to_kind_string value in
let id, kind, attribute_count =
match String.Map.find attributes attr_name with
| None -> attribute_count, value_kind, attribute_count + 1
| Some (id, kind) ->
if not (kind = value_kind)
then
failwith
(Printf.sprintf
"Attribute %s saw conflicting types %s and %s"
attr_name
kind
value_kind);
id, kind, attribute_count
in
( String.Map.set attributes attr_name (id, kind)
, attribute_count
, Int.Map.set node_attributes id value ))
in
{ t with
nodes = Int.Map.set t.nodes id { label; attributes = node_attributes }
; attributes
; attribute_count
}
;;
let add_edge t ~src_id ~dst_id =
{ t with edges = Edge.Set.add t.edges { src_id; dst_id } }
;;
let has_node t ~id = Int.Map.mem t.nodes id
let serialize_to_gexf t oc =
output_string
oc
{|<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.2draft" version="1.2">
<graph mode="static" defaultedgetype="directed">
<nodes>
|};
if t.attribute_count > 0
then (
output_string oc "<attributes class=\"node\">\n";
String.Map.iteri t.attributes ~f:(fun name (id, kind) ->
Printf.fprintf oc "<attribute id=\"%d\" title=\"%s\" type=\"%s\" />\n" id name kind);
output_string oc "</attributes>\n");
Int.Map.iteri t.nodes ~f:(fun id node ->
let label =
match node.label with
| None -> ""
| Some label -> Printf.sprintf {| label="%s"|} label
in
if Int.Map.cardinal node.attributes = 0
then Printf.fprintf oc "<node id=\"%d\"%s />\n" id label
else (
Printf.fprintf oc "<node id=\"%d\"%s>\n<attvalues>\n" id label;
Int.Map.iteri node.attributes ~f:(fun attr_id value ->
Printf.fprintf
oc
"<attvalue for=\"%d\" value=\"%s\" />\n"
attr_id
(Attribute.to_string value));
output_string oc "</attvalues>\n</node>\n"));
output_string oc "</nodes>\n<edges>\n";
let _ =
Edge.Set.fold t.edges ~init:0 ~f:(fun edge id ->
Printf.fprintf
oc
"<edge id=\"%d\" source=\"%d\" target=\"%d\" />\n"
id
edge.src_id
edge.dst_id;
id + 1)
in
output_string oc "</edges>\n</graph>\n</gexf>\n"
;;
let serialize_to_dot t oc =
output_string oc "strict digraph {\n";
Edge.Set.iter t.edges ~f:(fun edge ->
Printf.fprintf oc "n_%d -> n_%d\n" edge.src_id edge.dst_id);
output_string oc "}\n"
;;
module Aggregated = struct
type t =
{ count : int
; in_degree : int
; out_degree : int
; attributes : Attribute.t Int.Map.t
}
end
module String_opt_map = Map.Make (struct
type t = string option
let to_dyn t = Dyn.Option (Option.map t ~f:(fun s -> Dyn.String s))
let compare = Option.compare String.compare
end)
let serialize_summary t oc =
let open Aggregated in
let by_label =
Int.Map.fold t.nodes ~init:String_opt_map.empty ~f:(fun node acc ->
let label = node.label in
let attributes =
Option.value
~default:Int.Map.empty
(Option.map (String_opt_map.find acc label) ~f:(fun agg -> agg.attributes))
in
let attributes =
Int.Map.merge attributes node.attributes ~f:(fun _ old_val new_val ->
match old_val, new_val with
| None, new_val -> new_val
| old_val, None -> old_val
| Some (Int old_val), Some (Int new_val) -> Some (Int (old_val + new_val))
| Some (Float old_val), Some (Float new_val) ->
Some (Float (old_val +. new_val))
| _, _ -> None)
in
String_opt_map.update acc label ~f:(function
| None -> Some { count = 1; in_degree = 0; out_degree = 0; attributes }
| Some agg -> Some { agg with count = agg.count + 1; attributes }))
in
let by_label =
Edge.Set.fold t.edges ~init:by_label ~f:(fun edge acc ->
let get_label id =
Option.bind (Int.Map.find t.nodes id) ~f:(fun node -> node.label)
in
let src_label = get_label edge.src_id in
let dst_label = get_label edge.dst_id in
let acc =
String_opt_map.update
acc
src_label
~f:(Option.map ~f:(fun agg -> { agg with out_degree = agg.out_degree + 1 }))
in
let acc =
String_opt_map.update
acc
dst_label
~f:(Option.map ~f:(fun agg -> { agg with in_degree = agg.in_degree + 1 }))
in
acc)
in
Printf.fprintf
oc
"%14s %14s %14s %14s %14s %s\n"
"Count"
"Edges in"
"Edges out"
"Time (s)"
"Avg time (ms)"
"Label";
String_opt_map.to_list by_label
|> List.sort ~compare:(fun (_, a) (_, b) -> Int.compare b.count a.count)
|> List.iter ~f:(fun (label, agg) ->
let label =
match label with
| None -> "<unlabeled>"
| Some label -> Printf.sprintf "\"%s\"" label
in
let runtime, avg_runtime =
Option.bind (String.Map.find t.attributes "runtime") ~f:(fun (id, _) ->
Option.bind (Int.Map.find agg.attributes id) ~f:(fun value ->
match value with
| Float runtime ->
let avg_ms = 1000. *. runtime /. float_of_int agg.count in
Some (Printf.sprintf "%.4f" runtime, Printf.sprintf "%.4f" avg_ms)
| String _ | Int _ | Boolean _ -> None))
|> Option.value ~default:("", "")
in
Printf.fprintf
oc
"%14d %14d %14d %14s %14s %s\n"
agg.count
agg.in_degree
agg.out_degree
runtime
avg_runtime
label);
Printf.fprintf
oc
"nodes: %d edges: %d\n"
(Int.Map.cardinal t.nodes)
(Edge.Set.cardinal t.edges)
;;
let serialize_to_output_channel t oc ~format =
match (format : File_format.t) with
| Gexf -> serialize_to_gexf t oc
| Dot -> serialize_to_dot t oc
| Summary -> serialize_summary t oc
;;
let serialize t ~path ~format =
Io.with_file_out path ~f:(serialize_to_output_channel t ~format)
;;
let print t ~format = serialize_to_output_channel t Stdlib.stdout ~format
module For_tests = struct
let print t ~format ~opaque_attributes =
let opaque_graph =
{ t with
nodes =
Int.Map.map t.nodes ~f:(fun node ->
{ node with
attributes =
Int.Map.mapi node.attributes ~f:(fun id attr ->
if Int.Set.mem opaque_attributes id
then Attribute.String "<opaque>"
else attr)
})
}
in
print opaque_graph ~format
;;
end