Created
August 31, 2012 03:33
-
-
Save groveriffic/3548727 to your computer and use it in GitHub Desktop.
jruby-mathlink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'JLink.jar' | |
# Java::ComWolframJlink::KernelLink | |
# http://reference.wolfram.com/mathematica/JLink/ref/java/com/wolfram/jlink/KernelLink.html | |
# http://reference.wolfram.com/mathematica/JLink/ref/java/com/wolfram/jlink/MathLink.html | |
# Java::ComWolframJlink::MathLinkException | |
# Java::ComWolframJlink::MathLinkFactory | |
# http://reference.wolfram.com/mathematica/JLink/ref/java/com/wolfram/jlink/MathLinkFactory.html | |
class Mathlink | |
attr_accessor :timeout | |
attr_reader :kernel_link | |
attr_reader :debug | |
@@math_kernel_path = [ | |
"/usr/local/bin/math", | |
"/Applications/Mathematica.app/Contents/MacOS/MathKernel" | |
].find{ |f| File.exist?(f) } | |
@@jlink_libdir = [ | |
"/usr/local/Wolfram/Mathematica/8.0/SystemFiles/Links/JLink", | |
"/Applications/Mathematica.app/SystemFiles/Links/JLink" | |
].find{ |f| File.exist?(f) } | |
def connect | |
Java::JavaLang::System.setProperty("com.wolfram.jlink.libdir", @@jlink_libdir) | |
@kernel_link = Java::ComWolframJlink::MathLinkFactory.createKernelLink(kernel_link_args) | |
if @debug | |
@kernel_link.addPacketListener(packet_printer) | |
end | |
@timeout ||= 5000 | |
@kernel_link.connect(@timeout) | |
@kernel_link.discardAnswer | |
end | |
def disconnect | |
@kernel_link.close if @kernel_link | |
end | |
def terminate | |
@kernel_link.terminateKernel if @kernel_link | |
end | |
def eval(str) | |
result = @kernel_link.evaluateToOutputForm(str,0) | |
e = @kernel_link.getLastError | |
raise e if e | |
return result | |
end | |
def debug=(bool) | |
@debug = bool | |
if @kernel_link | |
if @debug | |
@kernel_link.addPacketListener(packet_printer) | |
else | |
@kernel_link.removePacketListener(packet_printer) | |
end | |
end | |
end | |
private | |
def packet_printer | |
@packet_printer ||= Java::ComWolframJlink::PacketPrinter.new(Java::JavaLang::System.out) | |
end | |
def kernel_link_args | |
"-linkmode launch -linkname '#{@@math_kernel_path} -mathlink'" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mathlink' | |
describe Mathlink do | |
before do | |
subject.debug = true if ENV["DEBUG"] | |
subject.connect | |
end | |
after do | |
subject.disconnect | |
end | |
it "does basic arithmatic" do | |
subject.eval("2+2").should == "4" | |
end | |
it "can access the kernel link directly" do | |
subject.kernel_link.evaluate("5+5") | |
subject.kernel_link.waitForAnswer | |
# TODO: | |
# There are a set of int constants representing next data type coming through the link | |
# This could be used to dynamically pull the next value of any type by mapping getter | |
# methods to int constants | |
subject.kernel_link.getNext.should == 43 | |
subject.kernel_link.getInteger.should == 10 | |
end | |
it "doesn't work after disconnect" do | |
subject.disconnect | |
expect { subject.eval("3+3") }.to raise_error Java::ComWolframJlink::MathLinkException | |
end | |
it "doesn't work after termination" do | |
subject.terminate | |
expect { subject.eval("3+3") }.to raise_error Java::ComWolframJlink::MathLinkException | |
end | |
it "does divide by zero" do | |
subject.eval("5/0").should == "ComplexInfinity" | |
end | |
it "bubbles up thrown exceptions with evaluated code" do | |
expect { subject.eval("Throw[\"Kamehameha!\"]") }.to raise_error Java::ComWolframJlink::MathLinkException | |
end | |
it "doesn't rethrow caught exceptions" do | |
subject.eval( "Catch[a; b; Throw[c]; d; e]" ).should == 'c' | |
end | |
it "allows debugging to STDOUT" do | |
pending "Would have to capture STDOUT in order to verify" | |
subject.debug = true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment