use of jep.JepException 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;
}
use of jep.JepException in project apex-malhar by apache.
the class InterpreterThread method processCommand.
/**
* Responsible for polling the request queue and formatting the request payload to make it compatible to the
* individual processing logic of the functionalities provided by the interpreter API methods.
* @param <T> Java templating signature enforcement
* @throws ApexPythonInterpreterException if an unrecognized command is issued.
* @throws InterruptedException if interrupted while trying to wait for a request from request queue
*/
private <T> void processCommand() throws ApexPythonInterpreterException, InterruptedException {
PythonRequestResponse requestResponseHandle = requestQueue.poll(timeOutToPollFromRequestQueue, timeUnitsToPollFromRequestQueue);
if (requestResponseHandle != null) {
LOG.debug("Processing command " + requestResponseHandle.getPythonInterpreterRequest().getCommandType());
busyFlag = true;
if (errorEncountered) {
LOG.debug("Error state detected from a previous command. Resetting state to the previous" + " state of the error");
try {
JEP_INSTANCE.eval(null);
errorEncountered = false;
} catch (JepException e) {
LOG.error("Error while trying to clear the state of the interpreter due to previous command" + " " + e.getMessage(), e);
}
}
PythonInterpreterRequest<T> request = requestResponseHandle.getPythonInterpreterRequest();
PythonInterpreterResponse<T> response = requestResponseHandle.getPythonInterpreterResponse();
Map<String, Boolean> commandStatus = new HashMap<>(1);
switch(request.getCommandType()) {
case EVAL_COMMAND:
EvalCommandRequestPayload evalPayload = request.getEvalCommandRequestPayload();
T responseVal = eval(evalPayload.getEvalCommand(), evalPayload.getVariableNameToExtractInEvalCall(), evalPayload.getParamsForEvalCommand(), evalPayload.isDeleteVariableAfterEvalCall(), request.getExpectedReturnType());
response.setResponse(responseVal);
if (responseVal != null) {
commandStatus.put(evalPayload.getEvalCommand(), Boolean.TRUE);
} else {
commandStatus.put(evalPayload.getEvalCommand(), Boolean.FALSE);
}
response.setCommandStatus(commandStatus);
break;
case SCRIPT_COMMAND:
ScriptExecutionRequestPayload scriptPayload = request.getScriptExecutionRequestPayload();
if (executeScript(scriptPayload.getScriptName())) {
commandStatus.put(scriptPayload.getScriptName(), Boolean.TRUE);
} else {
commandStatus.put(scriptPayload.getScriptName(), Boolean.FALSE);
}
response.setCommandStatus(commandStatus);
break;
case METHOD_INVOCATION_COMMAND:
MethodCallRequestPayload requestpayload = request.getMethodCallRequest();
response.setResponse(executeMethodCall(requestpayload.getNameOfMethod(), requestpayload.getArgs(), request.getExpectedReturnType()));
if (response.getResponse() == null) {
commandStatus.put(requestpayload.getNameOfMethod(), Boolean.FALSE);
} else {
commandStatus.put(requestpayload.getNameOfMethod(), Boolean.TRUE);
}
response.setCommandStatus(commandStatus);
break;
case GENERIC_COMMANDS:
response.setCommandStatus(runCommands(request.getGenericCommandsRequestPayload().getGenericCommands()));
break;
default:
throw new ApexPythonInterpreterException(new Exception("Unspecified Interpreter command"));
}
requestResponseHandle.setRequestCompletionTime(System.currentTimeMillis());
responseQueue.put(requestResponseHandle);
LOG.debug("Submitted the response and executed " + response.getCommandStatus().size() + " instances of command");
}
busyFlag = false;
}
use of jep.JepException in project apex-malhar by apache.
the class InterpreterThread method startInterpreter.
/**
* Starts the interpreter by loading the shared libraries
* @throws ApexPythonInterpreterException if the interpreter could not be started
*/
public void startInterpreter() throws ApexPythonInterpreterException {
Thread.currentThread().setName(threadID);
// To allow for time aware calls
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
loadMandatoryJVMLibraries();
JepConfig config = new JepConfig().setRedirectOutputStreams(true).setInteractive(false).setClassLoader(Thread.currentThread().getContextClassLoader());
if (initConfigs.containsKey(PYTHON_INCLUDE_PATHS)) {
List<String> includePaths = (List<String>) initConfigs.get(PYTHON_INCLUDE_PATHS);
if (includePaths != null) {
LOG.info("Adding include path for the in-memory interpreter instance");
for (String anIncludePath : includePaths) {
config.addIncludePaths(anIncludePath);
}
}
}
if (initConfigs.containsKey(PYTHON_SHARED_LIBS)) {
Set<String> sharedLibs = (Set<String>) initConfigs.get(PYTHON_SHARED_LIBS);
if (sharedLibs != null) {
config.setSharedModules(sharedLibs);
LOG.info("Loaded " + sharedLibs.size() + " shared libraries as config");
}
} else {
LOG.info(" No shared libraries loaded");
}
if (initConfigs.containsKey(IDLE_INTERPRETER_SPIN_POLICY)) {
spinPolicy = SpinPolicy.valueOf((String) initConfigs.get(IDLE_INTERPRETER_SPIN_POLICY));
LOG.debug("Configuring spin policy to be " + spinPolicy);
}
if (initConfigs.containsKey(SLEEP_TIME_MS_IN_CASE_OF_NO_REQUESTS)) {
sleepTimeMsInCaseOfNoRequests = (Long) initConfigs.get(SLEEP_TIME_MS_IN_CASE_OF_NO_REQUESTS);
LOG.debug("Configuring sleep time for no requests situation to be " + sleepTimeMsInCaseOfNoRequests);
}
try {
LOG.info("Launching the in-memory interpreter");
JEP_INSTANCE = new Jep(config);
} catch (JepException e) {
// Purposefully logging as this will help in startup issues being captured inline
LOG.error(e.getMessage(), e);
throw new ApexPythonInterpreterException(e);
}
}
use of jep.JepException in project twister2 by DSC-SPIDAL.
the class AbstractPythonProcessor method initialValue.
@Override
protected Jep initialValue() {
try {
// get the jep instance for this thread
Jep jep = JepInstance.getInstance();
// todo temporary workaround for JepArray issue.
// This won't be a significant performance bottleneck though
String lambdaString = Base64.getEncoder().encodeToString(bytes);
jep.set("func_bin", lambdaString);
jep.eval(this.objectId + " = cp.loads(base64.b64decode(func_bin))");
jep.eval("del func_bin");
return jep;
} catch (JepException e) {
throw new Twister2RuntimeException("Error in building lambda function", e);
}
}
use of jep.JepException in project twister2 by DSC-SPIDAL.
the class JepInstance method initialValue.
@Override
protected Jep initialValue() {
JepConfig jepConfig = new JepConfig();
jepConfig.setRedirectOutputStreams(true);
// );
try {
TSharedInterpreter jep = new TSharedInterpreter(jepConfig);
jep.eval("import cloudpickle as cp");
jep.eval("import base64");
jep.eval("from abc import ABC, abstractmethod");
jep.eval("import numpy as np");
jep.eval("import twister2.utils as utils");
return jep;
} catch (JepException jex) {
throw new Twister2RuntimeException("Couldn't create a JEP instance", jex);
}
}
Aggregations