/
Util.java
415 lines (336 loc) · 17.2 KB
/
Util.java
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
/*******************************************************************************
* Copyright (c) 2020 Bird Software Solutions Ltd
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Neil Mackenzie - initial API and implementation
*******************************************************************************/
package org.eclipse.efbt.cocamo.trl.component.translator.util;
import java.util.Iterator;
import org.eclipse.efbt.cocamo.core.model.functions.AggregateFunctionSpec;
import org.eclipse.efbt.cocamo.core.model.functions.BasicFunctionSpec;
import org.eclipse.efbt.cocamo.core.model.functions.Function;
import org.eclipse.efbt.cocamo.core.model.functions.FunctionsFactory;
import org.eclipse.efbt.cocamo.lineage.model.attribute_lineage.AttributeLineageModel;
import org.eclipse.efbt.cocamo.lineage.model.column_transformation_logic.ColumnFunction;
import org.eclipse.efbt.cocamo.lineage.model.cube_schema.CubeSchema;
import org.eclipse.efbt.cocamo.lineage.model.cube_transformation_logic.CubeTransformationLogic;
import org.eclipse.efbt.cocamo.lineage.model.cubes.BaseCube;
import org.eclipse.efbt.cocamo.lineage.model.cubes.FreeBirdToolsCube;
import org.eclipse.efbt.cocamo.lineage.model.lineagefunctions.CubeColumn;
import org.eclipse.efbt.cocamo.lineage.model.lineagefunctions.LineagefunctionsFactory;
import org.eclipse.efbt.cocamo.lineage.model.lineagefunctions.ResolvedCubeColumnParameter;
import org.eclipse.efbt.cocamo.lineage.model.lineagefunctions.SpeculativeCubeColumnParameter;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.BaseRowStructure;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.FilterAndGroupToOneRowCreationApproach;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.FilterRowCreationApproach;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.RowCreationApproach;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.RowCreationApproachForCube;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.RowJoinFunction;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.Row_transformation_logicFactory;
import org.eclipse.efbt.cocamo.lineage.model.row_transformation_logic.GroupByRowCreationApproach;
import org.eclipse.efbt.cocamo.lineage.query.attributelineage.AttributeLineageModelQuery;
import org.eclipse.efbt.cocamo.smcubes.model.core.VARIABLE;
import org.eclipse.efbt.cocamo.smcubes.model.data_definition.CUBE;
import org.eclipse.efbt.cocamo.smcubes.model.data_definition.CUBE_STRUCTURE;
import org.eclipse.efbt.cocamo.smcubes.model.data_definition.CUBE_STRUCTURE_ITEM;
import org.eclipse.efbt.cocamo.smcubes.model.efbt_data_definition.CubeModule;
import org.eclipse.efbt.cocamo.trl.model.transformation.VersionedComponentsSet;
import org.eclipse.efbt.cocamo.trl.model.transformation.VersionedCubeSchemaModule;
import org.eclipse.efbt.cocamo.trl.model.transformation.VersionedFunctionalModuleLogic;
import org.eclipse.efbt.cocamo.core.model.functionality_module.ReportCellCreationFunctionalityModule;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
/**
* @author Neil Mackenzie
*
*/
public class Util {
/**
* Get the list of VersionedCubeSchemaModule associated with a
* versionedComponentSet.
*
* @param functionalModule
* @param versionedComponentSet
* @return
*/
private static EList<VersionedCubeSchemaModule> getCubeSchemaModuleList(
ReportCellCreationFunctionalityModule functionalModule, VersionedComponentsSet versionedComponentSet) {
return null; // TODO need to check why this is null.
}
/**
* Set the set of fixed SpecialFunctionSpecs that should be linked to an
* attributeLineageModel.
*
* @param attributeLineageModel
* @return
*/
public static SpecialFunctionSpecs setSpecialFucntions(AttributeLineageModel attributeLineageModel) {
SpecialFunctionSpecs specialFunctions = new SpecialFunctionSpecs();
BasicFunctionSpec copyColumnSpec = FunctionsFactory.eINSTANCE.createBasicFunctionSpec();
copyColumnSpec.setIsInfix(false);
copyColumnSpec.setName("CopyColumn");
copyColumnSpec.setFunctionalName("funcs.CopyColumn");
copyColumnSpec.setSqlName("ID");
attributeLineageModel.getSpecialFunctionSpecs().add(copyColumnSpec);
specialFunctions.copyColumnSpec = copyColumnSpec;
AggregateFunctionSpec firstColumnSpec = FunctionsFactory.eINSTANCE.createAggregateFunctionSpec();
firstColumnSpec.setName("First");
firstColumnSpec.setFunctionalName("funcs.First");
firstColumnSpec.setSqlName("First");
attributeLineageModel.getSpecialFunctionSpecs().add(firstColumnSpec);
specialFunctions.firstColumnSpec = firstColumnSpec;
AggregateFunctionSpec sumColumnSpec = FunctionsFactory.eINSTANCE.createAggregateFunctionSpec();
sumColumnSpec.setName("SUM");
sumColumnSpec.setFunctionalName("funcs.SUM");
sumColumnSpec.setSqlName("SUM");
attributeLineageModel.getSpecialFunctionSpecs().add(sumColumnSpec);
specialFunctions.sumColumnSpec = sumColumnSpec;
BasicFunctionSpec equalsSpec = FunctionsFactory.eINSTANCE.createBasicFunctionSpec();
equalsSpec.setName("isEqualTo");
equalsSpec.setFunctionalName("=");
equalsSpec.setIsInfix(true);
equalsSpec.setSqlName("=");
equalsSpec.setFunctionalName("==");
attributeLineageModel.getSpecialFunctionSpecs().add(equalsSpec);
specialFunctions.equalsSpec = equalsSpec;
return specialFunctions;
}
/**
* Create a BaseRowStructure from a cubeSchema in the context of a
* functionalModuleLogicList and a cubeSchemaModuleList.
*
* @param cubeSchema
* @param functionalModuleLogicList
* @param cubeSchemaModuleList
* @return
*/
public static BaseRowStructure translateCubeSchemaToBaseRowStructure
(CubeSchema cubeSchema,
EList<VersionedFunctionalModuleLogic> functionalModuleLogicList,
EList<VersionedCubeSchemaModule> cubeSchemaModuleList) {
EList<VARIABLE> columnsList = getColumnsFromCubeSchema(cubeSchema);
Iterator<VARIABLE> columnsIter = columnsList.iterator();
BaseRowStructure baseRowStructure = Row_transformation_logicFactory.eINSTANCE.createBaseRowStructure();
baseRowStructure.setCube((BaseCube) cubeSchema.getBaseCube());
while (columnsIter.hasNext()) {
VARIABLE colitem = columnsIter.next();
CubeColumn column = LineagefunctionsFactory.eINSTANCE.createCubeColumn();
column.setVariable(colitem);
column.setCube(cubeSchema.getBaseCube());
column.setColumnID(cubeSchema.getBaseCube().getName() + ":" + colitem.getVariable_id());
baseRowStructure.getColumns().add(column);
}
return baseRowStructure;
}
/**
* Get the columns associated with a cubeSchema.
*
* @param cubeSchema
* @return
*/
static EList<VARIABLE> getColumnsFromCubeSchema(CubeSchema cubeSchema) {
EList<CUBE_STRUCTURE_ITEM> csi = TRLUtil.getCubeStructureItemsFromDefaultBIRDModel(cubeSchema);
EList<VARIABLE> returnVariables = new BasicEList<VARIABLE>();
for (Iterator iterator2 = csi.iterator(); iterator2.hasNext();) {
CUBE_STRUCTURE_ITEM cube_STRUCTURE_ITEM = (CUBE_STRUCTURE_ITEM) iterator2.next();
returnVariables.add(cube_STRUCTURE_ITEM.getVariable_id());
}
return returnVariables;
// return getVariablesFromBIRDCube(cubeSchema.getCube());
}
/**
* Replace SpeculativeCubeColumnParameters with ResolvedCubeColumnParameters in an
* attributeLineageModel.
*
* @param attributeLineageModel
*/
public static void replaceSpeculativeColumnReferencesWithResolvedColumnReference(
AttributeLineageModel attributeLineageModel) {
Iterator<CubeTransformationLogic> rowlogicIter = attributeLineageModel.getRowTransformations().iterator();
while (rowlogicIter.hasNext()) {
CubeTransformationLogic rowlogic = rowlogicIter.next();
replaceSpeculativeTableLogicWithResolvedTableLogic(rowlogic, attributeLineageModel);
TreeIterator<EObject> iter = rowlogic.eAllContents();
while (iter.hasNext()) {
EObject o = iter.next();
if (o instanceof SpeculativeCubeColumnParameter) {
SpeculativeCubeColumnParameter qcrp = (SpeculativeCubeColumnParameter) o;
Function qcrpContainer = (Function) qcrp.eContainer();
ResolvedCubeColumnParameter linkedColParam = LineagefunctionsFactory.eINSTANCE.createResolvedCubeColumnParameter();
CubeColumn col = resolveColumnFromRowLogicGroup(qcrp, attributeLineageModel);
linkedColParam.setCubeColumn(col);
int index = qcrpContainer.getParameters().indexOf(qcrp);
qcrpContainer.getParameters().remove(index);
qcrpContainer.getParameters().add(index, linkedColParam);
}
}
}
}
/**
* @param rowlogic
* @param attributeLineageModel
*/
private static void replaceSpeculativeTableLogicWithResolvedTableLogic(CubeTransformationLogic rowlogic,
AttributeLineageModel attributeLineageModel) {
RowCreationApproachForCube tableLogic = rowlogic.getRowCreationApproachForCube();
RowCreationApproach rf = tableLogic.getRowCreationApproach();
if (rf instanceof GroupByRowCreationApproach) {
EList<CubeTransformationLogic> dependantRowLogics = AttributeLineageModelQuery.getTheDependantFunctionalRowLogics(rowlogic);
EList<BaseRowStructure> dependantBaseRowStructures = AttributeLineageModelQuery.getTheDependantBaseRowStructures(rowlogic);
FreeBirdToolsCube dependantTable = null;
if(dependantRowLogics.size() > 0)
dependantTable = dependantRowLogics.get(0).getRowCreationApproachForCube().getCube();
if(dependantBaseRowStructures.size() > 0)
dependantTable = dependantBaseRowStructures.get(0).getCube();
Iterator<VARIABLE> groupByCols = ((GroupByRowCreationApproach) rf).getGroupByColumns().iterator();
while (groupByCols.hasNext()) {
VARIABLE col = groupByCols.next();
CubeColumn tableColumn = resolveColumnFromRowLogicGroup(col, dependantTable, attributeLineageModel);
((GroupByRowCreationApproach) rf).getGroupByCubeColumns().add(tableColumn);
ResolvedCubeColumnParameter linkedColParam = LineagefunctionsFactory.eINSTANCE.createResolvedCubeColumnParameter();
linkedColParam.setCubeColumn(tableColumn);
rf.getDependantCubeColumns().add(linkedColParam);
}
}
if (rf instanceof FilterAndGroupToOneRowCreationApproach) {
TreeIterator<EObject> iter = rf.eAllContents();
while (iter.hasNext()) {
EObject o = iter.next();
if (o instanceof SpeculativeCubeColumnParameter) {
SpeculativeCubeColumnParameter qcrp = (SpeculativeCubeColumnParameter) o;
Function qcrpContainer = (Function) qcrp.eContainer();
ResolvedCubeColumnParameter linkedColParam = LineagefunctionsFactory.eINSTANCE.createResolvedCubeColumnParameter();
CubeColumn col = resolveColumnFromRowLogicGroup(qcrp, attributeLineageModel);
linkedColParam.setCubeColumn(col);
int index = qcrpContainer.getParameters().indexOf(qcrp);
qcrpContainer.getParameters().remove(index);
qcrpContainer.getParameters().add(index, linkedColParam);
rf.getDependantCubeColumns().add(EcoreUtil.copy(linkedColParam));
}
}
}
if ((rf instanceof RowJoinFunction) || (rf instanceof FilterRowCreationApproach)) {
// copy the basic functions,replace speculative with resolved, set onthe
// rowfunctions
TreeIterator<EObject> iter = rf.eAllContents();
while (iter.hasNext()) {
EObject o = iter.next();
if (o instanceof SpeculativeCubeColumnParameter) {
SpeculativeCubeColumnParameter qcrp = (SpeculativeCubeColumnParameter) o;
Function qcrpContainer = (Function) qcrp.eContainer();
ResolvedCubeColumnParameter linkedColParam = LineagefunctionsFactory.eINSTANCE.createResolvedCubeColumnParameter();
CubeColumn col = resolveColumnFromRowLogicGroup(qcrp, attributeLineageModel);
linkedColParam.setCubeColumn(col);
int index = qcrpContainer.getParameters().indexOf(qcrp);
qcrpContainer.getParameters().remove(index);
qcrpContainer.getParameters().add(index, linkedColParam);
rf.getDependantCubeColumns().add(EcoreUtil.copy(linkedColParam));
}
}
}
}
/**
* @param theColumn
* @param theCube
* @param attributeLineageModel
* @return
*/
private static CubeColumn resolveColumnFromRowLogicGroup(VARIABLE theColumn, FreeBirdToolsCube theCube,
AttributeLineageModel attributeLineageModel) {
EList<CubeTransformationLogic> rowLogicList = attributeLineageModel.getRowTransformations();
Iterator<CubeTransformationLogic> rowLogicIter = rowLogicList.iterator();
CubeColumn returnCol = null;
while (rowLogicIter.hasNext()) {
CubeTransformationLogic rowLogic = rowLogicIter.next();
EList<ColumnFunction> columnFuncs = rowLogic.getColumnFunctionGroup().getColumnFunctions();
Iterator<ColumnFunction> columnFuncsIter = columnFuncs.iterator();
while (columnFuncsIter.hasNext()) {
ColumnFunction col = columnFuncsIter.next();
if ((theColumn.equals(col.getVariable())) && (theCube.equals(col.getCube()))) {
returnCol = col;
}
}
RowCreationApproachForCube tl = rowLogic.getRowCreationApproachForCube();
RowCreationApproach rf = tl.getRowCreationApproach();
Iterator<CubeColumn> createdColumnns = rf.getCreatedCubeColumns().iterator();
while (createdColumnns.hasNext()) {
CubeColumn createdColumnn = createdColumnns.next();
if ((theColumn.equals(createdColumnn.getVariable())) && (theCube.equals(createdColumnn.getCube()))) {
returnCol = createdColumnn;
}
}
}
EList<BaseRowStructure> evalschemaList = attributeLineageModel.getBaseSchemas();
Iterator<BaseRowStructure> evalschemaIter = evalschemaList.iterator();
while (evalschemaIter.hasNext()) {
BaseRowStructure evalschema = evalschemaIter.next();
EList<CubeColumn> cols = evalschema.getColumns();
Iterator<CubeColumn> colsIter = cols.iterator();
while (colsIter.hasNext()) {
CubeColumn col = colsIter.next();
if ((theColumn.equals(col.getVariable())) && (theCube.equals(col.getCube()))) {
returnCol = col;
}
}
}
return returnCol;
}
/**
* @param speculateCubeColumn
* @param attributeLineageModel
* @return
*/
public static CubeColumn resolveColumnFromRowLogicGroup(SpeculativeCubeColumnParameter speculateCubeColumn,
AttributeLineageModel attributeLineageModel) {
return resolveColumnFromRowLogicGroup(speculateCubeColumn.getColumn(), speculateCubeColumn.getCube(),
attributeLineageModel);
}
/**
* @param cube
* @return
*/
static EList<VARIABLE> getVariablesFromBIRDCube(CUBE cube) {
// get the module, then the cube structure, then the items and then the
// varaibles.
CubeModule cm = (CubeModule) cube.eContainer();
EList<VARIABLE> returnVariables = new BasicEList<VARIABLE>();
EList<CUBE_STRUCTURE> cs = cm.getCubeStructures();
EList<CUBE_STRUCTURE_ITEM> csi = cm.getCubeStructureItems();
for (Iterator iterator = cs.iterator(); iterator.hasNext();) {
CUBE_STRUCTURE cube_STRUCTURE = (CUBE_STRUCTURE) iterator.next();
if (cube_STRUCTURE.equals(cube.getCube_structure_id())) {
for (Iterator iterator2 = csi.iterator(); iterator2.hasNext();) {
CUBE_STRUCTURE_ITEM cube_STRUCTURE_ITEM = (CUBE_STRUCTURE_ITEM) iterator2.next();
if (cube_STRUCTURE_ITEM.getCube_structure_id().equals(cube_STRUCTURE))
returnVariables.add(cube_STRUCTURE_ITEM.getVariable_id());
}
}
}
return returnVariables;
}
/**
* @param cubeSchemaModules
* @return
*/
static EList<CubeSchema> getSchemas(EList<VersionedCubeSchemaModule> cubeSchemaModules) {
EList<CubeSchema> returnList = new BasicEList<CubeSchema>();
for (Iterator iterator = cubeSchemaModules.iterator(); iterator.hasNext();) {
VersionedCubeSchemaModule cubeSchemaModule = (VersionedCubeSchemaModule) iterator.next();
EList<CubeSchema> schemas = ((VersionedCubeSchemaModule) cubeSchemaModule).getSchemas();
for (Iterator iterator2 = schemas.iterator(); iterator2.hasNext();) {
CubeSchema schema = (CubeSchema) iterator2.next();
returnList.add(schema);
}
}
return returnList;
}
}