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