Code Monkey home page Code Monkey logo

juliacaller's Introduction

Build Status

JuliaCaller

A library for calling Julia from Java.

Initials

JuliaCaller creates a TCP server that listens on a special port in Julia side. This server executes Julia statements and expressions that sent from the Java side. The result is then handled in Java side as primitives, JSONObjects and JSONArrays.

First things first!

Don't forget to install the JSON package in your Julia environment before running JuliaCaller. JuliaCaller first tries to install the JSON package when its first use, however, this may take time and the maximum number of tries of connection may be exceed.

]add JSON

or

julia> using Pkg
julia> Pkg.add("JSON")

javax.script interface

JuliaCaller implements javax.script interface, that is, it can be used as a scripting engine in Java.

Examples

Here is the section of examples. For now, we have only tests in the source code. Please have a look at the test folder.

Getting primitives

This example create a scripting environment for Julia. The statement 'a = 3' is sent to Julia and the result is handled from Java.

Constants.setProperties(Constants.JULIA_PATH, "/usr/local/bin/julia");
Constants.setProperties(Constants.JULIA_PORT, "8001");

// Creating a scripting interface for Julia
manager = new ScriptEngineManager();
engine = manager.getEngineByName("Julia");

// Sending command 'a = 3' to Julia from Java
engine.eval("a = 3");

// Handling the result in Java
Object a = engine.get("a");

Getting arrays

Constants.setProperties(Constants.JULIA_PATH, "/usr/local/bin/julia");
Constants.setProperties(Constants.JULIA_PORT, "8001");

// Creating a scripting interface for Julia
manager = new ScriptEngineManager();
engine = manager.getEngineByName("Julia");

// Creating array in Julia
engine.eval("a = [1,2,3]");

// Handling result in Java
Object result = engine.get("a");

assertTrue(result instanceof JSONArray);

JSONArray arr = (JSONArray) result;
assertEquals(1, arr.getInt(0));
assertEquals(2, arr.getInt(1));
assertEquals(3, arr.getInt(2));

Function calls

Constants.setProperties(Constants.JULIA_PATH, "/usr/local/bin/julia");
Constants.setProperties(Constants.JULIA_PORT, "8001");

// Creating a scripting interface for Julia
manager = new ScriptEngineManager();
engine = manager.getEngineByName("Julia");

// Creating array in Julia
engine.eval("a = [1,2,3]");
engine.eval("f(x) = sum(x) / length(x)")
engine.eval("b = f(a)")

// Handling result in Java
Object result = engine.get("b");

Invocable interface

Constants.setProperties(Constants.JULIA_PATH, "/usr/local/bin/julia");
Constants.setProperties(Constants.JULIA_PORT, "8001");

// Creating a scripting interface for Julia
manager = new ScriptEngineManager();
engine = manager.getEngineByName("Julia");

Invocable invocable = (Invocable)engine;
engine.eval("using Statistics");
engine.eval("a = [1.0, 2.0, 3.0]");

// The result is average of 1.0, 2.0, and 3.0
// result = 2.0
Object result = invocable.invokeFunction("mean", "a");

Using JuliaCaller without javax.script interface

caller = new JuliaCaller("/usr/local/bin/julia", 8000);
caller.startServer();
caller.Connect();

caller.Execute("mypi = 3.14159265");
JSONObject obj = caller.GetAsJSONObject("mypi");
// Assertation is true
assertEquals(obj.getDouble("mypi"), 3.14159265);

// Creating object 'myarray' in Julia
caller.Execute("myarray = [1,2,3,4,5]");

// Getting the array back in Java
JSONArray obj = caller.GetAsJSONArray("myarray");

// Assertations are true
assertEquals(obj.length(), 5);
assertEquals(obj.getInt(0), 1);
assertEquals(obj.getInt(1), 2);
assertEquals(obj.getInt(2), 3);
assertEquals(obj.getInt(3), 4);
assertEquals(obj.getInt(4), 5);

caller.ShutdownServer();

Easy passing of objects

List<Double> values = List.of(1.0, 2.0, 10.0, -4.0);
caller.addJuliaObject(JuliaObject.createArrayVariable("a", values));
caller.Execute("using Statistics");
caller.Execute("ave = mean(a)");
double result = caller.getDouble("ave");
assertEquals(2.25, result);

Note that the library is in its early development stage.

juliacaller's People

Contributors

dependabot[bot] avatar jbytecode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lvyitian subes

juliacaller's Issues

fork with some fixes

Hi,

thanks a lot for this library. I have forked it and rewritten/fixed some parts here: https://github.com/invesdwin/invesdwin-context-julia/tree/main/invesdwin-context-julia-parent/invesdwin-context-julia-runtime-juliacaller/src/main/java/de/invesdwin/context/julia/runtime/juliacaller/pool

  • use begin/end to support multi line commands
    • need to escape newlines when sending through the socket
  • Pkg.installed is deprecated, I have implemented a workaround using Pkg.dependencies()
  • there was no way to read the stdout from the java side
  • there was no way to react to errors (stderr) from the java side
    • though the current solution can not differentiate between warnings (e.g. deprecation) and errors sadly
  • replaced org.json with jackson (better handling of array vs object)
  • make it recover from more error cases (e.g. errors during "get" operations and don't close during errors in "exec" operations)

If you like you can have a look and backport some fixes. The fork also includes a testsuite for writing/reading various data types (including vectors and matrices).
Though I guess https://github.com/org-arl/jajub could be a more sophisticated solution since it seems to support binary transport and does not require a socket. But still, it is good to have options to choose between. Using json/socket based communication might be more robust long term, despite being slower.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.