RobWorkProject  23.9.11-
Classes | Public Types | Public Member Functions | List of all members
Mathematica Class Reference

Implementation of the Wolfram Symbolic Transfer Protocol (WSTP) to allow communication with Mathematica. More...

#include <Mathematica.hpp>

Classes

class  Array
 An Array primitive. More...
 
class  AutoExpression
 Convenience class for automatic Expression deduction. More...
 
class  Expression
 A representation of a Mathematica expression. More...
 
class  Function
 A user definable function expression. More...
 
class  FunctionBase
 A base interface for function expressions. More...
 
class  Integer
 An integer primitive. More...
 
struct  Link
 Representation of a link. More...
 
class  Packet
 A Packet expression. More...
 
class  Real
 A real primitive. More...
 
class  String
 A string primitive. More...
 
class  Symbol
 A symbol primitive. More...
 

Public Types

enum  LinkProtocol { SharedMemory , TCP_IP }
 Available link protocols. More...
 
enum  PacketType {
  EnterExpression , EnterText , Evaluate , InputName ,
  Message , OutputName , ReturnExpression , Return ,
  ReturnText , Text
}
 Packet types. More...
 
typedef rw::core::Ptr< MathematicaPtr
 Smart pointer.
 

Public Member Functions

 Mathematica ()
 Constructor.
 
virtual ~Mathematica ()
 Destructor.
 
bool initialize ()
 Initialize the WSTP framework. More...
 
Link::Ptr createLink (const std::string &name="", LinkProtocol protocol=SharedMemory)
 Create a new link. More...
 
Link::Ptr connectToLink (const std::string &name)
 Connect to an existing link. More...
 
Link::Ptr launchKernel ()
 Launch a kernel. More...
 
bool closeLink (Link::Ptr link)
 Close a link. More...
 
void finalize ()
 Close all open links, and deinitialize the WSTP framework.
 

Detailed Description

Implementation of the Wolfram Symbolic Transfer Protocol (WSTP) to allow communication with Mathematica.

Example of basic usage:

m.initialize();
const Mathematica::Link::Ptr l = m.launchKernel();
*l >> result;
*l << "Solve[x^2 + 2 y^3 == 3681 && x > 0 && y > 0, {x, y}, Integers]";
*l >> result;
std::cout << *result << std::endl;
rw::core::Ptr< Packet > Ptr
Smart pointer type.
Definition: Mathematica.hpp:695

Construction of a Mathematica environment, m, makes it possible to create WSTP links (Mathematica::Link). Please note that the Mathematica object automatically closes all links at destruction. WSTP links can be used for different types of interprocess communication with Wolfram Symbolic expressions. In this example we launch and connect to a kernel.

The first expression (the In[1]:= prompt) is received from the kernel with the stream operator *l >> result. We simply ignore this value, and send the first command to be evaluated with the stream operator. The result is then retrieved and printed:

 ReturnPacket[
   List[
     List[
        Rule[x, 15],
        Rule[y, 12]],
     List[
        Rule[x, 41],
        Rule[y, 10]],
     List[
        Rule[x, 57],
        Rule[y, 6]]]]

Streaming of a string to the link will implicitly create a ToExpression expression and wrap it in an EvaluatePacket. The kernel will evaluate the expression in the packet and send back a ReturnPacket with the result (as an expression). Many different types of packets can be sent and received on the link (see Mathematica::PacketType). It is up to the user to deal with the different packet types, and to parse the results received.

Member Enumeration Documentation

◆ LinkProtocol

Available link protocols.

Enumerator
SharedMemory 

Use shared memory.

TCP_IP 

Use TCP/IP.

◆ PacketType

enum PacketType

Packet types.

Enumerator
EnterExpression 

EnterExpressionPacket.

EnterText 

EnterTextPacket.

Evaluate 

EvaluatePacket.

InputName 

InputNamePacket.

Message 

MessagePacket.

OutputName 

OutputNamePacket.

ReturnExpression 

ReturnExpressionPacket.

Return 

ReturnPacket.

ReturnText 

ReturnTextPacket.

Text 

TextPacket.

Member Function Documentation

◆ closeLink()

bool closeLink ( Link::Ptr  link)

Close a link.

Parameters
link[in] the link to close.
Returns
true if link was found and closed, false if it was already closed or not found.

◆ connectToLink()

Link::Ptr connectToLink ( const std::string &  name)

Connect to an existing link.

Parameters
name[in] the name of the link to connect to.
Returns
a pointer to the link, or NULL if connection failed.

◆ createLink()

Link::Ptr createLink ( const std::string &  name = "",
LinkProtocol  protocol = SharedMemory 
)

Create a new link.

Parameters
name[in] (optional) a named link allows other programs to connect to it.
protocol[in] (optional) the type of LinkProtocol to use - default is shared memory.
Returns
the link.

◆ initialize()

bool initialize ( )

Initialize the WSTP framework.

Returns
true if success.

◆ launchKernel()

Link::Ptr launchKernel ( )

Launch a kernel.

Returns
link to the kernel.

The documentation for this class was generated from the following file: