Search in sources :

Example 16 with AST2Expr

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

the class BasicPatternPropertiesTestCase method comparePriority.

public void comparePriority(String patternString1, String patternString2, int result) {
    try {
        EvalEngine engine = EvalEngine.get();
        ASTNode node = fParser.parse(patternString1);
        IExpr pat1 = new AST2Expr(false, engine).convert(node);
        PatternMatcher matcher1 = new PatternMatcher(pat1);
        node = fParser.parse(patternString2);
        IExpr pat2 = new AST2Expr(false, engine).convert(node);
        PatternMatcher matcher2 = new PatternMatcher(pat2);
        assertEquals(matcher1.equivalentTo(matcher2), result);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(Integer.MAX_VALUE, result);
    }
}
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 17 with AST2Expr

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

the class ElementPreprocessor method main.

public static void main(String[] args) {
    F.initSymbols();
    FileReader reader = null;
    try {
        EvalEngine engine = EvalEngine.get();
        boolean relaxedSyntax = false;
        String userHome = System.getProperty("user.home");
        String fileName = userHome + "/git/symja_android_library/symja_android_library/tools/src/main/java/org/matheclipse/core/preprocessor/element.csv";
        reader = new FileReader(fileName);
        AST2Expr ast2Expr = new AST2Expr(relaxedSyntax, engine);
        final Parser parser = new Parser(relaxedSyntax, true);
        CSVFormat csvFormat = CSVFormat.RFC4180.withDelimiter('\t');
        Iterable<CSVRecord> records = csvFormat.parse(reader);
        IASTAppendable rowList = F.ListAlloc(130);
        for (CSVRecord record : records) {
            IASTAppendable columnList = F.ListAlloc(record.size());
            for (String str : record) {
                str = str.trim();
                if (str.length() == 0) {
                // columnList.append(F.Null);
                } else if (str.equalsIgnoreCase("Not_applicable")) {
                    columnList.append(F.Missing(F.NotApplicable));
                } else if (str.equalsIgnoreCase("Not_available")) {
                    columnList.append(F.Missing(F.NotAvailable));
                } else if (str.equalsIgnoreCase("Not_known")) {
                    columnList.append(F.Missing(F.Unknown));
                } else {
                    final ASTNode node = parser.parse(str);
                    IExpr temp = ast2Expr.convert(node);
                    if (temp.isList() || temp.isReal()) {
                        columnList.append(temp);
                    } else {
                        if (str.charAt(0) == '\"') {
                            columnList.append(str.substring(1, str.length() - 1));
                        } else {
                            columnList.append(str);
                        }
                    }
                }
            }
            rowList.append(columnList);
        }
        for (int i = 2; i < rowList.size(); i++) {
            IAST columnList = (IAST) rowList.get(i);
            System.out.print(columnList.internalJavaString(JAVA_FORM_PROPERTIES, 1, x -> null));
            System.out.println(", ");
        }
    // return rowList;
    } catch (IOException ioe) {
        System.out.println("Import: file not found!");
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : EvalEngine(org.matheclipse.core.eval.EvalEngine) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) F(org.matheclipse.core.expression.F) IAST(org.matheclipse.core.interfaces.IAST) ASTNode(org.matheclipse.parser.client.ast.ASTNode) CSVRecord(org.apache.commons.csv.CSVRecord) IOException(java.io.IOException) Prefix(org.matheclipse.core.interfaces.IExpr.SourceCodeProperties.Prefix) SourceCodeProperties(org.matheclipse.core.interfaces.IExpr.SourceCodeProperties) CSVFormat(org.apache.commons.csv.CSVFormat) IExpr(org.matheclipse.core.interfaces.IExpr) FileReader(java.io.FileReader) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) IOException(java.io.IOException) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser) IASTAppendable(org.matheclipse.core.interfaces.IASTAppendable) EvalEngine(org.matheclipse.core.eval.EvalEngine) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FileReader(java.io.FileReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST)

Example 18 with AST2Expr

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

the class FileFunctions method evaluatePackage.

public static IExpr evaluatePackage(final List<ASTNode> node, final EvalEngine engine) {
    AST2Expr ast2Expr = new AST2Expr(engine.isRelaxedSyntax(), engine);
    String compoundExpression = engine.isRelaxedSyntax() ? "compoundexpression" : "CompoundExpression";
    return evaluatePackageRecursive(node, 0, compoundExpression, ast2Expr, engine);
}
Also used : AST2Expr(org.matheclipse.core.convert.AST2Expr)

Example 19 with AST2Expr

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

the class ImportString method ofString.

/**
 * Get arbitrary data represented as a Symja expression string
 *
 * @param str
 * @param engine
 * @return
 */
public static IExpr ofString(String str, EvalEngine engine) {
    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 : ASTNode(org.matheclipse.parser.client.ast.ASTNode) AST2Expr(org.matheclipse.core.convert.AST2Expr) Parser(org.matheclipse.parser.client.Parser)

Example 20 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) {
    if (Config.isFileSystemEnabled(engine)) {
        if (!(ast.arg1() instanceof IStringX)) {
            return F.NIL;
        }
        IStringX arg1 = (IStringX) ast.arg1();
        Extension format = Extension.importFilename(arg1.toString());
        String fileName = arg1.toString();
        if (ast.size() > 2) {
            if (!(ast.arg2() instanceof IStringX)) {
                return F.NIL;
            }
            format = Extension.importExtension(((IStringX) ast.arg2()).toString());
        }
        FileReader reader = null;
        try {
            File file = new File(fileName);
            switch(format) {
                case DOT:
                case GRAPHML:
                    // graph Format
                    reader = new FileReader(fileName);
                    return graphImport(reader, format, engine);
                case EXPRESSIONJSON:
                    return expressionJSONImport(fileName);
                case JSON:
                    return jsonImport(fileName);
                case M:
                    if (ast.isAST1()) {
                        return S.Get.of(engine, ast.arg1());
                    }
                    break;
                case TABLE:
                    reader = new FileReader(fileName);
                    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(reader);
                    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(file, engine);
                case TXT:
                    return ofText(file, engine);
                case WXF:
                    byte[] byteArray = com.google.common.io.Files.toByteArray(file);
                    return WL.deserialize(byteArray);
                default:
            }
        } catch (IOException ioe) {
            LOGGER.log(engine.getLogLevel(), "Import: file {} not found!", fileName, ioe);
        } catch (SyntaxError se) {
            LOGGER.log(engine.getLogLevel(), "Import: file {} syntax error!", fileName, se);
        } catch (Exception ex) {
            LOGGER.log(engine.getLogLevel(), "Import: file {} ", fileName, ex);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return F.NIL;
}
Also used : IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ImportException(org.jgrapht.nio.ImportException) 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) 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) IExpr(org.matheclipse.core.interfaces.IExpr) File(java.io.File)

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