Search in sources :

Example 1 with FlowControlException

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

the class EvalEngine method evalASTBuiltinFunction.

/**
 * @param symbol
 * @param ast
 * @return <code>F.NIL</code> if no evaluation happened
 */
private IExpr evalASTBuiltinFunction(final ISymbol symbol, IAST ast) {
    final int attributes = symbol.getAttributes();
    if (fEvalLHSMode) {
        if ((ISymbol.HOLDALL & attributes) == ISymbol.HOLDALL) {
            // (i.e. Sin, Cos,...)
            if (!(symbol.equals(S.Set) || symbol.equals(S.SetDelayed) || symbol.equals(S.UpSet) || symbol.equals(S.UpSetDelayed))) {
                return F.NIL;
            }
        } else {
            if ((ISymbol.NUMERICFUNCTION & attributes) != ISymbol.NUMERICFUNCTION) {
                return F.NIL;
            }
        }
    }
    if (!symbol.equals(S.Integrate)) {
        IExpr result;
        if ((result = symbol.evalDownRule(this, ast)).isPresent()) {
            return result;
        }
    }
    if (symbol.isBuiltInSymbol()) {
        final IEvaluator evaluator = ((IBuiltInSymbol) symbol).getEvaluator();
        if (evaluator instanceof IFunctionEvaluator) {
            if (ast.isEvalFlagOn(IAST.BUILT_IN_EVALED) && isSymbolicMode(attributes)) {
                return F.NIL;
            }
            // evaluate a built-in function.
            final IFunctionEvaluator functionEvaluator = (IFunctionEvaluator) evaluator;
            OptionsResult opres = checkBuiltinArguments(ast, functionEvaluator);
            if (opres == null) {
                return F.NIL;
            }
            ast = opres.result;
            try {
                if (evaluator instanceof AbstractFunctionOptionEvaluator) {
                    AbstractFunctionOptionEvaluator optionsEvaluator = (AbstractFunctionOptionEvaluator) evaluator;
                    IExpr result = optionsEvaluator.evaluate(ast, opres.argSize, opres.options, this);
                    if (result.isPresent()) {
                        return result;
                    }
                } else {
                    IExpr result = fNumericMode ? functionEvaluator.numericEval(ast, this) : functionEvaluator.evaluate(ast, this);
                    if (result.isPresent()) {
                        return result;
                    }
                }
            } catch (ValidateException ve) {
                return IOFunctions.printMessage(ast.topHead(), ve, this);
            } catch (FlowControlException e) {
                throw e;
            } catch (SymjaMathException ve) {
                LOGGER.log(getLogLevel(), ast.topHead(), ve);
                return F.NIL;
            }
        // cannot generally set the result as evaluated in built-in function. Especially problems in
        // `togetherMode`
        // if (isSymbolicMode(attributes) && !isTogetherMode()) {
        // ast.addEvalFlags(IAST.BUILT_IN_EVALED);
        // }
        }
    }
    return F.NIL;
}
Also used : IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) ValidateException(org.matheclipse.core.eval.exception.ValidateException) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) IFunctionEvaluator(org.matheclipse.core.eval.interfaces.IFunctionEvaluator) AbstractFunctionOptionEvaluator(org.matheclipse.core.eval.interfaces.AbstractFunctionOptionEvaluator) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException)

Example 2 with FlowControlException

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

the class ExprEvaluatorTests method smartFuzz.

/**
 * Fuzz testing - automated software testing that involves providing random arguments as inputs
 * for the input expressions in file <code>./data/harvest.sym</code> harvested from existing JUnit
 * tests of built-in functions.
 */
