Repository navigation
Expand file tree
/
Copy pathAssimilator.java
More file actions
359 lines (329 loc) · 14.8 KB
/
Copy pathAssimilator.java
File metadata and controls
359 lines (329 loc) · 14.8 KB
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
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.reactor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.nounmeta.NounMetadata;
import prerna.sablecc2.om.task.TaskUtility;
import prerna.util.Constants;
public class Assimilator extends AbstractReactor implements JavaExecutable {
private static final Logger classLogger = LogManager.getLogger(Assimilator.class);
// roles of the assimilator is simple, just assimilate an expression and then
// plug it into the parent
// filter is a good example of assimilator for example
private boolean containsStringValue = false;
protected List<String> formulas = new Vector<String>();
@Override
public NounMetadata execute() {
modifyForEmbeddedScripts();
modifySignatureFromLambdas();
for(String formula : formulas) {
this.signature = StringUtils.replaceOnce( this.signature, formula, "( 1.0 * " + formula.substring(1, formula.length()));
}
// get the assimilator evaluator
// this is the class we are going to be using to execute
// if we are running this again, we will not create the class and add
// it to the ClassPool, but if it is new, we will
AssimilatorEvaluator newInstance = getAssimilatorEvaluator();
// noun object to return
// need to cast to get the type of the NounMetadata object
NounMetadata noun = new NounMetadata(newInstance, PixelDataType.LAMBDA);
return noun;
// Object retVal = newInstance.execute();
// if(newInstance.containsStringValue) {
// noun = new NounMetadata(retVal.toString(), PkslDataTypes.CONST_STRING);
// } else if(allIntValue) {
// Number result = (Number) retVal;
// if(result.doubleValue() == Math.rint(result.doubleValue())) {
// noun = new NounMetadata( ((Number) retVal).intValue(), PkslDataTypes.CONST_INT);
// } else {
// // not a valid integer
// // return as a double
// noun = new NounMetadata( ((Number) retVal).doubleValue(), PkslDataTypes.CONST_DECIMAL);
// }
// } else {
// noun = new NounMetadata( ((Number) retVal).doubleValue(), PkslDataTypes.CONST_DECIMAL);
// }
//
// return noun;
}
/**
*
* method to get the run time class for the assimilator
*/
private AssimilatorEvaluator getAssimilatorEvaluator() {
// store the variables
// this will get added to by reference
Map<String, Object> vars = new HashMap<String, Object>();
//stringified method for the evaluator
String stringMethod = buildMethodString(vars);
return buildAssimilatorEvaluator(stringMethod, vars);
}
/**
*
* @param stringMethod
* @return
*
* method responsible for building a new assimilator class from a stringified method
*/
private AssimilatorEvaluator buildAssimilatorEvaluator(String stringMethod, Map<String, Object> vars) {
// evaluate the assimilator as an object
ClassMaker maker = new ClassMaker();
// add a super so we have a base method to execute
maker.addSuper(AssimilatorEvaluator.class.getName());
maker.addMethod(stringMethod);
Class newClass = maker.toClass();
try {
AssimilatorEvaluator newInstance = (AssimilatorEvaluator) newClass.newInstance();
newInstance.setVars(vars);
newInstance.containsStringValue = this.containsStringValue;
return newInstance;
} catch (InstantiationException | IllegalAccessException e) {
classLogger.error(Constants.STACKTRACE, e);
}
return null;
}
/**
* method responsible for building the method
*/
private String buildMethodString(Map<String, Object> vars) {
// keep a string to generate the method to execute that will
// return an object that runs the expression
StringBuilder expressionBuilder = new StringBuilder();
expressionBuilder.append("public Object getExpressionValue(){");
// we need to grab any variables and define them at the top of the method
appendVariables(expressionBuilder, vars);
// now that the variables are defined
// we just want to add the expression as a return
if(this.containsStringValue) {
expressionBuilder.append("return new String(").append(this.signature).append(");}");
} else {
// multiply by 1.0 to make sure everything is a double...
// as a pixel data type
expressionBuilder.append("return new java.math.BigDecimal(1.0 * ").append(this.signature).append(");}");
}
return expressionBuilder.toString();
}
/**
* Append the variables used within the expression
* @param expressionBuilder
*/
private void appendVariables(StringBuilder expressionBuilder, Map<String, Object> vars) {
List<String> inputColumns = curRow.getAllColumns();
// these input columns should be defined at the beginning of the expression
// technically, someone can use the same variable multiple times
// so need to account for this
// ... just add them to a set and call it a day
Set<String> uniqueInputs = new HashSet<String>();
uniqueInputs.addAll(inputColumns);
for(String input : uniqueInputs) {
NounMetadata data = planner.getVariableValue(input);
if(data == null) {
// this only happens when a variable is being used but isn't defined
throw new IllegalArgumentException("Undefined variable : " + input);
}
Object value = data.getValue();
PixelDataType dataType = data.getNounType();
if(dataType == PixelDataType.CONST_DECIMAL) {
expressionBuilder.append("double ").append(input).append(" = ").append("((Number)super.vars.get("+"\""+input+"\")).doubleValue()").append(";");
vars.put(input, value);
} else if(dataType == PixelDataType.CONST_INT) {
expressionBuilder.append("int ").append(input).append(" = ").append("((Number)super.vars.get("+"\""+input+"\")).intValue()").append(";");
vars.put(input, value);
} else if(dataType == PixelDataType.CONST_STRING) {
this.containsStringValue = true;
expressionBuilder.append("String ").append(input).append(" = ").append("(String)super.vars.get("+"\""+input+"\")").append(";");
vars.put(input, value);
} else if(dataType == PixelDataType.LAMBDA){
// in case the variable points to another reactor
// that we need to get the value from
// evaluate the lambda
// object better be a reactor to run
Object rVal = data.getValue();
if(rVal instanceof IReactor) {
Object newValue = data.getValue();
PixelDataType newDataType = data.getNounType();
if(newDataType == PixelDataType.CONST_DECIMAL) {
expressionBuilder.append("double ").append(input).append(" = ").append("((Number)super.vars.get("+"\""+input+"\")).doubleValue()").append(";");
vars.put(input, newValue);
} else if(newDataType == PixelDataType.CONST_INT) {
expressionBuilder.append("int ").append(input).append(" = ").append("((Number)super.vars.get("+"\""+input+"\")).intValue()").append(";");
vars.put(input, newValue);
} else {
this.containsStringValue = true;
expressionBuilder.append("String ").append(input).append(" = ").append("(String)super.vars.get("+"\""+input+"\")").append(";");
vars.put(input, newValue);
}
} else {
// this should never ever happen....
throw new IllegalArgumentException("Assimilator cannot handle this type if input");
}
} else if(dataType == PixelDataType.FORMATTED_DATA_SET || dataType == PixelDataType.TASK) {
NounMetadata formatData = TaskUtility.getTaskDataScalarElement(value);
if(formatData == null) {
throw new IllegalArgumentException("Can only handle query data that is a scalar input");
} else {
Object newValue = formatData.getValue();
PixelDataType newDataType = formatData.getNounType();
if(newDataType == PixelDataType.CONST_DECIMAL) {
expressionBuilder.append("double ").append(input).append(" = ").append("((Number)super.vars.get("+"\""+input+"\")).doubleValue()").append(";");
vars.put(input, newValue);
} else if(newDataType == PixelDataType.CONST_INT) {
expressionBuilder.append("int ").append(input).append(" = ").append("((Number)super.vars.get("+"\""+input+"\")).intValue()").append(";");
vars.put(input, newValue);
} else {
this.containsStringValue = true;
expressionBuilder.append("String ").append(input).append(" = ").append("(String)super.vars.get("+"\""+input+"\")").append(";");
vars.put(input, newValue);
}
}
} else {
this.containsStringValue = true;
expressionBuilder.append("String ").append(input).append(" = ").append("(String)(super.vars.get("+"\""+input+"\")+\"\")").append(";");
vars.put(input, value);
//throw new IllegalArgumentException("Unable to handle this type of input");
}
}
// need to how check if there are strings
int numValues = this.curRow.size();
for(int i = 0; i < numValues; i++) {
if(this.curRow.getNoun(i).getNounType() == PixelDataType.CONST_STRING) {
this.containsStringValue = true;
break;
}
}
}
/**
* Modify the expression for embedded scripts
*/
protected void modifyForEmbeddedScripts() {
// need to account for embedded scripts that we want to evaluate
List<NounMetadata> lambdas = curRow.getNounsOfType(PixelDataType.LAMBDA);
for(int i = 0; i < lambdas.size(); i++) {
NounMetadata n = lambdas.get(i);
if(n.getValue() instanceof EmbeddedScriptReactor) {
// replace the value with the output
NounMetadata output = ((EmbeddedScriptReactor) n.getValue()).execute();
PixelDataType dataType = output.getNounType();
if(dataType == PixelDataType.CONST_DECIMAL || dataType == PixelDataType.CONST_INT) {
this.signature = StringUtils.replaceOnce( this.signature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), output.getValue().toString());
} else if(dataType == PixelDataType.CONST_STRING) {
this.signature = StringUtils.replaceOnce( this.signature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), output.getValue().toString());
this.containsStringValue = true;
} else if(dataType == PixelDataType.FORMATTED_DATA_SET || dataType == PixelDataType.TASK) {
NounMetadata formatData = TaskUtility.getTaskDataScalarElement(output);
if(formatData == null) {
throw new IllegalArgumentException("Can only handle query data that is a scalar input");
} else {
Object newValue = formatData.getValue();
PixelDataType newDataType = formatData.getNounType();
if(newDataType == PixelDataType.CONST_DECIMAL || newDataType == PixelDataType.CONST_INT) {
this.signature = StringUtils.replaceOnce( this.signature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), newValue.toString());
} else {
this.signature = StringUtils.replaceOnce( this.signature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), newValue.toString());
this.containsStringValue = true;
}
}
} else {
throw new IllegalArgumentException("Unable to handle this type of input");
}
}
}
}
public void addFormula(String formula) {
// always append at the beginning
// so that we address the most inner one
this.formulas.add(0, formula);
}
@Override
public List<NounMetadata> getOutputs() {
List<NounMetadata> outputs = super.getOutputs();
if(outputs != null) {
return outputs;
}
outputs = new Vector<NounMetadata>();
NounMetadata output = new NounMetadata(this.signature, PixelDataType.LAMBDA);
outputs.add(output);
return outputs;
}
@Override
public String getJavaSignature() {
String javaSig = this.signature;
// replace all the values that is inside this. this could be a recursive call
for(int i = 0; i < curRow.size(); i++) {
NounMetadata thisLambdaMeta = curRow.getNoun(i);
Object nextValue = (Object)thisLambdaMeta.getValue();
String replaceValue;
if(nextValue instanceof JavaExecutable) {
replaceValue = ((JavaExecutable)nextValue).getJavaSignature();
} else {
continue;
}
String rSignature;
if(nextValue instanceof IReactor) {
rSignature = ((IReactor)nextValue).getOriginalSignature();
} else {
continue;
}
// NounMetadata result = thisReactor.execute();// this might further trigger other things
// // for compilation reasons
// // if we have a double
// // we dont want it to print with the exponential
// Object replaceValue = result.getValue();
// PkslDataTypes replaceType = result.getNounName();
// if(replaceType == PkslDataTypes.CONST_DECIMAL ||
// replaceType == PkslDataTypes.CONST_INT) {
// // big decimal is easiest way i have seen to do this formatting
// replaceValue = new BigDecimal( ((Number) replaceValue).doubleValue()).toPlainString();
// } else {
// replaceValue = replaceValue + "";
// }
javaSig = modifyJavaSignature(javaSig, rSignature, replaceValue);
}
return javaSig;
}
protected String modifyJavaSignature(String javaSignature, String stringToFind, String stringReplacement) {
return StringUtils.replaceOnce(javaSignature, stringToFind, stringReplacement);
}
@Override
public List<NounMetadata> getJavaInputs() {
return null;
}
@Override
public String getReturnType() {
return "double";
}
}