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;
}
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);
}
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);
}
}
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;
}
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);
}
}
Aggregations