-
Notifications
You must be signed in to change notification settings - Fork 0
/
gromit-lolevel.scm
588 lines (477 loc) · 17.6 KB
/
gromit-lolevel.scm
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; gromit-lolevel - Stuff that's required at expansion time and run time.
;;;
;;; gromit manages a set of typed objects that can be persisted to an SQLite
;;; database. The objects can be linked together with labelled edges.
;;;
;;; gromit objects are made up of attributes and properties. Attributes are
;;; simple, single valued data fields. Properties are lists of sets of fields.
;;; The user can select from the built-in data types for fields or define their
;;; own. Edges are much simpler: they specify a start point, an end point, have
;;; a label and are directional.
;;;
;;;
;;; Copyright (C) 2019, Andy Bennett, Register Dynamics Limited.
;;; All rights reserved.
;;;
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions are met:
;;;
;;; Redistributions of source code must retain the above copyright notice, this
;;; list of conditions and the following disclaimer.
;;; Redistributions in binary form must reproduce the above copyright notice,
;;; this list of conditions and the following disclaimer in the documentation
;;; and/or other materials provided with the distribution.
;;; Neither the name of the author nor the names of its contributors may be
;;; used to endorse or promote products derived from this software without
;;; specific prior written permission.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
;;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;;; POSSIBILITY OF SUCH DAMAGE.
;;;
;;; Andy Bennett <[email protected]>, 2019/07/01
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module gromit-lolevel
(; Helper Macros
fields->list
field-type->syntax-type*
; Object IDs
make-object-id
object-id-node-id
object-id-seqno
; Current User
current-gromit-user
; Attribute Types
make-attribute-type*
attribute-type-name
attribute-type-column-type
attribute-type-serialiser
attribute-type-deserialiser
attribute-type-initialiser
attribute-type-setter
attribute-type-getter
; Attribute Specs
attribute-spec?
attribute-spec-field-idx
attribute-spec-type
attribute-spec-name
attribute-spec-required?
attribute-spec-constant?
attribute-spec-key-part
; Property Types
property-spec?
make-property-type*
property-type-name
property-type-table
property-type-key-specs
property-type-attribute-specs
; Property Specs
property-spec-field-idx
property-spec-type
property-spec-name
; Object Fields Specs
field-spec-name
; Object Types
make-object-type*
object-type-name
object-type-table
object-type-metadata-type
object-type-attribute-specs
object-type-property-specs
object-type-field-names
)
(import chicken scheme)
; Units - http://api.call-cc.org/doc/chicken/language
(use data-structures extras srfi-1)
; Eggs - http://wiki.call-cc.org/chicken-projects/egg-index-4.html
(use matchable)
(use-for-syntax matchable)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Internal Helper Macros
; Extract and remove the named option from a field option list.
; Returns a list of matches.
(define-syntax option!
(syntax-rules ()
((option! option options)
(receive (match rest)
(partition
(lambda (o)
(if (list? o)
(eqv? (car o) option)
(eqv? o option)))
options)
(set! options rest)
match))))
; Converts a runtime field name such as integer or password into the name of
; the field definition available at expansion time such as syntax-integer or
; syntax-password.
(define (field-type->syntax-type* symbol)
(let* ((symbol* (strip-syntax symbol))
(syntax-symbol (symbol-append 'syntax- symbol*)))
syntax-symbol))
(define-syntax field-type->syntax-type
(ir-macro-transformer
(lambda (stx inject compare)
(match stx
((field-type->syntax-type NAME)
(let* ((NAME* (inject NAME))
(NAME* (strip-syntax NAME*))
(syntax-NAME (symbol-append 'syntax- NAME*)))
syntax-NAME))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; External Helper Macros
; Take a field list as supplied from define-object-type or define-property-type
; and quote it and evaluate it properly.
(define-syntax fields->list
(syntax-rules (runtime macrotime)
((fields->list runtime ((name type flags ...) ...))
`((name ,type flags ...) ...))
((fields->list macrotime ((name type flags ...) ...))
`((name ,(field-type->syntax-type type) flags ...) ...))
((fields->list macrotime fields)
(map
(match-lambda
((name type . flags)
(let ((type (##sys#slot (symbol-append 'syntax- (strip-syntax type)) 0)))
`(,name ,type ,@flags))))
fields))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Object IDs
(define (make-object-id node-id seqno)
(vector node-id seqno))
(define (object-id-node-id object-id)
(vector-ref object-id 0))
(define (object-id-seqno object-id)
(vector-ref object-id 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Information about the current user
; An object ID that represents the current gromit user.
(define current-gromit-user (make-parameter #f))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Structures to hold the schema information
;; Attribute Types
;; This is allocated when define-field is used.
(define-record attribute-type
name
column-type
serialiser
deserialiser
initialiser
setter
getter)
(define-record-printer (attribute-type ft port)
(fprintf port "#<gromit-attribute-type ~S ~S>"
(attribute-type-name ft)
(attribute-type-column-type ft)))
; Creates a new type of attribute.
;
; column-type : The type of the underlying column that is used for the
; serialised value when it is stored in the database.
;
; serialiser : Called when an object or property is being saved back to the
; database. A procedure of one argument that receives the
; current value of the attribute in memory and returns a
; serialised version of the attribute suitable for storing in
; SQLite. Each time an attempt is made to write the object or
; property back to the database, this is called no more than
; once.
;
; deserialiser : Called when an object or property is being read from the
; database. A procedure of one argument that receives the value
; of the attribute from the database and returns a deserialied
; version of the attribute suitable for storing in memory. Each
; time an attempt is makde to read the object or property back
; from the database, this is called no more than once.
;
; initialiser : Called when a new object that contains this attribute is being
; allocated. A procedure of no arguments that must return the
; initial value for the attribute. The value returned must be a
; deserialised value suitable for storing in memory.
; Defaults to #f.
; This is called the first time an attempt is made to allocate
; the object or property. If the allocation subsequently fails,
; it will not be called again.
;
; setter : Called when user code tries to set the value of an attribute.
; A procedure of one argument that receives the value supplied
; by the user as used in CHICKEN code and returns the value
; suitable for storing in memory. Usually the identity procedure
; or a guard. If the attribute is marked as constant by the
; structure that uses it then the setter procedure is only
; called before the structure has been written to disk for the
; first time.
; Defaults to the identity procedure.
;
; getter : Called when user code tries to get the value of an attribute.
; A procedure of one argument that receives the value stored in
; memory and returns a value for the user, suitable for use in
; CHICKEN code.
; Defaults to the identity procedure.
;
(define (make-attribute-type* name
column-type
phase
#!key
serialiser
deserialiser
initialiser
setter
getter)
(case phase
((runtime)
(assert (procedure? serialiser) (conc "make-attribute-type: serialiser must be a procedure. We got " serialiser "!"))
(assert (procedure? deserialiser) (conc "make-attribute-type: deserialiser must be a procedure. We got " deserialiser "!"))
(assert (procedure? initialiser) (conc "make-attribute-type: initialiser must be a procedure. We got " initialiser "!"))
(assert (procedure? setter) (conc "make-attribute-type: setter must be a procedure. We got " setter "!"))
(assert (procedure? getter) (conc "make-attribute-type: getter must be a procedure. We got " getter "!"))
(make-attribute-type name column-type serialiser deserialiser initialiser setter getter))
((macrotime)
(make-attribute-type name column-type #f #f #f #f #f))
(else
(assert #f (conc "Unrecognised phase " phase)))))
;; Attribute Specs
;; This is allocated when an object type declares a field that is an attribute.
(define-record attribute-spec
field-idx
type
name
required?
constant?
key-part)
;; Property Types
;; This is allocated when define-property is used.
(define-record property-type
name
table
key-specs
attribute-specs)
(define-record-printer (property-type pt port)
(fprintf port "#<gromit-property-type ~S ~S ~S>"
(property-type-name pt)
(map attribute-spec-name (property-type-key-specs pt))
(map attribute-spec-name (property-type-attribute-specs pt))))
; Creates a new type of property.
;
; Fields musy be attributes; they can't be other properties.
;
(define (make-property-type* name table fields)
(let* ((attribute-specs
(map
(match-lambda*
(((name type . flags) field-idx)
(assert (symbol? name) (conc "Name for a property field must be a symbol. We got " name "!"))
(cond
((attribute-type? type)
(let ((spec (make-attribute-spec
field-idx
type
name
(not (null? (option! 'required flags)))
(not (null? (option! 'constant flags)))
(let ((o (option! 'key flags)))
(cond
((null? o) #f)
((and
(= 1 (length o))
(list? (car o))
(= 2 (length (car o)))
(integer? (cadar o))
(exact? (cadar o)))
(cadar o))
(else
(assert #f (conc "Unrecognised key options for attribute " name ": " o))))))))
(assert (null? flags) (conc "Unrecognised options for attribute " name ": " flags))
spec))
(else
(assert #f (conc "Type for a property field must be a gromit-attribute-type. We got " type "!"))))))
fields
(iota (length fields))))
(keys
(sort ; Put the keys in index order as specified by the (key <n>) flag.
(fold ; Find the attribue specs that are keys
(lambda (spec lst)
(if (attribute-spec-key-part spec)
(cons spec lst)
lst))
'()
attribute-specs)
(lambda (a b)
(<
(attribute-spec-key-part a)
(attribute-spec-key-part b)))))
(attribute-names ; Extract the names of the attributes and sort them.
(sort
(map
attribute-spec-name
attribute-specs)
(lambda (a b)
(string<?
(symbol->string a)
(symbol->string b))))))
; Check that the key parts are specified monotonically, starting from 0.
(assert (equal?
(iota (length keys))
(map attribute-spec-key-part keys))
(conc "Key parts for Property Type " name " must be in order, starting from 0. We got "
(map (lambda (spec)
(conc (attribute-spec-name spec) ":" (attribute-spec-key-part spec)))
keys)))
; Check that each attribute name only appears once.
(if (not (null? attribute-names))
(fold
(lambda (attribute-name seed)
(assert
(not (eqv? attribute-name seed))
(conc "Attribute name " attribute-name " cannot be used more than once in definition for property type " name "!"))
attribute-name)
(car attribute-names)
(cdr attribute-names)))
(make-property-type
name
table
keys
attribute-specs)))
;; Property Specs
;; This is allocated when an object type declares a field that is a property.
(define-record property-spec
field-idx
type
name)
;; Object Types
;; This is allocated when define-object is used.
(define-record object-type
name
table
metadata-type
attribute-specs
property-specs
field-names)
(define (field-spec-name spec)
((cond
((attribute-spec? spec) attribute-spec-name)
((property-spec? spec) property-spec-name))
spec))
(define-record-printer (object-type ot port)
(fprintf port "#<gromit-object-type ~S ~S ~S>"
(object-type-name ot)
(object-type-table ot)
(object-type-field-names ot)))
; Creates a new type of object.
;
; Fields can be either attributes or properties.
; Each property type is allowed no more than once for any of the fields. This
; is for two reasons.
; 1) We don't store enough metadata in the property tables to work out which
; properties would belong to which field in the object.
; 2) It's not a very "relational" model. Users can get what they want by
; adding a key to the Property Type.
;
(define (make-object-type* name table metadata-type fields)
(define (make-counter)
(let ((c 0))
(lambda ()
(let ((r c))
(set! c (add1 r))
r))))
(let* ((next-attribute-idx (make-counter))
(next-property-idx (make-counter))
(field-specs ; Turn the list of fields into some field-specs.
(map
(match-lambda
((name type . flags)
(assert (symbol? name) (conc "Name for an object field must be a symbol. We got " name "!"))
(cond
((attribute-type? type)
(let ((spec (make-attribute-spec
(next-attribute-idx)
type
name
(not (null? (option! 'required flags)))
(not (null? (option! 'constant flags)))
#f)))
(assert (null? flags) (conc "Unrecognised options for attribute " name ": " flags))
spec))
((property-type? type)
(let ((spec (make-property-spec
(next-property-idx)
type
name)))
(assert (null? flags) (conc "Unrecognised options for property " name ": " flags))
spec))
(else
(assert #f (conc "Type for an object field must be a gromit-attribute-type or a gromit-property-type. We got " type "!"))))))
fields))
(attribute-specs ; Extract the attribute-specs.
(reverse
(fold
(lambda (spec lst)
(if (attribute-spec? spec)
(cons spec lst)
lst))
'()
field-specs)))
(property-specs ; Extract the property-specs.
(reverse
(fold
(lambda (spec lst)
(if (property-spec? spec)
(cons spec lst)
lst))
'()
field-specs)))
(property-specs* ; Order the property-specs based on type.
(sort
property-specs
(lambda (a b)
(string<?
(symbol->string (property-type-name (property-spec-type a)))
(symbol->string (property-type-name (property-spec-type b)))))))
(field-names ; Extract the names of the fields and sort them.
(sort
(map field-spec-name field-specs)
(lambda (a b)
(string<?
(symbol->string a)
(symbol->string b))))))
; Check that the metadata-type doesn't have any keys
(assert (null? (property-type-key-specs metadata-type)) (conc "Metadata Type must not have any key parts but we got " (property-type-name metadata-type) " with " (property-type-key-specs metadata-type "!")))
; Check that each proerty-type only appears once.
(if (not (null? property-specs*))
(fold
(lambda (spec seed)
(assert
(not (eqv? (property-spec-type spec) (property-spec-type seed)))
(conc "Property Type " (property-spec-type spec) " cannot be used more than once in definition for object type " name "!"))
spec)
(car property-specs*)
(cdr property-specs*)))
; Check that each field name only appears once.
(if (not (null? field-names))
(fold
(lambda (field-name seed)
(assert
(not (eqv? field-name seed))
(conc "Field name " field-name " cannot be used more than once in definition for object type " name "!"))
field-name)
(car field-names)
(cdr field-names)))
(make-object-type
name
table
metadata-type
attribute-specs
property-specs
field-names)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
)