Search in sources :

Example 6 with FailedException

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

the class MathScriptEngine method eval.

@Override
public Object eval(final String script, final ScriptContext context) {
    boolean relaxedSyntax = false;
    final Object enableStackTraceBoolean = get("PRINT_STACKTRACE");
    Level stackLogLevel = Boolean.TRUE.equals(enableStackTraceBoolean) ? Level.ERROR : Level.DEBUG;
    try {
        // first assign the EvalEngine to the current thread:
        EvalEngine.setReset(fEngine);
        // final Bindings bindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
        // ISymbol symbol;
        // for (Map.Entry<String, Object> currEntry : bindings.entrySet()) {
        // symbol = F.userSymbol(currEntry.getKey(), fEngine);
        // symbol.pushLocalVariable(Object2Expr.convert(currEntry.getValue()));
        // list.add(symbol);
        // }
        final Object decimalFormat = get("DECIMAL_FORMAT");
        if (decimalFormat instanceof String) {
            fDecimalFormat = (String) decimalFormat;
        }
        final Object relaxedSyntaxBoolean = get("RELAXED_SYNTAX");
        if (Boolean.TRUE.equals(relaxedSyntaxBoolean)) {
            relaxedSyntax = true;
            fEngine.setRelaxedSyntax(relaxedSyntax);
        }
        boolean disableHistory = true;
        final Object enableHistoryBoolean = get("ENABLE_HISTORY");
        if (Boolean.TRUE.equals(enableHistoryBoolean)) {
            disableHistory = false;
            fEngine.setOutListDisabled(disableHistory, (short) 100);
        }
        // evaluate an expression
        final Object stepwise = get("STEPWISE");
        IExpr result;
        String trimmedScript = script.trim();
        if (trimmedScript.length() == 0) {
            return "";
        }
        if (Boolean.TRUE.equals(stepwise)) {
            result = fUtility.evalTrace(script, null);
        } else {
            result = fUtility.evaluate(script);
        }
        final Object returnType = context.getAttribute("RETURN_OBJECT");
        if ((returnType != null) && returnType.equals(Boolean.TRUE)) {
            // return the object "as is"
            return result;
        } else {
            // return the object as String representation
            if (result != null) {
                return printResult(result, relaxedSyntax);
            }
            return "";
        }
    } catch (final AbortException e) {
        LOGGER.log(stackLogLevel, "Aborted", e);
        return printResult(S.$Aborted, relaxedSyntax);
    } catch (final FailedException e) {
        LOGGER.log(stackLogLevel, "Failed", e);
        return printResult(S.$Failed, relaxedSyntax);
    } catch (final MathException e) {
        // catches parser errors as well
        LOGGER.log(stackLogLevel, "evaluation failed", e);
        return e.getMessage();
    } catch (final ApfloatRuntimeException e) {
        LOGGER.log(stackLogLevel, "ApFloat error", e);
        // catch parser errors here
        return "Apfloat: " + e.getMessage();
    } catch (final Exception e) {
        LOGGER.log(stackLogLevel, "Exception", e);
        return "Exception: " + e.getMessage();
    } catch (final OutOfMemoryError e) {
        LOGGER.log(stackLogLevel, "Out of memory", e);
        return "OutOfMemoryError";
    } catch (final StackOverflowError e) {
        LOGGER.log(stackLogLevel, "Stack overflow", e);
        return "StackOverflowError";
    } finally {
        // if (list.size() > 0) {
        // for (int i = 0; i < list.size(); i++) {
        // list.get(i).popLocalVariable();
        // }
        // }
        EvalEngine.remove();
    }
}
Also used : ApfloatRuntimeException(org.apfloat.ApfloatRuntimeException) FailedException(org.matheclipse.core.eval.exception.FailedException) MathException(org.matheclipse.parser.client.math.MathException) Level(org.apache.logging.log4j.Level) IExpr(org.matheclipse.core.interfaces.IExpr) ApfloatRuntimeException(org.apfloat.ApfloatRuntimeException) MathException(org.matheclipse.parser.client.math.MathException) IOException(java.io.IOException) AbortException(org.matheclipse.core.eval.exception.AbortException) FailedException(org.matheclipse.core.eval.exception.FailedException) ScriptException(javax.script.ScriptException) AbortException(org.matheclipse.core.eval.exception.AbortException)

Example 7 with FailedException

use of org.matheclipse.core.eval.exception.FailedException 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)

Example 8 with FailedException

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

the class Console 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)

Example 9 with FailedException

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

the class PatternMatching method setDelayedDownRule.

private static void setDelayedDownRule(IExpr leftHandSide, int flags, IExpr rightHandSide, boolean packageMode) {
    ISymbol lhsSymbol = null;
    if (leftHandSide instanceof PatternNested) {
        PatternNested pn = (PatternNested) leftHandSide;
        IExpr pattern = pn.getPatternExpr();
        lhsSymbol = determineRuleTag(pattern);
    }
    if (leftHandSide.isAST()) {
        lhsSymbol = determineRuleTag(leftHandSide);
    }
    if (lhsSymbol != null) {
        if (lhsSymbol.isProtected()) {
            // Symbol `1` is Protected.
            IOFunctions.printMessage(S.SetDelayed, "wrsym", F.list(lhsSymbol), EvalEngine.get());
            throw new FailedException();
        }
        lhsSymbol.putDownRule(flags | IPatternMatcher.SET_DELAYED, false, leftHandSide, rightHandSide, packageMode);
        return;
    }
    if (leftHandSide.isSymbol()) {
        lhsSymbol = (ISymbol) leftHandSide;
        if (lhsSymbol.isProtected()) {
            // Symbol `1` is Protected.
            IOFunctions.printMessage(S.SetDelayed, "wrsym", F.list(lhsSymbol), EvalEngine.get());
            throw new FailedException();
        }
        ((ISymbol) leftHandSide).assignValue(rightHandSide, true);
        return;
    }
    throw new RuleCreationError(leftHandSide);
}
Also used : ISymbol(org.matheclipse.core.interfaces.ISymbol) FailedException(org.matheclipse.core.eval.exception.FailedException) PatternNested(org.matheclipse.core.expression.PatternNested) IExpr(org.matheclipse.core.interfaces.IExpr) RuleCreationError(org.matheclipse.core.eval.exception.RuleCreationError)

Aggregations

FailedException (org.matheclipse.core.eval.exception.FailedException)9 IExpr (org.matheclipse.core.interfaces.IExpr)9 AbortException (org.matheclipse.core.eval.exception.AbortException)8 MathException (org.matheclipse.parser.client.math.MathException)7 StringWriter (java.io.StringWriter)6 SyntaxError (org.matheclipse.parser.client.SyntaxError)6 EvalControlledCallable (org.matheclipse.core.eval.EvalControlledCallable)5 IOException (java.io.IOException)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 ReturnException (org.matheclipse.core.eval.exception.ReturnException)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ScriptException (javax.script.ScriptException)1 ServletException (javax.servlet.ServletException)1 Level (org.apache.logging.log4j.Level)1 ApfloatRuntimeException (org.apfloat.ApfloatRuntimeException)1 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)1 RecursionLimitExceeded (org.matheclipse.core.eval.exception.RecursionLimitExceeded)1 RuleCreationError (org.matheclipse.core.eval.exception.RuleCreationError)1 PatternNested (org.matheclipse.core.expression.PatternNested)1 GraphExpr (org.matheclipse.core.expression.data.GraphExpr)1