public static void smartFuzz() {
    Config.MAX_AST_SIZE = 10000;
    Config.MAX_OUTPUT_SIZE = 10000;
    Config.MAX_INPUT_LEAVES = 100L;
    Config.MAX_MATRIX_DIMENSION_SIZE = 100;
    Config.MAX_PRECISION_APFLOAT = 100;
    Config.MAX_BIT_LENGTH = 200000;
    Config.MAX_POLYNOMIAL_DEGREE = 100;
    Config.FILESYSTEM_ENABLED = false;
    boolean quietMode = true;
    EvalEngine engine = new EvalEngine(true);
    engine.setRecursionLimit(256);
    engine.setIterationLimit(1000);
    ExprEvaluator eval = new ExprEvaluator(engine, true, (short) 20);
    List<ASTNode> node = parseFileToList();
    IExpr temp;
    OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
    fInputFactory.setInputForm(true);
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    byte[] bArray = new byte[0];
    ByteArrayExpr ba = ByteArrayExpr.newInstance(bArray);
    byte[] b0Array = new byte[] { 0 };
    ByteArrayExpr b0a = ByteArrayExpr.newInstance(b0Array);
    F.x.setAttributes(ISymbol.PROTECTED);
    F.y.setAttributes(ISymbol.PROTECTED);
    double[] doubleArr = new double[] { 1.0, -1.0, 0.0, 2.0, 100.0, 200.0 };
    int[] dims = new int[] { 2, 3 };
    NumericArrayExpr nae = new NumericArrayExpr(doubleArr, dims, NumericArrayExpr.Real64);
    IAST seedList = // 
    F.List(// 
    ba, // 
    b0a, // 
    nae, // 
    F.complex(-0.5, 0.5), // 
    F.complex(0.0, 0.5), // 
    F.complex(0.0, -1.0), // 
    F.complex(0.0, 1.0), // 
    F.complex(2.0, -1.0), // 
    F.complex(2.0, 1.0), // 
    F.complex(-2.0, -2.0), // 
    F.complex(-2.0, 2.0), // 
    F.complexNum("-0.8", "1.2", 30), // 
    F.num(0.5), // 
    F.num(-0.5), // 
    F.num(Math.PI * (-0.5)), // 
    F.num(Math.PI * 0.5), // 
    F.num(-Math.PI), // 
    F.num(Math.PI), // 
    F.num(-Math.E), // 
    F.num(Math.E), // 
    F.num("-0.8", 30), // 
    F.C0, // 
    F.C1, // 
    F.CN1, // 
    F.CN1D2, // 
    F.C1D2, // 
    F.CNI, // 
    F.CI, // 
    F.CC(Long.MAX_VALUE, Long.MIN_VALUE, Long.MIN_VALUE, Long.MAX_VALUE), // 
    F.QQ(Long.MAX_VALUE, Long.MIN_VALUE), // 
    F.QQ(Long.MIN_VALUE, Long.MAX_VALUE), // 
    F.Slot2, // some primes
    F.C2, F.C3, F.C5, F.C7, F.ZZ(11), F.ZZ(13), F.ZZ(17), F.ZZ(19), F.ZZ(101), F.ZZ(1009), // 
    F.ZZ(10007), // 
    F.ZZ(Integer.MIN_VALUE), // 
    F.ZZ(Integer.MAX_VALUE), // 
    F.CInfinity, // 
    F.CNInfinity, // 
    F.Null, // 
    F.Power(F.x, F.C2), // 
    F.Indeterminate, // 
    F.ComplexInfinity, // 
    F.x_, // 
    F.y_, // any sequence of one or more expressions
    F.x__, // any sequence of one or more expressions
    F.y__, // any sequence of zero or more expressions
    F.x___, // any sequence of zero or more expressions
    F.y___, // 
    F.CEmptyList, // 
    F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1))), // 
    F.assoc(F.List()), // 
    F.assoc(F.List(F.Rule(F.stringx("s1"), F.C0), F.RuleDelayed(F.stringx("s2"), F.C1))), F.assoc(F.List(F.Rule(F.stringx("s1"), F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1)))), F.RuleDelayed(F.stringx("s2"), // 
    F.assoc(F.List(F.Rule(F.a, F.C0), F.RuleDelayed(F.b, F.C1)))))), // 
    SparseArrayExpr.newDenseList(F.List(F.C0, F.C0), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.C0, F.C1, F.C0, F.C2), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), F.C0), // 
    SparseArrayExpr.newDenseList(F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), F.C0), // 
    F.Function(F.EvenQ(F.Slot1)), // 
    F.Function(F.Expand(F.Power(F.Plus(F.C2, F.Slot1), F.C3))), // 
    F.Graph(F.List(F.Rule(F.C1, F.C2), F.Rule(F.C2, F.C3), F.Rule(F.C3, F.C1))), // 
    F.Graph(F.List()), // 
    F.CEmptySequence, // 
    F.CEmptyList, // 
    F.List(F.List(F.C0)), // 
    F.List(F.List(F.C1)), // 
    F.List(F.List(F.CN1)), // 
    F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1)), // 
    F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0)), // 
    F.List(F.List(F.C1, F.C0), F.List(F.C0, F.C1), F.C0), // 
    F.List(F.List(F.C0, F.C0), F.List(F.C0, F.C0), F.C0), F.List(F.num("-3.1415", 30), F.num("2.987", 30), F.num("-1", 30), F.num("0.0", 30), // 
    F.num("1", 30)), // 
    F.List(F.CN1, F.CN2, F.C3), // 
    F.List(F.CN1D2, F.CN2, F.C3), // 
    F.List(F.x, F.CN2, F.C3), // 
    F.List(F.x, F.C5, F.CN3), // 
    F.List(F.x, F.CN3, F.CN1D2), // 
    F.List(F.x, F.CN1D2, F.C1D2, F.C1D4), // 
    F.List(F.C0, F.C0), // 
    F.List(F.C0, F.C0, F.C0), // 
    F.List(F.C1, F.C2, F.C3), // 
    F.List(F.C1, F.C1, F.C1), // 
    F.List(F.C1, F.C2, F.C3, F.a), // 
    F.List(F.C0, F.C0, F.C0, F.C0), // 
    F.List(F.C1, F.C1, F.C1, F.C1), // 
    F.List(F.x, F.CN1, F.C1, F.C1), // 
    F.List(F.x, F.C0, F.C0, F.C0), // 
    F.List(F.x, F.C1, F.CN1, F.CN1), // 
    F.List(F.CN1), // 
    F.List(F.C0), // 
    F.List(F.C1), // simulate level spec
    F.List(F.CN5), // simulate level spec
    F.List(F.C7), // 
    F.List(F.complex(0.0, -1.0)), // 
    F.List(F.complex(0.0, 1.0)), // 
    F.List(F.x), // 
    F.List(F.CN3D2), // 
    F.List(F.C3D2), // 
    F.List(F.C3D4), // 
    F.Part(F.x, F.C1), // 
    F.Part(F.x, F.C2), // 
    F.Part(F.x, F.ZZ(Integer.MAX_VALUE)), // 
    F.Part(F.x, F.CN1, F.C1, F.C1), // 
    F.Part(F.x, F.C1, F.C1, F.C1, F.C1), // 
    F.C1DSqrt5, // GoldenRatio
    F.Divide(F.Plus(F.C1, F.Sqrt(5)), F.C2), // 1/GoldenRatio
    F.Divide(F.C2, F.Plus(F.C1, F.Sqrt(5))), // 
    F.Negate(F.Sqrt(2)), // 
    F.Divide(F.Sqrt(2), F.C2), // 
    F.Negate(F.Divide(F.Sqrt(2), F.C2)), // 
    F.Plus(F.Sqrt(2), F.C1), // 
    F.Plus(F.Sqrt(2), F.CN1), // 
    F.Exp(F.Times(F.Pi, F.CI, F.C1D3)), // 
    F.Plus(F.C1, F.CI), // 
    F.Plus(F.CN1, F.CI), // 
    F.Times(F.Sqrt(2), F.C7), // 
    F.Times(F.Sqrt(2), F.Sqrt(5)), // 
    F.CSqrt2, // 
    F.C2Pi, // 
    F.CN3D2, // 
    F.C3D2, // 
    F.C3D4, // 
    F.QQ(Long.MAX_VALUE, 7L), // 
    F.QQ(Long.MIN_VALUE, 11L), // 
    F.QQ(7, Long.MAX_VALUE), // 
    F.QQ(11, Long.MAX_VALUE), // 
    F.QQ(Long.MAX_VALUE, Long.MAX_VALUE), // 
    F.QQ(Long.MIN_VALUE, Long.MAX_VALUE), // 
    F.Slot2, // 
    F.Slot(Integer.MAX_VALUE), // 
    IQuantity.of(1.2, "m"), // 
    F.RegularExpression("?i)"), // 
    F.CEmptyString, // 
    F.stringx("\\"), // 
    F.stringx("\r"), // 
    F.stringx("\t"), // 
    F.stringx("\n"), // 
    F.stringx("\r\n"), // 
    F.stringx("\n   "), // 
    F.stringx("\uffff"), // division by zero problem
    F.Power(F.C0, F.CN1), // 
    F.Subtract(F.C1, F.C1), // 
    F.Rule(S.Modulus, F.C2), // 
    F.Rule(S.Modulus, F.C10), // 
    F.Rule(S.Heads, S.True), // 
    F.Rule(S.Heads, S.False), // 
    F.$OptionsPattern(), // 
    F.OptionValue(F.a), // 
    F.OptionValue(F.b), // 
    F.OptionValue(F.x), F.OptionValue(F.y));
    ThreadLocalRandom random = ThreadLocalRandom.current();
    SlowComputationThread thread = null;
    for (int j = 1; j < 10000; j++) {
        int i = 0;
        while (i < node.size()) {
            temp = ast2Expr.convert(node.get(i++));
            if (temp.isAST() && temp.size() > 1) {
                int seedIndex = random.nextInt(1, seedList.size());
                IExpr seed = seedList.get(seedIndex);
                String mutantStr = "initial";
                IASTMutable mutant = ((IAST) temp).copy();
                try {
                    ISymbol sym = mutant.topHead();
                    if (sym == S.PolynomialGCD || sym == S.TestReport || sym == S.VerificationTest || sym == S.On || sym == S.Off || sym == S.Compile || sym == S.CompiledFunction || sym == S.FactorialPower || sym == S.Pause || sym == S.OptimizeExpression || sym == S.Share || sym == S.Set || sym == S.SetDelayed || sym == S.UpSet || sym == S.UpSetDelayed) {
                        continue;
                    }
                    int randomIndex = random.nextInt(1, mutant.size());
                    if (mutant.isAssociation()) {
                        mutant.set(randomIndex, F.Rule(F.ZZ(randomIndex), seed));
                    } else {
                        mutant.set(randomIndex, seed);
                    }
                    for (int k = 0; k < 1; k++) {
                        seedIndex = random.nextInt(1, seedList.size());
                        seed = seedList.get(seedIndex);
                        randomIndex = random.nextInt(1, mutant.size());
                        if (mutant.isAssociation()) {
                            mutant.set(randomIndex, F.Rule(F.ZZ(randomIndex), seed));
                        } else {
                            mutant.set(randomIndex, seed);
                        }
                    }
                    engine.init();
                    engine.setQuietMode(quietMode);
                    engine.setRecursionLimit(256);
                    engine.setIterationLimit(1000);
                    // mutantStr = fInputFactory.toString(mutant);
                    // System.out.println(">> " + mutantStr);
                    // if (counter++ > 80) {
                    // System.out.println("");
                    // counter = 0;
                    // System.out.flush();
                    // System.err.flush();
                    // }
                    thread = new SlowComputationThread(">> " + mutant.toString(), engine);
                    thread.start();
                    engine.evaluate(mutant);
                } catch (FlowControlException mex) {
                    if (!quietMode) {
                        System.err.println(mutant.toString());
                        mex.printStackTrace();
                        System.err.println();
                    }
                } catch (SyntaxError se) {
                    System.err.println(mutant.toString());
                    se.printStackTrace();
                    System.err.println();
                // fail();
                } catch (ValidateException ve) {
                    System.err.println(mutant.toString());
                    ve.printStackTrace();
                    System.err.println();
                // fail();
                } catch (MathException mex) {
                    System.err.println(mutant.toString());
                    mex.printStackTrace();
                    System.err.println();
                    fail();
                } catch (RuntimeException rex) {
                    System.err.println(mutant.toString());
                    rex.printStackTrace();
                    fail();
                } catch (Error rex) {
                    System.err.println(mutant.toString());
                    if (rex instanceof StackOverflowError) {
                        System.err.println("java.lang.StackOverflowError");
                        rex.printStackTrace();
                        fail();
                    } else {
                        System.err.println(mutantStr);
                        rex.printStackTrace();
                        fail();
                    }
                } finally {
                    thread.terminate();
                    thread.interrupt();
                }
            }
        }
    }
