use of org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload 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 org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload in project apex-malhar by apache.
the class PythonRequestResponseUtil method buildRequestForEvalCommand.
/**
* Builds the request object for the Eval command request. See
* {@link ApexPythonEngine#eval(WorkerExecutionMode, long, long, PythonInterpreterRequest)} for details
* @param evalCommand The eval expression
* @param evalParams Variables that need to be substituted
* @param varNameToExtract The name of variable to extract if any after the expression is evaluated. Can be null
* @param deleteVarAfterExtract The name of the variable to delete if any. null allowed
* @param timeOut Timeout for the API to complete processing
* @param timeUnit Units of time for the time out variable
* @param clazz The Class that represents the return type
* @param <T> Template construct for Java type inference
* @return The request object that can be used for the Eval command
*/
public static <T> PythonInterpreterRequest<T> buildRequestForEvalCommand(String evalCommand, Map<String, Object> evalParams, String varNameToExtract, boolean deleteVarAfterExtract, long timeOut, TimeUnit timeUnit, Class<T> clazz) {
PythonInterpreterRequest<T> request = new PythonInterpreterRequest<>(clazz);
EvalCommandRequestPayload evalCommandRequestPayload = new EvalCommandRequestPayload();
evalCommandRequestPayload.setEvalCommand(evalCommand);
evalCommandRequestPayload.setVariableNameToExtractInEvalCall(varNameToExtract);
evalCommandRequestPayload.setParamsForEvalCommand(evalParams);
evalCommandRequestPayload.setDeleteVariableAfterEvalCall(deleteVarAfterExtract);
request.setTimeUnit(timeUnit);
request.setTimeout(timeOut);
request.setEvalCommandRequestPayload(evalCommandRequestPayload);
return request;
}
use of org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload in project apex-malhar by apache.
the class BaseJEPTest method buildRequestObjectForLongEvalCommand.
protected PythonInterpreterRequest<Long> buildRequestObjectForLongEvalCommand(String command, String returnVar, Map<String, Object> paramsForEval, long timeOut, TimeUnit timeUnit, boolean deleteVariable) {
PythonInterpreterRequest<Long> request = new PythonInterpreterRequest<>(Long.class);
request.setTimeout(timeOut);
request.setTimeUnit(timeUnit);
EvalCommandRequestPayload evalCommandRequestPayload = new EvalCommandRequestPayload();
request.setEvalCommandRequestPayload(evalCommandRequestPayload);
evalCommandRequestPayload.setParamsForEvalCommand(paramsForEval);
evalCommandRequestPayload.setDeleteVariableAfterEvalCall(deleteVariable);
evalCommandRequestPayload.setVariableNameToExtractInEvalCall(returnVar);
evalCommandRequestPayload.setEvalCommand(command);
request.setExpectedReturnType(Long.class);
return request;
}
use of org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload in project apex-malhar by apache.
the class BaseJEPTest method setCommonConstructsForRequestResponseObject.
private void setCommonConstructsForRequestResponseObject(PythonCommandType commandType, PythonInterpreterRequest request, PythonRequestResponse requestResponse) throws ApexPythonInterpreterException {
request.setCommandType(commandType);
requestResponse.setRequestStartTime(System.currentTimeMillis());
requestResponse.setRequestId(1L);
requestResponse.setWindowId(1L);
switch(commandType) {
case EVAL_COMMAND:
EvalCommandRequestPayload payload = new EvalCommandRequestPayload();
request.setEvalCommandRequestPayload(payload);
break;
case METHOD_INVOCATION_COMMAND:
MethodCallRequestPayload methodCallRequest = new MethodCallRequestPayload();
request.setMethodCallRequest(methodCallRequest);
break;
case SCRIPT_COMMAND:
ScriptExecutionRequestPayload scriptPayload = new ScriptExecutionRequestPayload();
request.setScriptExecutionRequestPayload(scriptPayload);
break;
case GENERIC_COMMANDS:
GenericCommandsRequestPayload payloadForGenericCommands = new GenericCommandsRequestPayload();
request.setGenericCommandsRequestPayload(payloadForGenericCommands);
break;
default:
throw new ApexPythonInterpreterException("Unsupported command type");
}
}
use of org.apache.apex.malhar.python.base.requestresponse.EvalCommandRequestPayload in project apex-malhar by apache.
the class InterpreterThreadTest method testEvalCall.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testEvalCall() throws Exception {
String expression = new String("x = a + b");
Random random = new Random();
int a = random.nextInt(100);
int b = random.nextInt(100);
Map<String, Object> argsForEval = new HashMap<>();
argsForEval.put("a", a);
argsForEval.put("b", b);
PythonRequestResponse<Long> methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.EVAL_COMMAND);
EvalCommandRequestPayload payload = methodCallRequest.getPythonInterpreterRequest().getEvalCommandRequestPayload();
payload.setEvalCommand(expression);
payload.setParamsForEvalCommand(argsForEval);
payload.setDeleteVariableAfterEvalCall(true);
payload.setVariableNameToExtractInEvalCall("x");
methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
PythonRequestResponse<Integer> methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
Map<String, Boolean> commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertTrue(commandStatus.get(expression));
assertEquals(methodCallResponse.getPythonInterpreterResponse().getResponse(), (long) (a + b));
}
Aggregations