[LutinJ2R-commits] r67 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r/jni test/java/org/codelutin/j2r
Author: jcouteau Date: 2009-04-28 16:25:41 +0000 (Tue, 28 Apr 2009) New Revision: 67 Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java Log: Adding Boolean management to JRIEngine Fixed Integer tests Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-28 15:42:01 UTC (rev 66) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-28 16:25:41 UTC (rev 67) @@ -26,7 +26,6 @@ import org.rosuda.JRI.REXP; import org.rosuda.JRI.Rengine; - /** * RJniEngine.java * @@ -39,212 +38,240 @@ */ public class RJniEngine implements REngine { - private Log log = LogFactory.getLog(RJniEngine.class); + private Log log = LogFactory.getLog(RJniEngine.class); - /** - * Le Rengine est fait pour tourner en static - */ - private static Rengine engine; + /** + * Le Rengine est fait pour tourner en static + */ + private static Rengine engine; - /* - * (non-Javadoc) - * - * @see org.codelutin.j2r.REngine#init() - */ - public boolean init() { - if (engine == null) { - try { - String[] args = { "--no-save" }; - engine = new Rengine(args, false, null); - if (!engine.waitForR()) { - if (log.isErrorEnabled()) { - log.error("Cannot load the R engine"); - } - return false; - } - } catch (Throwable twable) { - log.error("An error occured during R/JNI initialization.", - twable); - return false; - } - } - return true; - } + /* + * (non-Javadoc) + * + * @see org.codelutin.j2r.REngine#init() + */ + public boolean init() { + if (engine == null) { + try { + String[] args = { "--no-save" }; + engine = new Rengine(args, false, null); + if (!engine.waitForR()) { + if (log.isErrorEnabled()) { + log.error("Cannot load the R engine"); + } + return false; + } + } catch (Throwable twable) { + log.error("An error occured during R/JNI initialization.", + twable); + return false; + } + } + return true; + } - /* - * (non-Javadoc) - * - * @see org.codelutin.R.REngine#eval(java.lang.String) - */ - public Object eval(String expr) throws RException { - REXP result = null; - try { - log.info(expr); - result = engine.eval(expr); - } catch (Exception eee) { - throw new RException("Unable to evaluate the R expression " - + "over JNI", eee); - } - return convertResult(result); - } + /* + * (non-Javadoc) + * + * @see org.codelutin.R.REngine#eval(java.lang.String) + */ + public Object eval(String expr) throws RException { + REXP result = null; + try { + log.info(expr); + result = engine.eval(expr); + } catch (Exception eee) { + throw new RException("Unable to evaluate the R expression " + + "over JNI", eee); + } + return convertResult(result); + } - private Object convertResult(REXP rexp) { - if (rexp == null) { - log.debug("Null returned"); - return null; - } - if (log.isDebugEnabled()) { - log.debug("Converting : " + rexp.toString()); - } - int type = rexp.getType(); - Object result = null; - switch (type) { - case REXP.XT_STR: - result = rexp.asString(); - break; - /*case REXP.XT_INT: + private Object convertResult(REXP rexp) { + if (rexp == null) { + log.debug("Null returned"); + return null; + } + if (log.isDebugEnabled()) { + log.debug("Converting : " + rexp.toString()); + } + int type = rexp.getType(); + Object result = null; + switch (type) { + case REXP.XT_STR: + result = rexp.asString(); + break; + case REXP.XT_INT: result = (Integer)rexp.asInt(); - break;*/ - case REXP.XT_ARRAY_INT: + break; + case REXP.XT_ARRAY_INT: result = rexp.asIntArray(); int[] intarray = (int[]) result; if (intarray.length == 1) { - result = (Integer)intarray[0]; + result = (Integer) intarray[0]; } break; - case REXP.XT_ARRAY_DOUBLE: - result = rexp.asDoubleArray(); - double[] doublearray = (double[]) result; - if (doublearray.length == 1) { - result = doublearray[0]; - } - break; - case REXP.XT_BOOL: - result = rexp.asBool().isTRUE(); - break; - case REXP.XT_DOUBLE: - result = rexp.asDoubleArray(); - double[] doublearray2 = (double[]) result; - result = doublearray2[0]; - /*case REXP.XT_NULL: - result = null;*/ - /*case REXP.XT_ARRAY_BOOL: - result = rexp.asIntArray(); - boolean[] booleanarray = (boolean[]) result; - result = booleanarray;*/ - case REXP.XT_VECTOR: - result = rexp.asVector(); - default: - log.error("Unknown return type [" + type + "] " + "on : " - + rexp.toString()); - } - return result; - } + case REXP.XT_ARRAY_DOUBLE: + result = rexp.asDoubleArray(); + double[] doublearray = (double[]) result; + if (doublearray.length == 1) { + result = doublearray[0]; + } + break; + case REXP.XT_BOOL: + result = rexp.asBool().isTRUE(); + log.info(result); + break; + case REXP.XT_DOUBLE: + result = rexp.asDoubleArray(); + double[] doublearray2 = (double[]) result; + result = doublearray2[0]; + break; + case REXP.XT_NULL: + result = null; + break; + case 37: + result = rexp.asIntArray(); + int[] integers = ((int[]) result); + Boolean[] booleanArray = new Boolean[integers.length]; + for (int i = 0; i < integers.length; i++) { + if (integers[i] == 1) { + booleanArray[i] = Boolean.TRUE; + } else { + booleanArray[i] = Boolean.FALSE; + } + } + if (booleanArray.length == 1) { + result = booleanArray[0]; + } else { + result = booleanArray; + } - /* - * (non-Javadoc) - * - * @see org.codelutin.j2r.REngine#terminate() - */ - public void terminate() { - if (engine.isAlive()) { - engine.end(); - } - } + break; + case REXP.XT_VECTOR: + result = rexp.asVector(); + break; + default: + log.error("Unknown return type [" + type + "] " + "on : " + + rexp.toString()); + break; + } + return result; + } - /* - * (non-Javadoc) - * - * @see org.codelutin.j2r.REngine#voidEval(java.lang.String) - */ - public void voidEval(String expr) throws RException { - // voidEval is not really supproted by JRI, we just discard the result - // conversion - try { - log.info(expr); - engine.eval(expr); - } catch (Exception eee) { - throw new RException("An error occured while voidEval on JNI", eee); - } - } - - - /** - * Load .RData file located in directory - * - * @param directory directory where the .RData file is located - * @throws RException - */ - public void loadRData(File directory) throws RException { - setwd(directory); + /* + * (non-Javadoc) + * + * @see org.codelutin.j2r.REngine#terminate() + */ + public void terminate() { + if (engine.isAlive()) { + engine.end(); + } + } + + /* + * (non-Javadoc) + * + * @see org.codelutin.j2r.REngine#voidEval(java.lang.String) + */ + public void voidEval(String expr) throws RException { + // voidEval is not really supproted by JRI, we just discard the result + // conversion + try { + log.info(expr); + engine.eval(expr); + } catch (Exception eee) { + throw new RException("An error occured while voidEval on JNI", eee); + } + } + + /** + * Load .RData file located in directory + * + * @param directory + * directory where the .RData file is located + * @throws RException + */ + public void loadRData(File directory) throws RException { + setwd(directory); voidEval("load(\".RData\")"); - } - - /** - * Save the session in a .RData file in directory - * - * @param directory where the .RData file will be saved - * @throws RException - */ - public void saveRData(File directory) throws RException{ - setwd(directory); + } + + /** + * Save the session in a .RData file in directory + * + * @param directory + * where the .RData file will be saved + * @throws RException + */ + public void saveRData(File directory) throws RException { + setwd(directory); voidEval("save.image()"); - } - - /** - * Set the R working directory - * @param directory to set - * @throws RException - */ - public void setwd(File directory) throws RException { - voidEval("setwd(\""+directory.getAbsolutePath() + "\")"); - } - - /** - * Get the actual R session working directory - * - * @return a File that is the actual R session working directory - * @throws RException - */ - + } + + /** + * Set the R working directory + * + * @param directory + * to set + * @throws RException + */ + public void setwd(File directory) throws RException { + voidEval("setwd(\"" + directory.getAbsolutePath() + "\")"); + } + + /** + * Get the actual R session working directory + * + * @return a File that is the actual R session working directory + * @throws RException + */ + public File getwd() throws RException { - String directory = (String)eval("getwd()"); + String directory = (String) eval("getwd()"); return new File(directory); } - + /** * Use the dput R instruction to store the content of a R object to a file. * The file created will be in the working directory * - * @param rObject name of the R object to save - * @param outputFileName name of the file to save + * @param rObject + * name of the R object to save + * @param outputFileName + * name of the file to save * @throws RException */ public void dput(String rObject, String outputFileName) throws RException { String rInstruction = "dput(%s,file=\"%s\")"; voidEval(String.format(rInstruction, rObject, outputFileName)); } - - + /** - * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. - * The file used have to be in the working directory + * Use the dget rInstruction to store the content of a file (created with + * the dput instruction) into a R object. The file used have to be in the + * working directory * - * @param rObject name of the R object created - * @param inputFileName name of the file to load + * @param rObject + * name of the R object created + * @param inputFileName + * name of the file to load * @throws RException */ public void dget(String rObject, String inputFileName) throws RException { String rInstruction = "%s <- dget(\"%s\")"; voidEval(String.format(rInstruction, rObject, inputFileName)); - + } - + /** * Use the dput R instruction to store the content of a R object to a file. * - * @param rObject R object to save - * @param outputFileName the file to save + * @param rObject + * R object to save + * @param outputFileName + * the file to save * @throws RException */ public void dput(String rObject, File outputFile) throws RException { @@ -254,13 +281,15 @@ voidEval(String.format(rInstruction, rObject, outputFile.getName())); setwd(workingdir); } - - + /** - * Use the dget rInstruction to store the content of a file (created with the dput instruction) into a R object. + * Use the dget rInstruction to store the content of a file (created with + * the dput instruction) into a R object. * - * @param rObject name of the R object created - * @param inputFile file to load + * @param rObject + * name of the R object created + * @param inputFile + * file to load * @throws RException */ public void dget(String rObject, File inputFile) throws RException { @@ -268,11 +297,11 @@ setwd(inputFile.getParentFile()); String rInstruction = "%s <- dget(\"%s\")"; voidEval(String.format(rInstruction, rObject, inputFile.getName())); - setwd(workingdir); + setwd(workingdir); } - - public void remove (String rObject) throws RException { - voidEval("remove("+rObject+")"); + + public void remove(String rObject) throws RException { + voidEval("remove(" + rObject + ")"); } } // RJniEngine Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 15:42:01 UTC (rev 66) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 16:25:41 UTC (rev 67) @@ -84,17 +84,6 @@ } @Test - public void testIntArray() throws Exception { - Object result = engine.eval("5:10"); - Assert.assertEquals(int[].class, result.getClass()); - int[] intArray = (int[]) result; - Assert.assertEquals(6, intArray.length); - for (int i = 5; i < 11; i++) { - Assert.assertEquals(i, intArray[i - 5]); - } - } - - @Test public void testDoubleArray() throws Exception { Object result = engine.eval("5.5:10.5"); Assert.assertEquals(double[].class, result.getClass()); @@ -150,15 +139,27 @@ Assert.assertEquals("testing string", testString); } - /*@Test + @Test public void testInt() throws Exception { - engine.voidEval("a<-5"); + engine.voidEval("a<-as.integer(5)"); Integer testInteger = (Integer) engine.eval("a"); Integer toCompare = 5; Assert.assertEquals(toCompare, testInteger); - }*/ + } + + @Test + public void testIntArray() throws Exception { + engine.voidEval("a<-as.integer(5)"); + engine.voidEval("b<-as.integer(6)"); + engine.voidEval("z<-c(a,b)"); + int[] testIntArray = (int[]) engine.eval("z"); + int toCompare1 = 5; + Assert.assertEquals(toCompare1, testIntArray[0]); + int toCompare2 = 6; + Assert.assertEquals(toCompare2, testIntArray[1]); + } - /* @Test + @Test public void testBool() throws Exception { engine.voidEval("a<-TRUE"); engine.voidEval("b<-FALSE"); @@ -166,16 +167,16 @@ Boolean testB = (Boolean) engine.eval("b"); Assert.assertTrue(testA); Assert.assertFalse(testB); - }*/ + } - /* @Test + @Test public void testArrayBool() throws Exception { engine.voidEval("a<-c(TRUE,FALSE,TRUE)"); Boolean[] testBoolArray = (Boolean[]) engine.eval("a"); Assert.assertTrue(testBoolArray[0]); Assert.assertFalse(testBoolArray[1]); Assert.assertTrue(testBoolArray[2]); - }*/ + } /*@Test public void testDataFrame() throws Exception {
participants (1)
-
jcouteau@users.labs.libre-entreprise.org