[LutinJ2R-commits] r69 - in lutinj2r/trunk/src: main/java/org/codelutin/j2r main/java/org/codelutin/j2r/jni main/java/org/codelutin/j2r/net test/java/org/codelutin/j2r
Author: jcouteau Date: 2009-04-29 14:43:55 +0000 (Wed, 29 Apr 2009) New Revision: 69 Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java 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: Adding copy, move, list all objects and clear session capability Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java 2009-04-28 18:42:55 UTC (rev 68) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/REngine.java 2009-04-29 14:43:55 UTC (rev 69) @@ -30,6 +30,7 @@ package org.codelutin.j2r; import java.io.File; +import java.util.List; /** * Cette interface est le point commun entre les differentes technologies @@ -37,114 +38,171 @@ */ 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(); + * + * @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; + /** + * 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 + * + * @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; - - /** + * + * @throws RException + */ + public void terminate() throws RException; + + /** * Load .RData file located in directory * - * @param directory directory where the .RData file is located + * @param directory + * directory where the .RData file is located * @throws RException */ - public void loadRData(File directory) throws RException; - + public void loadRData(File directory) throws RException; + /** * Save the session in a .RData file in directory * - * @param directory where the .RData file will be saved + * @param directory + * where the .RData file will be saved * @throws RException */ public void saveRData(File directory) throws RException; - + /** * Set the R working directory - * @param directory to set + * + * @param directory + * to set * @throws RException */ public void setwd(File directory) throws RException; - + /** * 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; - + /** * 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; - - + /** - * 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; - + /** * 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; - - + /** - * 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; - + /** * Remove a R object from the actual session - * @param rObject to be removed from the session + * + * @param rObject + * to be removed from the session * @throws RException */ - public void remove (String rObject) throws RException; + public void remove(String rObject) throws RException; + /** + * Make a copy of an object and remove the old one. + * + * @param inputObject + * the object to be copied and deleted + * @param outputObject + * the object to be created + * @throws RException + */ + public void mv(String inputObject, String outputObject) throws RException; + + /** + * Copy an object. + * + * @param inputObject + * the object to be copied + * @param outputObject + * the object to be created + * @throws RException + */ + public void cp(String inputObject, String outputObject) throws RException; + + /** + * List all R object present in the actual session + * + * @return a list containing the name of all the R objects in the R session. + * @throws RException + */ + public String[] ls() throws RException; + + /** + * Remove all the objects present in the actual R session. + * + * @throws RException + */ + public void clearSession() throws RException; + } //RJniEngine Modified: lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java =================================================================== --- lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java 2009-04-28 18:42:55 UTC (rev 68) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/RProxy.java 2009-04-29 14:43:55 UTC (rev 69) @@ -41,21 +41,21 @@ * 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> + * <li><code>-DR.type=net</code></li> + * <li><code>-DR.type=jni</code></li> + * <li><code>-DR.type=...</code></li> * </ul> * Il est possible de parametrer la solution network, pour cela, voir la * documentation de {@link org.codelutin.j2r.net.RNetEngine} */ public class RProxy implements REngine { - private Log log = LogFactory.getLog(RProxy.class); + private Log log = LogFactory.getLog(RProxy.class); - private REngine engine; + private REngine engine; public RProxy() { - init(); + init(); } private boolean init(String RType) { @@ -64,255 +64,312 @@ RType = "net"; } if (RType.startsWith("net")) { - initSucceded = initOnNet(); - if (!initSucceded) { - if (log.isErrorEnabled()) { - log.error("Initialization of R with Network failed, trying JNI"); - } - initSucceded = initOnJNI(); - } + initSucceded = initOnNet(); + if (!initSucceded) { + if (log.isErrorEnabled()) { + log + .error("Initialization of R with Network failed, trying JNI"); + } + initSucceded = initOnJNI(); + } } else if ("jni".equalsIgnoreCase(RType)) { - initSucceded = initOnJNI(); - if (!initSucceded) { - if (log.isErrorEnabled()) { - log.error("Initialization of R over JNI failed, trying with Network"); - } - initSucceded = initOnNet(); - } + initSucceded = initOnJNI(); + if (!initSucceded) { + if (log.isErrorEnabled()) { + log + .error("Initialization of R over JNI failed, trying with Network"); + } + initSucceded = initOnNet(); + } } else if (log.isErrorEnabled()) { - log.error("Invalid type specified : " + RType); + log.error("Invalid type specified : " + RType); } return initSucceded; } private boolean initOnJNI() { - if (log.isInfoEnabled()) { - log.info("Trying to initialize the R Proxy over JNI"); - } - RJniEngine newEngine = new RJniEngine(); - if (newEngine.init()) { - engine = newEngine; - return true; - } - return false; + if (log.isInfoEnabled()) { + log.info("Trying to initialize the R Proxy over JNI"); + } + RJniEngine newEngine = new RJniEngine(); + if (newEngine.init()) { + engine = newEngine; + return true; + } + return false; } private boolean initOnNet() { - if (log.isInfoEnabled()) { - log.info("Trying to initialize the R Proxy with Network"); - } - RNetEngine newEngine = new RNetEngine(); - if (newEngine.init()) { - engine = newEngine; - return true; - } - return false; + if (log.isInfoEnabled()) { + log.info("Trying to initialize the R Proxy with Network"); + } + RNetEngine newEngine = new RNetEngine(); + if (newEngine.init()) { + engine = newEngine; + return true; + } + return false; } - /* (non-Javadoc) - * @see org.codelutin.R.REngine#eval(java.lang.String) - */ - public Object eval(String expr) throws RException { - if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); - return null; - } - return engine.eval(expr); - } + /* (non-Javadoc) + * @see org.codelutin.R.REngine#eval(java.lang.String) + */ + public Object eval(String expr) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return null; + } + return engine.eval(expr); + } - /* (non-Javadoc) - * @see org.codelutin.j2r.REngine#init() - */ - public boolean init() { + /* (non-Javadoc) + * @see org.codelutin.j2r.REngine#init() + */ + public boolean init() { String RType = System.getProperty("R.type"); if (RType == null || "".equals(RType)) { - log.warn("No R type specified.\nusage:\n\tjava -DR.type=net ...\n" + - "or\n\tjava -DR.type=jni ...\n\nConsidering network type"); - RType = "net"; + log + .warn("No R type specified.\nusage:\n\tjava -DR.type=net ...\n" + + "or\n\tjava -DR.type=jni ...\n\nConsidering network type"); + RType = "net"; } if (!init(RType)) { - if (log.isFatalEnabled()) { - log.fatal("The initializing of R failed."); - } - return false; + if (log.isFatalEnabled()) { + log.fatal("The initializing of R failed."); + } + return false; } - return true; - } + 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 " + - "occured during the initialization."); - } else { - engine.terminate(); - } - } + /* (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 " + + "occured during the initialization."); + } else { + engine.terminate(); + } + } /* (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 " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } engine.voidEval(expr); } - + /** * Load .RData file located in directory * - * @param directory directory where the .RData file is located + * @param directory + * directory where the .RData file is located * @throws RException */ public void loadRData(File directory) throws RException { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } engine.setwd(directory); engine.voidEval("load(\".RData\")"); } - + /** * Save the session in a .RData file in directory * - * @param directory where the .RData file will be saved + * @param directory + * where the .RData file will be saved * @throws RException */ - public void saveRData(File directory) throws RException{ + public void saveRData(File directory) throws RException { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } engine.setwd(directory); engine.voidEval("save.image()"); } - + /** * Set the R working directory - * @param directory to set + * + * @param directory + * to set * @throws RException */ public void setwd(File directory) throws RException { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } - engine.voidEval("setwd(\""+directory.getAbsolutePath() + "\")"); + engine.voidEval("setwd(\"" + directory.getAbsolutePath() + "\")"); } - + /** * Get the actual R session working directory * - * @return a File that is the actual R session working directory, null if the engine is not initialized + * @return a File that is the actual R session working directory, null if + * the engine is not initialized * @throws RException */ - + public File getwd() throws RException { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return null; } - String directory = (String)engine.eval("getwd()"); + String directory = (String) engine.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 { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } String rInstruction = "dput(%s,file=\"%s\")"; engine.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 { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } String rInstruction = "%s <- dget(\"%s\")"; engine.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 { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } File workingdir = getwd(); engine.setwd(outputFile.getParentFile()); String rInstruction = "dput(%s,file=\"%s\")"; - engine.voidEval(String.format(rInstruction, rObject, outputFile.getName())); + engine.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 { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } File workingdir = getwd(); engine.setwd(inputFile.getParentFile()); String rInstruction = "%s <- dget(\"%s\")"; - engine.voidEval(String.format(rInstruction, rObject, inputFile.getName())); - setwd(workingdir); + engine.voidEval(String.format(rInstruction, rObject, inputFile + .getName())); + setwd(workingdir); } - + public void remove(String rObject) throws RException { if (engine == null) { - log.fatal("The R Proxy is not initialized. An error probably " + - "occured during the initialization."); + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); return; } engine.remove(rObject); } + public void mv(String inputObject, String outputObject) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.mv(inputObject, outputObject); + + } + + public void cp(String inputObject, String outputObject) throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.cp(inputObject, outputObject); + + } + + public String[] ls() throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return null; + } + return engine.ls(); + } + + public void clearSession() throws RException { + if (engine == null) { + log.fatal("The R Proxy is not initialized. An error probably " + + "occured during the initialization."); + return; + } + engine.clearSession(); + + } + } //RProxy 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 18:42:55 UTC (rev 68) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/jni/RJniEngine.java 2009-04-29 14:43:55 UTC (rev 69) @@ -78,7 +78,7 @@ public Object eval(String expr) throws RException { REXP result = null; try { - log.info(expr); + log.debug(expr); result = engine.eval(expr); } catch (Exception eee) { throw new RException("Unable to evaluate the R expression " @@ -102,7 +102,7 @@ result = rexp.asString(); break; case REXP.XT_INT: - result = (Integer)rexp.asInt(); + result = (Integer) rexp.asInt(); break; case REXP.XT_ARRAY_INT: result = rexp.asIntArray(); @@ -120,7 +120,6 @@ break; case REXP.XT_BOOL: result = rexp.asBool().isTRUE(); - log.info(result); break; case REXP.XT_DOUBLE: result = rexp.asDoubleArray(); @@ -151,6 +150,9 @@ case REXP.XT_VECTOR: result = rexp.asVector(); break; + case 34: + result = rexp.asStringArray(); + break; default: log.error("Unknown return type [" + type + "] " + "on : " + rexp.toString()); @@ -179,7 +181,6 @@ // 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); @@ -304,4 +305,28 @@ voidEval("remove(" + rObject + ")"); } + public void mv(String inputObject, String outputObject) throws RException { + cp(inputObject, outputObject); + remove(inputObject); + + } + + public void cp(String inputObject, String outputObject) throws RException { + voidEval(outputObject + "<-" + inputObject); + + } + + public String[] ls() throws RException { + String[] Robjects = (String[]) eval("ls()"); + return Robjects; + } + + public void clearSession() throws RException { + String[] Robjects = ls(); + for (int i = 0; i < Robjects.length; i++) { + remove(Robjects[i]); + } + + } + } // 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-04-28 18:42:55 UTC (rev 68) +++ lutinj2r/trunk/src/main/java/org/codelutin/j2r/net/RNetEngine.java 2009-04-29 14:43:55 UTC (rev 69) @@ -30,6 +30,7 @@ package org.codelutin.j2r.net; import java.io.File; +import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -40,7 +41,6 @@ import org.rosuda.REngine.Rserve.RConnection; import org.rosuda.REngine.Rserve.RserveException; - /** * 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 @@ -53,252 +53,262 @@ */ public class RNetEngine implements REngine { - public static final int DEFAULT_PORT = 6311; - public static final String DEFAULT_HOST = "127.0.0.1"; + public static final int DEFAULT_PORT = 6311; + public static final String DEFAULT_HOST = "127.0.0.1"; - private Log log = LogFactory.getLog(RNetEngine.class); + private Log log = LogFactory.getLog(RNetEngine.class); - private RConnection conn; + private RConnection conn; - /* - * (non-Javadoc) - * - * @see org.codelutin.j2r.REngine#init() - */ - public boolean init() { - String typeProp = System.getProperty("R.type", "net"); - int urlPos = typeProp.indexOf("net://"); - String host = null; - String portAsString = null; - if (urlPos != -1) { - String url = typeProp.substring(urlPos + 6); - int commaPos = url.indexOf(":"); - if (commaPos != -1) { - host = url.substring(0, commaPos); - portAsString = url.substring(commaPos + 1); - } else { - host = url; - } - } - if (host == null || "".equals(host)) { - host = DEFAULT_HOST; - } - int port = DEFAULT_PORT; - if (portAsString != null) { - 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); - } + /* + * (non-Javadoc) + * + * @see org.codelutin.j2r.REngine#init() + */ + public boolean init() { + String typeProp = System.getProperty("R.type", "net"); + int urlPos = typeProp.indexOf("net://"); + String host = null; + String portAsString = null; + if (urlPos != -1) { + String url = typeProp.substring(urlPos + 6); + int commaPos = url.indexOf(":"); + if (commaPos != -1) { + host = url.substring(0, commaPos); + portAsString = url.substring(commaPos + 1); + } else { + host = url; + } + } + if (host == null || "".equals(host)) { + host = DEFAULT_HOST; + } + int port = DEFAULT_PORT; + if (portAsString != null) { + 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 (RserveException 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 conn.isConnected(); - } + 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 (RserveException 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 conn.isConnected(); + } - /* - * (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 = conn.eval(expr); - } catch (RserveException e) { - throw new RException("An error occured during the eval method", e); - } - 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.debug(expr); + result = conn.eval(expr); + } catch (RserveException e) { + throw new RException("An error occured during the eval method", e); + } + 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()); - } + private Object convertResult(REXP rexp) { + if (rexp == null) { + log.debug("Null returned"); + return null; + } + if (log.isDebugEnabled()) { + log.debug("Converting : " + rexp.toString()); + } - Object result = null; + Object result = null; - try { + try { - if (rexp.isInteger()) { - result = rexp.asIntegers(); - int[] intarray = (int[]) result; - if (intarray.length == 1) { - result = intarray[0]; - } - } + if (rexp.isInteger()) { + result = rexp.asIntegers(); + int[] intarray = (int[]) result; + if (intarray.length == 1) { + result = intarray[0]; + } + } - else if (rexp.isFactor()) { - result = rexp.asFactor(); - } + else if (rexp.isFactor()) { + result = rexp.asFactor(); + } - else if (rexp.isNumeric()) { - result = rexp.asDoubles(); - double[] doublearray = (double[]) result; - if (doublearray.length == 1) { - 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(); - String[] strings = ((String [])result); - Boolean[] stringArray = new Boolean[strings.length]; - for (int i=0;i<((String [])result).length;i++){ - stringArray[i]=Boolean.parseBoolean(strings[i]); - } - if (stringArray.length == 1) { - result = (Boolean)stringArray[0]; - } - else {result = (Boolean[])stringArray;} - } - - else if (rexp.isNull()) { - return null; - } + else if (rexp.isNumeric()) { + result = rexp.asDoubles(); + double[] doublearray = (double[]) result; + if (doublearray.length == 1) { + result = doublearray[0]; + } + } - else { - log.error("Unknown return type on : " + rexp.toString()); - } - } catch (REXPMismatchException e) { - e.printStackTrace(); - } - return result; - } + else if (rexp.isString()) { + result = rexp.asStrings(); + String[] stringArray = (String[]) result; + if (stringArray.length == 1) { + result = stringArray[0]; + } - /* - * (non-Javadoc) - * - * @see org.codelutin.j2r.REngine#terminate() - */ - public void terminate() throws RException { - if (conn != null && conn.isConnected()) { - conn.close(); - } - } + } - /* - * (non-Javadoc) - * - * @see org.codelutin.j2r.REngine#voidEval(java.lang.String) - */ - public void voidEval(String expr) throws RException { - try { - log.info(expr); - conn.voidEval(expr); - } catch (RserveException rse) { - throw new RException("An error occured while voidEval on net", rse); - } - } - - /** + else if (rexp.isLogical()) { + result = rexp.asStrings(); + String[] strings = ((String[]) result); + Boolean[] stringArray = new Boolean[strings.length]; + for (int i = 0; i < ((String[]) result).length; i++) { + stringArray[i] = Boolean.parseBoolean(strings[i]); + } + if (stringArray.length == 1) { + result = (Boolean) stringArray[0]; + } else { + result = (Boolean[]) stringArray; + } + } + + else if (rexp.isNull()) { + return null; + } + + else { + log.error("Unknown return type on : " + rexp.toString()); + } + } catch (REXPMismatchException e) { + e.printStackTrace(); + } + return result; + } + + /* + * (non-Javadoc) + * + * @see org.codelutin.j2r.REngine#terminate() + */ + public void terminate() throws RException { + if (conn != null && conn.isConnected()) { + conn.close(); + } + } + + /* + * (non-Javadoc) + * + * @see org.codelutin.j2r.REngine#voidEval(java.lang.String) + */ + public void voidEval(String expr) throws RException { + try { + conn.voidEval(expr); + } catch (RserveException rse) { + throw new RException("An error occured while voidEval on net", rse); + } + } + + /** * Load .RData file located in directory * - * @param directory directory where the .RData file is located + * @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 + * @param directory + * where the .RData file will be saved * @throws RException */ - public void saveRData(File directory) throws RException{ + public void saveRData(File directory) throws RException { setwd(directory); voidEval("save.image()"); } - + /** * Set the R working directory - * @param directory to set + * + * @param directory + * to set * @throws RException */ public void setwd(File directory) throws RException { - voidEval("setwd(\""+directory.getAbsolutePath() + "\")"); + 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 { @@ -308,13 +318,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 { @@ -322,11 +334,35 @@ 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 + ")"); } + public void mv(String inputObject, String outputObject) throws RException { + cp(inputObject, outputObject); + remove(inputObject); + + } + + public void cp(String inputObject, String outputObject) throws RException { + voidEval(outputObject + "<-" + inputObject); + + } + + public String[] ls() throws RException { + String[] Robjects = (String[]) eval("ls()"); + return Robjects; + } + + public void clearSession() throws RException { + String[] Robjects = ls(); + for (int i = 0; i < Robjects.length; i++) { + remove(Robjects[i]); + } + + } + } // RNetEngine Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-28 18:42:55 UTC (rev 68) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/JNITest.java 2009-04-29 14:43:55 UTC (rev 69) @@ -255,5 +255,31 @@ engine.remove("a"); Assert.assertNull(engine.eval("a")); } + + @Test + public void testMvCp() throws Exception { + //Test only mv as mv uses cp. + engine.voidEval("a<-5.0"); + engine.remove("b"); + engine.mv("a", "b"); + Assert.assertEquals(new Double(5.0), engine.eval("b")); + Assert.assertFalse((Boolean) engine.eval("exists(\"a\")")); + } + + @Test + public void testLsClearSession() throws Exception { + //Test only ClearSession as it uses ls + engine.voidEval("a<-5.0"); + engine.voidEval("b<-5.0"); + engine.voidEval("d<-5.0"); + engine.voidEval("e<-5.0"); + engine.voidEval("f<-5.0"); + engine.clearSession(); + Assert.assertFalse((Boolean) engine.eval("exists(\"a\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"b\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"d\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"e\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"f\")")); + } } // JNITest Modified: lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java =================================================================== --- lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-28 18:42:55 UTC (rev 68) +++ lutinj2r/trunk/src/test/java/org/codelutin/j2r/NetTest.java 2009-04-29 14:43:55 UTC (rev 69) @@ -49,129 +49,130 @@ 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; - @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"); - } - } + @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"); + } + } - @After - public void tearDown() throws Exception { - engine.terminate(); - System.setProperty("R.type", savedRType); - } + @After + public void tearDown() throws Exception { + engine.terminate(); + System.setProperty("R.type", savedRType); + } - @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); - } + @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); + } - @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]); - } - } + @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]); + } + } - @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]); - } + @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]); + } - @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 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 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<-as.integer(5)"); - Integer testInteger = (Integer) engine.eval("a"); - Integer toCompare = 5; - Assert.assertEquals(toCompare, testInteger); - } + @Test + public void testInt() throws Exception { + engine.voidEval("a<-as.integer(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 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 + @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 testWorkingDirectory() throws Exception { File workingDirectory = new File("/tmp"); engine.setwd(workingDirectory); File testWorkingDirectory = engine.getwd(); - Assert.assertEquals(workingDirectory.getAbsolutePath(), testWorkingDirectory.getAbsolutePath()); + Assert.assertEquals(workingDirectory.getAbsolutePath(), + testWorkingDirectory.getAbsolutePath()); } - + @Test public void testRData() throws Exception { File workingdir = new File("/tmp"); @@ -179,46 +180,73 @@ engine.saveRData(workingdir); engine.remove("a"); engine.loadRData(workingdir); - Double testDouble = (Double)engine.eval("a"); - Double compareTo = new Double (5.0); + Double testDouble = (Double) engine.eval("a"); + Double compareTo = new Double(5.0); Assert.assertEquals(compareTo, testDouble); } - + @Test public void testDputDget() throws Exception { File workingdir = new File("/tmp"); File testingFile = new File("/tmp/testfile"); - + // test method using the workingdir engine.voidEval("a<-5.0"); engine.setwd(workingdir); engine.dput("a", "testDputDgetfile"); engine.remove("a"); engine.dget("a", "testDputDgetfile"); - Double testDouble = (Double)engine.eval("a"); - Double compareTo = new Double (5.0); + Double testDouble = (Double) engine.eval("a"); + Double compareTo = new Double(5.0); Assert.assertEquals(compareTo, testDouble); - - workingdir= new File("/"); + + workingdir = new File("/"); //test method using absolute path engine.setwd(workingdir); engine.voidEval("a<-6.0"); - engine.dput("a",testingFile); + engine.dput("a", testingFile); engine.remove("a"); engine.dget("a", testingFile); - Double testDouble2 = (Double)engine.eval("a"); - Double compareTo2 = new Double (6.0); + Double testDouble2 = (Double) engine.eval("a"); + Double compareTo2 = new Double(6.0); Assert.assertEquals(compareTo2, testDouble2); File testWorkingDirectory = engine.getwd(); - Assert.assertEquals(workingdir.getAbsolutePath(), testWorkingDirectory.getAbsolutePath()); + Assert.assertEquals(workingdir.getAbsolutePath(), testWorkingDirectory + .getAbsolutePath()); } - + @Test public void testRemove() throws Exception { engine.voidEval("a<-6.0"); - Assert.assertEquals(new Double(6.0),engine.eval("a")); + Assert.assertEquals(new Double(6.0), engine.eval("a")); engine.remove("a"); - Assert.assertFalse((Boolean)engine.eval("exists(\"a\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"a\")")); } + + @Test + public void testMvCp() throws Exception { + //Test only mv as mv uses cp. + engine.voidEval("a<-5.0"); + engine.remove("b"); + engine.mv("a", "b"); + Assert.assertEquals(new Double(5.0), engine.eval("b")); + Assert.assertFalse((Boolean) engine.eval("exists(\"a\")")); + } + + @Test + public void testLsClearSession() throws Exception { + //Test only ClearSession as it uses ls + engine.voidEval("a<-5.0"); + engine.voidEval("b<-5.0"); + engine.voidEval("d<-5.0"); + engine.voidEval("e<-5.0"); + engine.voidEval("f<-5.0"); + engine.clearSession(); + Assert.assertFalse((Boolean) engine.eval("exists(\"a\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"b\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"d\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"e\")")); + Assert.assertFalse((Boolean) engine.eval("exists(\"f\")")); + } } // NetTest
participants (1)
-
jcouteau@users.labs.libre-entreprise.org