package catala

  1. Overview
  2. Docs

Source file graphs.ml

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
module JSON_Graph
    (V : sig
      include Graph.Sig.COMPARABLE

      val format : Format.formatter -> t -> unit
    end)
    (G : Graph.Sig.G with type V.t = V.t) =
struct
  module H = Hashtbl.Make (V)

  module Edges = Set.Make (struct
    type t = int * int

    let compare = compare
  end)

  let graph_to_json g =
    let ids = H.create (G.nb_vertex g) in
    let nodes =
      G.fold_vertex
        (fun v acc ->
          let id = H.length ids in
          H.add ids v id;
          (string_of_int id, `String (Format.asprintf "%a" V.format v)) :: acc)
        g []
    in
    let edges =
      G.fold_edges
        (fun v1 v2 acc -> Edges.add (H.find ids v1, H.find ids v2) acc)
        g Edges.empty
      |> Edges.elements
      |> List.map (fun (from, to_) ->
          `Assoc ["from", `Int from; "to", `Int to_])
    in
    `Assoc ["nodes", `Assoc nodes; "edges", `List edges]
end