Skip to content

Commit 097fa80

Browse files
committed
Refactor output on desktop to F#show() method
1 parent ed6623c commit 097fa80

File tree

4 files changed

+141
-138
lines changed

4 files changed

+141
-138
lines changed

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/basic/Config.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,86 @@ public static boolean isFileSystemEnabled(EvalEngine engine) {
308308
* Use visjs.org JavaScript library for visualizing graph theory objects
309309
*/
310310
public static boolean USE_VISJS = false;
311+
312+
/**
313+
* HTML template for the <a href="https://visjs.org/">VIS-network</a>
314+
*/
315+
public final static String VISJS_PAGE = //
316+
"<html>\n" + //
317+
"<head>\n" + //
318+
"<meta charset=\"utf-8\">\n" + //
319+
"<head>\n" + //
320+
" <title>VIS-Network</title>\n" + //
321+
"\n" + //
322+
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm/vis-network@5.0.0/dist/vis-network.min.js\"></script>\n"
323+
+ //
324+
" <style type=\"text/css\">\n" + //
325+
" #mynetwork {\n" + //
326+
" width: 600px;\n" + //
327+
" height: 400px;\n" + //
328+
" border: 1px solid lightgray;\n" + //
329+
" }\n" + //
330+
" </style>\n" + //
331+
"</head>\n" + //
332+
"<body>\n" + //
333+
"\n" + //
334+
"<h1>VIS-Network</h1>\n" + //
335+
"\n" + //
336+
"<div id=\"vis\"></div>\n" + //
337+
"\n" + //
338+
"<script type=\"text/javascript\">\n" + //
339+
"`1`\n" + //
340+
" // create a network\n" + //
341+
" var container = document.getElementById('vis');\n" + //
342+
" var data = {\n" + //
343+
" nodes: nodes,\n" + //
344+
" edges: edges\n" + //
345+
" };\n" + //
346+
" var options = {};\n" + //
347+
" var network = new vis.Network(container, data, options);\n" + //
348+
"</script>\n" + //
349+
"\n" + //
350+
"\n" + //
351+
"</body>\n" + //
352+
"</html>";//
353+
354+
/**
355+
* HTML template for the <a href="https://github.com/paulmasson/mathcell">MathCell</a> and
356+
* <a href="https://github.com/paulmasson/math">Math</a> JavaScript libraries.
357+
*/
358+
public final static String MATHCELL_PAGE = //
359+
"<html>\n" + //
360+
"<head>\n" + //
361+
"<meta charset=\"utf-8\">\n" + //
362+
"<title>MathCell</title>\n" + //
363+
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + //
364+
"<style></style>\n" + //
365+
"</head>\n" + //
366+
"\n" + //
367+
"<body>\n" + //
368+
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/math@1.2.2/build/math.js\"></script>" + //
369+
"\n" + //
370+
"\n" + //
371+
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/mathcell@1.7.0/build/mathcell.js\"></script>\n"
372+
+ //
373+
"<script src=\"https://cdn.jsdelivr.net/gh/mathjax/MathJax@2.7.5/MathJax.js?config=TeX-AMS_HTML\"></script>"
374+
+ //
375+
"\n" + //
376+
"<div class=\"mathcell\" style=\"width: 100%; height: 100%; padding: .25in .5in .5in .5in;\">\n" + //
377+
"<script>\n" + //
378+
"\n" + //
379+
"var parent = document.scripts[ document.scripts.length - 1 ].parentNode;\n" + //
380+
"\n" + //
381+
"var id = generateId();\n" + //
382+
"parent.id = id;\n" + //
383+
"\n" + //
384+
"`1`\n" + //
385+
"\n" + //
386+
"parent.update( id );\n" + //
387+
"\n" + //
388+
"</script>\n" + //
389+
"</div>\n" + //
390+
"\n" + //
391+
"</body>\n" + //
392+
"</html>";//
311393
}

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/Console.java

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
import java.io.IOException;
99
import java.io.InputStreamReader;
1010
import java.io.OutputStreamWriter;
11-
import java.io.PrintStream;
1211
import java.io.PrintWriter;
1312
import java.io.StringWriter;
1413
import java.nio.charset.StandardCharsets;
15-
import java.text.DecimalFormat;
16-
import java.text.DecimalFormatSymbols;
1714
import java.util.Locale;
1815
import java.util.concurrent.TimeUnit;
1916

