Author: jcouteau Date: 2009-04-07 09:35:53 +0000 (Tue, 07 Apr 2009) New Revision: 54 Modified: lutinj2r/trunk/changelog.txt lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java Log: Add handled types, add tests Modified: lutinj2r/trunk/changelog.txt =================================================================== --- lutinj2r/trunk/changelog.txt 2009-02-27 17:52:39 UTC (rev 53) +++ lutinj2r/trunk/changelog.txt 2009-04-07 09:35:53 UTC (rev 54) @@ -1,5 +1,8 @@ lutinj2r (0.3) unstable; urgency=low * use lutinproject 3.4 + * use JRI 0.7 + * use Rserve 0.6 + * increase the number or R expressions handled lutinj2r (0.2) unstable; urgency=low Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-02-27 17:52:39 UTC (rev 53) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-07 09:35:53 UTC (rev 54) @@ -24,118 +24,145 @@ import org.rosuda.JRI.REXP; import org.rosuda.JRI.Rengine; + /** * RJniEngine.java - * + * * Created: 22 aout 2006 - * + * * @author Arnaud Thimel <thimel@codelutin.com> * @version $Revision: $ - * - * Mise a jour: $Date: $ - * par : $Author: $ + * + * Mise a jour: $Date: $ par : $Author: $ */ 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 { - 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 { + 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_ARRAY_DOUBLE: - result = rexp.asDoubleArray(); - double[] doublearray = (double[]) result; - if (doublearray.length == 1) { - result = doublearray[0]; - } - break; - case REXP.XT_ARRAY_INT: - result = rexp.asIntArray(); - int[] intarray = (int[]) result; - if (intarray.length == 1) { - result = intarray[0]; - } - break; - default: - log.error("Unknown return type [" + type + "] " + - "on : " + rexp.toString()); - } - return 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: + result = (Integer)rexp.asInt(); + break;*/ + case REXP.XT_ARRAY_INT: + result = rexp.asIntArray(); + int[] intarray = (int[]) result; + if (intarray.length == 1) { + 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; + } - /* (non-Javadoc) - * @see org.codelutin.j2r.REngine#terminate() - */ - public void terminate() { - if (engine.isAlive()) { - engine.end(); - } - } + /* + * (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 { - engine.eval(expr); - }catch (Exception eee) { - throw new RException("An error occured while voidEval on JNI", eee); - } - } + /* + * (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 { + engine.eval(expr); + } catch (Exception eee) { + throw new RException("An error occured while voidEval on JNI", eee); + } + } + } // RJniEngine Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java 2009-02-27 17:52:39 UTC (rev 53) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java 2009-04-07 09:35:53 UTC (rev 54) @@ -33,12 +33,12 @@ import org.apache.commons.logging.LogFactory; import org.codelutin.j2r.REngine; import org.codelutin.j2r.RException; +import org.rosuda.REngine.REXP; +import org.rosuda.REngine.REXPMismatchException; import org.rosuda.REngine.Rserve.RConnection; import org.rosuda.REngine.Rserve.RserveException; -import org.rosuda.REngine.REXP; -import org.rosuda.REngine.REXPInteger; -import org.rosuda.REngine.REXPMismatchException; + /** * Cette classe represente le moteur reseau pour acceder a R. Par defaut, il * essaye de se connecter a l'adresse 127.0.0.1 sur le port 6311. Cependant, il @@ -148,10 +148,6 @@ } } - else if (rexp.isString()) { - result = rexp.asString(); - } - else if (rexp.isFactor()) { result = rexp.asFactor(); } @@ -163,6 +159,24 @@ result = doublearray[0]; } } + + else if (rexp.isString()) { + result = rexp.asStrings(); + String[] stringArray = (String[]) result; + if (stringArray.length == 1) { + result = stringArray[0]; + } + + } + + else if (rexp.isLogical()) { + result = rexp.asStrings(); + Boolean[] stringArray = (Boolean[])result; + if (stringArray.length == 1) { + result = (Boolean)stringArray[0]; + } + else {result = (Boolean[])result;} + } else { log.error("Unknown return type on : " + rexp.toString()); Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-02-27 17:52:39 UTC (rev 53) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-07 09:35:53 UTC (rev 54) @@ -29,21 +29,38 @@ package org.codelutin.j2r; -import static org.codelutin.j2r.TestConstants.*; -import junit.framework.TestCase; +import static org.codelutin.j2r.TestConstants.S_NB_LOOPS; +import static org.codelutin.j2r.TestConstants.S_OP; +import static org.codelutin.j2r.TestConstants.S_T_MAX; +import static org.codelutin.j2r.TestConstants.V_MAX; +import static org.codelutin.j2r.TestConstants.V_NB_LOOPS; +import static org.codelutin.j2r.TestConstants.V_OP_A; +import static org.codelutin.j2r.TestConstants.V_OP_AB; +import static org.codelutin.j2r.TestConstants.V_OP_B; +import static org.codelutin.j2r.TestConstants.V_OP_R; +import java.util.ArrayList; +import java.util.List; +import java.util.Vector; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.rosuda.JRI.REXP; +import org.rosuda.JRI.RVector; -public class JNITest extends TestCase { +public class JNITest { private static Log log = LogFactory.getLog(JNITest.class); private REngine engine; private String savedRType; - @Override - protected void setUp() throws Exception { + @Before + public void setUp() throws Exception { LutinTimer init = new LutinTimer(); init.startTiming(); savedRType = System.getProperty("R.type", ""); @@ -56,40 +73,44 @@ } } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() throws Exception { engine.terminate(); System.setProperty("R.type", savedRType); } + @Test public void testDouble() throws Exception { - assertEquals(5.0, engine.eval("5.0")); + Assert.assertEquals(5.0, engine.eval("5.0")); engine.voidEval("t<-sin(0)"); - double d = (Double)engine.eval("t"); - assertEquals(0.0, d); + double d = (Double) engine.eval("t"); + Assert.assertEquals(0.0, d, 0); } + @Test public void testIntArray() throws Exception { Object result = engine.eval("5:10"); - assertEquals(int[].class, result.getClass()); + Assert.assertEquals(int[].class, result.getClass()); int[] intArray = (int[]) result; - assertEquals(6, intArray.length); + Assert.assertEquals(6, intArray.length); for (int i = 5; i < 11; i++) { - assertEquals(i, intArray[i - 5]); + Assert.assertEquals(i, intArray[i - 5]); } } + @Test public void testDoubleArray() throws Exception { Object result = engine.eval("5.5:10.5"); - assertEquals(double[].class, result.getClass()); + Assert.assertEquals(double[].class, result.getClass()); double[] doubleArray = (double[]) result; - assertEquals(6, doubleArray.length); + Assert.assertEquals(6, doubleArray.length); for (int i = 5; i < 11; i++) { double d = i + 0.5; - assertEquals(d, doubleArray[i - 5]); + Assert.assertEquals(d, doubleArray[i - 5], 0); } } + @Test public void testSimpleOp() throws Exception { LutinTimer t = new LutinTimer(); for (int loop = 0; loop < S_NB_LOOPS; loop++) { @@ -97,8 +118,8 @@ t.startTiming(); engine.voidEval(S_OP); t.endTiming(); - double d = (Double)engine.eval("t"); - assertEquals((double)S_T_MAX, d); + double d = (Double) engine.eval("t"); + Assert.assertEquals((double) S_T_MAX, d, 0); } double[] results = t.computeResults(); System.err.println("[SO]min: " + results[0]); @@ -107,6 +128,7 @@ System.err.println("[SO]etype: " + results[3]); } + @Test public void testVector() throws Exception { System.err.println(V_OP_R); LutinTimer t = new LutinTimer(); @@ -116,7 +138,7 @@ engine.voidEval(V_OP_B); double[] r = (double[]) engine.eval(V_OP_AB); t.endTiming(); - assertEquals(V_MAX, r.length); + Assert.assertEquals(V_MAX, r.length); } double[] results = t.computeResults(); System.err.println("[V]min: " + results[0]); @@ -125,4 +147,48 @@ System.err.println("[V]etype: " + results[3]); } -} // NetTest + @Test + public void testString() throws Exception { + engine.voidEval("a<-\"testing string\""); + String testString = (String) engine.eval("a"); + Assert.assertEquals("testing string", testString); + } + + /*@Test + public void testInt() throws Exception { + engine.voidEval("a<-5"); + Integer testInteger = (Integer) engine.eval("a"); + Integer toCompare = 5; + Assert.assertEquals(toCompare, testInteger); + }*/ + + /* @Test + public void testBool() throws Exception { + engine.voidEval("a<-TRUE"); + engine.voidEval("b<-FALSE"); + Boolean testA = (Boolean) engine.eval("a"); + Boolean testB = (Boolean) engine.eval("b"); + Assert.assertTrue(testA); + Assert.assertFalse(testB); + }*/ + + /* @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 { + engine.voidEval("X1<-data.frame(matrix(runif(5*100),nrow=100))"); + Assert.assertTrue(engine.eval("X1") instanceof Vector); + Assert.assertEquals(((Vector<REXP>) engine.eval("X1")).size(), 5); + Assert.assertEquals(((Vector<REXP>) engine.eval("X1")).get(0) + .asDoubleArray().length, 100); + + } + +} // JNITest Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-02-27 17:52:39 UTC (rev 53) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-07 09:35:53 UTC (rev 54) @@ -37,88 +37,129 @@ import static org.codelutin.j2r.TestConstants.V_OP_A; import static org.codelutin.j2r.TestConstants.V_OP_AB; import static org.codelutin.j2r.TestConstants.V_OP_B; -import junit.framework.TestCase; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; -public class NetTest extends TestCase { +public class NetTest { - private static Log log = LogFactory.getLog(NetTest.class); + private static Log log = LogFactory.getLog(NetTest.class); - private REngine engine; - private String savedRType; + private REngine engine; + private String savedRType; - @Override - protected void setUp() throws Exception { - LutinTimer init = new LutinTimer(); - init.startTiming(); - savedRType = System.getProperty("R.type", ""); - System.setProperty("R.type", "net://:6311"); - if (engine == null) { - engine = new RProxy(); - } - if (log.isInfoEnabled()) { - log.info("net init: " + init.endTiming() + "ms"); - } - } + @Before + public void setUp() throws Exception { + LutinTimer init = new LutinTimer(); + init.startTiming(); + savedRType = System.getProperty("R.type", ""); + System.setProperty("R.type", "net://:6311"); + if (engine == null) { + engine = new RProxy(); + } + if (log.isInfoEnabled()) { + log.info("net init: " + init.endTiming() + "ms"); + } + } - @Override - protected void tearDown() throws Exception { - engine.terminate(); - System.setProperty("R.type", savedRType); - } + @After + public void tearDown() throws Exception { + engine.terminate(); + System.setProperty("R.type", savedRType); + } - public void testDouble() throws Exception { - assertEquals(5.0, engine.eval("5.0")); - engine.voidEval("t<-sin(0)"); - double d = (Double)engine.eval("t"); - assertEquals(0.0, d); - } + @Test + public void testDouble() throws Exception { + Assert.assertEquals(5.0, engine.eval("5.0")); + engine.voidEval("t<-sin(0)"); + double d = (Double) engine.eval("t"); + Assert.assertEquals(0.0, d, 0); + } - public void testIntArray() throws Exception { - Object result = engine.eval("5:10"); - assertNotNull(result); - assertEquals(int[].class, result.getClass()); - int[] intArray = (int[]) result; - assertEquals(6, intArray.length); - for (int i = 5; i < 11; i++) { - assertEquals(i, intArray[i - 5]); - } - } + @Test + public void testIntArray() throws Exception { + Object result = engine.eval("5:10"); + Assert.assertNotNull(result); + 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]); + } + } - public void testSimpleOp() throws Exception { - LutinTimer t = new LutinTimer(); - for (int loop = 0; loop < S_NB_LOOPS; loop++) { - engine.voidEval("t<-0"); - t.startTiming(); - engine.voidEval(S_OP); - t.endTiming(); - double d = (Double) engine.eval("t"); - assertEquals((double) S_T_MAX, d); - } - double[] results = t.computeResults(); - System.err.println("[SO]min: " + results[0]); - System.err.println("[SO]avg: " + results[1]); - System.err.println("[SO]max: " + results[2]); - System.err.println("[SO]etype: " + results[3]); - } + @Test + public void testSimpleOp() throws Exception { + LutinTimer t = new LutinTimer(); + for (int loop = 0; loop < S_NB_LOOPS; loop++) { + engine.voidEval("t<-0"); + t.startTiming(); + engine.voidEval(S_OP); + t.endTiming(); + double d = (Double) engine.eval("t"); + Assert.assertEquals((double) S_T_MAX, d, 0); + } + double[] results = t.computeResults(); + System.err.println("[SO]min: " + results[0]); + System.err.println("[SO]avg: " + results[1]); + System.err.println("[SO]max: " + results[2]); + System.err.println("[SO]etype: " + results[3]); + } - public void testVector() throws Exception { - LutinTimer t = new LutinTimer(); - for (int loop = 0; loop < V_NB_LOOPS; loop++) { - t.startTiming(); - engine.voidEval(V_OP_A); - engine.voidEval(V_OP_B); - double[] r = (double[]) engine.eval(V_OP_AB); - t.endTiming(); - assertEquals(V_MAX, r.length); - } - double[] results = t.computeResults(); - System.err.println("[V]min: " + results[0]); - System.err.println("[V]avg: " + results[1]); - System.err.println("[V]max: " + results[2]); - System.err.println("[V]etype: " + results[3]); - } + @Test + public void testVector() throws Exception { + LutinTimer t = new LutinTimer(); + for (int loop = 0; loop < V_NB_LOOPS; loop++) { + t.startTiming(); + engine.voidEval(V_OP_A); + engine.voidEval(V_OP_B); + double[] r = (double[]) engine.eval(V_OP_AB); + t.endTiming(); + Assert.assertEquals(V_MAX, r.length); + } + double[] results = t.computeResults(); + System.err.println("[V]min: " + results[0]); + System.err.println("[V]avg: " + results[1]); + System.err.println("[V]max: " + results[2]); + System.err.println("[V]etype: " + results[3]); + } + @Test + public void testString() throws Exception { + engine.voidEval("a<-\"testing string\""); + String testString = (String) engine.eval("a"); + Assert.assertEquals("testing string", testString); + } + + /*@Test + public void testInt() throws Exception { + engine.voidEval("a<-5"); + Integer testInteger = (Integer) engine.eval("a"); + Integer toCompare = 5; + Assert.assertEquals(toCompare, testInteger); + } + + @Test + public void testBool() throws Exception { + engine.voidEval("a<-TRUE"); + engine.voidEval("b<-FALSE"); + Boolean testA = (Boolean) engine.eval("a"); + Boolean testB = (Boolean) engine.eval("b"); + Assert.assertTrue(testA); + Assert.assertFalse(testB); + } + + @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]); + }*/ + } // NetTest