diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
| commit | 830e7c5d27d161b91402414e6ecd49f1ed818bfb (patch) | |
| tree | 448864635d29b1ea66424cbb9e0816fbcf1e1907 /lib/configuration/graph_tree.ml | |
Initial commit
Diffstat (limited to 'lib/configuration/graph_tree.ml')
| -rw-r--r-- | lib/configuration/graph_tree.ml | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/configuration/graph_tree.ml b/lib/configuration/graph_tree.ml new file mode 100644 index 0000000..25c9366 --- /dev/null +++ b/lib/configuration/graph_tree.ml @@ -0,0 +1,71 @@ +(** 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 |
