Group Abstract Group Abstract

Message Boards Message Boards

0
|
4.1K Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Connecting to the Wolfram Engine from C++

Posted 2 years ago
POSTED BY: Bart R.
6 Replies

Thanks, I cloned your project and played with it for a while. Actually, you were really close to succeeding. Your setup and everything you do until sending the expression via WSTP is fine.

The problem in your code (I will refer to what you have on github, which is slightly different from what you posted here) is that you send a string "2+2+2". When WSStream sees a string it sends it as a String expression, which in WL evaluates to itself.

The expression that you want to send is Plus[2, 2, 2], which in WL evaluates to 6. So I modified your code as follows:

    // Write the expression to the stream
    std::cout << "Add 2+2+2 to the stream" << std::endl;
    LLU::WS::Function enterExpressionFunction {"EnterExpressionPacket", 1};

    test << enterExpressionFunction << LLU::WS::Function("Plus", 3) << 2 << 2 << 2;

    // Wait for output packet Out[1]:= and the computed results

And the output I get from your debug prints is:

(...)
Add 2+2+2 to the stream
Wait for output packet
Function head:OutputNamePacket Arguments :1 String : Out[1]= 
Function head:ReturnExpressionPacket Arguments :1 String : 6
(...)

So the result is 6, as expected.

POSTED BY: Rafal Chojna

Interesting. LLU was designed to be used in LibraryLink-based paclets and I'm not aware of any use in a standalone application but it might be possible.

Can you please share your entire code and build commands, e.g. as a github gist or even here?

POSTED BY: Rafal Chojna
Posted 2 years ago

I have been reading in the documentation a bit and wondered if binary data-exchange is also possible with WSTP. I want to do some image processing and to do that binary data-echange is the fastest way. I've been playing around with the command ImageData[] but it generates a lot of text for a small image. Secondly when you receive a reply is it also possible to get the metadata of the reply. I mean when the Engine sends a reply is it possible to get a message that the engine is going to send an array of 10.000 bytes for example. I saw you have the TypeOf command but that only works for simple datatypes.

POSTED BY: Bart R.
Posted 2 years ago

Thanks for you're reply. I changed the application a bit and found out that the function EnterTextPacket did the trick for me. But it was a long struggle in the documentation. Do you have some documentation on how the kernel behave's on the interface? For example a state diagram of the WSTP interface of the kernel? Which functions should be send and what to expect back. Or what if a function can't be evaluated ? What errors can occur ? Mainly which function packets are used and in what order ?

POSTED BY: Bart R.
Posted 2 years ago

Thanks for responding. I've published a github with my application. You can find it: in my public repository. It contains a readme.md what I did to come to this point. I''d like to know if it's possible with the WSTP part inside the Library Link Utitlities to connect directly to the Wolfram Engine with the WSStream object. Thanks in advance.

POSTED BY: Bart R.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard