Search in sources :

Example 1 with IStringX

use of org.matheclipse.core.interfaces.IStringX 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 IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Package method loadPackage.

/**
	 * Load a package from the given reader
	 * 
	 * @param is
	 */
public static void loadPackage(final EvalEngine engine, final Reader is) {
    String record = null;
    // new
    final BufferedReader r = new BufferedReader(is);
    try {
        StringBuilder builder = new StringBuilder(2048);
        while ((record = r.readLine()) != null) {
            builder.append(record);
            builder.append('\n');
        }
        IExpr parsedExpression = engine.parse(builder.toString());
        if (parsedExpression != null && parsedExpression.isAST()) {
            IAST ast = (IAST) parsedExpression;
            if (ast.size() != 4 || !(ast.arg1() instanceof IStringX) || !ast.arg2().isList() || !ast.arg3().isList()) {
                throw new WrongNumberOfArguments(ast, 3, ast.size() - 1);
            }
            IAST symbols = (IAST) ast.arg2();
            IAST list = (IAST) ast.arg3();
            evalPackage(symbols, list, engine);
        }
    } catch (final Exception e) {
        e.printStackTrace();
    } finally {
        try {
            r.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : BufferedReader(java.io.BufferedReader) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IStringX(org.matheclipse.core.interfaces.IStringX) IOException(java.io.IOException) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments) IOException(java.io.IOException)

Example 3 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Print method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    PrintStream stream = engine.getOutPrintStream();
    if (stream == null) {
        stream = System.out;
    }
    try {
        final StringBuilder buf = new StringBuilder();
        IExpr temp;
        for (int i = 1; i < ast.size(); i++) {
            temp = engine.evaluate(ast.get(i));
            if (temp instanceof IStringX) {
                buf.append(temp.toString());
            } else {
                OutputFormFactory.get().convert(buf, temp);
            }
        }
        stream.println(buf.toString());
    } catch (IOException e) {
        stream.println(e.getMessage());
        if (Config.DEBUG) {
            e.printStackTrace();
        }
    }
    return F.Null;
}
Also used : PrintStream(java.io.PrintStream) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX) IOException(java.io.IOException)

Example 4 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class MessageName method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkSize(ast, 3);
    if (!ast.arg1().isSymbol()) {
        throw new WrongArgumentType(ast, ast.arg1(), 1, "");
    }
    IExpr arg2 = engine.evaluate(ast.arg2());
    if (arg2 instanceof IStringX || arg2.isSymbol()) {
        return F.NIL;
    }
    if (!ast.arg2().isAST(F.Set, 3)) {
        // !ast.arg2().isSymbol()) {
        throw new WrongArgumentType(ast, ast.arg2(), 2, "");
    }
    // symbol.putDownRule(RuleType.SET, true, symbol, ast.arg2(), true);
    return F.Null;
}
Also used : WrongArgumentType(org.matheclipse.core.eval.exception.WrongArgumentType) IExpr(org.matheclipse.core.interfaces.IExpr) IStringX(org.matheclipse.core.interfaces.IStringX)

Example 5 with IStringX

use of org.matheclipse.core.interfaces.IStringX in project symja_android_library by axkr.

the class Export method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    if (!(ast.arg1() instanceof IStringX)) {
        throw new WrongNumberOfArguments(ast, 1, ast.size() - 1);
    }
    String format = "Table";
    if (ast.size() == 4) {
        if (!(ast.arg3() instanceof IStringX)) {
            throw new WrongNumberOfArguments(ast, 3, ast.size() - 1);
        }
        format = ((IStringX) ast.arg3()).toString();
    }
    IStringX arg1 = (IStringX) ast.arg1();
    IExpr arg2 = ast.arg2();
    FileWriter writer = null;
    try {
        writer = new FileWriter(arg1.toString());
        if (format.equals("Table")) {
            int[] dims = arg2.isMatrix();
            if (dims != null) {
                for (int j = 0; j < dims[0]; j++) {
                    IAST rowList = (IAST) arg2.getAt(j + 1);
                    for (int i = 1; i <= dims[1]; i++) {
                        if (rowList.get(i).isSignedNumber()) {
                            writer.append(rowList.get(i).toString());
                        } else {
                            writer.append("\"");
                            writer.append(rowList.get(i).toString());
                            writer.append("\"");
                        }
                        if (i < dims[1]) {
                            writer.append(" ");
                        }
                    }
                    writer.append("\n");
                }
                return arg1;
            } else {
                if (arg2.isList()) {
                }
            }
        }
    } catch (IOException ioe) {
        engine.printMessage("Export: file " + arg1.toString() + " not found!");
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            }
        }
    }
    return F.NIL;
}
Also used : FileWriter(java.io.FileWriter) IStringX(org.matheclipse.core.interfaces.IStringX) IExpr(org.matheclipse.core.interfaces.IExpr) IAST(org.matheclipse.core.interfaces.IAST) IOException(java.io.IOException) WrongNumberOfArguments(org.matheclipse.core.eval.exception.WrongNumberOfArguments)

Aggregations

IStringX (org.matheclipse.core.interfaces.IStringX)12 IExpr (org.matheclipse.core.interfaces.IExpr)9 IAST (org.matheclipse.core.interfaces.IAST)7 IOException (java.io.IOException)5 WrongNumberOfArguments (org.matheclipse.core.eval.exception.WrongNumberOfArguments)5 TermOrder (edu.jas.poly.TermOrder)2 FileReader (java.io.FileReader)2 FileWriter (java.io.FileWriter)2 JASIExpr (org.matheclipse.core.convert.JASIExpr)2 VariablesSet (org.matheclipse.core.convert.VariablesSet)2 JASConversionException (org.matheclipse.core.eval.exception.JASConversionException)2 Options (org.matheclipse.core.eval.util.Options)2 ExprPolynomial (org.matheclipse.core.polynomials.ExprPolynomial)2 ExprPolynomialRing (org.matheclipse.core.polynomials.ExprPolynomialRing)2 ExprTermOrder (org.matheclipse.core.polynomials.ExprTermOrder)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1