Search in sources :

Example 6 with Parser

use of org.matheclipse.parser.client.Parser in project symja_android_library by axkr.

the class BasicPatternPropertiesTestCase method setUp.

/**
	 * The JUnit setup method
	 */
protected void setUp() {
    try {
        // setup the evaluation engine (and bind to current thread)
        // EvalEngine.get();
        EvalEngine engine = new EvalEngine();
        EvalEngine.set(engine);
        engine.setSessionID("BasicPatternPropertiesTestCase");
        engine.setRecursionLimit(256);
        engine.setIterationLimit(1024 * 1024);
        fParser = new Parser();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) Parser(org.matheclipse.parser.client.Parser)

Example 7 with Parser

use of org.matheclipse.parser.client.Parser in project symja_android_library by axkr.

the class LowercaseTestCase method testParserFixedPoint.

public void testParserFixedPoint() {
    try {
        Parser p = new Parser(true);
        ASTNode obj = p.parse("{28, 21} /. {a_, b_} /; b != 0 -> {b, Mod(a, b)}");
        assertEquals(obj.toString(), "ReplaceAll(List(28, 21), Rule(Condition(List(a_, b_), Unequal(b, 0)), List(b, Mod(a, b))))");
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals("", e.getMessage());
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) Parser(org.matheclipse.parser.client.Parser)

Example 8 with Parser

use of org.matheclipse.parser.client.Parser in project symja_android_library by axkr.

the class Import method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (!(ast.arg1() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    if (!(ast.arg2() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 2, ast.size() - 1);
    }
    IStringX arg1 = (IStringX) ast.arg1();
    IStringX arg2 = (IStringX) ast.arg2();
    FileReader reader = null;
    try {
        reader = new FileReader(arg1.toString());
        if (arg2.contentEquals("Table")) {
            AST2Expr ast2Expr = AST2Expr.CONST;
            if (engine.isRelaxedSyntax()) {
                ast2Expr = AST2Expr.CONST_LC;
            }
            final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
            CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(' ');
            Iterable<CSVRecord> records = csvFormat.parse(reader);
            IAST rowList = F.List();
            for (CSVRecord record : records) {
                IAST columnList = F.List();
                for (String string : record) {
                    final ASTNode node = parser.parse(string);
                    IExpr temp = ast2Expr.convert(node, engine);
                    columnList.append(temp);
                }
                rowList.append(columnList);
            }
            return rowList;
        }
    } catch (IOException ioe) {
        engine.printMessage("Import: file " + arg1.toString() + " not found!");
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
    return F.NIL;
}
Also used : IOException(java.io.IOException) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IAST(org.matheclipse.core.interfaces.IAST) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 9 with Parser

use of org.matheclipse.parser.client.Parser in project symja_android_library by axkr.

the class RulePreprocessor method parseFileToList.

public static ASTNode parseFileToList(File file) {
    try {
        final BufferedReader f = new BufferedReader(new FileReader(file));
        final StringBuffer buff = new StringBuffer(1024);
        String line;
        while ((line = f.readLine()) != null) {
            buff.append(line);
            buff.append('\n');
        }
        f.close();
        String inputString = buff.toString();
        Parser p = new Parser(true, false);
        return p.parse(inputString);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) Parser(org.matheclipse.parser.client.Parser)

Example 10 with Parser

use of org.matheclipse.parser.client.Parser in project symja_android_library by axkr.

the class RelaxedParserTestCase method testParser0.

public void testParser0() {
    try {
        Parser p = new Parser(true);
        Object obj = p.parse("Integrate(Sin(x)^2+3*x^4, x)");
        assertEquals(obj.toString(), "Integrate(Plus(Power(Sin(x), 2), Times(3, Power(x, 4))), x)");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Parser(org.matheclipse.parser.client.Parser)

Aggregations

Parser (org.matheclipse.parser.client.Parser)40 ASTNode (org.matheclipse.parser.client.ast.ASTNode)32 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 EvalEngine (org.matheclipse.core.eval.EvalEngine)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)2 BufferedReader (java.io.BufferedReader)1 StringWriter (java.io.StringWriter)1 CSVFormat (org.apache.commons.csv.CSVFormat)1 CSVRecord (org.apache.commons.csv.CSVRecord)1 AST2Expr (org.matheclipse.core.convert.AST2Expr)1 EvalUtilities (org.matheclipse.core.eval.EvalUtilities)1 TimeConstrainedEvaluator (org.matheclipse.core.eval.TimeConstrainedEvaluator)1 WrongNumberOfArguments (org.matheclipse.core.eval.exception.WrongNumberOfArguments)1 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)1 IAST (org.matheclipse.core.interfaces.IAST)1 IStringX (org.matheclipse.core.interfaces.IStringX)1