Search in sources :

Example 1 with EvalControlledCallable

use of org.matheclipse.core.eval.EvalControlledCallable in project symja_android_library by axkr.

the class SymjaMMACodeRunner method interpreter.

public Object interpreter(ExprEvaluator fEvaluator, OutputFormFactory fOutputFactory, final String inputExpression) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        String trimmedInput = inputExpression.trim();
        if (trimmedInput.length() >= 4 && trimmedInput.charAt(0) == '/') {
            Object meta = symjammaEvaluator.metaCommand(this, trimmedInput);
            if (meta != null) {
                return meta;
            }
        }
        if (symjammaEvaluator.fSeconds <= 0) {
            result = fEvaluator.eval(inputExpression);
        } else {
            result = fEvaluator.evaluateWithTimeout(inputExpression, symjammaEvaluator.fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            if (result.equals(S.Null)) {
                return "Null";
            } else {
                return result;
            }
        }
    } catch (final Exception e) {
        LOGGER.error("SymjaMMACodeRunner.interpreter() failed", e);
    }
    return new MarkdownNotebookOutput(buf.toString());
}
Also used : MarkdownNotebookOutput(com.twosigma.beakerx.symjamma.output.MarkdownNotebookOutput) StringWriter(java.io.StringWriter) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) IExpr(org.matheclipse.core.interfaces.IExpr) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with EvalControlledCallable

use of org.matheclipse.core.eval.EvalControlledCallable in project symja_android_library by axkr.

