Search in sources :

Example 6 with TryResult

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");
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

Example 7 with TryResult

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());
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test) KernelTest(com.twosigma.beakerx.KernelTest)

Example 8 with TryResult

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;
}
Also used : StringWriter(java.io.StringWriter) TryResult(com.twosigma.beakerx.TryResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrintWriter(java.io.PrintWriter)

Example 9 with TryResult

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;
}
Also used : TryResult(com.twosigma.beakerx.TryResult) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with TryResult

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;
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

TryResult (com.twosigma.beakerx.TryResult)55 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)41 Test (org.junit.Test)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 NamespaceClient (com.twosigma.beakerx.NamespaceClient)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 KernelTest (com.twosigma.beakerx.KernelTest)4 EvaluatorParameters (com.twosigma.beakerx.kernel.EvaluatorParameters)3 PlainCode.createSimpleEvaluationObject (com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject)3 MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)3 HashMap (java.util.HashMap)3 Plot (com.twosigma.beakerx.chart.xychart.Plot)2 MagicCommandOutcomeItem (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem)2 Message (com.twosigma.beakerx.message.Message)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 ReplEvalResult (org.jetbrains.kotlin.cli.common.repl.ReplEvalResult)2 Code (com.twosigma.beakerx.kernel.Code)1