-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackage2Database_Lazy.atl
149 lines (135 loc) · 3.93 KB
/
Package2Database_Lazy.atl
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
module Package2Database;
create OUT : Relational from IN : Class;
-- uses strings;
-- if there is a configuration problem with the library string,
-- delete the two firstToLower() operations and you no longer need the library "strings"
-- the transfomarion will execute
-- inheritance not supported yet
-- issue: choose an object-id Type (Integer, String?).
-- We choose Integer here, assuming this type is defined in the source model.
-- global variable
-- context
helper def: objectIdType : Relational!Type =
Class!DataType.allInstances()->select(e | e.name = 'Integer')->first();
rule Package2Database {
from
p : Class!Package
to
out : Relational!Database (
name <- p.name,
-- Columns are generated from Attributes in another rule not explicitly called here !
tables <-
p.classifiers->select(c | c.oclIsKindOf(Class!Class))
-> collect( c | thisModule.Class2Table( c ) )
-> union(
p.classifiers
-> select(c | c.oclIsKindOf(Class!Class)) -> collect( c | c.attr ) -> flatten()
-> select( a | a.type.oclIsKindOf(Class!DataType) and a.multiValued )
-> collect( a | thisModule.MultiValuedDataTypeAttribute2Column(a) )
)
-> union (
p.classifiers
->select(c | c.oclIsKindOf(Class!Class)) -> collect( c | c.attr ) -> flatten()
-> select( a | a.type.oclIsKindOf(Class!Class) and a.multiValued )
-> collect( a | thisModule.MultiValuedClassAttribute2Column(a) )
)
)
}
unique lazy rule Class2Table {
from
c : Class!Class
to
out : Relational!Table (
name <- c.name,
-- Columns are generated from Attributes in another rule not explicitly called here !
col <- Sequence {key}
-> union(
c.attr
-> select( a | a.type.oclIsKindOf(Class!DataType) and not a.multiValued )
-> collect( a | thisModule.DataTypeAttribute2Column(a) )
)
-> union (
c.attr
-> select( a | a.type.oclIsKindOf(Class!Class) and not a.multiValued )
-> collect( a | thisModule.ClassAttribute2Column(a) )
),
key <- Set {key}
),
key : Relational!Column (
name <- 'objectId',
type <- thisModule.objectIdType
)
}
rule DataType2Type {
from
dt : Class!DataType
to
out : Relational!Type (
name <- dt.name
)
}
unique lazy rule DataTypeAttribute2Column {
from
a : Class!Attribute (
a.type.oclIsKindOf(Class!DataType) and not a.multiValued
)
to
out : Relational!Column (
name <- a.name,
type <- a.type
-- explicit use of implicit tracking links (first expected syntax, then present actual syntax)
-- owner <- [Class2Type.key]a.owner
-- owner <- thisModule.resolveTemp(a.owner, 'key')
)
}
unique lazy rule MultiValuedDataTypeAttribute2Column {
from
a : Class!Attribute (
a.type.oclIsKindOf(Class!DataType) and a.multiValued
)
to
out : Relational!Table (
name <- a.owner.name + '_' + a.name,
col <- Sequence {id, value}
),
id : Relational!Column (
-- name <- a.owner.name.firstToLower() + 'Id',
name <- a.owner.name + 'Id',
type <- thisModule.objectIdType
),
value : Relational!Column (
name <- a.name,
type <- a.type
)
}
unique lazy rule ClassAttribute2Column {
from
a : Class!Attribute (
a.type.oclIsKindOf(Class!Class) and not a.multiValued
)
to
foreignKey : Relational!Column (
name <- a.name + 'Id',
type <- thisModule.objectIdType
)
}
unique lazy rule MultiValuedClassAttribute2Column {
from
a : Class!Attribute (
a.type.oclIsKindOf(Class!Class) and a.multiValued
)
to
t : Relational!Table (
name <- a.owner.name + '_' + a.name,
col <- Sequence {id, foreignKey}
),
id : Relational!Column (
-- name <- a.owner.name.firstToLower() + 'Id',
name <- a.owner.name + 'Id',
type <- thisModule.objectIdType
),
foreignKey : Relational!Column (
name <- a.name + 'Id',
type <- thisModule.objectIdType
)
}