the class AbstractRubiTestCase method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 */
public String interpreter(final String inputExpression, final String expectedResult, String manuallyCheckedResult) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    IExpr integral = fEvaluator.parse(inputExpression).first();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.eval(inputExpression);
        } else {
            EvalEngine engine = fEvaluator.getEvalEngine();
            engine.setSeconds(fSeconds);
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(integral, result, expectedResult, manuallyCheckedResult);
        }
    } catch (final AbortException re) {
        return printResult(integral, F.$Aborted, expectedResult, manuallyCheckedResult);
    } catch (final FailedException re) {
        return printResult(integral, F.$Failed, expectedResult, manuallyCheckedResult);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println(msg);
        System.err.println();
        System.err.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalEngine(org.matheclipse.core.eval.EvalEngine) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 3 with EvalControlledCallable

use of org.matheclipse.core.eval.EvalControlledCallable in project symja_android_library by axkr.

the class AbstractRubiTestCase method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 */
public String interpreter(final String inputExpression, final String expectedResult, String manuallyCheckedResult) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (fSeconds <= 0) {
            result = fEvaluator.eval(inputExpression);
        } else {
            EvalEngine engine = fEvaluator.getEvalEngine();
            engine.setSeconds(fSeconds);
            result = fEvaluator.evaluateWithTimeout(inputExpression, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(result, expectedResult, manuallyCheckedResult);
        }
    } catch (final AbortException re) {
        return printResult(F.$Aborted, expectedResult, manuallyCheckedResult);
    } catch (final FailedException re) {
        return printResult(F.$Failed, expectedResult, manuallyCheckedResult);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        System.err.println(msg);
        System.err.println();
        System.err.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        System.err.println(buf.toString());
        System.err.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalEngine(org.matheclipse.core.eval.EvalEngine) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 4 with EvalControlledCallable

use of org.matheclipse.core.eval.EvalControlledCallable in project symja_android_library by axkr.

the class SymjaBot method interpreter.

private static String interpreter(final String trimmedInput) {
    ExprEvaluator evaluator = new ExprEvaluator(false, (short) 100);
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
            StringBuilder docBuf = new StringBuilder();
            Documentation.getMarkdown(docBuf, trimmedInput.substring(1));
            String docString = docBuf.toString();
            if (docString.length() > 0) {
                int indx = docString.indexOf("### Github");
                if (indx > 0) {
                    docString = docString.substring(0, indx);
                }
                return docString;
            }
            Documentation.usageDocumentation(docBuf, trimmedInput.substring(1));
            docString = docBuf.toString();
            if (docString.length() > 0) {
                int indx = docString.indexOf("### Github");
                if (indx > 0) {
                    docString = docString.substring(0, indx);
                }
                return docString;
            }
            return "No help page found";
        }
        System.out.println(trimmedInput);
        result = evaluator.evaluateWithTimeout(trimmedInput, 30, TimeUnit.SECONDS, true, new EvalControlledCallable(evaluator.getEvalEngine()));
        if (result != null) {
            return printResultShortened(trimmedInput, result);
        }
    } catch (final AbortException re) {
        // try {
        return printResultShortened(trimmedInput, S.$Aborted);
    // } catch (IOException e) {
    // Validate.printException(buf, e);
    // stderr.println(buf.toString());
    // stderr.flush();
    // return "";
    // }
    } catch (final FailedException re) {
        // try {
        return printResultShortened(trimmedInput, S.$Failed);
    // } catch (IOException e) {
    // Validate.printException(buf, e);
    // stderr.println(buf.toString());
    // stderr.flush();
    // return "";
    // }
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        // stderr.flush();
        return msg;
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        // stderr.flush();
        return "";
    } catch (final Exception e) {
        Validate.printException(buf, e);
        // stderr.flush();
        return "";
    } catch (final OutOfMemoryError e) {
        Validate.printException(buf, e);
        // stderr.flush();
        return "";
    } catch (final StackOverflowError e) {
        Validate.printException(buf, e);
        // stderr.flush();
        return "";
    }
    return buf.toString();
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 5 with EvalControlledCallable

use of org.matheclipse.core.eval.EvalControlledCallable in project symja_android_library by axkr.

the class MMAConsole method interpreter.

/**
 * Evaluates the given string-expression and returns the result in <code>OutputForm</code>
 *
 * @param trimmedInput a trimmed input string
 * @return
 */
/* package private */
String interpreter(final String trimmedInput) {
    IExpr result;
    final StringWriter buf = new StringWriter();
    try {
        if (trimmedInput.length() > 1 && trimmedInput.charAt(0) == '?') {
            IExpr doc = Documentation.findDocumentation(trimmedInput);
            return printResult(doc);
        }
        if (fSeconds <= 0) {
            result = fEvaluator.eval(trimmedInput);
        } else {
            result = fEvaluator.evaluateWithTimeout(trimmedInput, fSeconds, TimeUnit.SECONDS, true, new EvalControlledCallable(fEvaluator.getEvalEngine()));
        }
        if (result != null) {
            return printResult(result);
        }
    } catch (final AbortException re) {
        return printResult(S.$Aborted);
    } catch (final FailedException re) {
        return printResult(S.$Failed);
    } catch (final SyntaxError se) {
        String msg = se.getMessage();
        stderr.println(msg);
        // stderr.println();
        stderr.flush();
        return "";
    } catch (final RuntimeException re) {
        Throwable me = re.getCause();
        if (me instanceof MathException) {
            Validate.printException(buf, me);
        } else {
            Validate.printException(buf, re);
        }
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    } catch (final Exception | OutOfMemoryError | StackOverflowError e) {
        Validate.printException(buf, e);
        stderr.println(buf.toString());
        stderr.flush();
        return "";
    }
    return buf.toString();
}
Also used : MathException(org.matheclipse.parser.client.math.MathException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ReturnException(org.matheclipse.core.eval.exception.ReturnException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SyntaxError(org.matheclipse.parser.client.SyntaxError) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) EvalControlledCallable(org.matheclipse.core.eval.EvalControlledCallable) IExpr(org.matheclipse.core.interfaces.IExpr) AbortException(org.matheclipse.core.eval.exception.AbortException)

Aggregations

StringWriter (java.io.StringWriter)6 EvalControlledCallable (org.matheclipse.core.eval.EvalControlledCallable)6 IExpr (org.matheclipse.core.interfaces.IExpr)6 AbortException (org.matheclipse.core.eval.exception.AbortException)5 FailedException (org.matheclipse.core.eval.exception.FailedException)5 SyntaxError (org.matheclipse.parser.client.SyntaxError)5 MathException (org.matheclipse.parser.client.math.MathException)5 IOException (java.io.IOException)2 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 ReturnException (org.matheclipse.core.eval.exception.ReturnException)2 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)1 MarkdownNotebookOutput (com.twosigma.beakerx.symjamma.output.MarkdownNotebookOutput)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)1