Wolfram ResearchPRODUCTSPURCHASEFOR USERSCOMPANYOUR SITES
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.

Documentation / Mathematica / Add-ons & Links / J/Link / Part 2. Writing Java Programs That Use Mathematica / Using Expr Objects /

2.14.4 Exprs as a Means to Get String Representations of Expressions

A particularly useful method in the Expr class is toString(), which produces an InputForm-type string representation of the expression (without involving the kernel, of course). This is particularly handy for debugging purposes, when you want a quick way to see what is arriving on the link. In Section 2.8.7 it was mentioned that J/Link has a class PacketPrinter that implements the PacketListener interface and can be used to easily print out the contents of packets as they arrive in your program, without modifying your program. Below is the packetArrived() method of that class, which uses an Expr object and its toString() method to get the printable text representation of an arbitrary expression.

public boolean packetArrived(PacketArrivedEvent evt) throws MathLinkException {

    KernelLink ml = (KernelLink) evt.getSource();

    Expr e = ml.getExpr();

    strm.println("Packet type was " + pktToName(evt.getPktType()) +

                    ". Contents follows.");

    strm.println(e.toString());

    e.dispose();

    return true;

}

Whether you use the PacketPrinter class or not, this technique is useful to see what expressions are being passed around. This is often used in conjunction with the MathLink peekExpr() method, which reads an expression off the link but then resets the link so that the expression is not consumed. In this way, you can look at expressions arriving on links without interfering with the rest of the link-reading code in your program. The PacketPrinter code above does not use peekExpr(), but it has the same effect since the resetting of the link is handled elsewhere.



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information


 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy | [ja] |
Sign up for our newsletter: