-
Notifications
You must be signed in to change notification settings - Fork 0
/
lf-to-abstract-sql-prep.ometajs
214 lines (205 loc) · 7.34 KB
/
lf-to-abstract-sql-prep.ometajs
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
var SBVRCompilerLibs = require('./sbvr-compiler-libs').SBVRCompilerLibs,
LFOptimiser = require('@balena/sbvr-parser/lf-optimiser').LFOptimiser;
export ometa LF2AbstractSQLPrep <: LFOptimiser {
AttrConceptType :termName =
^AttrConceptType(termName):conceptType
( ?(this.primitives[termName] === false && this.primitives[conceptType] !== false)
{this.primitives[conceptType] = false}
SetHelped
)?
-> conceptType,
AttrDatabaseAttribute :termOrFactType =
:attrVal
{ (termOrFactType[0] == 'Term' && (!this.attributes.hasOwnProperty(termOrFactType[3]) || this.attributes[termOrFactType[3]] === true))
|| (termOrFactType[0] == 'FactType' && termOrFactType.length == 4
&& (!this.attributes.hasOwnProperty(termOrFactType[3]) || this.attributes[termOrFactType[3]] === true)
&& this.primitives.hasOwnProperty(termOrFactType[3]) && this.primitives[termOrFactType[3]] !== false)
}:newAttrVal
{this.attributes[termOrFactType] = newAttrVal}
( ?(newAttrVal != attrVal)
// {console.log('Changing DatabaseAttribute attr to:', newAttrVal, termOrFactType)}
SetHelped
)?
-> newAttrVal,
AttrDatabasePrimitive :termOrFactType =
:attrVal
{attrVal}:newAttrVal
( ?(this.primitives.hasOwnProperty(termOrFactType))
{this.primitives[termOrFactType]}:newAttrVal
?(newAttrVal != attrVal)
// {console.log('Changing DatabasePrimitive attr to:', newAttrVal, termOrFactType)}
SetHelped
)?
{this.primitives[termOrFactType] = newAttrVal}
-> newAttrVal,
AttrTermForm :factType =
{this.termForms[factType] = true}
anything
,
AtMostNQuantification
"MaximumCardinality":maxCard "Variable":v trans*:xs SetHelped
{maxCard[1][1]++}
-> ['LogicalNegation', ['AtLeastNQuantification', ['MinimumCardinality', maxCard[1]], v].concat(xs)],
CardinalityOptimisation2 :v1 =
// Just a basic first var, no ClosedProjection.
?(v1.length == 3)
( [ 'ExactQuantification'
"Cardinality":card
?(card[1][1] == 1)
"Variable":v2
"AtomicFormulation":atomicForm
]
-> true
| [ 'AtMostNQuantification'
"MaximumCardinality":card
?(card[1][1] == 1)
"Variable":v2
"AtomicFormulation":atomicForm
]
-> false
):required
end
// Just a basic second var, no ClosedProjection.
?(v2.length == 3)
{atomicForm[1]}:factType
// Not referenced as a term form
?(!this.termForms[factType])
// A term verb term fact type
?(atomicForm.length == 4 && factType.length == 4)
UnmappedFactType(factType.slice(1)):actualFactType
{['FactType'].concat(actualFactType)}:actualFactType
( // In the same order as the vars appear.
?(this.IdentifiersEqual(v1[2], actualFactType[1]) && this.IdentifiersEqual(v2[2], actualFactType[3]))
{this.foreignKeys[actualFactType] = required}
| // In reverse order
?(this.IdentifiersEqual(v1[2], actualFactType[3]) && this.IdentifiersEqual(v2[2], actualFactType[1]))
{this.uniqueKeys[actualFactType] = required}
)
// We have matched a foreign key or unique, so call SetHelped.
SetHelped,
CardinalityOptimisation
[ 'UniversalQuantification'
"Variable":v1
CardinalityOptimisation2(v1)
| 'LogicalNegation'
[ 'ExistentialQuantification'
"Variable":v1
[ 'LogicalNegation'
CardinalityOptimisation2(v1)
]
]
],
NecessityOptimisation =
CardinalityOptimisation,
ObligationOptimisation =
CardinalityOptimisation,
Rule
( [ 'ObligationFormulation'
ObligationOptimisation
| 'NecessityFormulation'
NecessityOptimisation
]
"StructuredEnglish"
// We have matched a foreign key, return an empty array to remove the rule.
-> null
| ^Rule
)
}
LF2AbstractSQLPrep.initialize = function() {
Object.assign(this, SBVRCompilerLibs);
SBVRCompilerLibs.initialize.call(this);
LFOptimiser.initialize.call(this);
this.foreignKeys = {};
this.uniqueKeys = {};
this.primitives = {};
this.attributes = {};
this.termForms = {};
};
LF2AbstractSQLPrep.defaultAttributes = function(termOrFactType, attrsFound, attrs) {
switch(termOrFactType[0]) {
case 'Name':
case 'Term':
// We don't add a DatabaseIDField to a primitive.
if(!this.IsPrimitive(termOrFactType)
&& !attrsFound.hasOwnProperty('DatabaseIDField')) {
attrs.splice(1, 0, ['DatabaseIDField', 'id']);
this.SetHelped();
}
if(!attrsFound.hasOwnProperty('ReferenceScheme')) {
attrs.splice(1, 0, ['ReferenceScheme', 'id']);
this.SetHelped();
}
if(!attrsFound.hasOwnProperty('DatabaseTableName')) {
var tableName = this.GetResourceName(termOrFactType[1]);
attrs.splice(1, 0, ['DatabaseTableName', tableName]);
this.SetHelped();
}
if(!attrsFound.hasOwnProperty('DatabasePrimitive')) {
if(!this.primitives.hasOwnProperty(termOrFactType)) {
this.primitives[termOrFactType] = this.IsPrimitive(termOrFactType);
}
// console.log('Adding primitive attr', this.primitives[termOrFactType], termOrFactType);
attrs.splice(1, 0, ['DatabasePrimitive', this.primitives[termOrFactType]]);
this.SetHelped();
}
break;
case 'FactType':
// Use the actual fact type info for finding the relevant default attributes.
var actualFactType = this.UnmappedFactType(termOrFactType.slice(1));
actualFactType = ['FactType'].concat(actualFactType);
// We don't add extra attributes to a fact type starting with a primitive.
if(!this.IsPrimitive(actualFactType[1])) {
if(!attrsFound.hasOwnProperty('DatabaseIDField')) {
attrs.splice(1, 0, ['DatabaseIDField', 'id']);
this.SetHelped();
}
if(!attrsFound.hasOwnProperty('DatabaseTableName')) {
var tableName = this.GetResourceName(actualFactType.slice(1));
attrs.splice(1, 0, ['DatabaseTableName', tableName]);
this.SetHelped();
}
if(this.uniqueKeys.hasOwnProperty(actualFactType)) {
if(!attrsFound.hasOwnProperty('Unique')) {
attrs.splice(1, 0, ['Unique', this.uniqueKeys[actualFactType]]);
this.SetHelped();
}
else if(attrsFound['Unique'] != this.uniqueKeys[actualFactType]) {
console.error(attrsFound['Unique'], this.uniqueKeys[actualFactType]);
___MISMATCHED_UNIQUE_KEY___.die();
}
}
if(this.foreignKeys.hasOwnProperty(actualFactType)) {
if(!attrsFound.hasOwnProperty('DatabaseAttribute')) {
attrs.splice(1, 0, ['DatabaseAttribute', false]);
this.SetHelped();
}
if(!attrsFound.hasOwnProperty('ForeignKey')) {
// console.log('Adding FK attr', this.foreignKeys[actualFactType], actualFactType);
attrs.splice(1, 0, ['ForeignKey', this.foreignKeys[actualFactType]]);
this.SetHelped();
}
else if(attrsFound['ForeignKey'] != this.foreignKeys[actualFactType]) {
console.error(attrsFound['ForeignKey'], this.foreignKeys[actualFactType]);
___MISMATCHED_FOREIGN_KEY___.die();
}
}
// Boolean fact type
if(actualFactType.length == 3) {
if(!this.primitives.hasOwnProperty(actualFactType[1]) || this.primitives[actualFactType[1]] !== false) {
this.SetHelped();
}
// The term of a boolean fact type cannot be a primitive.
this.primitives[actualFactType[1]] = false;
} // Tertiary or higher fact type
else if(actualFactType.length > 4) {
// The first term in a tertiary or higher fact type cannot become an attribute.
if(!this.attributes.hasOwnProperty(actualFactType[1]) || this.attributes[actualFactType[1]] !== false) {
this.SetHelped();
}
this.attributes[actualFactType[1]] = false;
}
}
break;
}
termOrFactType.push(attrs);
};