[LutinJ2R-commits] r4 - in src: java/org/codelutin/j2r java/org/codelutin/j2r/jni java/org/codelutin/j2r/net test/org/codelutin/j2r
Author: thimel Date: 2006-08-31 07:57:43 +0000 (Thu, 31 Aug 2006) New Revision: 4 Modified: src/java/org/codelutin/j2r/REngine.java src/java/org/codelutin/j2r/RProxy.java src/java/org/codelutin/j2r/jni/RJniEngine.java src/java/org/codelutin/j2r/net/RNetEngine.java src/test/org/codelutin/j2r/JNITest.java src/test/org/codelutin/j2r/JPurTest.java src/test/org/codelutin/j2r/NetTest.java Log: Ajout de javadoc r?\195?\169seau : meilleure d?\195?\169tection de params JNI : plus stable Modified: src/java/org/codelutin/j2r/REngine.java =================================================================== --- src/java/org/codelutin/j2r/REngine.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/java/org/codelutin/j2r/REngine.java 2006-08-31 07:57:43 UTC (rev 4) @@ -38,17 +38,35 @@ */ public interface REngine { + /** + * Effectue l'initialisation du moteur. Les parametres sont passes par les + * options de JVM, -D... + * @return true/false pour indiquer si l'initialisation a pu se faire sans + * encombre + */ public boolean init(); /** * Delegue a R le traitement passe en parametre. * @param expr l'expression a evaluer * @return La valeur renvoyee par la commande + * @throws RException */ public Object eval(String expr) throws RException; + /** + * Effectue un eval mais sans retour de resultat. Quand c'est possible cela + * permet donc de na pas consommer du temps de transfert et conversion de R + * a Java. + * @param expr l'expression a evaluer + * @throws RException + */ public void voidEval(String expr) throws RException; + /** + * Met fin a l'utilisation de l'engine + * @throws RException + */ public void terminate() throws RException; } //RJniEngine Modified: src/java/org/codelutin/j2r/RProxy.java =================================================================== --- src/java/org/codelutin/j2r/RProxy.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/java/org/codelutin/j2r/RProxy.java 2006-08-31 07:57:43 UTC (rev 4) @@ -38,7 +38,14 @@ import org.codelutin.j2r.net.RNetEngine; /** - * + * Cette classe permet d'initialiser un REngine sans savoir quelle est + * l'implatation choisie. La detection est basee sur l'option de demarrage de la + * JVM : + * <ul> + * <li><code>-DR.type=net</code></li> + * <li><code>-DR.type=jni</code></li> + * <li><code>-DR.type=...</code></li> + * </ul> */ public class RProxy implements REngine { @@ -110,6 +117,9 @@ return engine.eval(expr); } + /* (non-Javadoc) + * @see org.codelutin.j2r.REngine#init() + */ public boolean init() { String RType = System.getProperty("R.type"); if (RType == null || "".equals(RType)) { @@ -126,6 +136,9 @@ return true; } + /* (non-Javadoc) + * @see org.codelutin.j2r.REngine#terminate() + */ public void terminate() throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + @@ -135,6 +148,9 @@ } } + /* (non-Javadoc) + * @see org.codelutin.j2r.REngine#voidEval(java.lang.String) + */ public void voidEval(String expr) throws RException { if (engine == null) { log.fatal("The R Proxy is not initialized. An error probably " + Modified: src/java/org/codelutin/j2r/jni/RJniEngine.java =================================================================== --- src/java/org/codelutin/j2r/jni/RJniEngine.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/java/org/codelutin/j2r/jni/RJniEngine.java 2006-08-31 07:57:43 UTC (rev 4) @@ -46,24 +46,29 @@ private Log log = LogFactory.getLog(RJniEngine.class); - private 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() { - try { - String[] args = { "--no-save" }; - engine = new Rengine(args, false, null); - if (!engine.waitForR()) { - if (log.isErrorEnabled()) { - log.error("Cannot load the R engine"); + 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; } - } catch (Throwable twable) { - log.error("An error occured during R/JNI initialization.", twable); - return false; } return true; } Modified: src/java/org/codelutin/j2r/net/RNetEngine.java =================================================================== --- src/java/org/codelutin/j2r/net/RNetEngine.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/java/org/codelutin/j2r/net/RNetEngine.java 2006-08-31 07:57:43 UTC (rev 4) @@ -45,28 +45,48 @@ */ public class RNetEngine implements REngine { + public static final int DEFAULT_PORT = 6311; + private Log log = LogFactory.getLog(RNetEngine.class); private Rconnection conn; + /* (non-Javadoc) + * @see org.codelutin.j2r.REngine#init() + */ public boolean init() { - return init("127.0.0.1"); + String host = System.getProperty("R.net.host", "127.0.0.1"); + return init(host); } public boolean init(String host) { - return init(host, 6311); + String portAsString = System.getProperty("R.net.port", ""+DEFAULT_PORT); + int port = DEFAULT_PORT; + try { + port = Integer.parseInt(portAsString); + } catch (NumberFormatException nfe) { + if (log.isWarnEnabled()) { + log.warn("Bad port format " + portAsString + ", using default" + + " port : " + port); + } + } + return init(host, port); } public boolean init(String host, int port) { + if (log.isInfoEnabled()) { + log.info("Trying to connect to the Rserve on '" + host + ":" + port + "'"); + } try { conn = new Rconnection(host, port); } catch (RSrvException e) { log.error("Unable to establish a connection to the R server. " + "Maybe you forgot to start it. " + "Try using the command \"R CMD Rserve\"."); + e.printStackTrace(); return false; } - return true; + return conn.isConnected(); } /* (non-Javadoc) @@ -130,7 +150,7 @@ * @see org.codelutin.j2r.REngine#terminate() */ public void terminate() throws RException { - if (conn.isConnected()) { + if (conn != null && conn.isConnected()) { conn.close(); } } Modified: src/test/org/codelutin/j2r/JNITest.java =================================================================== --- src/test/org/codelutin/j2r/JNITest.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/test/org/codelutin/j2r/JNITest.java 2006-08-31 07:57:43 UTC (rev 4) @@ -1,112 +1,125 @@ /* *##% -* Copyright (C) 2002, 2003, 2004, 2005, 2006 Code Lutin, -* Cedric Pineau, Benjamin Poussin, Arnaud Thimel -* -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*##%*/ + * Copyright (C) 2002, 2003, 2004, 2005, 2006 Code Lutin, + * Cedric Pineau, Benjamin Poussin, Arnaud Thimel + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ /* * -* NetTest.java -* -* Created: 23 août 06 -* -* @author Arnaud Thimel <thimel@codelutin.com> -* @version $Revision: $ -* -* Mise a jour: $Date: $ -* par : $Author: $ -*/ + * NetTest.java + * + * Created: 23 août 06 + * + * @author Arnaud Thimel <thimel@codelutin.com> + * @version $Revision: $ + * + * Mise a jour: $Date: $ + * par : $Author: $ + */ package org.codelutin.j2r; - import static org.codelutin.j2r.TestConstants.*; - import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + public class JNITest extends TestCase { + private static Log log = LogFactory.getLog(JNITest.class); + private REngine engine; - @Override - protected void setUp() throws Exception { + @Override + protected void setUp() throws Exception { LutinTimer init = new LutinTimer(); init.startTiming(); - System.setProperty("R.type", "jni"); - if (engine == null) { - engine = new RProxy(); - } - System.err.println("jni init: " + init.endTiming() + "ms"); - } + System.setProperty("R.type", "jni"); + if (engine == null) { + engine = new RProxy(); + } + if (log.isInfoEnabled()) { + log.info("jni init: " + init.endTiming() + "ms"); + } + } - @Override + @Override protected void tearDown() throws Exception { engine.terminate(); } -// -// public void testDouble() throws Exception { -// assertEquals(5.0, engine.eval("5.0")); -// } -// -// public void testIntArray() throws Exception { -// Object result = engine.eval("5:10"); -// 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]); -// } -// } + public void testDouble() throws Exception { + assertEquals(5.0, engine.eval("5.0")); + } + + public void testIntArray() throws Exception { + Object result = engine.eval("5:10"); + 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]); + } + } + + public void testDoubleArray() throws Exception { + Object result = engine.eval("5.5:10.5"); + assertEquals(double[].class, result.getClass()); + double[] doubleArray = (double[]) result; + assertEquals(6, doubleArray.length); + for (int i = 5; i < 11; i++) { + double d = i + 0.5; + assertEquals(d, doubleArray[i - 5]); + } + } + public void testSimpleOp() throws Exception { LutinTimer t = new LutinTimer(); - for (int loop=0; loop<S_NB_LOOPS; loop++) { + for (int loop = 0; loop < S_NB_LOOPS; loop++) { engine.voidEval("t<-0"); t.startTiming(); engine.voidEval(S_OP); t.endTiming(); - System.err.println((Double)engine.eval("t")); + double d = (Double)engine.eval("t"); + assertEquals((double)S_T_MAX, d); } double[] results = t.computeResults(); - System.err.println("min: " + results[0]); - System.err.println("avg: " + results[1]); - System.err.println("max: " + results[2]); - System.err.println("etype: " + results[3]); + 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 { System.err.println(V_OP_R); - for (int loop=0; loop<V_NB_LOOPS; loop++) { - LutinTimer t = new LutinTimer(); + 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); - long end = t.endTiming(); - System.err.println("duration: " + end); - double sum = 0.0; - for (int i=0; i<r.length; i++) { - // if (i%10000==0) { - // System.err.println(r[i]); - // } - sum += r[i]; - } -// System.err.println("sum: " + sum); -// System.err.println(r.length); + 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]); } -} //NetTest +} // NetTest Modified: src/test/org/codelutin/j2r/JPurTest.java =================================================================== --- src/test/org/codelutin/j2r/JPurTest.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/test/org/codelutin/j2r/JPurTest.java 2006-08-31 07:57:43 UTC (rev 4) @@ -51,15 +51,15 @@ lt.endTiming(); } double[] results = lt.computeResults(); - System.err.println("min: " + results[0]); - System.err.println("avg: " + results[1]); - System.err.println("max: " + results[2]); - System.err.println("etype: " + results[3]); + 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 lt = new LutinTimer(); for (int loop=0; loop<V_NB_LOOPS; loop++) { - LutinTimer lt = new LutinTimer(); lt.startTiming(); double[] a = new double[V_MAX]; double[] b = new double[V_MAX]; @@ -72,18 +72,13 @@ for (int i=0; i<a.length; i++) { r[i] = a[i] * b[i]; } - long end = lt.endTiming(); - System.err.println("duration: " + end); - double sum = 0.0; - for (int i=0; i<r.length; i++) { - // if (i%10000==0) { - // System.err.println(r[i]); - // } - sum += r[i]; - } -// System.err.println("sum: " + sum); -// System.err.println(r.length); + lt.endTiming(); } + double[] results = lt.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]); } } Modified: src/test/org/codelutin/j2r/NetTest.java =================================================================== --- src/test/org/codelutin/j2r/NetTest.java 2006-08-29 10:40:09 UTC (rev 3) +++ src/test/org/codelutin/j2r/NetTest.java 2006-08-31 07:57:43 UTC (rev 4) @@ -1,106 +1,118 @@ /* *##% -* Copyright (C) 2002, 2003, 2004, 2005, 2006 Code Lutin, -* Cedric Pineau, Benjamin Poussin, Arnaud Thimel -* -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*##%*/ + * Copyright (C) 2002, 2003, 2004, 2005, 2006 Code Lutin, + * Cedric Pineau, Benjamin Poussin, Arnaud Thimel + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ /* * -* NetTest.java -* -* Created: 23 août 06 -* -* @author Arnaud Thimel <thimel@codelutin.com> -* @version $Revision: $ -* -* Mise a jour: $Date: $ -* par : $Author: $ -*/ + * NetTest.java + * + * Created: 23 août 06 + * + * @author Arnaud Thimel <thimel@codelutin.com> + * @version $Revision: $ + * + * Mise a jour: $Date: $ + * par : $Author: $ + */ package org.codelutin.j2r; -import static org.codelutin.j2r.TestConstants.*; - +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 junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + public class NetTest extends TestCase { - private REngine engine; + private static Log log = LogFactory.getLog(NetTest.class); - @Override - protected void setUp() throws Exception { + private REngine engine; + + @Override + protected void setUp() throws Exception { LutinTimer init = new LutinTimer(); init.startTiming(); - System.setProperty("R.type", "net"); - if (engine == null) { - engine = new RProxy(); - } - System.err.println("net init: " + init.endTiming() + "ms"); - } -// -// public void testDouble() throws Exception { -// assertEquals(5.0, engine.eval("5.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]); -// } -// } + System.setProperty("R.type", "net"); +// System.setProperty("R.net.host", "monitor"); +// System.setProperty("R.net.port", "6311"); + if (engine == null) { + engine = new RProxy(); + } + if (log.isInfoEnabled()) { + log.info("net init: " + init.endTiming() + "ms"); + } + } + public void testDouble() throws Exception { + assertEquals(5.0, engine.eval("5.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]); + } + } + public void testSimpleOp() throws Exception { LutinTimer t = new LutinTimer(); - for (int loop=0; loop<S_NB_LOOPS; loop++) { + for (int loop = 0; loop < S_NB_LOOPS; loop++) { engine.voidEval("t<-0"); t.startTiming(); engine.voidEval(S_OP); t.endTiming(); - System.err.println((Double)engine.eval("t")); + double d = (Double) engine.eval("t"); + assertEquals((double) S_T_MAX, d); } double[] results = t.computeResults(); - System.err.println("min: " + results[0]); - System.err.println("avg: " + results[1]); - System.err.println("max: " + results[2]); - System.err.println("etype: " + results[3]); + 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 { - for (int loop=0; loop<V_NB_LOOPS; loop++) { - LutinTimer t = new LutinTimer(); + 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); - long end = t.endTiming(); - System.err.println("duration: " + end); - double sum = 0.0; - for (int i=0; i<r.length; i++) { - // if (i%10000==0) { - // System.err.println(r[i]); - // } - sum += r[i]; - } -// System.err.println("sum: " + sum); -// System.err.println(r.length); + 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]); } -} //NetTest +} // NetTest
participants (1)
-
thimel@users.labs.libre-entreprise.org