use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class GroovyEvaluatorStackTraceTest method unableToResolveClass.
@Test
public void unableToResolveClass() throws Exception {
String code = "new IntSlider()";
SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
// when
TryResult evaluate = groovyEvaluator.evaluate(seo, code);
// then
assertThat(evaluate.isError()).isTrue();
System.out.println(evaluate.error());
assertThat(evaluate.error()).contains("unable to resolve class IntSlider");
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class GroovyOutputContainerTest method shouldAddPlotToOutputContainerTest.
@Test
public void shouldAddPlotToOutputContainerTest() throws Exception {
// given
String code = "import com.twosigma.beakerx.groovy.evaluator.ResourceLoaderTest;\n" + "import com.twosigma.beakerx.jvm.object.OutputContainer;\n" + "import com.twosigma.beakerx.chart.xychart.SimpleTimePlot;\n" + "List<Map<?, ?>> rates = ResourceLoaderTest.readAsList(\"tableRowsTest.csv\");\n" + "plot2 = new SimpleTimePlot(rates, [\"m3\", \"y1\"], showLegend:false, initWidth: 300, initHeight: 400)\n" + "new OutputContainer() << plot2";
// when
SimpleEvaluationObject evaluationObject = PlainCode.createSimpleEvaluationObject(code, groovyKernel, HEADER_MESSAGE, 1);
TryResult seo = groovyEvaluatorManager.executeCode(code, evaluationObject);
// then
assertThat(seo.result()).isNotNull();
verifyPlot(groovyKernel.getPublishedMessages());
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class JavaCodeRunner method call.
@Override
public TryResult call() throws Exception {
ClassLoader oldld = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(javaEvaluator.getJavaClassLoader());
InternalVariable.setValue(theOutput);
TryResult either;
try {
theOutput.setOutputHandler();
InternalVariable.setValue(theOutput);
either = runCode(j);
} catch (Throwable e) {
if (e instanceof InvocationTargetException)
e = ((InvocationTargetException) e).getTargetException();
if ((e instanceof InterruptedException) || (e instanceof ThreadDeath)) {
either = TryResult.createError(INTERUPTED_MSG);
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
either = TryResult.createError(sw.toString());
}
} finally {
theOutput.clrOutputHandler();
Thread.currentThread().setContextClassLoader(oldld);
}
return either;
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class JavaCodeRunner method runCode.
private TryResult runCode(JobDescriptor j) {
TryResult either;
j.outputObject.started();
String code = ParserUtil.normalizeCode(j.codeToBeExecuted).replaceAll("\r\n", "\n");
Codev codev = new Codev(code, javaEvaluator);
try {
either = compileCode(j, codev);
} catch (Exception e) {
either = TryResult.createError(e.getMessage());
}
return either;
}
use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.
the class JavaCodeRunner method compileAndRunCode.
private TryResult compileAndRunCode(JobDescriptor j, Codev codev) {
TryResult either;
String classId = generateClassId();
String returnType = "Object";
Codev copyCodev = new Codev(codev.getCode(), javaEvaluator);
boolean compile = compile(codev, classId, returnType);
if (!compile) {
classId = generateClassId();
returnType = "void";
copyCodev = new Codev(codev.getCode(), javaEvaluator);
compile(copyCodev, classId, returnType);
}
try {
Class<?> fooClass = javaEvaluator.getJavaClassLoader().loadClass(copyCodev.getPname() + "." + JavaEvaluator.WRAPPER_CLASS_NAME + classId);
Method mth = fooClass.getDeclaredMethod("beakerRun", (Class[]) null);
Object o = mth.invoke(null, (Object[]) null);
if (returnType.equals("Object")) {
either = TryResult.createResult(o);
} else {
either = TryResult.createResult(null);
}
} catch (CompilationException e) {
either = TryResult.createError(buildErrorMessage(e, copyCodev.lineNumbersMapping));
} catch (Exception e) {
either = TryResult.createError("ERROR: " + e.getCause());
}
return either;
}
Aggregations