blob: 64cef4020834963f01e29b96000a898432efa6e5 (
plain)
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
|
(** 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.content
let compare t1 t2 =
Model.Identifier.compare (Model.Content.id t1) (Model.Content.id t2)
let equal t1 t2 =
Model.Identifier.equal (Model.Content.id t1) (Model.Content.id t2)
let hash t = Model.Identifier.hash (Model.Content.id t)
end
module Edge = struct
type t = Default | Field of Model.field
let compare e1 e2 =
match (e1, e2) with
| Field f1, Field f2 ->
Model.Identifier.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.Identifier.to_string (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.content -> Model.field -> Model.content -> unit =
fun t src field dst -> G.add_edge_e t (src, Edge.Field field, dst)
let add_content : t -> Model.content -> unit = G.add_vertex
let pp : Format.formatter -> t -> unit = GV_Printer.fprint_graph
let fold : (Model.content -> 'a -> 'a) -> t -> 'a -> 'a = Topological.fold
let iter : (Model.content -> unit) -> t -> unit = Topological.iter
exception Cycle of Model.content list
let check_cycle : t -> unit =
fun t -> Cycle.iter_cycles (fun vertexes -> raise_notrace (Cycle vertexes)) t
|