Skip to main content
summaryrefslogtreecommitdiffstats
blob: 235c170f89fb4295eceeca065ff6e3e886af350f (plain) (blame)
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
import UML: 'ManualUML.ecore'::uml;
import RDBMS: 'ManualRDBMS.ecore'::rdbms;
import UML2RDBMS: 'ManualUML2RDBMS.ecore'::uml2rdbms;

transformation ManualUML2RDBMS {
    uml imports UML;
    rdbms imports RDBMS;
    middle imports UML2RDBMS;
}

map __root__ in ManualUML2RDBMS {
    for p1 : UML::Package in UML::Package.allInstances() {
        call packageToSchemaLM {
            p := p1;
        }
    }
}

map packageToSchemaLM in ManualUML2RDBMS {
    check uml (p:Package) { }
    enforce middle () {
        realize p2s:PackageToSchema
    }
    set p2s.umlPackage := p;
    set p2s.name := p.name;
    /* L to M */
    -- A package has elements, elements can be Classifiers or Associations. A
    -- classifier can be a Class or a PrimitiveDataType
    
    -- PrimitiveDataType
    for child in p.elements {
	    call integerToNumberLM {
	        p := p;
	        prim := child;
	        p2s := p2s;
	    }
	    call booleanToBooleanLM {
	        p := p;
	        prim := child;
	        p2s := p2s;
	    }
	    call stringToVarcharLM {
	        p := p;
	        prim := child;
	        p2s := p2s;
	    }
	}
    for child in p.elements {
	    -- Class
	    call classToTableLM {
	        p := p;
	        c := child;
	        p2s := p2s;
	    }
	    -- Associations
		    call associationToForeignKeyLM {
	        p := p;
	        a := child;
	        p2s := p2s;
	    }
	}
    
    /* M to M */
    
    
    /* M to R */
    call packageToSchemaMR {
        p2s := p2s;
    }
    
}

map packageToSchemaMR in ManualUML2RDBMS {
    check middle (p2s:PackageToSchema) {}
    enforce rdbms () {
        realize s:Schema
    }
    set p2s.schema := s;
    call packageToSchemaMR_1 {
        s_1 := s;
        p2s_1 := p2s;
    }
    for child in p2s.primitivesToNames {
	    call integerToNumberMR {
	        p2s := p2s;
	        p2n := child;
	    }
	    call booleanToBooleanMR {
	        p2s := p2s;
	        p2n := child;
	    }
	    call stringToVarcharMR {
	        p2s := p2s;
	        p2n := child;
	    }
    }
    for child in p2s.classesToTables {
	    call classToTableMR {
	        p2s := p2s;
	        c2t := child;
	        s := s;
	    }
	}
}

map packageToSchemaMR_1 in ManualUML2RDBMS {
    check middle (p2s_1:PackageToSchema) {}
    enforce rdbms (s_1:Schema) {}
    set s_1.name := p2s_1.name;
}

map integerToNumberLM in ManualUML2RDBMS {
    check uml (p:Package, prim:PrimitiveDataType) {}
    enforce middle (p2s:PackageToSchema) {
    	realize p2n:PrimitiveToName
    }
    where (prim.namespace = p;
        prim.name = 'Integer';
        p2s.umlPackage=p;
    ) {}
    set p2n.owner := p2s;
    set p2n._'primitive' := prim;
    set p2n.name := prim.name + '2' + 'NUMBER';     
	set p2n.typeName := 'NUMBER';
}

map integerToNumberMR in ManualUML2RDBMS {
    check middle (p2s:PackageToSchema, p2n:PrimitiveToName) {}
    enforce rdbms () {}
    where (
    	p2n.owner = p2s;
        p2n.name = 'Integer' + '2' + 'NUMBER';
    ) {}
}

map booleanToBooleanLM in ManualUML2RDBMS {
    check uml (p:Package, prim:PrimitiveDataType) { }
    enforce middle (p2s:PackageToSchema) {
    	realize p2n:PrimitiveToName
    }
    where (
        prim.namespace = p;
        prim.name = 'Boolean';
        p2s.umlPackage=p;
    ) {}
    set p2n.owner := p2s;
    set p2n._'primitive' := prim;
    set p2n.name := prim.name + '2' + 'BOOLEAN';
	set p2n.typeName := 'BOOLEAN';
}

map booleanToBooleanMR in ManualUML2RDBMS {
    check middle (p2s:PackageToSchema, p2n:PrimitiveToName) {}
    enforce rdbms () {}
    where (
        p2n.owner = p2s;
        p2n.name = 'Boolean' + '2' + 'BOOLEAN';
    ) {}
}

