From 6953116b797972ab3c5042060e0f7ceca0626faa Mon Sep 17 00:00:00 2001 From: Kopilov Aleksandr Date: Thu, 12 Aug 2021 19:25:48 +0300 Subject: [PATCH] TryCatch disable by default, 4.0.0 release --- RCaller/build.gradle | 2 +- RCaller/pom.xml | 2 +- .../benchmark/PassingArraysAndMatrices.java | 4 +-- .../com/github/rcaller/rstuff/RCaller.java | 27 ++++++++++++++++++- .../java/com/github/rcaller/rstuff/RCode.java | 4 +-- RCaller/src/main/resources/arrow_bridge.R | 1 - .../com/github/rcaller/RunOnlineTest.java | 4 +-- 7 files changed, 34 insertions(+), 10 deletions(-) diff --git a/RCaller/build.gradle b/RCaller/build.gradle index ff0694d..2c5f931 100644 --- a/RCaller/build.gradle +++ b/RCaller/build.gradle @@ -8,7 +8,7 @@ repositories { } group = 'com.github.jbytecode' -version = '4.0.0-SNAPSHOT' +version = '4.0.0' description = 'RCaller is a software library which simplifies performing data analysis and statistical calculations in Java using R. The details are hidden from users including transferring data between platforms, function calls, and retrieving results.' dependencies { diff --git a/RCaller/pom.xml b/RCaller/pom.xml index 1a30cee..b1ffc2c 100644 --- a/RCaller/pom.xml +++ b/RCaller/pom.xml @@ -3,7 +3,7 @@ com.github.jbytecode RCaller - 4.0.0-SNAPSHOT + 4.0.0 jar RCaller diff --git a/RCaller/src/main/java/benchmark/PassingArraysAndMatrices.java b/RCaller/src/main/java/benchmark/PassingArraysAndMatrices.java index 5b21a65..97201ab 100644 --- a/RCaller/src/main/java/benchmark/PassingArraysAndMatrices.java +++ b/RCaller/src/main/java/benchmark/PassingArraysAndMatrices.java @@ -53,7 +53,7 @@ private final static double[][] generateRandomMatrix(int n, int m) { public static void main(String[] args) { - performSimulation(/* size of vector */ 10, 10 /* times */ , SimType.Vector); + performSimulation(/* size of vector */ 2, 100 /* times */ , SimType.Vector); } @@ -77,7 +77,7 @@ public static void performSimulation(int VectorSize, int SimCount, SimType type) code.addRCode("result <- t(randommatrix)"); } caller.setRCode(code); - caller.runAndReturnResultOnline("result"); + caller.runAndReturnResultOnline("result", false); /* return variable is not handled */ caller.getParser().getAsDoubleArray("result"); elapsed[simulations] = (int) System.currentTimeMillis() - timeStart; diff --git a/RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java b/RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java index fbb5f60..5a4d310 100644 --- a/RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java +++ b/RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java @@ -278,11 +278,30 @@ private void runRCode() throws ExecutionException { * re-used by invoking this method again. When you are done with this * process, you must explicitly stop it. * + * If R raises an error, it is ignored. For throwing them in Java, use + * {@link #runAndReturnResultOnline(String, boolean)} with addTryCatch=true + * * @see #stopStreamConsumers() * @param var The R variable to return * @throws ExecutionException if R cannot be started */ public void runAndReturnResultOnline(String var) throws ExecutionException { + runAndReturnResultOnline(var, false); + } + + /** + * Runs the current code in the existing R instance (or in a new one) and + * returns the R variable "var". The R process is kept alive and can be + * re-used by invoking this method again. When you are done with this + * process, you must explicitly stop it. + * + * @since 4.0.0 + * @see #stopStreamConsumers() + * @param var The R variable to return + * @param addTryCatch wrap original R code to tryCatch function (can impact performance) + * @throws ExecutionException if R cannot be started or raise error inside while addTryCatch parameter is true + */ + public void runAndReturnResultOnline(String var, boolean addTryCatch) throws ExecutionException { rCallerOptions.resetRetries(); boolean done = false; do { @@ -323,7 +342,13 @@ public void runAndReturnResultOnline(String var) throws ExecutionException { } try { - var script = rCode.toTryCatchScript(errorFile) + rCode.createEndSignalCode(resultReadyControlFile); + String script; + if (addTryCatch) { + script = rCode.toTryCatchScript(errorFile); + } else { + script = rCode.toString(); + } + script += rCode.createEndSignalCode(resultReadyControlFile); rInput.write(script.getBytes(Globals.standardCharset)); rInput.flush(); } catch (IOException e) { diff --git a/RCaller/src/main/java/com/github/rcaller/rstuff/RCode.java b/RCaller/src/main/java/com/github/rcaller/rstuff/RCode.java index 4577659..c4e7009 100644 --- a/RCaller/src/main/java/com/github/rcaller/rstuff/RCode.java +++ b/RCaller/src/main/java/com/github/rcaller/rstuff/RCode.java @@ -267,8 +267,8 @@ public String toString() { /** * Wrap current code to standard tryCatch function. * Error handler saves details to errorOutputFile if the error occurs. - * @param errorOutputFile - * @return + * @param errorOutputFile file to save error if it occurs + * @return generated script to be evaluated */ String toTryCatchScript(File errorOutputFile) { //Using code snippet "An improved “error handler”" with withCallingHandlers nested in tryCatch diff --git a/RCaller/src/main/resources/arrow_bridge.R b/RCaller/src/main/resources/arrow_bridge.R index 465a2e1..7d5fc0f 100644 --- a/RCaller/src/main/resources/arrow_bridge.R +++ b/RCaller/src/main/resources/arrow_bridge.R @@ -28,7 +28,6 @@ send_element_by_arrow <- function(obj, name, stream) { if (length(dim(obj)) > 2) { #3- and more-D arrays are not supported stop(paste(length(dim(obj)), "-D arrays are not supported")) - #TODO add try-catch support on toplevel } #1-D array and empty matrix can be converted to Vector dim(obj) <- c() diff --git a/RCaller/src/test/java/com/github/rcaller/RunOnlineTest.java b/RCaller/src/test/java/com/github/rcaller/RunOnlineTest.java index 91fee76..b2d4c8a 100644 --- a/RCaller/src/test/java/com/github/rcaller/RunOnlineTest.java +++ b/RCaller/src/test/java/com/github/rcaller/RunOnlineTest.java @@ -216,7 +216,7 @@ public void exceptionCatchTest() { RCaller rcaller = RCaller.create(); RCode code = rcaller.getRCode(); code.addRCode("a <- log(\"not a number\")"); - rcaller.runAndReturnResultOnline("a"); + rcaller.runAndReturnResultOnline("a", true); } @Test @@ -234,7 +234,7 @@ public void rHaltedTest() { rcaller.runAndReturnResultOnline("a"); } catch (ExecutionException ex) { Logger.getLogger(RunOnlineTest.class.getName()).log(Level.SEVERE, ex.getMessage()); - if (ex.getMessage().contains("R code throw an error:")) { + if (ex.getMessage().contains("R process died, stderr:")) { exceptionThrown = true; } rcaller.stopRCallerOnline();