// return result;
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) SyntaxError(org.matheclipse.parser.client.SyntaxError) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IAST(org.matheclipse.core.interfaces.IAST) ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) ISymbol(org.matheclipse.core.interfaces.ISymbol) NumericArrayExpr(org.matheclipse.core.expression.data.NumericArrayExpr) SyntaxError(org.matheclipse.parser.client.SyntaxError) AST2Expr(org.matheclipse.core.convert.AST2Expr) ByteArrayExpr(org.matheclipse.core.expression.data.ByteArrayExpr) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 3 with FlowControlException

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

the class ExprEvaluatorTests method generateASTs.

private static void generateASTs(IBuiltInSymbol sym, int start, int end, IAST seedList, ThreadLocalRandom random, int[] counter, IFunctionEvaluator evaluator, EvalEngine engine, boolean nestedChaos, boolean headerExpr) {
    boolean quietMode = true;
    ExprEvaluator eval;
    System.out.flush();
    for (int j = start; j <= end; j++) {
        eval = new ExprEvaluator(engine, true, (short) 20);
        engine.init();
        engine.setQuietMode(quietMode);
        IASTAppendable ast;
        if (headerExpr) {
            int seedIndex = random.nextInt(1, seedList.size());
            IExpr seed = seedList.get(seedIndex);
            ast = F.ast(F.unaryAST1(sym, seed));
        } else {
            ast = F.ast(sym);
        }
        SlowComputationThread thread = null;
        try {
            for (int k = 0; k < j; k++) {
                int seedIndex = random.nextInt(1, seedList.size());
                IExpr seed = seedList.get(seedIndex);
                if (nestedChaos && seed.isAST() && seed.size() > 1) {
                    int seedIndex2 = random.nextInt(1, seed.size());
                    seedIndex = random.nextInt(1, seedList.size());
                    IExpr seed2 = seedList.get(seedIndex);
                    if (seed != seed2) {
                        if (seed.isAssociation()) {
                            seed = ((IAssociation) seed).setAtCopy(seedIndex2, F.Rule(F.ZZ(seedIndex2), seed2));
                        } else {
                            seed = ((IAST) seed).setAtCopy(seedIndex2, seed2);
                        }
                    }
                }
                ast.append(seed);
            }
            if (counter[0]++ > 80) {
                // System.out.println("");
                counter[0] = 0;
                System.out.flush();
                System.err.flush();
            }
            // System.out.println(">> " + ast.toString());
            // System.out.print(".");
            thread = new SlowComputationThread(">> " + ast.toString(), engine);
            thread.start();
            // engine.evaluate(ast);
            if (evaluator != null) {
                evaluator.evaluate(ast, engine);
            } else {
                eval.eval(ast);
            }
        } catch (FlowControlException mex) {
            if (!quietMode) {
                System.err.println(ast.toString());
                mex.printStackTrace();
                System.err.println();
            }
        } catch (SyntaxError se) {
            System.err.println(ast.toString());
            se.printStackTrace();
            System.err.println();
        // fail();
        } catch (ValidateException ve) {
            System.err.println(ve.getMessage());
            System.err.println(ast.toString());
            System.err.println();
        // fail();
        } catch (MathException mex) {
            System.err.println(ast.toString());
            mex.printStackTrace();
            System.err.println();
            fail();
        } catch (RuntimeException rex) {
            System.err.println(ast.toString());
            rex.printStackTrace();
            fail();
        } catch (Error rex) {
            System.err.println(ast.toString());
            if (rex instanceof StackOverflowError) {
                System.err.println("java.lang.StackOverflowError");
                rex.printStackTrace();
                fail();
            } else {
                System.err.println(ast.toString());
                rex.printStackTrace();
                fail();
            }
        } finally {
            if (thread != null) {
                thread.terminate();
                thread.interrupt();
            }
        }
    }
}
Also used : ExprEvaluator(org.matheclipse.core.eval.ExprEvaluator) ValidateException(org.matheclipse.core.eval.exception.ValidateException) SyntaxError(org.matheclipse.parser.client.SyntaxError) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 4 with FlowControlException

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

