Skip to content

Commit

Permalink
TryCatch disable by default, 4.0.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Kopilov committed Aug 12, 2021
1 parent 6b884a1 commit 6953116
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion RCaller/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion RCaller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.github.jbytecode</groupId>
<artifactId>RCaller</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0</version>
<packaging>jar</packaging>

<name>RCaller</name>
Expand Down
4 changes: 2 additions & 2 deletions RCaller/src/main/java/benchmark/PassingArraysAndMatrices.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand All @@ -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;

Expand Down
27 changes: 26 additions & 1 deletion RCaller/src/main/java/com/github/rcaller/rstuff/RCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions RCaller/src/main/java/com/github/rcaller/rstuff/RCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion RCaller/src/main/resources/arrow_bridge.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions RCaller/src/test/java/com/github/rcaller/RunOnlineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit 6953116

Please sign in to comment.