Search in sources :

Example 1 with AST2Expr

use of org.matheclipse.core.convert.AST2Expr 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 2 with AST2Expr

use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.

the class Import method ofString.

/**
 * Get arbitrary data represented as a Symja expression string
 *
 * @param file
 * @param engine
 * @return
 * @throws IOException
 */
public static IExpr ofString(File file, EvalEngine engine) throws IOException {
    String filename = file.getName();
    Extension extension = Extension.importFilename(filename);
    // Extension extension = filename.extension();
    if (extension.equals(Extension.JPG) || extension.equals(Extension.PNG)) {
        // if (filename.hasExtension("jpg") || filename.hasExtension("png")) {
        return ImageFormat.from(ImageIO.read(file));
    }
    String str = com.google.common.io.Files.asCharSource(file, Charset.defaultCharset()).read();
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
    final ASTNode node = parser.parse(str);
    return ast2Expr.convert(node);
}
Also used : Extension(org.matheclipse.core.io.Extension) ASTNode(org.matheclipse.parser.client.ast.ASTNode) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser)

Example 3 with AST2Expr

use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.

the class BasicPatternPropertiesTestCase method checkPriority.

public void checkPriority(String patternString, String priority) {
    try {
        EvalEngine engine = EvalEngine.get();
        ASTNode node = fParser.parse(patternString);
        IExpr pat = new AST2Expr(false, engine).convert(node);
        PatternMatcher matcher = new PatternMatcher(pat);
        assertEquals(Integer.toString(matcher.getLHSPriority()), priority);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals("0", priority);
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 4 with AST2Expr

use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.

the class ImportString method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    if (!(ast.arg1() instanceof IStringX)) {
        return F.NIL;
    }
    String str1 = ((IStringX) ast.arg1()).toString();
    Extension format = Extension.TXT;
    if (ast.size() > 2) {
        if (!(ast.arg2() instanceof IStringX)) {
            return F.NIL;
        }
        format = Extension.importExtension(((IStringX) ast.arg2()).toString());
    }
    try {
        switch(format) {
            case JSON:
                return JSONConvert.importJSON(str1);
            case EXPRESSIONJSON:
                return ExpressionJSONConvert.importExpressionJSON(str1);
            case TABLE:
                AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
                final Parser parser = new Parser(engine.isRelaxedSyntax(), true);
                CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter(',');
                Iterable<CSVRecord> records = csvFormat.parse(new StringReader(str1));
                IASTAppendable rowList = F.ListAlloc(256);
                for (CSVRecord record : records) {
                    IASTAppendable columnList = F.ListAlloc(record.size());
                    for (String string : record) {
                        final ASTNode node = parser.parse(string);
                        IExpr temp = ast2Expr.convert(node);
                        columnList.append(temp);
                    }
                    rowList.append(columnList);
                }
                return rowList;
            case STRING:
                return ofString(str1, engine);
            case TXT:
                return ofText(str1, engine);
            default:
        }
    } catch (SyntaxError se) {
        LOGGER.log(engine.getLogLevel(), "ImportString: syntax error!", se);
    } catch (Exception ex) {
        LOGGER.log(engine.getLogLevel(), "ImportString", ex);
    }
    return F.NIL;
}
Also used : AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) Extension(org.matheclipse.core.io.Extension) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) SyntaxError(org.matheclipse.parser.client.SyntaxError) StringReader(java.io.StringReader) ASTNode(org.matheclipse.parser.client.ast.ASTNode) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr)

Example 5 with AST2Expr

use of org.matheclipse.core.convert.AST2Expr in project symja_android_library by axkr.

the class PatternMatchingTestCase method checkPriority.

public void checkPriority(String patternString, int priority) {
    try {
        EvalEngine engine = EvalEngine.get();
        ASTNode node = fParser.parse(patternString);
        IExpr pat = new AST2Expr(false, engine).convert(node);
        PatternMatcher matcher = new PatternMatcher(pat);
        assertEquals(matcher.getLHSPriority(), priority);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(0, priority);
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher) AST2Expr(org.matheclipse.core.convert.AST2Expr)

Aggregations

AST2Expr (org.matheclipse.core.convert.AST2Expr)21 IExpr (org.matheclipse.core.interfaces.IExpr)18 ASTNode (org.matheclipse.parser.client.ast.ASTNode)17 EvalEngine (org.matheclipse.core.eval.EvalEngine)8 IAST (org.matheclipse.core.interfaces.IAST)8 Parser (org.matheclipse.parser.client.Parser)7 IOException (java.io.IOException)5 IASTAppendable (org.matheclipse.core.interfaces.IASTAppendable)5 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)5 FileReader (java.io.FileReader)4 CSVFormat (org.apache.commons.csv.CSVFormat)4 CSVRecord (org.apache.commons.csv.CSVRecord)4 SyntaxError (org.matheclipse.parser.client.SyntaxError)4 OutputFormFactory (org.matheclipse.core.form.output.OutputFormFactory)3 IStringX (org.matheclipse.core.interfaces.IStringX)3 Extension (org.matheclipse.core.io.Extension)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 FlowControlException (org.matheclipse.core.eval.exception.FlowControlException)2 Context (org.matheclipse.core.expression.Context)2 ContextPath (org.matheclipse.core.expression.ContextPath)2