the class TeXTests method testSmartFuzz.

public void testSmartFuzz() {
    boolean quietMode = true;
    EvalEngine engine = EvalEngine.get();
    List<ASTNode> node = parseFileToList();
    IExpr temp;
    TeXFormFactory fTeXFactory = new TeXFormFactory();
    OutputFormFactory fInputFactory = OutputFormFactory.get(true, false, 5, 7);
    fInputFactory.setInputForm(true);
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    IAST seedList = // 
    F.List(// 
    F.complex(-0.5, 0.5), // 
    F.complex(0.0, 0.5), // 
    F.complex(0.0, -1.0), // 
    F.complex(0.0, 1.0), // 
    F.num(-0.5), // 
    F.num(0.5), // 
    F.num(Math.PI * (-0.5)), // 
    F.num(Math.PI * 0.5), // 
    F.num(-Math.PI), // 
    F.num(Math.PI), // 
    F.num(-Math.E), // 
    F.num(Math.E), // 
    S.True, // 
    S.False, // 
    F.assoc(F.CEmptyList), // 
    F.assoc(F.List(F.Rule(F.x, F.y))), // 
    F.CEmptyList, // 
    F.List(F.Rule(F.C1, F.C0)), // 
    F.List(F.Rule(F.x, F.CN1)), // 
    F.C0, // 
    F.C1, // 
    F.CN1, // 
    F.C2, // 
    F.CN2, // 
    F.CN10, // 
    F.CN1D2, // 
    F.C1D2, // 
    F.CNI, // 
    F.CI, // 
    F.CInfinity, // 
    F.CNInfinity, // 
    F.Null, // 
    F.Power(F.x, F.C2), // 
    F.ComplexInfinity, // 
    F.x_, // 
    F.y_, // 
    F.C1DSqrt5, // 
    F.Slot1, // 
    F.stringx(""), // 
    F.stringx("\uffff"), F.Subtract(F.C1, F.C1));
    int counter = 0;
    ThreadLocalRandom random = ThreadLocalRandom.current();
    for (int j = 1; j < 10000; j++) {
        int i = 0;
        while (i < node.size()) {
            temp = ast2Expr.convert(node.get(i++));
            if (temp.isAST() && temp.size() > 1) {
                final StringBuilder buf = new StringBuilder();
                int seedIndex = random.nextInt(1, seedList.size());
                IExpr seed = seedList.get(seedIndex);
                IASTMutable mutant = ((IAST) temp).copy();
                int randomIndex = random.nextInt(1, mutant.size());
                mutant.set(randomIndex, seed);
                for (int k = 0; k < 1; k++) {
                    seedIndex = random.nextInt(1, seedList.size());
                    seed = seedList.get(seedIndex);
                    randomIndex = random.nextInt(1, mutant.size());
                    mutant.set(randomIndex, seed);
                }
                engine.init();
                engine.setQuietMode(quietMode);
                engine.setRecursionLimit(256);
                engine.setIterationLimit(1000);
                final String mutantStr = fInputFactory.toString(mutant);
                try {
                    // System.out.print(".");
                    if (counter++ > 80) {
                        // System.out.println("");
                        counter = 0;
                        System.out.flush();
                        System.err.flush();
                    }
                    // eval.eval(mutantStr);
                    fTeXFactory.convert(buf, mutant, 0);
                    System.out.println(buf.toString());
                } catch (FlowControlException mex) {
                    if (!quietMode) {
                        System.err.println(mutantStr);
                        mex.printStackTrace();
                        System.err.println();
                    }
                } catch (SyntaxError se) {
                    if (!quietMode) {
                        System.err.println(mutantStr);
                        se.printStackTrace();
                        System.err.println();
                    }
                // fail();
                } catch (MathException mex) {
                    System.err.println(mutantStr);
                    mex.printStackTrace();
                    System.err.println();
                    fail();
                } catch (RuntimeException rex) {
                    System.err.println(mutantStr);
                    rex.printStackTrace();
                    fail();
                } catch (Error rex) {
                    System.err.println(mutantStr);
                    if (rex instanceof StackOverflowError) {
                        System.err.println("java.lang.StackOverflowError");
                        rex.printStackTrace();
                    } else {
                        rex.printStackTrace();
                        fail();
                    }
                }
            }
        }
    }
// return result;
}
Also used : SyntaxError(org.matheclipse.parser.client.SyntaxError) IASTMutable(org.matheclipse.core.interfaces.IASTMutable) OutputFormFactory(org.matheclipse.core.form.output.OutputFormFactory) AST2Expr(org.matheclipse.core.convert.AST2Expr) SyntaxError(org.matheclipse.parser.client.SyntaxError) MathException(org.matheclipse.parser.client.math.MathException) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) TeXFormFactory(org.matheclipse.core.form.tex.TeXFormFactory) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 5 with FlowControlException

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

