JRI- . , JRI JVM, .
:
// Start Rengine.
Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);
. , JVM .
, JRI (eval, assign ..), , , eval.
, JRI Rserve, JRI, JVM, concurrency ( RConnection).
Rserve , JRI.
@PostConstruct:
@PostConstruct
public void setUpR() {
REngine engine = null;
try {
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Starting RServe process...");
}
ProcessBuilder builder = new ProcessBuilder("/bin/sh", "-c", String.format("echo 'library(Rserve);Rserve(FALSE,args=\"--no-save --slave --RS-conf %s\")'|%s --no-save --slave", rserveConf, rexe));
builder.inheritIO();
Process rProcess = builder.start();
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Waiting for Rserve to start...");
}
int execCodeResult = rProcess.waitFor();
if(execCodeResult != SUCCESS_CODE) {
LOGGER.error(String.format("Unexpected error code starting RServe: %d", execCodeResult));
} else {
LOGGER.error("RServe started successfully");
}
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Opening connection to RServe daemon....");
}
engine = new RConnection();
if(LOGGER.isInfoEnabled()) {
LOGGER.info(String.format("Obtaining R server version: %d", ((RConnection)engine).getServerVersion()));
}
} catch(Exception e) {
LOGGER.error("Unexpected error setting up RServe environment", e);
} finally {
closeRServeConnection(engine);
}
}
JRI:
@PostConstruct
public void setUpR() {
try {
System.loadLibrary("jri");
if (!Rengine.versionCheck()) {
LOGGER.error("** Version mismatch - Java files don't match library version.");
LOGGER.error(String.format("Invalid versions. Rengine must have the same version of native library. Rengine version: %d. RNI library version: %d", Rengine.getVersion(), Rengine.rniGetVersion()));
}
Rengine.DEBUG = 1;
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Creating Rengine (with arguments)");
}
this.engine = REngine.engineForClass("org.rosuda.REngine.JRI.JRIEngine", new String[] { "--no-save" }, new REngineStdOutCallback(LOGGER), false);
if(LOGGER.isInfoEnabled()) {
LOGGER.info("Rengine created...");
LOGGER.info("Loading blockFunction from " + getBlockFunction());
}
REXP result = engine.parseAndEval(getBlockFunction());
if(result == null) {
LOGGER.error("blockFunction is not loaded!");
} else if(LOGGER.isInfoEnabled()) {
LOGGER.info("blockFunction loaded successfully");
}
} catch(Exception|UnsatisfiedLinkError e) {
LOGGER.error("Unexpected error setting up R", e);
}
}
Rengine (, ).
repo