2017
import org.matheclipse.core.basic.Config;
21-
import org.matheclipse.core.builtin.GraphFunctions;
2218
import org.matheclipse.core.eval.exception.AbortException;
2319
import org.matheclipse.core.eval.exception.ReturnException;
2420
import org.matheclipse.core.eval.exception.Validate;
@@ -27,7 +23,6 @@
2723
import org.matheclipse.core.form.output.OutputFormFactory;
2824
import org.matheclipse.core.graphics.Show2SVG;
2925
import org.matheclipse.core.interfaces.IAST;
30-
import org.matheclipse.core.interfaces.IDataExpr;
3126
import org.matheclipse.core.interfaces.IExpr;
3227
import org.matheclipse.parser.client.Scanner;
3328
import org.matheclipse.parser.client.SyntaxError;
@@ -39,80 +34,6 @@
3934
* See {@link MMAConsole}
4035
*/
4136
public class Console {
42-
protected final static String VISJS_PAGE = //
43-
"<html>\n" + //
44-
"<head>\n" + //
45-
"<meta charset=\"utf-8\">\n" + //
46-
"<head>\n" + //
47-
" <title>VIS-Network</title>\n" + //
48-
"\n" + //
49-
" <script type=\"text/javascript\" src=\"https://cdn.jsdelivr.net/npm/vis-network@5.0.0/dist/vis-network.min.js\"></script>\n"
50-
+ //
51-
" <style type=\"text/css\">\n" + //
52-
" #mynetwork {\n" + //
53-
" width: 600px;\n" + //
54-
" height: 400px;\n" + //
55-
" border: 1px solid lightgray;\n" + //
56-
" }\n" + //
57-
" </style>\n" + //
58-
"</head>\n" + //
59-
"<body>\n" + //
60-
"\n" + //
61-
"<h1>VIS-Network</h1>\n" + //
62-
"\n" + //
63-
"<div id=\"vis\"></div>\n" + //
64-
"\n" + //
65-
"<script type=\"text/javascript\">\n" + //
66-
"`1`\n" + //
67-
" // create a network\n" + //
68-
" var container = document.getElementById('vis');\n" + //
69-
" var data = {\n" + //
70-
" nodes: nodes,\n" + //
71-
" edges: edges\n" + //
72-
" };\n" + //
73-
" var options = {};\n" + //
74-
" var network = new vis.Network(container, data, options);\n" + //
75-
"</script>\n" + //
76-
"\n" + //
77-
"\n" + //
78-
"</body>\n" + //
79-
"</html>";//
80-
protected final static String MATHCELL_PAGE = //
81-
"<html>\n" + //
82-
"<head>\n" + //
83-
"<meta charset=\"utf-8\">\n" + //
84-
"<title>MathCell</title>\n" + //
85-
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n" + //
86-
"<style></style>\n" + //
87-
"</head>\n" + //
88-
"\n" + //
89-
"<body>\n" + //
90-
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/math@1.2.2/build/math.js\"></script>" + //
91-
"\n" + //
92-
"\n" + //
93-
"<script src=\"https://cdn.jsdelivr.net/gh/paulmasson/mathcell@1.7.0/build/mathcell.js\"></script>\n"
94-
+ //
95-
"<script src=\"https://cdn.jsdelivr.net/gh/mathjax/MathJax@2.7.5/MathJax.js?config=TeX-AMS_HTML\"></script>"
96-
+ //
97-
"\n" + //
98-
"<div class=\"mathcell\" style=\"width: 100%; height: 100%; padding: .25in .5in .5in .5in;\">\n" + //
99-
"<script>\n" + //
100-
"\n" + //
101-
"var parent = document.scripts[ document.scripts.length - 1 ].parentNode;\n" + //
102-
"\n" + //
103-
"var id = generateId();\n" + //
104-
"parent.id = id;\n" + //
105-
"\n" + //
106-
"`1`\n" + //
107-
"\n" + //
108-
"parent.update( id );\n" + //
109-
"\n" + //
110-
"</script>\n" + //
111-
"</div>\n" + //
112-
"\n" + //
113-
"</body>\n" + //
114-
"</html>";//
115-
11637
/**
11738
* 60 seconds timeout limit as the default value for Symja expression evaluation.
11839
*/
@@ -590,35 +511,9 @@ private String printResult(IExpr result) throws IOException {
590511
if (result.isAST(F.Graphics)) {// || result.isAST(F.Graphics3D)) {
591512
outExpr = F.Show(outExpr);
592513
}
593-
if (outExpr.isSameHeadSizeGE(F.Show, 2)) {
594-
try {
595-
IAST show = (IAST) outExpr;
596-
if (show.size() > 1 && show.arg1().isSameHeadSizeGE(F.Graphics, 2)) {
597-
return openSVGOnDesktop(show);
598-
}
599-
} catch (Exception ex) {
600-
if (Config.SHOW_STACKTRACE) {
601-
ex.printStackTrace();
602-
}
603-
}
604-
} else if (result.head().equals(F.Graph) && result instanceof IDataExpr) {
605-
String javaScriptStr = GraphFunctions.graphToJSForm((IDataExpr) result);
606-
if (javaScriptStr != null) {
607-
String html = VISJS_PAGE;
608-
html = html.replaceAll("`1`", javaScriptStr);
609-
return Console.openHTMLOnDesktop(html);
610-
}
611-
} else if (result.isAST(F.JSFormData, 3) && result.second().toString().equals("mathcell")) {
612-
try {
613-
String manipulateStr = ((IAST) result).arg1().toString();
614-
String html = MATHCELL_PAGE;
615-
html = html.replaceAll("`1`", manipulateStr);
616-
return Console.openHTMLOnDesktop(html);
617-
} catch (Exception ex) {
618-
if (Config.SHOW_STACKTRACE) {
619-
ex.printStackTrace();
620-
}
621-
}
514+
String html = F.show(outExpr);
515+
if (html != null) {
516+
return html;
622517
}
623518
}
624519
StringBuilder strBuffer = new StringBuilder();

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/eval/MMAConsole.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -578,36 +578,10 @@ private String printResult(IExpr result) throws IOException {
578578
if (result.isAST(F.Graphics)) {// || result.isAST(F.Graphics3D)) {
579579
outExpr = F.Show(outExpr);
580580
}
581-
if (outExpr.isSameHeadSizeGE(F.Show, 2)) {
582-
try {
583-
IAST show = (IAST) outExpr;
584-
if (show.size() > 1 && show.arg1().isSameHeadSizeGE(F.Graphics, 2)) {
585-
return Console.openSVGOnDesktop(show);
586-
}
587-
} catch (Exception ex) {
588-
if (Config.SHOW_STACKTRACE) {
589-
ex.printStackTrace();
590-
}
591-
}
592-
} else if (result.head().equals(F.Graph) && result instanceof IDataExpr) {
593-
String javaScriptStr = GraphFunctions.graphToJSForm((IDataExpr) result);
594-
if (javaScriptStr != null) {
595-
String html = Console.VISJS_PAGE;
596-
html = html.replaceAll("`1`", javaScriptStr);
597-
return Console.openHTMLOnDesktop(html);
598-
}
599-
} else if (result.isAST(F.JSFormData, 3) && result.second().toString().equals("mathcell")) {
600-
try {
601-
String manipulateStr = ((IAST) result).arg1().toString();
602-
String html = Console.MATHCELL_PAGE;
603-
html = html.replaceAll("`1`", manipulateStr);
604-
return Console.openHTMLOnDesktop(html);
605-
} catch (Exception ex) {
606-
if (Config.SHOW_STACKTRACE) {
607-
ex.printStackTrace();
608-
}
609-
}
610-
}
581+
String html = F.show(outExpr);
582+
if (html != null) {
583+
return html;
584+
}
611585
}
612586

613587
StringBuilder strBuffer = new StringBuilder();

symja_android_library/matheclipse-core/src/main/java/org/matheclipse/core/expression/F.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.matheclipse.core.expression;
22

3+
import java.io.IOException;
34
import java.math.BigInteger;
45
import java.util.HashMap;
56
import java.util.IdentityHashMap;
@@ -62,6 +63,7 @@
6263
import org.matheclipse.core.builtin.WindowFunctions;
6364
import org.matheclipse.core.convert.AST2Expr;
6465
import org.matheclipse.core.convert.Object2Expr;
66+
import org.matheclipse.core.eval.Console;
6567
import org.matheclipse.core.eval.EvalAttributes;
6668
import org.matheclipse.core.eval.EvalEngine;
6769
import org.matheclipse.core.eval.interfaces.AbstractCoreFunctionEvaluator;
@@ -76,6 +78,7 @@
7678
import org.matheclipse.core.interfaces.IBuiltInSymbol;
7779
import org.matheclipse.core.interfaces.IComplex;
7880
import org.matheclipse.core.interfaces.IComplexNum;
81+
import org.matheclipse.core.interfaces.IDataExpr;
7982
import org.matheclipse.core.interfaces.IEvaluator;
8083
import org.matheclipse.core.interfaces.IExpr;
8184
import org.matheclipse.core.interfaces.IFraction;
@@ -9164,4 +9167,53 @@ public static IExpr fromString(String string) {
91649167
return stringx(string);
91659168
}
91669169

9170+
/**
9171+
* Show the result in an HTML page with the help of the Java <code>Desktop.getDesktop().open()</code> method. On
9172+
* some platforms the Desktop API may not be supported; use the <code>isDesktopSupported()</code> method todetermine
9173+
* if the current desktop is supported.
9174+
*
9175+
* @param expr
9176+
* @return
9177+
* @throws IOException
9178+
*/
9179+
public static String show(IExpr expr) {
9180+
if (expr.isSameHeadSizeGE(Show, 2)) {
9181+
try {
9182+
IAST show = (IAST) expr;
9183+
if (show.size() > 1 && show.arg1().isSameHeadSizeGE(Graphics, 2)) {
9184+
return Console.openSVGOnDesktop(show);
9185+
}
9186+
} catch (Exception ex) {
9187+
if (Config.SHOW_STACKTRACE) {
9188+
ex.printStackTrace();
9189+
}
9190+
}
9191+
} else if (expr.head().equals(Graph) && expr instanceof IDataExpr) {
9192+
String javaScriptStr = GraphFunctions.graphToJSForm((IDataExpr) expr);
9193+
if (javaScriptStr != null) {
9194+
try {
9195+
String html = Config.VISJS_PAGE;
9196+
html = html.replaceAll("`1`", javaScriptStr);
9197+
return Console.openHTMLOnDesktop(html);
9198+
} catch (Exception ex) {
9199+
if (Config.SHOW_STACKTRACE) {
9200+
ex.printStackTrace();
9201+
}
9202+
}
9203+
}
9204+
} else if (expr.isAST(JSFormData, 3) && expr.second().toString().equals("mathcell")) {
9205+
try {
9206+
String manipulateStr = ((IAST) expr).arg1().toString();
9207+
String html = Config.MATHCELL_PAGE;
9208+
html = html.replaceAll("`1`", manipulateStr);
9209+
return Console.openHTMLOnDesktop(html);
9210+
} catch (Exception ex) {
9211+
if (Config.SHOW_STACKTRACE) {
9212+
ex.printStackTrace();
9213+
}
9214+
}
9215+
}
9216+
return null;
9217+
}
9218+
91679219
}

0 commit comments

Comments
 (0)