use of org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload 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.MethodCallRequestPayload 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.MethodCallRequestPayload in project apex-malhar by apache.
the class PythonRequestResponseUtil method buildRequestForMethodCallCommand.
/**
* Builds the request object for the Method call command. See
* {@link ApexPythonEngine#executeMethodCall(WorkerExecutionMode, long, long, PythonInterpreterRequest)} for details
* @param methodName Name of the method
* @param methodParams parames to the method
* @param timeOut Time allocated for completing the API
* @param timeUnit The units of time
* @param clazz The Class that represents the return type
* @param <T> Java templating signature
* @return The request object that can be used for method calls
*/
public static <T> PythonInterpreterRequest<T> buildRequestForMethodCallCommand(String methodName, List<Object> methodParams, long timeOut, TimeUnit timeUnit, Class<T> clazz) {
PythonInterpreterRequest<T> request = new PythonInterpreterRequest<>(clazz);
MethodCallRequestPayload methodCallRequestPayload = new MethodCallRequestPayload();
methodCallRequestPayload.setNameOfMethod(methodName);
methodCallRequestPayload.setArgs(methodParams);
request.setTimeUnit(timeUnit);
request.setTimeout(timeOut);
request.setMethodCallRequest(methodCallRequestPayload);
return request;
}
use of org.apache.apex.malhar.python.base.requestresponse.MethodCallRequestPayload in project apex-malhar by apache.
the class InterpreterThreadTest method testMethodCall.
@JepPythonTestContext(jepPythonBasedTest = true)
@Test
public void testMethodCall() throws Exception {
String methodName = "jepMultiply";
List<String> commands = new ArrayList();
commands.add("def " + methodName + "(firstnum, secondnum):\n" + // Note that this cannot be split as multiple commands
"\treturn (firstnum * secondnum)\n");
runCommands(commands);
List<Object> params = new ArrayList<>();
params.add(5L);
params.add(25L);
PythonRequestResponse<Long> methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.METHOD_INVOCATION_COMMAND);
MethodCallRequestPayload requestPayload = methodCallRequest.getPythonInterpreterRequest().getMethodCallRequest();
requestPayload.setNameOfMethod(methodName);
requestPayload.setArgs(params);
methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
PythonRequestResponse<Long> methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
assertEquals(methodCallResponse.getPythonInterpreterResponse().getResponse(), 125L);
Map<String, Boolean> commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertTrue(commandStatus.get(methodName));
params.remove(1);
methodCallRequest = buildRequestResponseObjectForLongPayload(PythonCommandType.METHOD_INVOCATION_COMMAND);
requestPayload = methodCallRequest.getPythonInterpreterRequest().getMethodCallRequest();
requestPayload.setNameOfMethod(methodName);
requestPayload.setArgs(params);
methodCallRequest.getPythonInterpreterRequest().setExpectedReturnType(Long.class);
pythonEngineThread.getRequestQueue().put(methodCallRequest);
// wait for command to be processed
Thread.sleep(1000);
methodCallResponse = pythonEngineThread.getResponseQueue().poll(1, TimeUnit.SECONDS);
commandStatus = methodCallResponse.getPythonInterpreterResponse().getCommandStatus();
assertFalse(commandStatus.get(methodName));
}
Aggregations