map stringToVarcharLM in ManualUML2RDBMS {
    check uml (p:Package, prim:PrimitiveDataType) { }
    enforce middle (p2s:PackageToSchema) {
    	realize p2n:PrimitiveToName
    }
    where (
        prim.namespace = p;
        prim.name = 'String';
        p2s.umlPackage=p;
    ) {}
    set p2n.owner := p2s;
    set p2n._'primitive' := prim;
    set p2n.name := prim.name + '2' + 'VARCHAR';
	set	p2n.typeName := 'VARCHAR';
}

map stringToVarcharMR in ManualUML2RDBMS {
    check middle (p2s:PackageToSchema, p2n:PrimitiveToName) {}
    enforce rdbms () {}
    where (
        p2n.owner = p2s;
        p2n.name = 'String' + '2' + 'VARCHAR';
    ) {}
}

query ManualUML2RDBMS::getAllSupers(cls : UML::Class[1]) : Set(UML::Class) {
    cls.general->collect(gen | getAllSupers(gen))->including(cls)->asSet()
}

query ManualUML2RDBMS::getAllAttributes(cls :  UML::Class[1]) : Set(UML::Attribute) {
    getAllSupers(cls).attributes->asSet() -- ->collect(c | c._'attribute')
}
    
query ManualUML2RDBMS::getAllForwards(cls : UML::Class[1]) : Set(UML::Association) {
    getAllSupers(cls).forward->asSet()
}

map classToTableLM in ManualUML2RDBMS {
    check uml (p:Package, c:Class) { }
    enforce middle (p2s:PackageToSchema) {
    	realize c2t:ClassToTable
    }
    where (
        c.kind = 'persistent';
        c.namespace = p;
        p2s.umlPackage=p;
    ) {}
    set c2t.owner := p2s;
    set c2t.umlClass := c;
    set c2t.name := c.name;
    -- A Class has attributes
    for anAttribute in c.attributes {
	    call classPrimitiveAttributesLM {
	        c := c;
	        a := anAttribute;
	        fao := c2t;
	    }
	    call classComplexAttributesLM {
	        c := c;
	        a := anAttribute;
	        fao := c2t;
	    }
	    call complexAttributePrimitiveAttributesLM {
	        ca := anAttribute;
	        c := anAttribute.type;
	    }
	    call complexAttributeComplexAttributesLM {
	        ca := anAttribute;
	        c := anAttribute.type;
	    }
    }
}

map classToTableMR in ManualUML2RDBMS {
    check middle (p2s:PackageToSchema, c2t:ClassToTable) {}
    enforce rdbms (s:Schema) {
        realize t:Table
    }
    where (
        c2t.owner = p2s;
    ) {}
    set t.kind := 'base';
    set t.schema := s;
    call classToTableMR_1 {
        c2t_1 := c2t;
        t_1 := t;
    }    
    call classToTableMR_2 {
        t_2 := t;
        c2t_2 := c2t;
    }
    for child in c2t.associationsToForeignKeys {
	    call associationToForeignKeyMR {
	        p2s := p2s;
	        dt := t;
	--        sc2t := c2t;
	--        dc2t <= c2t.associationsToForeignKeys.referenced;
        sc2t := t.ClassToTable;
        dc2t := t.ClassToTable;
	        a2f := child;
	        s := s;
	        st := t;
	        rk := c2t.primaryKey;
	    }
    }
    -- A Class has attributes
    for child in c2t.fromAttributes->union(c2t.fromAttributes->selectByKind(UML2RDBMS::NonLeafAttribute).fromAttributes) {
	    call attributeColumnsMR {
	        c2t := c2t;
	        a2c := child;
	        t := t;
	    }
    }
}

map classToTableMR_1 in ManualUML2RDBMS {
    check middle (c2t_1:ClassToTable) {}
    enforce rdbms (t_1:Table) { }
    set c2t_1.table := t_1;
    set t_1.name := c2t_1.name;
}

map classToTableMR_2 in ManualUML2RDBMS {
    check middle (c2t_2:ClassToTable) {}
    enforce rdbms (t_2:Table) {
        realize pk:Key,
        realize pc:Column
    }
    set pk.owner := t_2;
    set pk.kind := 'primary';
    set pc.owner := t_2;
    set pc._'key' := OrderedSet(Key){pk};
    set pc.type := 'NUMBER';
    call classToTableMR_2_1 {
        c2t_2_1 := c2t_2;
        pk_2_1 := pk;
        pc_2_1 := pc; -- TODO Check this
    }
    call classToTableMR_2_2 {
        pk_2_2 := pk;
        pc_2_2 := pc;
        t_2_2 := t_2;
    }
}

map classToTableMR_2_1 in ManualUML2RDBMS {
    check middle (c2t_2_1:ClassToTable) {}
	enforce rdbms (pk_2_1:Key, pc_2_1:Column) {}
    set c2t_2_1.primaryKey := pk_2_1;
    set c2t_2_1.column := pc_2_1;
}
        
