use of org.matheclipse.io.expression.ASTDataset in project symja_android_library by axkr.
the class AJAXQueryServlet method evaluateString.
private String[] evaluateString(EvalEngine engine, final String inputString, final String numericMode, final String function, StringWriter outWriter, StringWriter errorWriter) {
String input = inputString.trim();
if (input.length() > 1 && input.charAt(0) == '?') {
IExpr doc = Documentation.findDocumentation(input);
return JSONBuilder.createJSONResult(engine, doc, outWriter, errorWriter);
}
try {
EvalEngine.setReset(engine);
ExprParser parser = new ExprParser(engine, isRelaxedSyntax());
// throws SyntaxError exception, if syntax isn't valid
IExpr inExpr = parser.parse(input);
if (inExpr != null) {
long numberOfLeaves = inExpr.leafCount();
if (numberOfLeaves > Config.MAX_INPUT_LEAVES) {
return JSONBuilder.createJSONError("Input expression too big!");
}
if (numericMode.equals("N")) {
inExpr = F.N(inExpr);
}
// inExpr contains the user input from the web interface in
// internal format now
StringWriter outBuffer = new StringWriter();
IExpr outExpr;
outExpr = evalTopLevel(engine, outBuffer, inExpr);
if (outExpr != null) {
if (outExpr.isAST(S.Graphics)) {
try {
String html = Config.SVG_PAGE;
StringBuilder stw = new StringBuilder();
GraphicsFunctions.graphicsToSVG((IAST) outExpr, stw);
html = StringUtils.replace(html, "`1`", stw.toString());
html = StringEscapeUtils.escapeHtml4(html);
return JSONBuilder.createJSONJavaScript("<iframe srcdoc=\"" + html + "\" style=\"display: block; width: 100%; height: 100%; border: none;\" ></iframe>");
} catch (Exception ex) {
LOGGER.debug("{}.evaluateString() failed", getClass().getSimpleName(), ex);
}
} else if (outExpr.isASTSizeGE(S.Graphics3D, 2)) {
StringBuilder buf = new StringBuilder();
if (GraphicsFunctions.renderGraphics3D(buf, (IAST) outExpr, engine)) {
try {
return JSONBuilder.createGraphics3DIFrame(JSBuilder.GRAPHICS3D_IFRAME_TEMPLATE, buf.toString());
} catch (Exception ex) {
LOGGER.debug("{}.evaluateString() failed", getClass().getSimpleName(), ex);
}
}
}
if (outExpr.isASTSizeGE(S.Show, 2)) {
IAST show = (IAST) outExpr;
return JSONBuilder.createJSONShow(engine, show);
} else if (outExpr instanceof GraphExpr) {
String javaScriptStr = GraphFunctions.graphToJSForm((GraphExpr) outExpr);
if (javaScriptStr != null) {
String html = VISJS_IFRAME;
html = StringUtils.replace(html, "`1`", javaScriptStr);
html = //
StringUtils.replace(//
html, //
"`2`", //
" var options = { };\n");
html = StringEscapeUtils.escapeHtml4(html);
return JSONBuilder.createJSONJavaScript("<iframe srcdoc=\"" + html + "\" style=\"display: block; width: 100%; height: 100%; border: none;\" ></iframe>");
}
} else if (outExpr instanceof ASTDataset) {
String javaScriptStr = ((ASTDataset) outExpr).datasetToJSForm();
if (javaScriptStr != null) {
String htmlSnippet = javaScriptStr.trim();
// html = StringEscapeUtils.escapeHtml4(html);
return JSONBuilder.createJSONHTML(engine, htmlSnippet, outWriter, errorWriter);
// return JSONBuilder.createJSONJavaScript(
// "<iframe srcdoc=\""
// + html
// + "\" style=\"display: block; width: 100%; height: 100%;
// border: none;\"></iframe>");
}
} else if (outExpr.isAST(S.JSFormData, 3)) {
IAST jsFormData = (IAST) outExpr;
String jsLibraryType = jsFormData.arg2().toString();
if (jsLibraryType.equals("mathcell")) {
try {
return JSONBuilder.createMathcellIFrame(JSBuilder.MATHCELL_IFRAME_TEMPLATE, jsFormData.arg1().toString());
} catch (Exception ex) {
LOGGER.debug("{}.evaluateString() failed", getClass().getSimpleName(), ex);
}
} else if (jsLibraryType.equals("jsxgraph")) {
try {
return JSONBuilder.createJSXGraphIFrame(JSBuilder.JSXGRAPH_IFRAME_TEMPLATE, jsFormData.arg1().toString());
} catch (Exception ex) {
LOGGER.debug("{}.evaluateString() failed", getClass().getSimpleName(), ex);
}
} else if (jsLibraryType.equals("plotly")) {
try {
return JSONBuilder.createPlotlyIFrame(JSBuilder.PLOTLY_IFRAME_TEMPLATE, jsFormData.arg1().toString());
} catch (Exception ex) {
LOGGER.debug("{}.evaluateString() failed", getClass().getSimpleName(), ex);
}
} else if (jsLibraryType.equals("treeform")) {
try {
String manipulateStr = jsFormData.arg1().toString();
String html = VISJS_IFRAME;
html = StringUtils.replace(html, "`1`", manipulateStr);
html = //
StringUtils.replace(//
html, //
"`2`", " var options = {\n" + " edges: {\n" + " smooth: {\n" + " type: 'cubicBezier',\n" + " forceDirection: 'vertical',\n" + " roundness: 0.4\n" + " }\n" + " },\n" + " layout: {\n" + " hierarchical: {\n" + " direction: \"UD\"\n" + " }\n" + " },\n" + " nodes: {\n" + " shape: 'box'\n" + " },\n" + " physics:false\n" + //
" }; ");
html = StringEscapeUtils.escapeHtml4(html);
return JSONBuilder.createJSONJavaScript("<iframe srcdoc=\"" + html + "\" style=\"display: block; width: 100%; height: 100%; border: none;\" ></iframe>");
} catch (Exception ex) {
LOGGER.debug("{}.evaluateString() failed", getClass().getSimpleName(), ex);
}
}
} else if (outExpr.isString()) {
IStringX str = (IStringX) outExpr;
if (str.getMimeType() == IStringX.TEXT_HTML) {
String htmlSnippet = str.toString();
String htmlPage = HTML_IFRAME;
htmlPage = StringUtils.replace(htmlPage, "`1`", htmlSnippet);
return JSONBuilder.createJSONJavaScript("<iframe srcdoc=\"" + htmlPage + "\" style=\"display: block; width: 100%; height: 100%; border: none;\" ></iframe>");
}
}
return JSONBuilder.createJSONResult(engine, outExpr, outWriter, errorWriter);
}
return createOutput(outBuffer, null, engine, function);
} else {
return JSONBuilder.createJSONError("Input string parsed to null");
}
} catch (AbortException se) {
return JSONBuilder.createJSONResult(engine, S.$Aborted, outWriter, errorWriter);
} catch (FailedException se) {
return JSONBuilder.createJSONResult(engine, S.$Failed, outWriter, errorWriter);
} catch (SyntaxError se) {
return JSONBuilder.createJSONSyntaxError(se.getMessage());
} catch (MathException se) {
return JSONBuilder.createJSONError(se.getMessage());
} catch (IOException e) {
String msg = e.getMessage();
if (msg != null) {
return JSONBuilder.createJSONError("IOException occured: " + msg);
}
return JSONBuilder.createJSONError("IOException occured");
} catch (Exception e) {
// error message
LOGGER.error("{}.evaluateString() failed", getClass().getSimpleName(), e);
String msg = e.getMessage();
if (msg != null) {
return JSONBuilder.createJSONError("Error in evaluateString: " + msg);
}
return JSONBuilder.createJSONError("Error in evaluateString: " + e.getClass().getSimpleName());
}
}
use of org.matheclipse.io.expression.ASTDataset in project symja_android_library by axkr.
the class SerializableDataSetTest method testDataset.
public void testDataset() {
Table table = Table.read().csv(//
"Products,Sales,Market_Share\n" + //
"a,5500,3\n" + //
"b,12200,4\n" + "c,60000,33\n", "");
ASTDataset ds = ASTDataset.newTablesawTable(table);
equalsStringCopy(ds);
}
Aggregations