Search in sources :

Example 11 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class KotlinCodeRunner method call.

@Override
public TryResult call() throws Exception {
    TryResult either;
    ClassLoader oldld = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(loader);
    InternalVariable.setValue(theOutput);
    try {
        theOutput.setOutputHandler();
        InternalVariable.setValue(theOutput);
        ReplEvalResult eval = repl.eval(this.codeToBeExecuted);
        either = interpretResult(eval);
    } 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 : ReplEvalResult(org.jetbrains.kotlin.cli.common.repl.ReplEvalResult) StringWriter(java.io.StringWriter) TryResult(com.twosigma.beakerx.TryResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) PrintWriter(java.io.PrintWriter)

Example 12 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class KotlinWorkerThread method call.

@Override
public TryResult call() throws Exception {
    NamespaceClient nc = null;
    TryResult either;
    try {
        nc = NamespaceClient.getBeaker(kotlinEvaluator.getSessionId());
        nc.setOutputObj(j.outputObject);
        j.outputObject.started();
        try {
            KotlinCodeRunner kotlinCodeRunner = new KotlinCodeRunner(j.outputObject, kotlinEvaluator.getClassLoader(), kotlinEvaluator.getRepl(), j.codeToBeExecuted);
            either = kotlinEvaluator.executeTask(kotlinCodeRunner);
            if (nc != null) {
                nc.setOutputObj(null);
                nc = null;
            }
        } catch (Exception e) {
            either = TryResult.createError(e.getMessage());
        }
    } catch (Throwable e) {
        e.printStackTrace();
        either = TryResult.createError(e.getMessage());
    } finally {
        if (nc != null) {
            nc.setOutputObj(null);
            nc = null;
        }
    }
    return either;
}
Also used : NamespaceClient(com.twosigma.beakerx.NamespaceClient) TryResult(com.twosigma.beakerx.TryResult) IOException(java.io.IOException)

Example 13 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class JavaEvaluatorTest method evaluateDivisionByZero_shouldReturnArithmeticException.

@Test
public void evaluateDivisionByZero_shouldReturnArithmeticException() throws Exception {
    // given
    String code = "return 16/0;";
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    // when
    TryResult evaluate = javaEvaluator.evaluate(seo, code);
    // then
    assertThat(evaluate.error()).contains("java.lang.ArithmeticException");
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

Example 14 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class JavaEvaluatorTest method evaluateStreamInMultipleLines.

@Test
public void evaluateStreamInMultipleLines() throws Exception {
    // given
    String code = "import java.util.stream.Stream;\n" + "return Stream.of(1, 2, 3, 4).map(i -> { \n" + "    return i * 10;\n" + "});";
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    // when
    TryResult evaluate = javaEvaluator.evaluate(seo, code);
    // then
    assertThat(evaluate.result()).isNotNull();
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

Example 15 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class JavaEvaluatorTest method evaluatePlot_shouldCreatePlotObject.

@Test
public void evaluatePlot_shouldCreatePlotObject() throws Exception {
    // given
    String code = "import com.twosigma.beakerx.chart.xychart.*;\n" + "Plot plot = new Plot(); plot.setTitle(\"test title\");\n" + "return plot;";
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    // when
    TryResult evaluate = javaEvaluator.evaluate(seo, code);
    // then
    assertThat(evaluate.result() instanceof Plot).isTrue();
    assertThat(((Plot) evaluate.result()).getTitle()).isEqualTo("test title");
}
Also used : TryResult(com.twosigma.beakerx.TryResult) Plot(com.twosigma.beakerx.chart.xychart.Plot) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

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