[LutinJ2R-commits] r112 - in trunk/src/main/java/org/nuiton/j2r: . jni net
Author: jcouteau Date: 2009-07-26 16:19:16 +0200 (Sun, 26 Jul 2009) New Revision: 112 Added: trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java Log: Create an abstract REngine to avoid similar code in different files. Added: trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java (rev 0) +++ trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java 2009-07-26 14:19:16 UTC (rev 112) @@ -0,0 +1,229 @@ +package org.nuiton.j2r; + +import java.io.File; +import java.util.LinkedList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.j2r.jni.RJniEngine; +import org.rosuda.JRI.Rengine; +import org.rosuda.REngine.REXP; + +public class REngineAbstract implements REngine { + + private Log log = LogFactory.getLog(RJniEngine.class); + + /** + * Rengine is made to be static + */ + private static Rengine engine; + + /** + * If true, commit each R instruction on the fly, if false, commit only when + * the commit() method is called. + */ + private Boolean autocommit = true; + + /** + * List used to store all the R instructions when not in autocommit mode + * (when autocommit == false). + */ + private List<String> rInstructions = new LinkedList<String>(); + + @Override + public boolean init() { + return false; + } + + public boolean init(String host, int port) { + return false; + } + + @Override + public Object eval(String expr) throws RException { + return null; + } + + private Object convertResult(REXP rexp) { + return null; + } + + @Override + public void terminate() throws RException { + } + + @Override + public void voidEval(String expr) throws RException { + } + + /** + * Load .RData file located in directory + * + * @param directory + * directory where the .RData file is located + * @throws RException + */ + @Override + 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 + */ + @Override + public void saveRData(File directory) throws RException { + setwd(directory); + voidEval("save.image()"); + } + + /** + * Set the R working directory + * + * @param directory + * to set + * @throws RException + */ + @Override + public void setwd(File directory) throws RException { + voidEval("setwd(\"" + + directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")"); + } + + /** + * Get the actual R session working directory + * + * @return a File that is the actual R session working directory + * @throws RException + */ + + @Override + public File getwd() throws RException { + 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 + * @throws RException + */ + @Override + 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 + * + * @param rObject + * name of the R object created + * @param inputFileName + * name of the file to load + * @throws RException + */ + @Override + 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 outputFile + * the file to save + * @throws RException + */ + @Override + public void dput(String rObject, File outputFile) throws RException { + File workingdir = getwd(); + setwd(outputFile.getParentFile()); + String rInstruction = "dput(%s,file=\"%s\")"; + 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. + * + * @param rObject + * name of the R object created + * @param inputFile + * file to load + * @throws RException + */ + @Override + public void dget(String rObject, File inputFile) throws RException { + File workingdir = getwd(); + setwd(inputFile.getParentFile()); + String rInstruction = "%s <- dget(\"%s\")"; + voidEval(String.format(rInstruction, rObject, inputFile.getName())); + setwd(workingdir); + } + + @Override + public void remove(String rObject) throws RException { + voidEval("remove(" + rObject + ")"); + } + + @Override + public void mv(String inputObject, String outputObject) throws RException { + cp(inputObject, outputObject); + remove(inputObject); + + } + + @Override + public void cp(String inputObject, String outputObject) throws RException { + voidEval(outputObject + "<-" + inputObject); + + } + + @Override + public String[] ls() throws RException { + String[] rObjects = (String[]) eval("ls()"); + return rObjects; + } + + @Override + public void clearSession() throws RException { + voidEval("rm(list=ls())"); + } + + @Override + public void setAutoCommit(Boolean autocommit) throws RException { + if ((!this.autocommit) && (autocommit)) { + commit(); + } + this.autocommit = autocommit; + } + + @Override + public Boolean isAutoCommit() { + return this.autocommit; + } + + @Override + public void commit() throws RException { + } + +} Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-07-26 14:09:47 UTC (rev 111) +++ trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-07-26 14:19:16 UTC (rev 112) @@ -17,13 +17,13 @@ package org.nuiton.j2r.jni; -import java.io.File; import java.util.LinkedList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.j2r.REngine; +import org.nuiton.j2r.REngineAbstract; import org.nuiton.j2r.RException; import org.rosuda.JRI.REXP; import org.rosuda.JRI.Rengine; @@ -38,7 +38,7 @@ * * Mise a jour: $Date: $ par : $Author: $ */ -public class RJniEngine implements REngine { +public class RJniEngine extends REngineAbstract implements REngine { private Log log = LogFactory.getLog(RJniEngine.class); @@ -131,7 +131,7 @@ } break; case REXP.XT_ARRAY_DOUBLE: - //if double array, return the rexp as double array. Check if only one double, return a double. + //if double array, return the rexp as double array. Check if only one double, return a double. result = rexp.asDoubleArray(); double[] doublearray = (double[]) result; if (doublearray.length == 1) { @@ -224,167 +224,7 @@ } } - /** - * Load .RData file located in directory - * - * @param directory - * directory where the .RData file is located - * @throws RException - */ @Override - 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 - */ - @Override - public void saveRData(File directory) throws RException { - setwd(directory); - voidEval("save.image()"); - } - - /** - * Set the R working directory - * - * @param directory - * to set - * @throws RException - */ - @Override - public void setwd(File directory) throws RException { - voidEval("setwd(\"" - + directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")"); - } - - /** - * Get the actual R session working directory - * - * @return a File that is the actual R session working directory - * @throws RException - */ - - @Override - public File getwd() throws RException { - 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 - * @throws RException - */ - @Override - 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 - * - * @param rObject - * name of the R object created - * @param inputFileName - * name of the file to load - * @throws RException - */ - @Override - 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 outputFile - * the file to save - * @throws RException - */ - @Override - public void dput(String rObject, File outputFile) throws RException { - File workingdir = getwd(); - setwd(outputFile.getParentFile()); - String rInstruction = "dput(%s,file=\"%s\")"; - 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. - * - * @param rObject - * name of the R object created - * @param inputFile - * file to load - * @throws RException - */ - @Override - public void dget(String rObject, File inputFile) throws RException { - File workingdir = getwd(); - setwd(inputFile.getParentFile()); - String rInstruction = "%s <- dget(\"%s\")"; - voidEval(String.format(rInstruction, rObject, inputFile.getName())); - setwd(workingdir); - } - - @Override - public void remove(String rObject) throws RException { - voidEval("remove(" + rObject + ")"); - } - - @Override - public void mv(String inputObject, String outputObject) throws RException { - cp(inputObject, outputObject); - remove(inputObject); - - } - - @Override - public void cp(String inputObject, String outputObject) throws RException { - voidEval(outputObject + "<-" + inputObject); - - } - - @Override - public String[] ls() throws RException { - String[] rObjects = (String[]) eval("ls()"); - return rObjects; - } - - @Override - public void clearSession() throws RException { - voidEval("rm(list=ls())"); - } - - @Override - public void setAutoCommit(Boolean autocommit) throws RException { - if ((!this.autocommit)&&(autocommit)){ - commit(); - } - this.autocommit = autocommit; - } - - @Override public void commit() throws RException { for (int i = 0; i < rInstructions.size(); i++) { try { @@ -398,10 +238,4 @@ } rInstructions = new LinkedList<String>(); } - - @Override - public Boolean isAutoCommit() { - return this.autocommit; - } - } // RJniEngine Modified: trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java 2009-07-26 14:09:47 UTC (rev 111) +++ trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java 2009-07-26 14:19:16 UTC (rev 112) @@ -29,13 +29,13 @@ package org.nuiton.j2r.net; -import java.io.File; import java.util.LinkedList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.j2r.REngine; +import org.nuiton.j2r.REngineAbstract; import org.nuiton.j2r.RException; import org.rosuda.REngine.REXP; import org.rosuda.REngine.REXPMismatchException; @@ -52,7 +52,7 @@ * l'adresse de la machine distante et 6312 le port sur lequel tourne le * serveur. */ -public class RNetEngine implements REngine { +public class RNetEngine extends REngineAbstract implements REngine { public static final int DEFAULT_PORT = 6311; public static final String DEFAULT_HOST = "127.0.0.1"; @@ -111,6 +111,7 @@ return init(host, port); } + @Override public boolean init(String host, int port) { if (log.isInfoEnabled()) { log.info("Trying to connect to the Rserve on '" + host + ":" + port @@ -246,168 +247,7 @@ } } - /** - * Load .RData file located in directory - * - * @param directory - * directory where the .RData file is located - * @throws RException - */ @Override - 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 - */ - @Override - public void saveRData(File directory) throws RException { - setwd(directory); - voidEval("save.image()"); - } - - /** - * Set the R working directory - * - * @param directory - * to set - * @throws RException - */ - @Override - public void setwd(File directory) throws RException { - voidEval("setwd(\"" - + directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")"); - } - - /** - * Get the actual R session working directory - * - * @return a File that is the actual R session working directory - * @throws RException - */ - - @Override - public File getwd() throws RException { - 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 - * @throws RException - */ - @Override - 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 - * - * @param rObject - * name of the R object created - * @param inputFileName - * name of the file to load - * @throws RException - */ - @Override - 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 outputFile - * the file to save - * @throws RException - */ - @Override - public void dput(String rObject, File outputFile) throws RException { - File workingdir = getwd(); - setwd(outputFile.getParentFile()); - String rInstruction = "dput(%s,file=\"%s\")"; - 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. - * - * @param rObject - * name of the R object created - * @param inputFile - * file to load - * @throws RException - */ - @Override - public void dget(String rObject, File inputFile) throws RException { - File workingdir = getwd(); - setwd(inputFile.getParentFile()); - String rInstruction = "%s <- dget(\"%s\")"; - voidEval(String.format(rInstruction, rObject, inputFile.getName())); - setwd(workingdir); - } - - @Override - public void remove(String rObject) throws RException { - voidEval("remove(" + rObject + ")"); - } - - @Override - public void mv(String inputObject, String outputObject) throws RException { - cp(inputObject, outputObject); - remove(inputObject); - - } - - @Override - public void cp(String inputObject, String outputObject) throws RException { - voidEval(outputObject + "<-" + inputObject); - - } - - @Override - public String[] ls() throws RException { - String[] rObjects = (String[]) eval("ls()"); - return rObjects; - } - - @Override - public void clearSession() throws RException { - voidEval("rm(list=ls())"); - - } - - @Override - public void setAutoCommit(Boolean autocommit) throws RException{ - if ((!this.autocommit)&&(autocommit)){ - commit(); - } - this.autocommit = autocommit; - } - - @Override public void commit() throws RException { for (int i = 0; i < rInstructions.size(); i++) { String expr = rInstructions.get(i); @@ -421,10 +261,4 @@ } rInstructions = new LinkedList<String>(); } - - @Override - public Boolean isAutoCommit(){ - return this.autocommit; - } - } // RNetEngine
participants (1)
-
jcouteau@users.labs.libre-entreprise.org