the class AbstractAST method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public IExpr evaluate(EvalEngine engine) {
    LOGGER.debug("Evaluate {}", this);
    final IExpr head = head();
    final int argSize = argSize();
    if (head instanceof ISymbol) {
        ISymbol headSymbol = (ISymbol) head;
        Class<?> clazz = headSymbol.getContext().getJavaClass();
        if (clazz != null) {
            String staticMethodName = headSymbol.getSymbolName();
            // try {
            // Method method = clazz.getMethod(staticMethodName);
            // if (Modifier.isStatic(method.getModifiers())) {
            // Parameter[] parameters = method.getParameters();
            // if (parameters.length == argSize()) {
            // Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
            // if (params != null) {
            // Object result;
            // try {
            // result = method.invoke(null, params);
            // if (result instanceof String) {
            // return F.stringx((String) result);
            // }
            // return Object2Expr.convert(result);
            // } catch (IllegalAccessException
            // | IllegalArgumentException
            // | InvocationTargetException e) {
            // // fall through?
            // }
            // }
            // }
            // }
            // 
            // } catch (IllegalArgumentException | NoSuchMethodException | SecurityException e) {
            // // fall through?
            // }
            Method[] methods = clazz.getMethods();
            for (int i = 0; i < methods.length; i++) {
                if (Modifier.isStatic(methods[i].getModifiers())) {
                    if (staticMethodName.equals(methods[i].getName())) {
                        Parameter[] parameters = methods[i].getParameters();
                        if (parameters.length == argSize()) {
                            Object[] params = JavaFunctions.determineParameters(this, parameters, 1);
                            if (params != null) {
                                Object result;
                                try {
                                    result = methods[i].invoke(null, params);
                                    if (result instanceof String) {
                                        return F.stringx((String) result);
                                    }
                                    return Object2Expr.convert(result, false, true);
                                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                                // fall through?
                                }
                            }
                        }
                    }
                }
            }
        }
        if (head instanceof IBuiltInSymbol) {
            final IEvaluator evaluator = ((IBuiltInSymbol) head).getEvaluator();
            if (evaluator instanceof ICoreFunctionEvaluator) {
                try {
                    ICoreFunctionEvaluator functionEvaluator = (ICoreFunctionEvaluator) evaluator;
                    EvalEngine.OptionsResult opres = engine.checkBuiltinArguments(this, functionEvaluator);
                    if (opres == null) {
                        return F.NIL;
                    }
                    IAST ast = opres.result;
                    IBuiltInSymbol header = ((IBuiltInSymbol) head);
                    if ((header.getAttributes() & ISymbol.SEQUENCEHOLD) != ISymbol.SEQUENCEHOLD) {
                        IExpr temp;
                        if ((temp = F.flattenSequence(this)).isPresent()) {
                            return temp;
                        }
                    }
                    if (isBooleanFunction()) {
                        IExpr temp = extractConditionalExpression(false);
                        if (temp.isPresent()) {
                            return temp;
                        }
                    }
                    IExpr evaluateTemp = evalEvaluate(engine);
                    if (evaluateTemp.isPresent()) {
                        return evaluateTemp;
                    }
                    return functionEvaluator.evaluate(ast, engine);
                } catch (ValidateException ve) {
                    return IOFunctions.printMessage(topHead(), ve, engine);
                } catch (FlowControlException e) {
                    throw e;
                } catch (SymjaMathException ve) {
                    LOGGER.log(engine.getLogLevel(), topHead(), ve);
                    return F.NIL;
                }
            }
        }
    }
    if (head.isAssociation() && argSize == 1) {
        return ((IAssociation) head).getValue(arg1());
    }
    final ISymbol symbol = topHead();
    IExpr temp = engine.evalAttributes(symbol, this);
    if (temp.isPresent()) {
        return temp;
    }
    return engine.evalRules(symbol, this);
}
Also used : ValidateException(org.matheclipse.core.eval.exception.ValidateException) ICoreFunctionEvaluator(org.matheclipse.core.eval.interfaces.ICoreFunctionEvaluator) IBuiltInSymbol(org.matheclipse.core.interfaces.IBuiltInSymbol) EvalEngine(org.matheclipse.core.eval.EvalEngine) PatternMatcherEvalEngine(org.matheclipse.core.patternmatching.PatternMatcherEvalEngine) IAST(org.matheclipse.core.interfaces.IAST) IAssociation(org.matheclipse.core.interfaces.IAssociation) ISymbol(org.matheclipse.core.interfaces.ISymbol) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) SymjaMathException(org.matheclipse.core.eval.exception.SymjaMathException) IEvaluator(org.matheclipse.core.interfaces.IEvaluator) FlowControlException(org.matheclipse.core.eval.exception.FlowControlException) Parameter(java.lang.reflect.Parameter) IPatternObject(org.matheclipse.core.interfaces.IPatternObject) IExpr(org.matheclipse.core.interfaces.IExpr)

Aggregations

FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)5 IExpr (org.matheclipse.core.interfaces.IExpr)5 ValidateException (org.matheclipse.core.eval.exception.ValidateException)4 EvalEngine (org.matheclipse.core.eval.EvalEngine)3 IAST (org.matheclipse.core.interfaces.IAST)3 SyntaxError (org.matheclipse.parser.client.SyntaxError)3 MathException (org.matheclipse.parser.client.math.MathException)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 AST2Expr (org.matheclipse.core.convert.AST2Expr)2 ExprEvaluator (org.matheclipse.core.eval.ExprEvaluator)2 SymjaMathException (org.matheclipse.core.eval.exception.SymjaMathException)2 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)2 IASTMutable (org.matheclipse.core.interfaces.IASTMutable)2 IBuiltInSymbol (org.matheclipse.core.interfaces.IBuiltInSymbol)2 IEvaluator (org.matheclipse.core.interfaces.IEvaluator)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ASTNode (org.matheclipse.parser.client.ast.ASTNode)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1