map classToTableMR_2_2 in ManualUML2RDBMS {
    check middle () {}
    enforce rdbms (pk_2_2:Key, pc_2_2:Column, t_2_2:Table) {}
    set pc_2_2.name := t_2_2.name+'_tid';
    set pk_2_2.name := t_2_2.name+'_pk';
}

map associationToForeignKeyLM in ManualUML2RDBMS {
    check uml (p:Package, sc:Class := a.source, dc:Class := a.destination, a:Association) {
--            sc.namespace = p;
        }
    enforce middle (p2s:PackageToSchema, sc2t:ClassToTable := sc.ClassToTable, dc2t:ClassToTable := dc.ClassToTable) {
	     realize a2f:AssociationToForeignKey
	}
    where (
        a.namespace = p;
        sc.namespace = p;
        -- getAllForwards(sc)->includes(a);
        -- getAllSupers(dc)->includes(a.destination);
        p2s.umlPackage = p;
    ) {}
    set sc2t.owner := p2s;
    set a2f.owner := sc2t;
    set a2f.referenced := dc2t;
    set a2f.association := a;
        -- FIXME Bug 461994 working around Bug 459397; dc2 is gratuitous
    set a2f.name := let dc2 = dc in if a.destination=dc and a.source=sc
                        then a.name
                        else if a.destination<>dc and a.source=sc
                        then dc2.name +'_'+a.name
                        else if a.destination=dc and a.source<>sc
                        then a.name+'_'+sc.name
                        else dc2.name+'_'+a.name+'_'+sc.name
                        endif endif endif;
}

map associationToForeignKeyMR in ManualUML2RDBMS {
    check middle (p2s:PackageToSchema, sc2t:ClassToTable, dc2t:ClassToTable, a2f:AssociationToForeignKey) {}
    enforce rdbms (s:Schema, st:Table, dt:Table, rk:Key) {
        realize fk:ForeignKey,
        realize fc:Column
    }
    where (
        a2f.owner = sc2t;
        a2f.referenced = dc2t;
        p2s.schema = s;
        --sc2t.table = st;
        --dc2t.table = dt; 
--        sc2t := st.ClassToTable;
--        dc2t := dt.ClassToTable;
        st.schema = s;
        -- rk.owner = dt;
        -- rk.kind = 'primary';
    ) {}
    set sc2t.owner := p2s;
    set fk.name := a2f.name;
    set fc.name := a2f.name +'_tid';
    set fk.owner := st;
    set fc.owner := st;
    call associationToForeignKeyMR_1 {
        fk := fk;
        fc := fc;
        dt := a2f.referenced.table;
        rk := rk;
    }
    call associationToForeignKeyMR_2 {
        a2f_1 := a2f;
        fk_1 := fk;
        fc_1 := fc;
    }
}

map associationToForeignKeyMR_1 in ManualUML2RDBMS {
    check middle () {}
    enforce rdbms (fk:ForeignKey, fc:Column, dt:Table, rk:Key) {}
    set fk.refersTo := rk;
    set fc.foreignKeys := OrderedSet(ForeignKey){fk};
    set fc.type := rk.column->first().type;
}

map associationToForeignKeyMR_2 in ManualUML2RDBMS {
    check middle (a2f_1:AssociationToForeignKey) {}
    enforce rdbms (fk_1:ForeignKey, fc_1:Column) {}   
    set a2f_1.foreignKey := fk_1;
    set a2f_1.column := fc_1;
}

map classPrimitiveAttributesLM in ManualUML2RDBMS {
    check uml (c:Class, t:PrimitiveDataType := a.type, a:Attribute) {
        }
    enforce middle (fao:ClassToTable, p2n:PrimitiveToName := t.PrimitiveToName) {
    	realize atc:AttributeToColumn
    }
    where (
        a.owner = c;
        --getAllAttributes(c)->includes(a);
    	fao.umlClass = c;
    
    ) {}
    set atc._'attribute' := a;
    set atc.owner := fao;
    set atc.type := p2n;
    set atc.kind := a.kind;
    set atc.name := a.name;
    set atc.leafs := Set(AttributeToColumn) {atc};
}

map classComplexAttributesLM in ManualUML2RDBMS {
    check uml (c:Class, t:Class := a.type, a:Attribute) { 
        }
    enforce middle (fao:ClassToTable) {
    	realize fa:NonLeafAttribute
    }
    where (
        a.owner = c;
        --getAllAttributes(c)->includes(a);
        fao.umlClass=c;
    ) {}
    set fa._'attribute' := a;
    set fa.owner := fao;
    set fa.kind := a.kind;
    set fa.name := a.name;
    set fa.leafs := fao.fromAttributes.leafs->asSet();
}

