blob: 6d29d0c9df4b6137c8fb777d219b34961d23b169 (
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
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
|
open StdLabels
module Splay = Splay.Make (Model.Identifier)
let escaped = String.escaped
type setting = {
mutable contents : Model.content Splay.t;
lists : Model.List.t list;
}
let init : unit -> setting = fun () -> { contents = Splay.empty (); lists = [] }
(** Get the content from the setting. *)
let get_content : setting -> Model.Identifier.t -> Model.content option =
fun settings name -> Splay.find name settings.contents
(** Get the list declared with the given name, if any *)
let get_list : setting -> Model.Identifier.t -> Model.List.t option =
fun setting name ->
List.find_opt setting.lists ~f:(fun l ->
Model.Identifier.equal l.Model.List.id name)
(** All the lists declared in the setting *)
let lists : setting -> Model.List.t list = fun setting -> setting.lists
exception BrokenLink : ('a Type.Id.t * 'a * Model.Identifier.t) -> exn
(** Register a content and all the associated subcontent in the top level,
giving the possibility to fetch any content or subcontent by its name
without traversing the whole tree each time. *)
let register_content : setting -> Model.content -> unit =
fun setting content ->
let rec traverse content =
let () =
setting.contents <-
Splay.add (Model.Content.id content) content setting.contents
in
List.iter ~f:traverse (Model.Content.subcontents content)
in
traverse content
let register_list : setting -> Model.List.t -> setting =
fun t l -> { t with lists = l :: t.lists }
(** Force the evaluation of the field target and ensure it points to an existing
content in the application.
Return the content targeted *)
let get_reference_target : Model.field -> Model.content option =
fun field ->
let (T ty) = Model.Field.type_ field in
match ty with
| Text -> None
| Integer -> None
| MonoReference r | MultiReference r -> (
(* References to static lists are leaves of the dependency graph: they
are not followed here. *)
match r.Model.Typ.t with
| Model.Typ.List _ -> None
| Model.Typ.Content ref -> (
try Some (Model.Reference.get ref)
with BrokenLink _ as e -> raise_notrace e))
(** Extract the other contents pointed by a given content, and the field used
for it. *)
let targeted_contents : Model.content -> (Model.field * Model.content) Seq.t =
fun t ->
Seq.filter_map
(fun field ->
match get_reference_target field with
| None -> None
| Some ref -> Some (field, ref))
(Model.Content.fields t)
let tree : ?required:bool -> setting -> Graph_tree.t =
fun ?(required = false) setting ->
let tree = Graph_tree.create () in
let () =
Splay.fold
(fun () (_, src) ->
let targets = targeted_contents src in
let root = Model.Content.root src in
Graph_tree.add_content tree root;
Seq.iter
(fun (field, dst) ->
(* We build the dependencies tree with the root contents only.
This does not change the logic, and this makes things easer
too *)
let add_edge = (not required) || Model.Field.required field in
match add_edge with
| false -> ()
| true ->
Graph_tree.add_edge tree root field (Model.Content.root dst))
targets)
() setting.contents
in
tree
(** Return all the contents declared in the settings.
The list provide the root contents in the topological order *)
let root_contents : setting -> Model.content list =
fun setting -> Graph_tree.fold (fun a acc -> a :: acc) (tree setting) []
let check : setting -> (unit, exn) result =
fun setting ->
(* Ensure each content referenced exists.
We accept mutually recursive dependancy and need to load all the content
first before checking the settings *)
try
let _ = tree setting in
(* Check cycle dependencies *)
let required_tree = tree ~required:true setting in
Graph_tree.check_cycle required_tree;
Ok ()
with e -> Error e
|