(** Content graph Each content is associated with a vertex, and the links between each content (through the references fields) are the graph vertexes. This module provide function for the topological order traversing, or the cycle analysis. *) module Vertex = struct type t = Model.Reference.t Model.Content.t let compare t1 t2 = String.compare (Model.Content.id t1) (Model.Content.id t2) let equal t1 t2 = String.equal (Model.Content.id t1) (Model.Content.id t2) let hash t = Hashtbl.hash (Model.Content.id t) end module Edge = struct type t = Default | Field of Model.Reference.t Model.Field.t let compare e1 e2 = match (e1, e2) with | Field f1, Field f2 -> String.compare (Model.Field.name f1) (Model.Field.name f2) | Default, Default -> 0 | _, _ -> Stdlib.compare e1 e2 let default = Default end module G = Graph.Imperative.Digraph.ConcreteLabeled (Vertex) (Edge) module GV_Printer = Graph.Graphviz.Dot (struct include G let graph_attributes _ = [] let vertex_name v = Model.Content.id v let vertex_attributes _ = [] let default_vertex_attributes _ = [] let edge_attributes _ = [] let default_edge_attributes _ = [] let get_subgraph _ = None end) module Topological = Graph.Topological.Make (G) module Cycle = Graph.Cycles.Johnson (G) type t = G.t let create : unit -> t = G.create let add_edge : t -> Model.Reference.t Model.Content.t -> Model.Reference.t Model.Field.t -> Model.Reference.t Model.Content.t -> unit = fun t src field dst -> G.add_edge_e t (src, Edge.Field field, dst) let add_content : t -> Model.Reference.t Model.Content.t -> unit = G.add_vertex let pp : Format.formatter -> t -> unit = GV_Printer.fprint_graph let fold : (Model.Reference.t Model.Content.t -> 'a -> 'a) -> t -> 'a -> 'a = Topological.fold let iter : (Model.Reference.t Model.Content.t -> unit) -> t -> unit = Topological.iter exception Cycle of Model.Reference.t Model.Content.t list let check_cycle : t -> unit = fun t -> Cycle.iter_cycles (fun vertexes -> raise_notrace (Cycle vertexes)) t