public String getSuggestions() throws MathLinkException, ExprFormatException { StringBuilder result = new StringBuilder(); result.append("["); kernelLink.evaluate("$ContextPath"); kernelLink.waitForAnswer(); Expr contexts = kernelLink.getExpr(); for (int j = 1; j <= contexts.length(); j++) { String context = contexts.part(j).asString(); kernelLink.evaluate("Names[\"" + context + "*\"]"); kernelLink.waitForAnswer(); Expr symbols = kernelLink.getExpr(); for (int i = 1; i <= symbols.length(); i++) { result.append('"'); result.append(symbols.part(i).asString()); result.append('"'); result.append(","); } } result.append("]"); return result.toString(); }
public void evaluate(String query, boolean evalToImage, Session session) throws MathLinkException, IOException { int currentResource = -1; long mark = System.currentTimeMillis(); session.sendInline("<div id='resource_" + currentCount + "' class='cellgroup'>"); // Log the input Resource input = new Resource(query); resources.add(input); currentResource = resources.size(); session.sendInline(input.render(true)); kernelLink.evaluate(query); kernelLink.waitForAnswer(); Expr result = kernelLink.getExpr(); // Append intermediate packets received by listener callback while (currentResource < resources.size()) { session.sendInline(resources.get(currentResource).render(true)); currentResource++; } if (!result.equals(NULLEXPR)) { // Log the output as fullform text Resource textResource = new Resource(MathLink.RETURNPKT, result); byte[] data = null; if (evalToImage || textResource.isGraphics()) data = kernelLink.evaluateToImage(result, 0, 0); if (data != null) { Resource graphicsResource = new Resource(MathLink.DISPLAYPKT, data); resources.add(graphicsResource); session.sendInline(graphicsResource.render(true)); textResource.subdue(); session.sendInline(textResource.render(false)); } else { session.sendInline(textResource.render(true)); } resources.add(textResource); } // Done with this. Move on... kernelLink.newPacket(); currentCount++; input.setTime(System.currentTimeMillis() - mark); session.sendInline("<div class='time'>" + commify(input.getTime()) + "ms</div></div>"); }