Source file paje.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
let magic_trace = "Paje trace\000(magic\001)"
let magic_kind = "Paje kind\000(magic\001)"
let t = magic_trace
let () = Utils.paje_t := t
type properties = (string * string) list
type name = string
type alias = string
type color = float * float * float
let string_of_color (r, g, b) =
Printf.sprintf "%f %f %f" r g b
type field_type =
| Date
| Double
| String
| Color
let string_of_field_type = function
| Date -> "date"
| Double -> "double"
| String -> "string"
| Color -> "color"
type event = {
event_name : string;
event_id : int;
event_fields : (string * field_type) list;
}
let string_of_event ev =
let field_def (nam, typ) =
Printf.sprintf "%%\t%s\t%s" nam (string_of_field_type typ) in
((Printf.sprintf "%%EventDef\t%s\t%d" ev.event_name ev.event_id)
:: (List.map field_def ev.event_fields))
@ ["%EndEventDef"]
let event_def =
let id = ref 0 in
fun name l ->
incr id;
{ event_name = name;
event_id = !id;
event_fields = l; }
let predefined_events = [
event_def "PajeDefineContainerType"
["Name", String; "Type", String; "Alias", String];
event_def "PajeDefineStateType"
["Name", String; "Type", String; "Alias", String];
event_def "PajeDefineEventType"
["Name", String; "Type", String; "Alias", String];
event_def "PajeDefineVariableType"
["Name", String; "Type", String; "Color", Color; "Alias", String];
event_def "PajeDefineLinkType"
["Name", String; "Type", String; "StartContainerType", String; "EndContainerType", String; "Alias", String];
event_def "PajeDefineEntityValue"
["Name", String; "Type", String; "Color", Color; "Alias", String];
event_def "PajeCreateContainer"
["Time", Date; "Name", String; "Type", String; "Container", String; "Alias", String];
event_def "PajeDestroyContainer"
["Time", Date; "Name", String; "Type", String];
event_def "PajeSetState"
["Time", Date; "Type", String; "Container", String; "Value", String];
event_def "PajePushState"
["Time", Date; "Type", String; "Container", String; "Value", String];
event_def "PajePopState"
["Time", Date; "Type", String; "Container", String];
event_def "PajeResetState"
["Time", Date; "Type", String; "Container", String];
event_def "PajeNewEvent"
["Time", Date; "Type", String; "Container", String; "Value", String];
event_def "PajeSetVariable"
["Time", Date; "Type", String; "Container", String; "Value", Double];
event_def "PajeAddVariable"
["Time", Date; "Type", String; "Container", String; "Value", Double];
event_def "PajeSubVariable"
["Time", Date; "Type", String; "Container", String; "Value", Double];
event_def "PajeStartLink"
["Time", Date; "Type", String; "Container", String; "StartContainer", String; "Value", String; "Key", String];
event_def "PajeEndLink"
["Time", Date; "Type", String; "Container", String; "EndContainer", String; "Value", String; "Key", String]
]
let define_container_type ~name ?(typ="0") ?(alias=name) l =
[magic_kind, "PajeDefineContainerType";
"Name", name;
"Type", typ;
"Alias", alias]
@ l
let define_state_type ~name ~typ ?(alias=name) l =
[magic_kind, "PajeDefineStateType";
"Name", name;
"Type", typ;
"Alias", alias]
@ l
let define_event_type ~name ~typ ?(alias=name) l =
[magic_kind, "PajeDefineEventType";
"Name", name;
"Type", typ;
"Alias", alias]
@ l
let define_variable_type ~name ~typ ~color ?(alias=name) l =
[magic_kind, "PajeDefineVariableType";
"Name", name;
"Type", typ;
"Color", string_of_color color;
"Alias", alias]
@ l
let define_link_type ~name ~typ ~start_container_type ~end_container_type ?(alias=name) l =
[magic_kind, "PajeDefineLinkType";
"Name", name;
"Type", typ;
"StartContainerType", start_container_type;
"EndContainerType", end_container_type;
"Alias", alias]
@ l
let define_entity_value ~name ~typ ~color ?(alias=name) l =
[magic_kind, "PajeDefineEntityValue";
"Name", name;
"Type", typ;
"Color", string_of_color color;
"Alias", alias]
@ l
let create_container ~name ~typ ?(container="0") ?(alias=name) l =
[magic_kind, "PajeCreateContainer";
"Name", name;
"Alias", alias;
"Type", typ;
"Container", container]
@ l
let destroy_container ~name ~typ l =
[magic_kind, "PajeDestroyContainer";
"Name", name;
"Type", typ]
@ l
let set_state ~typ ~container ~value l =
[magic_kind, "PajeSetState";
"Type", typ;
"Container", container;
"Value", value]
@ l
let push_state ~typ ~container ~value l =
[magic_kind, "PajePushState";
"Type", typ;
"Container", container;
"Value", value]
@ l
let pop_state ~typ ~container l =
[magic_kind, "PajePopState";
"Type", typ;
"Container", container]
@ l
let reset_state ~typ ~container l =
[magic_kind, "PajeResetState";
"Type", typ;
"Container", container]
@ l
let new_event ~typ ~container ~value l =
[magic_kind, "PajeNewEvent";
"Type", typ;
"Container", container;
"Value", value]
@ l
let set_variable ~typ ~container ~value l =
[magic_kind, "PajeSetVariable";
"Type", typ;
"Container", container;
"Value", string_of_float value]
@ l
let add_variable ~typ ~container ~value l =
[magic_kind, "PajeAddVariable";
"Type", typ;
"Container", container;
"Value", string_of_float value]
@ l
let sub_variable ~typ ~container ~value l =
[magic_kind, "PajeSubVariable";
"Type", typ;
"Container", container;
"Value", string_of_float value]
@ l
let start_link ~typ ~container ~start_container ~value ~key l =
[magic_kind, "PajeStartLink";
"Type", typ;
"Container", container;
"StartContainer", start_container;
"Value", value;
"Key", key]
@ l
let end_link ~typ ~container ~end_container ~value ~key l =
[magic_kind, "PajeEndLink";
"Type", typ;
"Container", container;
"EndContainer", end_container;
"Value", value;
"Key", key]
@ l
let l = List.partition (fun (x, _) -> x = magic_kind) l
let render e =
let default = function
| Date -> string_of_float ((float e.Event.relative) /. 1000.)
| Double -> "0.0"
| String -> "\"\""
| Color -> "1.0 1.0 1.0" in
let make_fields id fields properties =
let res =
List.map
(fun (nam, typ) ->
try
let res = List.assoc nam properties in
if typ = String then Printf.sprintf "%S" res else res
with Not_found -> default typ)
fields in
let res = (string_of_int id) :: res in
String.concat " " res in
if e.Event.message = magic_trace then
try
let kind, rest = extract_kind e.Event.properties in
match kind with
| [_, kind] ->
let def =
List.find
(fun ed -> kind = ed.event_name)
predefined_events in
make_fields def.event_id def.event_fields rest
| _ -> ""
with _ -> ""
else
""
let = List.concat (List.map string_of_event predefined_events)
let layout = header, [], render
let = [], [], render
let () =
List.iter
(fun (x, y) -> Layout.register x y)
[ "paje", layout ;
"paje_noheader", layout_noheader ]
type type_kind = Container | State | Event | Variable | Link | Entity_value
let string_of_type_kind = function
| Container -> "container"
| State -> "state"
| Event -> "event"
| Variable -> "variable"
| Link -> "link"
| Entity_value -> "entity value"
exception Invalid_type of type_kind
let () =
Printexc.register_printer
(function
| Invalid_type tk ->
let msg = Printf.sprintf "Invalid Pajé %s" (string_of_type_kind tk) in
Some msg
| _ -> None)
module type Definitions = sig
val logger : string
val level : Level.t
type container_type
val container_types : (container_type * name * (container_type option) * alias) list
type state_type
val state_types : (state_type * name * container_type * alias) list
type event_type
val event_types : (event_type * name * container_type * alias) list
type variable_type
val variable_types : (variable_type * name * container_type * color * alias) list
type link_type
val link_types : (link_type * name * container_type * container_type * container_type * alias) list
type entity_value_type
val entity_value_types : (entity_value_type * name * container_type * color * alias) list
end
module type S = sig
val t : string
type properties = (string * string) list
type name = string
type alias = string
type color = float * float * float
type container_type
type state_type
type event_type
type variable_type
type link_type
type entity_value_type
val create_container : name:name -> typ:container_type -> ?container:name -> ?alias:alias -> properties -> properties
val destroy_container : name:name -> typ:container_type -> properties -> properties
val set_state : typ:state_type -> container:name -> value:string -> properties -> properties
val push_state : typ:state_type -> container:name -> value:string -> properties -> properties
val pop_state : typ:state_type -> container:name -> properties -> properties
val reset_state : typ:state_type -> container:name -> properties -> properties
val new_event : typ:event_type -> container:name -> value:string -> properties -> properties
val set_variable : typ:variable_type -> container:name -> value:float -> properties -> properties
val add_variable : typ:variable_type -> container:name -> value:float -> properties -> properties
val sub_variable : typ:variable_type -> container:name -> value:float -> properties -> properties
val start_link : typ:link_type -> container:name -> start_container:name -> value:string -> key:string -> properties -> properties
val end_link : typ:link_type -> container:name -> end_container:name -> value:string -> key:string -> properties -> properties
end
module Make (D : Definitions) = struct
let t = t
type properties = (string * string) list
type name = string
type alias = string
type color = float * float * float
type container_type = D.container_type
type state_type = D.state_type
type event_type = D.event_type
type variable_type = D.variable_type
type link_type = D.link_type
type entity_value_type = D.entity_value_type
let make tk =
let h = Hashtbl.create 17 in
let g x =
try
Hashtbl.find h x
with Not_found ->
raise (Invalid_type tk) in
h, g
let containers, get_container = make Container
let states, get_state = make State
let events, get_event = make Event
let variables, get_variable = make Variable
let links, get_link = make Link
let entity_values, _ = make Entity_value
let () =
List.iter
(fun (x, name, parent, alias) ->
Hashtbl.add containers x name;
let typ = match parent with
| Some x ->
(try
Hashtbl.find containers x
with _ -> "0")
| None -> "0" in
let properties = define_container_type ~name ~typ ~alias [] in
Logger.log D.logger D.level ~properties t)
D.container_types;
List.iter
(fun (x, name, typ, alias) ->
Hashtbl.add states x name;
let typ = get_container typ in
let properties = define_state_type ~name ~typ ~alias [] in
Logger.log D.logger D.level ~properties t)
D.state_types;
List.iter
(fun (x, name, typ, alias) ->
Hashtbl.add events x name;
let typ = get_container typ in
let properties = define_event_type ~name ~typ ~alias [] in
Logger.log D.logger D.level ~properties t)
D.event_types;
List.iter
(fun (x, name, typ, color, alias) ->
Hashtbl.add variables x name;
let typ = get_container typ in
let properties = define_variable_type ~name ~typ ~color ~alias [] in
Logger.log D.logger D.level ~properties t)
D.variable_types;
List.iter
(fun (x, name, typ, start_container_type, end_container_type, alias) ->
Hashtbl.add links x name;
let typ = get_container typ in
let start_container_type = get_container start_container_type in
let end_container_type = get_container end_container_type in
let properties = define_link_type ~name ~typ ~start_container_type ~end_container_type ~alias [] in
Logger.log D.logger D.level ~properties t)
D.link_types;
List.iter
(fun (x, name, typ, color, alias) ->
Hashtbl.add entity_values x name;
let typ = get_container typ in
let properties = define_entity_value ~name ~typ ~color ~alias [] in
Logger.log D.logger D.level ~properties t)
D.entity_value_types
let create_container ~name ~typ ?(container="0") ?(alias=name) l =
let typ = get_container typ in
create_container ~name ~typ ~container ~alias l
let destroy_container ~name ~typ l =
let typ = get_container typ in
destroy_container ~name ~typ l
let set_state ~typ ~container ~value l =
let typ = get_state typ in
set_state ~typ ~container ~value l
let push_state ~typ ~container ~value l =
let typ = get_state typ in
push_state ~typ ~container ~value l
let pop_state ~typ ~container l =
let typ = get_state typ in
pop_state ~typ ~container l
let reset_state ~typ ~container l =
let typ = get_state typ in
reset_state ~typ ~container l
let new_event ~typ ~container ~value l =
let typ = get_event typ in
new_event ~typ ~container ~value l
let set_variable ~typ ~container ~value l =
let typ = get_variable typ in
set_variable ~typ ~container ~value l
let add_variable ~typ ~container ~value l =
let typ = get_variable typ in
add_variable ~typ ~container ~value l
let sub_variable ~typ ~container ~value l =
let typ = get_variable typ in
sub_variable ~typ ~container ~value l
let start_link ~typ ~container ~start_container ~value ~key l =
let typ = get_link typ in
start_link ~typ ~container ~start_container ~value ~key l
let end_link ~typ ~container ~end_container ~value ~key l =
let typ = get_link typ in
end_link ~typ ~container ~end_container ~value ~key l
end