map complexAttributePrimitiveAttributesLM in ManualUML2RDBMS {
    check uml (c:Class, ca:Attribute) {
        }
    enforce middle (fao:NonLeafAttribute := ca.FromAttribute) {}
    where (
        ca.type = c;
        -- getAllAttributes(c)->includes(a);
    ) {}
    for anAttribute in c.attributes {
	    call complexAttributePrimitiveAttributesLM_1 {
	        c_1 := c;
	        ca_1 := ca;
	        fao_1 := fao;
	        a_1 := anAttribute;
	    }
	}
}

map complexAttributePrimitiveAttributesLM_1 in ManualUML2RDBMS {
    check uml (c_1:Class, t_1:PrimitiveDataType := a_1.type, a_1:Attribute, ca_1:Attribute) {
        }
    enforce middle (fao_1:NonLeafAttribute, p2n_1:PrimitiveToName := t_1.PrimitiveToName)
    {
        realize fa:AttributeToColumn
    }
    where (
        --getAllAttributes(c)->includes(a);
     ) {}
    set fa.owner := fao_1;    
    set fa.leafs := Set(AttributeToColumn) {fa};
    set fa._'attribute' := a_1;
    set fa.type := p2n_1;
    set fa.kind := a_1.kind;
    set fa.name := ca_1.name + '_' + a_1.name;
}

map complexAttributeComplexAttributesLM in ManualUML2RDBMS {
    check uml (c:Class, ca:Attribute) {
        }
    enforce middle (fao:NonLeafAttribute := ca.FromAttribute) {}
    where (
        ca.type = c;
        --getAllAttributes(c)->includes(a);
    ) {}
    for anAttribute in c.attributes {
    	call complexAttributeComplexAttributesLM_1 {
	        ca_1 := ca;
	        a_1 := anAttribute;
	        c_1 := c;
	        fao_1 := fao;
	    }
	}
}

map complexAttributeComplexAttributesLM_1 in ManualUML2RDBMS {
    check uml (c_1:Class, ca_1:Attribute, t_1:Class := a_1.type, a_1:Attribute) {
        }
    enforce middle (fao_1:NonLeafAttribute) {
    	realize fa:NonLeafAttribute
    }
    where (
        a_1.owner = c_1;
        --getAllAttributes(c)->includes(a);
    ) {}
    set fa.owner := fao_1;
    set fa.leafs := fao_1.fromAttributes.leafs->asSet(); 
    set fa._'attribute' := a_1;
    set fa.kind := a_1.kind;
    set fa.name := ca_1.name + '_' + a_1.name;
--    set fa._'attribute' := t_1;
}


map attributeColumnsMR in ManualUML2RDBMS {
    check middle (c2t:ClassToTable, a2c:AttributeToColumn, p2n:PrimitiveToName := a2c.type) {}
    enforce rdbms (t:Table, ct:String := p2n.typeName) {
        realize c:Column
    }
    where (
        -- c2t.fromAttributes.leafs->includes(a2c); -- need to go deeper in the recursion
        -- a2c.owner = c2t;   NOT ALL a2c are owned by c2t (complex attributes) 
        c2t.table = t;
    ) {}
    set c.owner := t;
    call attributeColumnsMR_1 {
        a2c_1 := a2c;
        c_1 := c;
    }
    call attributeColumnsMR_2 {
     	a2c_2 := a2c;
    	c_2 := c;
        p2n_2 := p2n;
        ct_2 := ct;
    }
    call attributeColumnsMR_3 {
        c_3 := c;
        a2c_3 := a2c;
    }
}

map attributeColumnsMR_1 in ManualUML2RDBMS {
    check middle (a2c_1:AttributeToColumn) {}
    enforce rdbms (c_1:Column) {}
    set a2c_1.column := c_1;
}

map attributeColumnsMR_2 in ManualUML2RDBMS {
    check middle (p2n_2:PrimitiveToName, a2c_2:AttributeToColumn) {}
    enforce rdbms (c_2:Column, ct_2:String) {}
    where (
        a2c_2.type = p2n_2;
    ) {}
    set c_2.type := ct_2;
    call attributeColumnsMR_2_1 {
        p2n_2_1 := p2n_2;
        ct_2_1 := ct_2;
    }
}

map attributeColumnsMR_2_1 in ManualUML2RDBMS {
    check middle (p2n_2_1:PrimitiveToName) {}
    enforce rdbms (ct_2_1:String) {}
    set p2n_2_1.typeName := ct_2_1;
}

map attributeColumnsMR_3 in ManualUML2RDBMS {
    check middle (a2c_3:AttributeToColumn) {}
    enforce rdbms (c_3:Column) { }
    set c_3.name := a2c_3.name;
    set c_3.kind := a2c_3.kind;
}

Back to the top