Repository navigation
Expand file tree
/
Copy pathPowAssimilator.java
More file actions
248 lines (227 loc) · 10.7 KB
/
Copy pathPowAssimilator.java
File metadata and controls
248 lines (227 loc) · 10.7 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
/*******************************************************************************
* 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.HashSet;
import java.util.List;
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 PowAssimilator extends Assimilator {
private static final Logger LOGGER = LogManager.getLogger(PowAssimilator.class.getName());
private String lSignature;
private String rSignature;
@Override
public NounMetadata execute() {
modifyForEmbeddedScripts();
modifySignatureFromLambdas();
// in order to ensure that we are properly creating the object as a double
// we need to remove any wrapping parenthesis such that we can
// properly make it a double by multiplying by 1.0
for(String formula : formulas) {
this.lSignature = StringUtils.replaceOnce( this.lSignature, formula, "( 1.0 * " + formula.substring(1, formula.length()));
this.rSignature = StringUtils.replaceOnce( this.rSignature, formula, "( 1.0 * " + formula.substring(1, formula.length()));
}
// while(lSignature.startsWith("(") && lSignature.endsWith(")")) {
// lSignature = lSignature.substring(1, lSignature.length()-1).trim();
// }
// while(rSignature.startsWith("(") && rSignature.endsWith(")")) {
// rSignature = rSignature.substring(1, rSignature.length()-1).trim();
// }
// evaluate the assimilator as an object
ClassMaker maker = new ClassMaker();
// 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);
// now that the variables are defined
// we just want to add the expression as a return
expressionBuilder.append("return new java.math.BigDecimal(Math.pow( 1.0 * " + lSignature + ", 1.0 * " + rSignature + "));}");
maker.addMethod(expressionBuilder.toString());
// add a super so we have a base method to execute
maker.addSuper(AssimilatorEvaluator.class.getName());
Class newClass = maker.toClass();
// noun object to return
// need to cast to get the type of the NounMetadata object
NounMetadata noun = null;
try {
AssimilatorEvaluator newInstance = (AssimilatorEvaluator) newClass.newInstance();
noun = newInstance.execute();
// to avoid java error which cannot be caught
// if the return is null
// we will throw the exception here
// which means we could not evaluate the signature
if(noun == null) {
throw new IllegalArgumentException("Error!!! Could not properly evaluate expression = " + this.signature);
}
} catch (InstantiationException | IllegalAccessException e) {
LOGGER.error(Constants.STACKTRACE, e);
}
return noun;
}
@Override
public void modifySignature(String stringToFind, String stringReplacement) {
// for this special reactor
// we need to run this on both the
// left hand side and
// right hand size
LOGGER.debug("Original left signature value = " + this.lSignature);
this.lSignature = StringUtils.replaceOnce( this.lSignature, stringToFind, stringReplacement);
LOGGER.debug("New left signature value = " + this.lSignature);
LOGGER.debug("Original right signature value = " + this.rSignature);
this.rSignature = StringUtils.replaceOnce( this.rSignature, stringToFind, stringReplacement);
LOGGER.debug("New right signature value = " + this.rSignature);
}
/**
* Modify the expression for embedded scripts
*/
@Override
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.lSignature = StringUtils.replaceOnce( this.lSignature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), output.getValue().toString());
this.rSignature = StringUtils.replaceOnce( this.rSignature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), output.getValue().toString());
} 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.lSignature = StringUtils.replaceOnce( this.lSignature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), newValue.toString());
this.rSignature = StringUtils.replaceOnce( this.rSignature, ((EmbeddedScriptReactor) n.getValue()).getOriginalSignature(), newValue.toString());
}
}
} else {
throw new IllegalArgumentException("Unable to handle this type of input");
}
}
}
}
/**
* Append the variables used within the expression
* @param expressionBuilder
*/
private void appendVariables(StringBuilder expressionBuilder) {
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);
}
PixelDataType dataType = data.getNounType();
// we have a double that is stored
if(dataType == PixelDataType.CONST_DECIMAL) {
expressionBuilder.append("double ").append(input).append(" = ").append(data.getValue()).append(";");
}
// we have an integer that is stored
else if(dataType == PixelDataType.CONST_INT) {
expressionBuilder.append("int ").append(input).append(" = ").append(data.getValue()).append(";");
}
// we have a lambda
// so we execute and return the operation output
// TODO: this should no longer be needed since getVariableValue will evaluate a lambda
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) {
NounMetadata newNoun = ((IReactor) rVal).execute();
PixelDataType newDataType = data.getNounType();
if(newDataType == PixelDataType.CONST_DECIMAL) {
expressionBuilder.append("double ").append(input).append(" = ").append(newNoun.getValue()).append(";");
} else if(newDataType == PixelDataType.CONST_INT) {
expressionBuilder.append("int ").append(input).append(" = ").append(newNoun.getValue()).append(";");
}
} else {
// this should never ever happen....
throw new IllegalArgumentException("Assimilator cannot handle this type if input");
}
} else {
throw new IllegalArgumentException("Assimilator can currently only handle outputs of scalar variables");
}
}
}
public void setExpressions(String lSignature, String rSignature) {
this.lSignature = lSignature;
this.rSignature = rSignature;
}
@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() {
// 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 strToFind = null;
String replaceValue = null;
if(nextValue instanceof JavaExecutable && nextValue instanceof IReactor) {
replaceValue = ((JavaExecutable)nextValue).getJavaSignature();
strToFind = ((IReactor)nextValue).getOriginalSignature();
} else {
continue;
}
modifySignature(strToFind, replaceValue);
}
return "new Double(Math.pow( 1.0 * " + lSignature + ", 1.0 * " + rSignature + "));";
}
}