Skip to content

Commit

Permalink
Refactoring (remove r2js package for backward compatibility) and fix …
Browse files Browse the repository at this point in the history
…getting resources from external program.
  • Loading branch information
Nicolas CHABALIER committed Dec 19, 2023
1 parent ecc96fe commit 8379c5c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.math.R.R2js;
package org.math.R;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -13,7 +13,6 @@
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.math.R.*;

/**
* This class evaluate an R expression by parsing it in javascript expression
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package org.math.R.R2js;
package org.math.R;

import org.graalvm.polyglot.*;
import org.math.R.RLog;
import org.math.R.RLogPrintStream;

import java.io.File;
import java.io.PrintStream;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class R2jsSession extends AbstractR2jsSession {
Expand Down Expand Up @@ -58,19 +56,29 @@ protected void putVariable(String varname, Object var) {
context.getBindings("js").putMember(varname, var);
}

private static StringBuilder resourceToStringBuilder(String resource) {
InputStream inputStream = R2jsSession.class.getResourceAsStream(MATH_JS_FILE);
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return textBuilder;
}

protected synchronized void loadJSLibraries() throws Exception {
if(MATH_SOURCE == null) {
URL math_url = getClass().getResource(MATH_JS_FILE);
URL rand_url = getClass().getResource(RAND_JS_FILE);
URL r_url = getClass().getResource(R_JS_FILE);
if (math_url == null || rand_url == null || r_url == null) {
throw new IllegalArgumentException("file not found!");
} else {
MATH_SOURCE = Source.newBuilder("js",new File(math_url.toURI())).build();
RAND_SOURCE = Source.newBuilder("js",new File(rand_url.toURI())).build();
R_SOURCE = Source.newBuilder("js",new File(r_url.toURI())).build();
}
Reader mathReader = new InputStreamReader(R2jsSession.class.getResourceAsStream(MATH_JS_FILE));
Reader randReader = new InputStreamReader(R2jsSession.class.getResourceAsStream(RAND_JS_FILE));
Reader rReader = new InputStreamReader(R2jsSession.class.getResourceAsStream(R_JS_FILE));

MATH_SOURCE = Source.newBuilder("js",mathReader, "math.js").build();
RAND_SOURCE = Source.newBuilder("js",randReader, "rand.js").build();
R_SOURCE = Source.newBuilder("js",rReader, "R.js").build();
}

this.context.eval(MATH_SOURCE);
Expand Down Expand Up @@ -230,61 +238,6 @@ public Object cast(Object o) throws ClassCastException {
} catch (Exception e) {
}
return o;


// return o;
//// // If it's a ScriptObjectMirror, it can be an array or a matrix
// if (o instanceof Integer) {
// return Double.valueOf((int) o);
// } else if (o instanceof ScriptObjectMirror) {
// try {
//// System.err.println("// Casting of the ScriptObjectMirror to a double matrix");
// return ((ScriptObjectMirror) o).to(double[][].class);
// } catch (Exception e) {//e.printStackTrace();
// }
//
// try {
//// System.err.println("// Casting of the ScriptObjectMirror to a string array");
// String[] stringArray = ((ScriptObjectMirror) o).to(String[].class);
//
//// System.err.println("// Check if the String[] array can be cast to a double[] array");
// try {
// for (String string : stringArray) {
// Double.valueOf(string);
// }
// } catch (Exception e) {//e.printStackTrace();
// // It can't be cast to double[] so we return String[]
// return stringArray;
// }
//
//// System.err.println("// return double[] array");
// return ((ScriptObjectMirror) o).to(double[].class);
//
// } catch (Exception e) {//e.printStackTrace();
// }
//
// try {
//// System.err.println("// Casting of the ScriptObjectMirror to a double array");
// return ((ScriptObjectMirror) o).to(double[].class);
// } catch (Exception e) {//e.printStackTrace();
// }
//
// try {
//// System.err.println(" // Casting of the ScriptObjectMirror to a list/map");
// Map m = ((ScriptObjectMirror) o).to(Map.class);
// try {
// return asMatrix(m);
// } catch (ClassCastException c) {
// //c.printStackTrace();
// return m;
// }
// } catch (Exception e) {//e.printStackTrace();
// }
//
// throw new IllegalArgumentException("Impossible to cast object: ScriptObjectMirror");
// } else {
// return o;
// }
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.math.R.R2js;
package org.math.R;

import java.util.ArrayList;
import java.util.List;
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/math/R/BasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.math.R.R2js.AbstractR2jsSession;
import org.math.R.R2js.R2jsSession;

/**
*
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/math/R/BrentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.math.R.R2js.R2jsSession;
import org.math.R.R2js.AbstractR2jsSession;
import org.math.array.DoubleArray;
import org.math.array.LinearAlgebra;

Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/math/R/GradientDescent1DTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.math.R.R2js.AbstractR2jsSession;
import org.math.R.R2js.R2jsSession;
import org.math.R.Rsession.RException;
import org.math.array.DoubleArray;

Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/math/R/GradientDescentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.math.R.R2js.AbstractR2jsSession;
import org.math.R.R2js.R2jsSession;
import org.math.R.Rsession.RException;
import org.math.array.DoubleArray;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.math.R.R2js;
package org.math.R;

import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.math.R.AbstractR2jsSession;
import org.math.R.R2jsUtils;
import org.math.R.Rsession;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.math.R.R2js;
package org.math.R;

import java.io.*;
import java.net.InetAddress;
Expand All @@ -16,8 +16,6 @@
//import org.graalvm.polyglot.*;

import org.junit.Test;
import org.math.R.RLog;
import org.math.R.RLogSlf4j;
import org.math.R.Rsession.RException;

import static org.junit.Assert.*;
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/org/math/R/RPanelsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.math.R.R2js.AbstractR2jsSession;
import org.math.R.R2js.R2jsSession;
import org.math.R.Rsession.RException;

/**
Expand Down

0 comments on commit 8379c5c

Please sign in to comment.