use of org.apache.apex.malhar.python.base.util.NDimensionalArray in project apex-malhar by apache.
the class InterpreterThread method eval.
/**
* Evaluates a string expression by passing in any variable subsitution into the Interpreter space if required. Also
* handles the garbage collection of the variables passed and offers a configurable way to delete any variable created
* as part of the evaluation expression.
* @param command The string equivalent of the command
* @param variableToExtract The name of the variable that would need to be extracted from the python interpreter space
* to the JVM space.
* @param variableSubstituionParams Key value pairs representing the variables that need to be passed into the
* interpreter space and are part of the eval expression.
* @param deleteExtractedVariable if the L.H.S. of an assignment expression variable needs to be deleted. This is
* essentially the variable that is being requested to extract i.e. the second
* parameter to this method.
* @param expectedReturnType Class representing the expected return type
* @param <T> Template signature for the expected return type
* @return The value that is extracted from the interpreter space ( possibly created as part of the eval expression or
* otherwise ). Returns null if any error
*/
private <T> T eval(String command, String variableToExtract, Map<String, Object> variableSubstituionParams, boolean deleteExtractedVariable, Class<T> expectedReturnType) {
T variableToReturn = null;
LOG.debug("Executing eval expression " + command + " with return type : " + expectedReturnType);
try {
for (String aKey : variableSubstituionParams.keySet()) {
Object keyVal = variableSubstituionParams.get(aKey);
if (keyVal instanceof NDimensionalArray) {
keyVal = ((NDimensionalArray) keyVal).toNDArray();
}
JEP_INSTANCE.set(aKey, keyVal);
}
} catch (JepException e) {
errorEncountered = true;
LOG.error("Error while setting the params for eval expression " + command, e);
return null;
}
try {
LOG.debug("Executing the eval expression in the interpreter instance " + command);
JEP_INSTANCE.eval(command);
} catch (JepException e) {
errorEncountered = true;
LOG.error("Error while evaluating the expression " + command, e);
return null;
}
try {
if (variableToExtract != null) {
Object extractedVariable = JEP_INSTANCE.getValue(variableToExtract);
if (extractedVariable instanceof NDArray) {
LOG.debug(" Return type is a NumPy Array. Hence converting to NDimensionalArray instance");
NDArray ndArrayJepVal = (NDArray) extractedVariable;
NDimensionalArray nDimArray = new NDimensionalArray();
nDimArray.setData(ndArrayJepVal.getData());
nDimArray.setSignedFlag(ndArrayJepVal.isUnsigned());
int[] dimensions = ndArrayJepVal.getDimensions();
nDimArray.setDimensions(dimensions);
int lengthInOneDimension = 1;
for (int i = 0; i < dimensions.length; i++) {
lengthInOneDimension *= dimensions[i];
}
nDimArray.setLengthOfSequentialArray(lengthInOneDimension);
variableToReturn = expectedReturnType.cast(nDimArray);
} else {
variableToReturn = expectedReturnType.cast(extractedVariable);
}
if (deleteExtractedVariable) {
LOG.debug("Deleting the extracted variable from the Python interpreter space");
JEP_INSTANCE.eval(PYTHON_DEL_COMMAND + variableToExtract);
}
}
LOG.debug("Deleting all the variables from the python interpreter space ");
for (String aKey : variableSubstituionParams.keySet()) {
LOG.debug("Deleting " + aKey);
JEP_INSTANCE.eval(PYTHON_DEL_COMMAND + aKey);
}
} catch (JepException e) {
errorEncountered = true;
LOG.error("Error while evaluating delete part of expression " + command, e);
return null;
}
return variableToReturn;
}
Aggregations