Nuiton-j2r-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- 316 discussions
Author: tchemit
Date: 2009-08-23 19:20:21 +0200 (Sun, 23 Aug 2009)
New Revision: 145
Modified:
trunk/pom.xml
trunk/src/site/site.xml
Log:
- bump versions
- use jrst
- clean sites
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2009-08-21 12:14:04 UTC (rev 144)
+++ trunk/pom.xml 2009-08-23 17:20:21 UTC (rev 145)
@@ -10,7 +10,7 @@
<parent>
<groupId>org.nuiton</groupId>
<artifactId>mavenpom</artifactId>
- <version>1.0.0-rc-5-SNAPSHOT</version>
+ <version>1.0.0</version>
</parent>
<artifactId>nuiton-j2r</artifactId>
@@ -92,6 +92,21 @@
<build>
<defaultGoal>install</defaultGoal>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-site-plugin</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.nuiton.jrst</groupId>
+ <artifactId>doxia-module-jrst</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ </dependencies>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+
</build>
<!-- Source control management. -->
Modified: trunk/src/site/site.xml
===================================================================
--- trunk/src/site/site.xml 2009-08-21 12:14:04 UTC (rev 144)
+++ trunk/src/site/site.xml 2009-08-23 17:20:21 UTC (rev 145)
@@ -1,23 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="${project.name}">
-
- <publishDate format="dd/MM/yyyy"/>
-
- <skin>
- <groupId>org.nuiton</groupId>
- <artifactId>maven-nuiton-skin</artifactId>
- <version>1.0.0</version>
- </skin>
<bannerLeft>
<name>${project.name}</name>
<href>index.html</href>
</bannerLeft>
-
- <bannerRight>
- <src>http://www.codelutin.com/images/lutinorange-codelutin.png</src>
- <href>http://www.codelutin.com</href>
- </bannerRight>
<poweredBy>
<logo href="http://maven.apache.org" name="Maven" img="images/logos/maven-feather.png"/>
1
0
Author: jcouteau
Date: 2009-08-21 14:14:04 +0200 (Fri, 21 Aug 2009)
New Revision: 144
Modified:
trunk/src/main/java/org/nuiton/j2r/RInstructions.java
Log:
remove unused static final modifiers
Modified: trunk/src/main/java/org/nuiton/j2r/RInstructions.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/RInstructions.java 2009-08-21 10:35:06 UTC (rev 143)
+++ trunk/src/main/java/org/nuiton/j2r/RInstructions.java 2009-08-21 12:14:04 UTC (rev 144)
@@ -9,45 +9,45 @@
/**
* Load the .RData file located in the working directory
*/
- static final String LOAD_RDATA = "load(\".RData\")";
+ String LOAD_RDATA = "load(\".RData\")";
/**
* Load the file.RData file located in the working directory
*/
- static final String LOAD_RDATA_FILE = "load(\"%s.RData\")";
- static final String SAVE_IMAGE = "save.image()";
- static final String SAVE_IMAGE_FILE = "save.image(file=\"%s\")";
- static final String SET_WORKING_DIRECTORY = "setwd(\"%s\")";
- static final String GET_WORKING_DIRECTORY = "getwd()";
- static final String DPUT = "dput(%s,file=\"%s\")";
- static final String DGET = "%s <- dget(\"%s\")";
- static final String REMOVE = "remove(%s)";
- static final String LS = "ls()";
- static final String CLEAR_SESSION = "rm(list=ls())";
- static final String RTRY = "try(%s,silent=TRUE)";
- static final String CLASS_ERROR = "try-error";
- static final String CLASS_DATAFRAME = "data.frame";
- static final String ATTRIBUTE_CLASS = "class";
- static final String ATTRIBUTE_NAMES = "names";
- static final String ATTRIBUTE_ROWNAMES = "row.names";
- static final String GET_ROW_NAMES = "row.names(%s)";
- static final String GET_ROW_NAME = "row.names(%s)[%s]";
- static final String SET_ROW_NAMES = "row.names(%s)<-c(%s)";
- static final String SET_ROW_NAME = "row.names(%s)[%s]<-\"%s\"";
- static final String GET_NAMES = "names(%s)";
- static final String GET_NAME = "names(%s)[%s]";
- static final String SET_NAMES = "names(%s)<-c(%s)";
- static final String SET_NAME = "names(%s)[%s]<-\"%s\"";
- static final String SET_ATTRIBUTE = "attr(%s,\"%s\")<-%s";
- static final String GET_ATTRIBUTE = "attr(%s,\"%s\")";
- static final String LENGTH = "length(%s)";
- static final String LENGTH_COLUMN = "length(%s[,%s])";
- static final String LENGTH_ATTRIBUTES = "length(attributes(%s))";
- static final String GET_ATTRIBUTE_NAME = "names(attributes(%s))[%s]";
- static final String GET_LIST_ITEM = "%s[[%s]]";
- static final String GET_DATAFRAME_ITEM ="%s[%s,%s]";
- static final String SET_DATAFRAME_ITEM ="%s[%s,%s]<-%s";
- static final String SET_LIST_ITEM = "%s[[%s]]<-%s";
- static final String TRUE = "TRUE";
- static final String FALSE = "FALSE";
- static final String AS_INTEGER="as.integer(%s)";
+ String LOAD_RDATA_FILE = "load(\"%s.RData\")";
+ String SAVE_IMAGE = "save.image()";
+ String SAVE_IMAGE_FILE = "save.image(file=\"%s\")";
+ String SET_WORKING_DIRECTORY = "setwd(\"%s\")";
+ String GET_WORKING_DIRECTORY = "getwd()";
+ String DPUT = "dput(%s,file=\"%s\")";
+ String DGET = "%s <- dget(\"%s\")";
+ String REMOVE = "remove(%s)";
+ String LS = "ls()";
+ String CLEAR_SESSION = "rm(list=ls())";
+ String RTRY = "try(%s,silent=TRUE)";
+ String CLASS_ERROR = "try-error";
+ String CLASS_DATAFRAME = "data.frame";
+ String ATTRIBUTE_CLASS = "class";
+ String ATTRIBUTE_NAMES = "names";
+ String ATTRIBUTE_ROWNAMES = "row.names";
+ String GET_ROW_NAMES = "row.names(%s)";
+ String GET_ROW_NAME = "row.names(%s)[%s]";
+ String SET_ROW_NAMES = "row.names(%s)<-c(%s)";
+ String SET_ROW_NAME = "row.names(%s)[%s]<-\"%s\"";
+ String GET_NAMES = "names(%s)";
+ String GET_NAME = "names(%s)[%s]";
+ String SET_NAMES = "names(%s)<-c(%s)";
+ String SET_NAME = "names(%s)[%s]<-\"%s\"";
+ String SET_ATTRIBUTE = "attr(%s,\"%s\")<-%s";
+ String GET_ATTRIBUTE = "attr(%s,\"%s\")";
+ String LENGTH = "length(%s)";
+ String LENGTH_COLUMN = "length(%s[,%s])";
+ String LENGTH_ATTRIBUTES = "length(attributes(%s))";
+ String GET_ATTRIBUTE_NAME = "names(attributes(%s))[%s]";
+ String GET_LIST_ITEM = "%s[[%s]]";
+ String GET_DATAFRAME_ITEM ="%s[%s,%s]";
+ String SET_DATAFRAME_ITEM ="%s[%s,%s]<-%s";
+ String SET_LIST_ITEM = "%s[[%s]]<-%s";
+ String TRUE = "TRUE";
+ String FALSE = "FALSE";
+ String AS_INTEGER="as.integer(%s)";
}
1
0
Author: jcouteau
Date: 2009-08-21 12:35:06 +0200 (Fri, 21 Aug 2009)
New Revision: 143
Modified:
trunk/src/main/java/org/nuiton/j2r/RInstructions.java
Log:
remove unused public modifier
Modified: trunk/src/main/java/org/nuiton/j2r/RInstructions.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/RInstructions.java 2009-08-21 08:24:26 UTC (rev 142)
+++ trunk/src/main/java/org/nuiton/j2r/RInstructions.java 2009-08-21 10:35:06 UTC (rev 143)
@@ -9,45 +9,45 @@
/**
* Load the .RData file located in the working directory
*/
- public static final String LOAD_RDATA = "load(\".RData\")";
+ static final String LOAD_RDATA = "load(\".RData\")";
/**
* Load the file.RData file located in the working directory
*/
- public static final String LOAD_RDATA_FILE = "load(\"%s.RData\")";
- public static final String SAVE_IMAGE = "save.image()";
- public static final String SAVE_IMAGE_FILE = "save.image(file=\"%s\")";
- public static final String SET_WORKING_DIRECTORY = "setwd(\"%s\")";
- public static final String GET_WORKING_DIRECTORY = "getwd()";
- public static final String DPUT = "dput(%s,file=\"%s\")";
- public static final String DGET = "%s <- dget(\"%s\")";
- public static final String REMOVE = "remove(%s)";
- public static final String LS = "ls()";
- public static final String CLEAR_SESSION = "rm(list=ls())";
- public static final String RTRY = "try(%s,silent=TRUE)";
- public static final String CLASS_ERROR = "try-error";
- public static final String CLASS_DATAFRAME = "data.frame";
- public static final String ATTRIBUTE_CLASS = "class";
- public static final String ATTRIBUTE_NAMES = "names";
- public static final String ATTRIBUTE_ROWNAMES = "row.names";
- public static final String GET_ROW_NAMES = "row.names(%s)";
- public static final String GET_ROW_NAME = "row.names(%s)[%s]";
- public static final String SET_ROW_NAMES = "row.names(%s)<-c(%s)";
- public static final String SET_ROW_NAME = "row.names(%s)[%s]<-\"%s\"";
- public static final String GET_NAMES = "names(%s)";
- public static final String GET_NAME = "names(%s)[%s]";
- public static final String SET_NAMES = "names(%s)<-c(%s)";
- public static final String SET_NAME = "names(%s)[%s]<-\"%s\"";
- public static final String SET_ATTRIBUTE = "attr(%s,\"%s\")<-%s";
- public static final String GET_ATTRIBUTE = "attr(%s,\"%s\")";
- public static final String LENGTH = "length(%s)";
- public static final String LENGTH_COLUMN = "length(%s[,%s])";
- public static final String LENGTH_ATTRIBUTES = "length(attributes(%s))";
- public static final String GET_ATTRIBUTE_NAME = "names(attributes(%s))[%s]";
- public static final String GET_LIST_ITEM = "%s[[%s]]";
- public static final String GET_DATAFRAME_ITEM ="%s[%s,%s]";
- public static final String SET_DATAFRAME_ITEM ="%s[%s,%s]<-%s";
- public static final String SET_LIST_ITEM = "%s[[%s]]<-%s";
- public static final String TRUE = "TRUE";
- public static final String FALSE = "FALSE";
- public static final String AS_INTEGER="as.integer(%s)";
+ static final String LOAD_RDATA_FILE = "load(\"%s.RData\")";
+ static final String SAVE_IMAGE = "save.image()";
+ static final String SAVE_IMAGE_FILE = "save.image(file=\"%s\")";
+ static final String SET_WORKING_DIRECTORY = "setwd(\"%s\")";
+ static final String GET_WORKING_DIRECTORY = "getwd()";
+ static final String DPUT = "dput(%s,file=\"%s\")";
+ static final String DGET = "%s <- dget(\"%s\")";
+ static final String REMOVE = "remove(%s)";
+ static final String LS = "ls()";
+ static final String CLEAR_SESSION = "rm(list=ls())";
+ static final String RTRY = "try(%s,silent=TRUE)";
+ static final String CLASS_ERROR = "try-error";
+ static final String CLASS_DATAFRAME = "data.frame";
+ static final String ATTRIBUTE_CLASS = "class";
+ static final String ATTRIBUTE_NAMES = "names";
+ static final String ATTRIBUTE_ROWNAMES = "row.names";
+ static final String GET_ROW_NAMES = "row.names(%s)";
+ static final String GET_ROW_NAME = "row.names(%s)[%s]";
+ static final String SET_ROW_NAMES = "row.names(%s)<-c(%s)";
+ static final String SET_ROW_NAME = "row.names(%s)[%s]<-\"%s\"";
+ static final String GET_NAMES = "names(%s)";
+ static final String GET_NAME = "names(%s)[%s]";
+ static final String SET_NAMES = "names(%s)<-c(%s)";
+ static final String SET_NAME = "names(%s)[%s]<-\"%s\"";
+ static final String SET_ATTRIBUTE = "attr(%s,\"%s\")<-%s";
+ static final String GET_ATTRIBUTE = "attr(%s,\"%s\")";
+ static final String LENGTH = "length(%s)";
+ static final String LENGTH_COLUMN = "length(%s[,%s])";
+ static final String LENGTH_ATTRIBUTES = "length(attributes(%s))";
+ static final String GET_ATTRIBUTE_NAME = "names(attributes(%s))[%s]";
+ static final String GET_LIST_ITEM = "%s[[%s]]";
+ static final String GET_DATAFRAME_ITEM ="%s[%s,%s]";
+ static final String SET_DATAFRAME_ITEM ="%s[%s,%s]<-%s";
+ static final String SET_LIST_ITEM = "%s[[%s]]<-%s";
+ static final String TRUE = "TRUE";
+ static final String FALSE = "FALSE";
+ static final String AS_INTEGER="as.integer(%s)";
}
1
0
r142 - in trunk/src/main/java/org/nuiton/j2r: . jni net types
by jcouteau@users.nuiton.org 21 Aug '09
by jcouteau@users.nuiton.org 21 Aug '09
21 Aug '09
Author: jcouteau
Date: 2009-08-21 10:24:26 +0200 (Fri, 21 Aug 2009)
New Revision: 142
Added:
trunk/src/main/java/org/nuiton/j2r/RInstructions.java
Modified:
trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java
trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java
trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java
trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java
trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
trunk/src/main/java/org/nuiton/j2r/types/RList.java
Log:
Centralize R commands.
Modified: trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java 2009-08-20 15:09:52 UTC (rev 141)
+++ trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -14,7 +14,6 @@
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/
-
package org.nuiton.j2r;
import java.io.File;
@@ -44,7 +43,7 @@
@Override
public void loadRData(File directory) throws RException {
setwd(directory);
- voidEval("load(\".RData\")");
+ voidEval(RInstructions.LOAD_RDATA);
}
/**
@@ -60,7 +59,7 @@
@Override
public void loadRData(File directory, String fileName) throws RException {
setwd(directory);
- voidEval("load(\""+fileName+".RData\")");
+ voidEval(String.format(RInstructions.LOAD_RDATA_FILE, fileName));
}
/**
@@ -74,7 +73,7 @@
@Override
public void saveRData(File directory) throws RException {
setwd(directory);
- voidEval("save.image()");
+ voidEval(RInstructions.SAVE_IMAGE);
}
/**
@@ -89,7 +88,7 @@
@Override
public void saveRData(File directory, String fileName) throws RException {
setwd(directory);
- voidEval("save.image(file=\""+fileName+"\")");
+ voidEval(String.format(RInstructions.SAVE_IMAGE_FILE,fileName));
}
/**
@@ -101,8 +100,8 @@
*/
@Override
public void setwd(File directory) throws RException {
- voidEval("setwd(\"" +
- directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")");
+ voidEval(String.format(RInstructions.SET_WORKING_DIRECTORY,
+ directory.getAbsolutePath().replaceAll("\\\\", "/")));
}
/**
@@ -113,7 +112,7 @@
*/
@Override
public File getwd() throws RException {
- String directory = (String) eval("getwd()");
+ String directory = (String) eval(RInstructions.GET_WORKING_DIRECTORY);
return new File(directory);
}
@@ -129,8 +128,7 @@
*/
@Override
public void dput(String rObject, String outputFileName) throws RException {
- String rInstruction = "dput(%s,file=\"%s\")";
- voidEval(String.format(rInstruction, rObject, outputFileName));
+ voidEval(String.format(RInstructions.DPUT, rObject, outputFileName));
}
/**
@@ -146,8 +144,7 @@
*/
@Override
public void dget(String rObject, String inputFileName) throws RException {
- String rInstruction = "%s <- dget(\"%s\")";
- voidEval(String.format(rInstruction, rObject, inputFileName));
+ voidEval(String.format(RInstructions.DGET, rObject, inputFileName));
}
@@ -166,9 +163,8 @@
File workingdir = getwd();
//Set the destination working directory
setwd(outputFile.getParentFile());
- String rInstruction = "dput(%s,file=\"%s\")";
//Save the R object.
- voidEval(String.format(rInstruction, rObject, outputFile.getName()));
+ voidEval(String.format(RInstructions.DPUT, rObject, outputFile.getName()));
//Go back to previous working directory
setwd(workingdir);
}
@@ -189,9 +185,8 @@
File workingdir = getwd();
//Set the source working directory
setwd(inputFile.getParentFile());
- String rInstruction = "%s <- dget(\"%s\")";
//Load the inputFile
- voidEval(String.format(rInstruction, rObject, inputFile.getName()));
+ voidEval(String.format(RInstructions.DGET, rObject, inputFile.getName()));
//Go back to the previous working directory
setwd(workingdir);
}
@@ -205,7 +200,7 @@
*/
@Override
public void remove(String rObject) throws RException {
- voidEval("remove(" + rObject + ")");
+ voidEval(String.format(RInstructions.REMOVE, rObject));
}
/**
@@ -255,7 +250,7 @@
*/
@Override
public String[] ls() throws RException {
- String[] rObjects = (String[]) eval("ls()");
+ String[] rObjects = (String[]) eval(RInstructions.LS);
return rObjects;
}
@@ -267,7 +262,7 @@
*/
@Override
public void clearSession() throws RException {
- voidEval("rm(list=ls())");
+ voidEval(RInstructions.CLEAR_SESSION);
}
/**
Added: trunk/src/main/java/org/nuiton/j2r/RInstructions.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/RInstructions.java (rev 0)
+++ trunk/src/main/java/org/nuiton/j2r/RInstructions.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -0,0 +1,53 @@
+package org.nuiton.j2r;
+
+/**
+ *
+ * @author couteau
+ */
+public interface RInstructions {
+
+ /**
+ * Load the .RData file located in the working directory
+ */
+ public static final String LOAD_RDATA = "load(\".RData\")";
+ /**
+ * Load the file.RData file located in the working directory
+ */
+ public static final String LOAD_RDATA_FILE = "load(\"%s.RData\")";
+ public static final String SAVE_IMAGE = "save.image()";
+ public static final String SAVE_IMAGE_FILE = "save.image(file=\"%s\")";
+ public static final String SET_WORKING_DIRECTORY = "setwd(\"%s\")";
+ public static final String GET_WORKING_DIRECTORY = "getwd()";
+ public static final String DPUT = "dput(%s,file=\"%s\")";
+ public static final String DGET = "%s <- dget(\"%s\")";
+ public static final String REMOVE = "remove(%s)";
+ public static final String LS = "ls()";
+ public static final String CLEAR_SESSION = "rm(list=ls())";
+ public static final String RTRY = "try(%s,silent=TRUE)";
+ public static final String CLASS_ERROR = "try-error";
+ public static final String CLASS_DATAFRAME = "data.frame";
+ public static final String ATTRIBUTE_CLASS = "class";
+ public static final String ATTRIBUTE_NAMES = "names";
+ public static final String ATTRIBUTE_ROWNAMES = "row.names";
+ public static final String GET_ROW_NAMES = "row.names(%s)";
+ public static final String GET_ROW_NAME = "row.names(%s)[%s]";
+ public static final String SET_ROW_NAMES = "row.names(%s)<-c(%s)";
+ public static final String SET_ROW_NAME = "row.names(%s)[%s]<-\"%s\"";
+ public static final String GET_NAMES = "names(%s)";
+ public static final String GET_NAME = "names(%s)[%s]";
+ public static final String SET_NAMES = "names(%s)<-c(%s)";
+ public static final String SET_NAME = "names(%s)[%s]<-\"%s\"";
+ public static final String SET_ATTRIBUTE = "attr(%s,\"%s\")<-%s";
+ public static final String GET_ATTRIBUTE = "attr(%s,\"%s\")";
+ public static final String LENGTH = "length(%s)";
+ public static final String LENGTH_COLUMN = "length(%s[,%s])";
+ public static final String LENGTH_ATTRIBUTES = "length(attributes(%s))";
+ public static final String GET_ATTRIBUTE_NAME = "names(attributes(%s))[%s]";
+ public static final String GET_LIST_ITEM = "%s[[%s]]";
+ public static final String GET_DATAFRAME_ITEM ="%s[%s,%s]";
+ public static final String SET_DATAFRAME_ITEM ="%s[%s,%s]<-%s";
+ public static final String SET_LIST_ITEM = "%s[[%s]]<-%s";
+ public static final String TRUE = "TRUE";
+ public static final String FALSE = "FALSE";
+ public static final String AS_INTEGER="as.integer(%s)";
+}
Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-08-20 15:09:52 UTC (rev 141)
+++ trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -26,6 +26,7 @@
import org.nuiton.j2r.REngine;
import org.nuiton.j2r.REngineAbstract;
import org.nuiton.j2r.RException;
+import org.nuiton.j2r.RInstructions;
import org.nuiton.j2r.types.RDataFrame;
import org.nuiton.j2r.types.RList;
import org.rosuda.JRI.REXP;
@@ -56,8 +57,6 @@
*/
private List<String> rInstructions = new LinkedList<String>();
- private String evaluation = "try(%s,silent=TRUE)";
-
/**
* Initialize the R engine.
*
@@ -99,16 +98,17 @@
public Object eval(String expr) throws RException {
REXP result = null;
if (log.isDebugEnabled()) {
- log.debug(String.format(evaluation,expr));
+ log.debug(String.format(RInstructions.RTRY, expr));
}
//encapsulate the R expression in a try method/object to get the R error
//message if thrown
- result = engine.eval(String.format(evaluation,expr));
- if (result.getAttribute("class") != null) {
+ result = engine.eval(String.format(RInstructions.RTRY, expr));
+ if (result.getAttribute(RInstructions.ATTRIBUTE_CLASS) != null) {
//if the "class" attribute of the R expression is "try-error"
//throw a new exception with the error message from R.
- String classe = result.getAttribute("class").asString();
- if (classe.equals("try-error")) {
+ String klass =
+ result.getAttribute(RInstructions.ATTRIBUTE_CLASS).asString();
+ if (klass.equals(RInstructions.CLASS_ERROR)) {
throw new RException(result.asString());
}
}
@@ -210,13 +210,14 @@
//dataframes, lists and vectors are recognized as vectors.
//get the class of the vector (to successfully detect data.frames)
String klass = "";
- REXP klassAttribute = rexp.getAttribute("class");
+ REXP klassAttribute = rexp.getAttribute(
+ RInstructions.ATTRIBUTE_CLASS);
if (klassAttribute != null) {
klass = klassAttribute.asString();
}
//get REXP asList to successfully detect lists.
org.rosuda.JRI.RList list = rexp.asList();
- if (klass.equals("data.frame")) {
+ if (klass.equals(RInstructions.CLASS_DATAFRAME)) {
//if rexp is a data.frame
RDataFrame temp = new RDataFrame((REngine) this);
@@ -233,7 +234,7 @@
REXP tempREXP = dataList.at(i);
Object[] convertedREXP = (Object[]) convertResult(
tempREXP);
- templist=Arrays.asList(convertedREXP);
+ templist = Arrays.asList(convertedREXP);
//add this list to the data list.
data.add(templist);
@@ -242,11 +243,11 @@
//gotten from rexp. It has no variable name so throws a
//RException.
temp = new RDataFrame((REngine) this, rexp.getAttribute(
- "names").asStringArray(),
- rexp.getAttribute("row.names").asStringArray(),
+ RInstructions.ATTRIBUTE_NAMES).asStringArray(),
+ rexp.getAttribute(RInstructions.ATTRIBUTE_ROWNAMES).asStringArray(),
data, "");
result = temp;
- } else if (list!=null) {
+ } else if (list != null) {
RList temp = new RList((REngine) this);
List<Object> data = new ArrayList<Object>();
org.rosuda.JRI.RList dataList = rexp.asList();
@@ -264,7 +265,7 @@
//RException.
try {
temp = new RList(
- rexp.getAttribute("names").asStringArray(),
+ rexp.getAttribute(RInstructions.ATTRIBUTE_NAMES).asStringArray(),
data, (REngine) this, "");
} catch (RException re) {
//don't propagate the error as it is normal. Log it for debug.
@@ -322,14 +323,14 @@
//encapsulate the R expression in a try method/object to get the R error
//message if thrown
if (log.isDebugEnabled()) {
- log.debug(String.format(evaluation,expr));
+ log.debug(String.format(RInstructions.RTRY, expr));
}
- REXP r = engine.eval(String.format(evaluation,expr));
- if (r.getAttribute("class") != null) {
+ REXP r = engine.eval(String.format(RInstructions.RTRY, expr));
+ if (r.getAttribute(RInstructions.ATTRIBUTE_CLASS) != null) {
//if the "class" attribute of the R expression is "try-error"
//throw a new exception with the error message from R.
- String classe = r.getAttribute("class").asString();
- if (classe.equals("try-error")) {
+ String classe = r.getAttribute(RInstructions.ATTRIBUTE_CLASS).asString();
+ if (classe.equals(RInstructions.CLASS_ERROR)) {
throw new RException(r.asString());
}
}
Modified: trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java 2009-08-20 15:09:52 UTC (rev 141)
+++ trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -34,6 +34,7 @@
import org.nuiton.j2r.REngine;
import org.nuiton.j2r.REngineAbstract;
import org.nuiton.j2r.RException;
+import org.nuiton.j2r.RInstructions;
import org.nuiton.j2r.types.RDataFrame;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
@@ -67,8 +68,6 @@
*/
private List<String> rInstructions = new LinkedList<String>();
- private String evaluation = "try(%s,silent=TRUE)";
-
/**
* Initialize the engine.
*
@@ -150,10 +149,10 @@
//Encapsulate the R expression to get back the R error message
//if thrown.
if (log.isDebugEnabled()) {
- log.debug(String.format(evaluation,expr));
+ log.debug(String.format(RInstructions.RTRY, expr));
}
- result = conn.eval(String.format(evaluation,expr));
- if (result.inherits("try-error")) {
+ result = conn.eval(String.format(RInstructions.RTRY, expr));
+ if (result.inherits(RInstructions.CLASS_ERROR)) {
//If the R expression is an error, throw an expression with the
//real R error message
throw new RException(result.asString());
@@ -255,7 +254,7 @@
} else if (rexp.isNull()) {
//if rexp contains a null R expression
return null;
- } else if (rexp.inherits("data.frame")) {
+ } else if (rexp.inherits(RInstructions.CLASS_DATAFRAME)) {
//if rexp is a data.frame
RDataFrame temp = new RDataFrame((REngine) this);
@@ -279,8 +278,8 @@
//gotten from rexp. It has no variable name so throws a
//RException.
temp = new RDataFrame((REngine) this, rexp.getAttribute(
- "names").asStrings(),
- rexp.getAttribute("row.names").asStrings(),
+ RInstructions.ATTRIBUTE_NAMES).asStrings(),
+ rexp.getAttribute(RInstructions.ATTRIBUTE_ROWNAMES).asStrings(),
data, "");
result = temp;
} else if (rexp.isList()) {
@@ -301,7 +300,8 @@
//gotten from rexp. It has no variable name so throws a
//RException.
try {
- temp = new RList(rexp.getAttribute("names").asStrings(),
+ temp = new RList(rexp.getAttribute(
+ RInstructions.ATTRIBUTE_NAMES).asStrings(),
data, (REngine) this, "");
} catch (RException re) {
//don't propagate the error as it is normal. Log it for debug.
@@ -357,12 +357,12 @@
} else {
try {
if (log.isDebugEnabled()) {
- log.debug(String.format(evaluation,expr));
+ log.debug(String.format(RInstructions.RTRY, expr));
}
//Encapsulate the R expression to get back the R error message
//if thrown.
- REXP r = conn.eval(String.format(evaluation,expr));
- if (r.inherits("try-error")) {
+ REXP r = conn.eval(String.format(RInstructions.RTRY, expr));
+ if (r.inherits(RInstructions.CLASS_ERROR)) {
//If the R expression is an error, throw an expression with the
//real R error message
throw new RException(r.asString());
Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2009-08-20 15:09:52 UTC (rev 141)
+++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -32,6 +32,7 @@
import org.apache.commons.logging.LogFactory;
import org.nuiton.j2r.REngine;
import org.nuiton.j2r.RException;
+import org.nuiton.j2r.RInstructions;
/**
* Java implementation of the R data.frame
@@ -47,10 +48,6 @@
private List<String> rowNames;
//Vector containing the vectors of the data.frame
private List<List<? extends Object>> data;
- private String getRowNamesString = "row.names(%s)";
- private String getRowNameString = "row.names(%s)[%s]";
- private String setRowNamesString = "row.names(%s)<-c(%s)";
- private String setRowNameString = "row.names(%s)[%s]<-\"%s\"";
/**
* Constructor
@@ -199,7 +196,7 @@
if (engine.isAutoCommit()) {
//Get back the names from R.
String[] rowNamesArray = (String[]) engine.eval(String.format(
- getRowNamesString, this.variable));
+ RInstructions.GET_ROW_NAMES, this.variable));
//Check if size is correct, if yes, modify local data.
if (rowNamesArray.length <= this.data.get(0).size()) {
@@ -237,7 +234,7 @@
if (engine.isAutoCommit()) {
//Get back the names from R.
String name = (String) engine.eval(String.format(
- getRowNameString, this.variable, y + 1));
+ RInstructions.GET_ROW_NAME, this.variable, y + 1));
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -280,8 +277,8 @@
}
rowNamesString += "\"" + rowNames.get(i) + "\"";
}
- String rexp = String.format(setRowNamesString, this.variable,
- rowNamesString);
+ String rexp = String.format(RInstructions.SET_ROW_NAMES,
+ this.variable, rowNamesString);
//Send the r instruction to the engine.
engine.voidEval(rexp);
@@ -310,8 +307,8 @@
rowNames.set(x, rowName);
//create the r instruction (row.names(var)[x]<-"rowName")
- String rexp = String.format(setRowNameString, this.variable, x + 1,
- rowName);
+ String rexp = String.format(RInstructions.SET_ROW_NAME,
+ this.variable, x + 1, rowName);
//Send the R instruction to the engine.
engine.voidEval(rexp);
@@ -347,16 +344,16 @@
} else if (data.get(i).get(0) instanceof Boolean) {
for (int j = 0; j < data.get(i).size(); j++) {
if ((Boolean) data.get(i).get(j)) {
- returnString += "TRUE,";
+ returnString += RInstructions.TRUE + ",";
} else {
- returnString += "FALSE,";
+ returnString += RInstructions.FALSE + ",";
}
}
} else if (data.get(i).get(0) instanceof Integer) {
for (int j = 0; j < data.get(i).size(); j++) {
- returnString += "as.integer(" + data.get(i).get(j) +
- "),";
+ returnString += String.format(RInstructions.AS_INTEGER,
+ data.get(i).get(j)) + ",";
}
} else {
for (int j = 0; j < data.get(i).size(); j++) {
@@ -403,34 +400,33 @@
checkVariable();
checkX(x);
checkY(y);
- String setString = "%s[%s,%s]<-%s";
try {
if (this.data.get(x).get(y) instanceof Double) {
((ArrayList<Double>) this.data.get(x)).set(y, (Double) data);
- engine.voidEval(String.format(setString, this.variable, y + 1,
+ engine.voidEval(String.format(RInstructions.SET_DATAFRAME_ITEM,
+ this.variable, y + 1,
x + 1,
data));
} else if (this.data.get(x).get(y) instanceof Boolean) {
((ArrayList<Boolean>) this.data.get(x)).set(y, (Boolean) data);
if ((Boolean) data) {
- engine.voidEval(String.format(setString, this.variable, y +
- 1,
- x + 1, "TRUE"));
+ engine.voidEval(String.format(
+ RInstructions.SET_DATAFRAME_ITEM, this.variable,
+ y + 1, x + 1, RInstructions.TRUE));
} else {
- engine.voidEval(String.format(setString, this.variable, y +
- 1,
- x + 1, "FALSE"));
+ engine.voidEval(String.format(
+ RInstructions.SET_DATAFRAME_ITEM, this.variable,
+ y + 1, x + 1, RInstructions.FALSE));
}
} else if (this.data.get(x).get(y) instanceof String) {
((ArrayList<String>) this.data.get(x)).set(y, (String) data);
- engine.voidEval(String.format(setString, this.variable, y + 1,
- x + 1,
- "\"" + data + "\""));
+ engine.voidEval(String.format(RInstructions.SET_DATAFRAME_ITEM,
+ this.variable, y + 1, x + 1, "\"" + data + "\""));
} else if (this.data.get(x).get(y) instanceof Integer) {
((ArrayList<Integer>) this.data.get(x)).set(y, (Integer) data);
- engine.voidEval(String.format(setString, this.variable, y + 1,
- x + 1,
- "as.integer(" + data + ")"));
+ engine.voidEval(String.format(RInstructions.SET_DATAFRAME_ITEM,
+ this.variable, y + 1, x + 1, String.format(
+ RInstructions.AS_INTEGER, data)));
} else {
throw new ArrayStoreException(
"The data.frame does not accept this type on those coordinates : " +
@@ -461,8 +457,9 @@
checkX(x);
checkY(y);
if (engine.isAutoCommit()) {
- Object returnObject = engine.eval(this.variable + "[" + (y + 1) +
- "," + (x + 1) + "]");
+ Object returnObject = engine.eval(String.format(
+ RInstructions.GET_DATAFRAME_ITEM, this.variable, y + 1,
+ x + 1));
if (returnObject instanceof String) {
((ArrayList<String>) this.data.get(x)).set(y,
(String) returnObject);
@@ -543,44 +540,47 @@
//update row names
String[] rowNamesArray = (String[]) engine.eval(String.format(
- getRowNamesString, this.variable));
+ RInstructions.GET_ROW_NAMES, this.variable));
for (int i = 0; i < rowNamesArray.length; i++) {
rowNames.add(rowNamesArray[i]);
}
//update names
String[] namesArray = (String[]) engine.eval(String.format(
- getNamesString, this.variable));
+ RInstructions.GET_NAMES, this.variable));
for (int i = 0; i < namesArray.length; i++) {
names.add(namesArray[i]);
}
//update data
- Integer dataframelength = (Integer) engine.eval("length(" +
- this.variable + ")");
+ Integer dataframelength = (Integer) engine.eval(String.format(
+ RInstructions.LENGTH, this.variable));
for (int i = 0; i < dataframelength; i++) {
- Integer arrayListLength = (Integer) engine.eval("length(" +
- this.variable + "[," + (i + 1) + "])");
+ Integer arrayListLength = (Integer) engine.eval(String.format(
+ RInstructions.LENGTH_COLUMN, this.variable, i + 1));
ArrayList<Serializable> thisColumn =
new ArrayList<Serializable>();
for (int j = 0; j < arrayListLength; j++) {
- thisColumn.add((Serializable) engine.eval(this.variable +
- "[" + (j + 1) + "," + (i + 1) + "]"));
+ thisColumn.add((Serializable) engine.eval(String.format(
+ RInstructions.GET_DATAFRAME_ITEM, this.variable,
+ j + 1, i + 1)));
}
data.add(thisColumn);
+
}
//update attributes
- Integer attributeslength = (Integer) engine.eval(
- "length(attributes(" + this.variable + "))");
+ Integer attributeslength = (Integer) engine.eval(String.format(
+ RInstructions.LENGTH_ATTRIBUTES, this.variable));
- for (int i = 0; i < attributeslength; i++) {
- String key = (String) engine.eval("names(attributes(" +
- this.variable + "))[" + (i + 1) + "]");
+ for (int i = 0;
+ i < attributeslength; i++) {
+ String key = (String) engine.eval(String.format(
+ RInstructions.GET_ATTRIBUTE_NAME, this.variable, i + 1));
- String attribute = (String) engine.eval("toString(attributes(" +
- this.variable + ")$" + key + ")");
+ String attribute = (String) engine.eval("toString(" + String.format(
+ RInstructions.GET_ATTRIBUTE, this.variable, key) + ")");
attributes.put(key, attribute);
}
}
@@ -803,4 +803,4 @@
throw new RException("Not supported type");
}
}
-}
+}
\ No newline at end of file
Modified: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-08-20 15:09:52 UTC (rev 141)
+++ trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -23,6 +23,7 @@
import org.apache.commons.logging.LogFactory;
import org.nuiton.j2r.REngine;
import org.nuiton.j2r.RException;
+import org.nuiton.j2r.RInstructions;
/**
* Abstract class for REXP interface, in order to avoid duplicate code for attributes related methods.
@@ -44,22 +45,10 @@
//Vector containing the names of the data.frame vectors
protected List<String> names;
-
- //R instruction to assign one attribute value.
- protected String assignAttribute = "attr(%s,\"%s\")<-%s";
-
- //R instruction to get one attribute value.
- protected String getAttribute = "attr(%s,\"%s\")";
- protected String getNamesString = "names(%s)";
- protected String getNameString = "names(%s)[%s]";
protected String indexExceptionText =
"Cannot perform operation, index is superior to size.\nIndex : %s\nSize : %s";
protected String dataInconsistencyText =
"There is an inconsistency between the local and distant data.\nLocal data size : %s\nDistant data size : %s";
- protected String setNamesString = "names(%s)<-c(%s)";
- //R instruction to assign one name
- protected String setNameString = "names(%s)[%s]<-\"%s\"";
- //R instruction to get the names
protected String noVariable = "No variable name given";
/** {@inheritDoc} */
@@ -76,29 +65,35 @@
for (int i = 0; i < keys.length; i++) {
//foreach type of attribute, adapt the R instruction.
if (this.attributes.get(keys[i]) instanceof String) {
- engine.eval(String.format(assignAttribute, this.variable,
- keys[i], "\"" + this.attributes.get(keys[i]) + "\""));
+ engine.eval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable, keys[i],
+ "\"" + this.attributes.get(keys[i]) + "\""));
//String, value between quotes
} else if (this.attributes.get(keys[i]) instanceof Double) {
- engine.eval(String.format(assignAttribute, this.variable,
+ engine.eval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable,
keys[i], this.attributes.get(keys[i])));
} else if (this.attributes.get(keys[i]) instanceof Integer) {
- engine.eval(String.format(assignAttribute, this.variable,
- keys[i], "as.integer(" +
- this.attributes.get(keys[i]) + ")"));
+ engine.eval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable,
+ keys[i], String.format(RInstructions.AS_INTEGER,
+ this.attributes.get(keys[i]))));
//Integer, value formated by the R function : as.integer()
} else if (this.attributes.get(keys[i]) instanceof Boolean) {
if ((Boolean) this.attributes.get(keys[i])) {
- engine.eval(String.format(assignAttribute, this.variable,
- keys[i], "TRUE"));
+ engine.eval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable,
+ keys[i], RInstructions.TRUE));
//Boolean true replaced by TRUE
} else {
- engine.eval(String.format(assignAttribute, this.variable,
- keys[i], "FALSE"));
+ engine.eval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable,
+ keys[i], RInstructions.FALSE));
//Boolean false replaced by REPLACE
}
} else if (this.attributes.get(keys[i]) instanceof REXP) {
- engine.eval(String.format(assignAttribute, this.variable,
+ engine.eval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable,
keys[i],
((REXP) this.attributes.get(keys[i])).toRString()));
//REXP replaced by the result of their toRString() method.
@@ -119,13 +114,13 @@
public Map<String, Object> getAttributes() throws RException {
if (engine.isAutoCommit()) {
//get the number of attributes
- Integer attributeslength = (Integer) engine.eval(
- "length(attributes(" + this.variable + "))");
+ Integer attributeslength = (Integer) engine.eval(String.format(
+ RInstructions.LENGTH_ATTRIBUTES, this.variable));
for (int i = 0; i < attributeslength; i++) {
//Get the name of the i attribute
- String key = (String) engine.eval("names(attributes(" +
- this.variable + "))[" + (i + 1) + "]");
+ String key = (String) engine.eval(String.format(
+ RInstructions.GET_ATTRIBUTE_NAME, this.variable, i + 1));
//Get the attribute
getAttribute(key);
@@ -141,23 +136,22 @@
Object returnedAttribute;
//test if the attribute is a data.frame
if ((Boolean) engine.eval("is.data.frame(" + String.format(
- getAttribute, this.variable, attribute) + ")")) {
+ RInstructions.GET_ATTRIBUTE, this.variable, attribute) + ")")) {
//if attribute is a list, import the data.frame from R
returnedAttribute = new RDataFrame(engine);
((RDataFrame) returnedAttribute).getFrom(String.format(
- getAttribute, this.variable, attribute));
+ RInstructions.GET_ATTRIBUTE, this.variable, attribute));
//test if the attribute is a list
} else if ((Boolean) engine.eval("is.list(" + String.format(
- getAttribute, this.variable, attribute) + ")")) {
-
+ RInstructions.GET_ATTRIBUTE, this.variable, attribute) + ")")) {
//if attribute is a list, import it from R.
returnedAttribute = new RList(engine);
- ((RList) returnedAttribute).getFrom(String.format(getAttribute,
- this.variable, attribute));
+ ((RList) returnedAttribute).getFrom(String.format(
+ RInstructions.GET_ATTRIBUTE, this.variable, attribute));
} else {
//else attribute is imported as any other R expression.
- returnedAttribute = engine.eval(String.format(getAttribute,
- this.variable, attribute));
+ returnedAttribute = engine.eval(String.format(
+ RInstructions.GET_ATTRIBUTE, this.variable, attribute));
}
//put the attribute in the attribute list
@@ -188,28 +182,29 @@
boolean isOK = false;
if (value instanceof String) {
isOK = true;
- engine.voidEval(String.format(assignAttribute, this.variable,
- attribute, "\"" + value + "\""));
+ engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable, attribute, "\"" + value + "\""));
//if String, between quotes
} else if (value instanceof Double) {
isOK = true;
- engine.voidEval(String.format(assignAttribute, this.variable,
- attribute, value));
+ engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable, attribute, value));
} else if (value instanceof Integer) {
isOK = true;
- engine.voidEval(String.format(assignAttribute, this.variable,
- attribute, "as.integer(" + value + ")"));
+ engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable, attribute, String.format(
+ RInstructions.AS_INTEGER, value)));
//If integer in the R function : as.integer()
} else if (value instanceof Boolean) {
isOK = true;
//R boolean is upper case
- engine.voidEval(String.format(assignAttribute, this.variable,
- attribute, value.toString().toUpperCase()));
+ engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable, attribute, value.toString().toUpperCase()));
} else if (value instanceof REXP) {
isOK = true;
- engine.voidEval(String.format(assignAttribute, this.variable,
- attribute, ((REXP) value).toRString()));
- //if REXP, use its method toRString()
+ engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE,
+ this.variable, attribute, ((REXP) value).toRString()));
+ //if REXP, use its method toRString()
} else {
log.warn("This attribute is not valid : " + attribute + " ; " +
value + ". It will not be processed");
@@ -267,7 +262,7 @@
if (engine.isAutoCommit()) {
//Get back the names from R.
String[] namesArray = (String[]) engine.eval(String.format(
- getNamesString, this.variable));
+ RInstructions.GET_NAMES, this.variable));
//Check if size is correct, if yes, modify local data.
checkX(namesArray.length);
@@ -303,8 +298,8 @@
if (x < names.size()) {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String name = (String) engine.eval(String.format(getNameString,
- this.variable, x + 1));
+ String name = (String) engine.eval(String.format(
+ RInstructions.GET_NAME, this.variable, x + 1));
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -387,7 +382,7 @@
}
namesString += "\"" + names.get(i) + "\"";
}
- String rexp = String.format(setNamesString, this.variable,
+ String rexp = String.format(RInstructions.SET_NAMES, this.variable,
namesString);
//Send the r instruction to the engine.
@@ -425,7 +420,7 @@
}
//create the r instruction (names(var)[x]<-"name")
- String rexp = String.format(setNameString, this.variable, x + 1,
+ String rexp = String.format(RInstructions.SET_NAME, this.variable, x + 1,
name);
//Send the R instruction to the engine.
Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-08-20 15:09:52 UTC (rev 141)
+++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-08-21 08:24:26 UTC (rev 142)
@@ -24,6 +24,7 @@
import org.apache.commons.logging.LogFactory;
import org.nuiton.j2r.REngine;
import org.nuiton.j2r.RException;
+import org.nuiton.j2r.RInstructions;
/**
* Java implementation of the R List.
@@ -34,11 +35,9 @@
//Content of the list (eg elements of the list in R)
private List<Object> data;
-
private Log log = LogFactory.getLog(RDataFrame.class);
//TODO implement the logger use !
-
/**
* Create a default RList linked to a R engine (the List is not initialized
* in R.)
@@ -93,7 +92,7 @@
String variable) throws RException {
super();
this.names = new ArrayList<String>();
- for (int i=0;i<names.length;i++){
+ for (int i = 0; i < names.length; i++) {
this.names.add(names[i]);
}
this.data = (ArrayList<Object>) data;
@@ -114,8 +113,6 @@
}
}
-
-
/**
* Method to export the list in a String for evaluation in R.
*
@@ -136,12 +133,13 @@
returnString += "\"" + data.get(i) + "\",";
} else if ((data.get(i) instanceof Boolean) &&
((Boolean) data.get(i))) {
- returnString += "TRUE,";
+ returnString += RInstructions.TRUE + ",";
} else if ((data.get(i) instanceof Boolean) &&
(!(Boolean) data.get(i))) {
- returnString += "FALSE,";
+ returnString += RInstructions.FALSE + ",";
} else if (data.get(i) instanceof Integer) {
- returnString += "as.integer(" + data.get(i) + "),";
+ returnString += String.format(RInstructions.AS_INTEGER,
+ data.get(i)) + ",";
} else if (data.get(i) instanceof REXP) {
returnString += ((REXP) (data.get(i))).toRString() + ",";
} else {
@@ -182,23 +180,33 @@
}
if (data instanceof Double) {
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-" + data);
+ //in R, all data is numeric, no need to transformation, simple
+ //assignation
+ engine.voidEval(String.format(RInstructions.SET_LIST_ITEM,
+ this.variable, x + 1, data));
} else if (data instanceof Integer) {
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-as.integer(" +
- data + ")");
+ //transform the data in R integer
+ String asInteger = String.format(RInstructions.AS_INTEGER, data);
+ //assign the transformed data
+ engine.voidEval(String.format(RInstructions.SET_LIST_ITEM,
+ this.variable, x + 1, asInteger));
} else if (data instanceof Boolean) {
if ((Boolean) data) {
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-TRUE");
+ engine.voidEval(String.format(RInstructions.SET_LIST_ITEM,
+ this.variable, x + 1, RInstructions.TRUE));
} else {
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-FALSE");
+ engine.voidEval(String.format(RInstructions.SET_LIST_ITEM,
+ this.variable, x + 1, RInstructions.FALSE));
}
} else if (data instanceof String) {
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-\"" + data +
- "\"");
+ engine.voidEval(String.format(RInstructions.SET_LIST_ITEM,
+ this.variable, x + 1, "\"" + data + "\""));
} else if (data instanceof REXP) {
+ //Send the REXP to R
engine.voidEval(((REXP) data).toRString());
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-" +
- ((REXP) data).getVariable());
+ //Set the REXP as list item
+ engine.voidEval(String.format(RInstructions.SET_LIST_ITEM,
+ this.variable, x + 1, ((REXP) data).getVariable()));
}
}
@@ -214,8 +222,8 @@
public Object get(int x) throws RException {
checkX(x);
if (engine.isAutoCommit()) {
- Object returnObject = engine.eval(this.variable + "[[" + (x + 1) +
- "]]");
+ Object returnObject = engine.eval(String.format(
+ RInstructions.GET_LIST_ITEM, this.variable, x + 1));
if (returnObject instanceof String) {
this.data.set(x, (String) returnObject);
} else if (returnObject instanceof Double) {
@@ -281,27 +289,29 @@
//update names
String[] namesArray = (String[]) engine.eval(String.format(
- getNamesString, this.variable));
+ RInstructions.GET_NAMES, this.variable));
for (int i = 0; i < namesArray.length; i++) {
names.add(namesArray[i]);
}
//update data
- int length = (Integer) engine.eval("length(" + variable + ")");
+ int length = (Integer) engine.eval(String.format(RInstructions.LENGTH,
+ variable));
for (int i = 0; i < length; i++) {
- data.add(engine.eval(this.variable + "[[" + (i + 1) + "]]"));
+ data.add(engine.eval(String.format(RInstructions.GET_LIST_ITEM,
+ this.variable, i + 1)));
}
//update attributes
- Integer attributeslength = (Integer) engine.eval("length(attributes(" +
- this.variable + "))");
+ Integer attributeslength = (Integer) engine.eval(String.format(
+ RInstructions.LENGTH_ATTRIBUTES, this.variable));
for (int i = 0; i < attributeslength; i++) {
- String key = (String) engine.eval("names(attributes(" +
- this.variable + "))[" + (i + 1) + "]");
+ String key = (String) engine.eval(String.format(
+ RInstructions.GET_ATTRIBUTE_NAME, this.variable, i + 1));
- Object attribute = engine.eval(
- "attributes(" + this.variable + ")$" + key);
+ Object attribute = engine.eval(String.format(
+ RInstructions.GET_ATTRIBUTE, this.variable, key));
attributes.put(key, attribute);
}
}
1
0
Author: tchemit
Date: 2009-08-20 17:09:52 +0200 (Thu, 20 Aug 2009)
New Revision: 141
Modified:
trunk/pom.xml
Log:
use thirdparty artifacts
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2009-08-16 09:08:48 UTC (rev 140)
+++ trunk/pom.xml 2009-08-20 15:09:52 UTC (rev 141)
@@ -25,26 +25,26 @@
</dependency>
<!-- dependency>
- <groupId>externallib</groupId>
+ <groupId>org.nuiton.thirdparty</groupId>
<artifactId>JRclient</artifactId>
<version>RF503</version>
<scope>compile</scope>
</dependency-->
<dependency>
- <groupId>externallib</groupId>
+ <groupId>org.nuiton.thirdparty</groupId>
<artifactId>JRI</artifactId>
<version>0.7-0</version>
<scope>compile</scope>
</dependency>
<dependency>
- <groupId>externallib</groupId>
+ <groupId>org.nuiton.thirdparty</groupId>
<artifactId>REngine</artifactId>
<version>${r.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
- <groupId>externallib</groupId>
+ <groupId>org.nuiton.thirdparty</groupId>
<artifactId>Rserve</artifactId>
<version>${r.version}</version>
<scope>compile</scope>
1
0
Author: tchemit
Date: 2009-08-16 11:08:48 +0200 (Sun, 16 Aug 2009)
New Revision: 140
Modified:
trunk/src/site/site.xml
Log:
update skin
Modified: trunk/src/site/site.xml
===================================================================
--- trunk/src/site/site.xml 2009-08-13 09:29:59 UTC (rev 139)
+++ trunk/src/site/site.xml 2009-08-16 09:08:48 UTC (rev 140)
@@ -6,7 +6,7 @@
<skin>
<groupId>org.nuiton</groupId>
<artifactId>maven-nuiton-skin</artifactId>
- <version>1.0.0-rc-1</version>
+ <version>1.0.0</version>
</skin>
<bannerLeft>
1
0
Bonjour à tous,
Dans l'optique de dissocier au maximum les projets libres initiés par
la société Code Lutin de celle-ci et permettre une meilleur implication
de la communauté du libre dans ces derniers, nous vous annonçons que le
site de développement du projet n'est plus hébergé sur le labs
libre-entreprise mais sur la plateforme www.nuiton.org. La plupart de la
migration a été réalisée, nous vous invitons à utiliser d'ors et déjà la
plateforme http://www.nuiton.org (le projet sur le labs libre-entreprise n'est
plus accessible publiquement)
Les listes de diffusions ont été migrées et tous les utilisateurs ont
normalement du être réabonnés aux nouvelles listes.
La nouvelle adresse de cette liste: nuiton-j2r-commits(a)list.nuiton.org
Nous sommes désolés des désagréments causés et vous remercions de votre
soutien.
Cordialement,
L'équipe de développement.
----------------------------
Dear all,
In order to separate the libre projects that Code Lutin has initiated
and the company itself, and also to encourage the implication of the
libre community, we recently moved the project's development site from
the libre-entreprise labs to the www.nuiton.org platform. Most of the
migration is complete, we invite you to use, from now on, the platform
http://www.nuiton.org (the project on libre-entreprise labs is now longer
publicly available).
The mailing-lists have been migrated and everybody should have been
automatically subscribing the new lists.
New mail address for this list: nuiton-j2r-commits(a)list.nuiton.org
We are sorry for the inconvenience, and thank you for your support.
Best regards,
The development team.
1
0
[LutinJ2R-commits] r118 - trunk/src/main/java/org/nuiton/j2r/types
by jcouteau@users.labs.libre-entreprise.org 31 Jul '09
by jcouteau@users.labs.libre-entreprise.org 31 Jul '09
31 Jul '09
Author: jcouteau
Date: 2009-07-31 16:27:55 +0200 (Fri, 31 Jul 2009)
New Revision: 118
Modified:
trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
Log:
Refactor and add some comments
Modified: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-07-31 09:18:50 UTC (rev 117)
+++ trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-07-31 14:27:55 UTC (rev 118)
@@ -41,8 +41,14 @@
//Engine used for R instructions.
protected REngine engine;
+ //R instruction to assign one attribute value.
+ private String assignAttribute = "attr(%s,\"%s\")<-%s";
+
+ //R instruction to get one attribute value.
+ private String getAttribute = "attr(%s,\"%s\")";
+
/**
- * Always need to be implemented by extending classes
+ * Always need to be implemented by extending classes.
* @return null
* @throws org.nuiton.j2r.RException
*/
@@ -60,91 +66,110 @@
public void getFrom(String variable) throws RException {
}
+ /** {@inheritDoc} */
@Override
public void setAttributes(Map<String, Object> attributes) throws RException {
+ //TODO should be useful to test the validity of attributes before
+ //assigning it.
this.attributes = attributes;
-
if ((this.attributes != null) && (!this.attributes.isEmpty())) {
Set<String> keyset = attributes.keySet();
String[] keys = keyset.toArray(new String[0]);
for (int i = 0; i < keys.length; i++) {
+ //foreach type of attribute, adapt the R instruction.
if (this.attributes.get(keys[i]) instanceof String) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] +
- "\")<-\"" + this.attributes.get(keys[i]) + "\"");
+ engine.eval(String.format(assignAttribute, this.variable,
+ keys[i], "\"" + this.attributes.get(keys[i]) + "\""));
+ //String, value between quotes
} else if (this.attributes.get(keys[i]) instanceof Double) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] +
- "\")<-" + this.attributes.get(keys[i]) + "");
+ engine.eval(String.format(assignAttribute, this.variable,
+ keys[i], this.attributes.get(keys[i])));
} else if (this.attributes.get(keys[i]) instanceof Integer) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] +
- "\")<-as.integer(" + this.attributes.get(keys[i]) +
- ")");
+ engine.eval(String.format(assignAttribute, this.variable,
+ keys[i], "as.integer(" +
+ this.attributes.get(keys[i]) + ")"));
+ //Integer, value formated by the R function : as.integer()
} else if (this.attributes.get(keys[i]) instanceof Boolean) {
if ((Boolean) this.attributes.get(keys[i])) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] +
- "\")<-TRUE");
+ engine.eval(String.format(assignAttribute, this.variable,
+ keys[i], "TRUE"));
+ //Boolean true replaced by TRUE
} else {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] +
- "\")<-FALSE");
+ engine.eval(String.format(assignAttribute, this.variable,
+ keys[i], "FALSE"));
+ //Boolean false replaced by REPLACE
}
} else if (this.attributes.get(keys[i]) instanceof REXP) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] +
- "\")<-" + ((REXP) this.attributes.get(keys[i])).
- toRString());
+ engine.eval(String.format(assignAttribute, this.variable,
+ keys[i], ((REXP) this.attributes.get(keys[i])).
+ toRString()));
+ //REXP replaced by the result of their toRString() method.
} else {
- log.warn("This attribute is not valid : " + keys[i] + " ; " +
- this.attributes.get(keys[i]) +
- ". It will not be sent to R");
+ if (log.isWarnEnabled()) {
+ log.warn("This attribute is not valid : " + keys[i] +
+ " ; " + this.attributes.get(keys[i]) +
+ ". It will not be sent to R");
+ //if the attribute is not recognised.
+ }
}
}
}
}
+ /** {@inheritDoc} */
@Override
public Map<String, Object> getAttributes() throws RException {
if (engine.isAutoCommit()) {
+ //get the number of attributes
Integer attributeslength = (Integer) engine.eval(
"length(attributes(" + this.variable + "))");
for (int i = 0; i < attributeslength; i++) {
+ //Get the name of the i attribute
String key = (String) engine.eval("names(attributes(" +
this.variable + "))[" + (i + 1) + "]");
- Object newAttribute = engine.eval(
- "attributes(" + this.variable + ")$" + key);
- if (attributes.containsKey(key)) {
- attributes.remove(key);
- attributes.put(key, newAttribute);
- } else {
- attributes.put(key, newAttribute);
- }
+ //Get the attribute
+ getAttribute(key);
}
}
return this.attributes;
}
+ /** {@inheritDoc} */
@Override
public Object getAttribute(String attribute) throws RException {
if (engine.isAutoCommit()) {
Object returnedAttribute;
- if ((Boolean) engine.eval("is.data.frame(attr(" + this.variable +
- ",\"" + attribute + "\"))")) {
+ //test if the attribute is a data.frame
+ if ((Boolean) engine.eval("is.data.frame(" + String.format(
+ getAttribute, this.variable, attribute) + ")")) {
+ //if attribute is a list, import the data.frame from R
returnedAttribute = new RDataFrame(engine);
- ((RDataFrame) returnedAttribute).getFrom("attr(" + this.variable +
- ",\"" + attribute + "\")");
- } else if ((Boolean) engine.eval("is.list(attr(" + this.variable +
- ",\"" + attribute + "\"))")) {
+ ((RDataFrame) returnedAttribute).getFrom(String.format(
+ getAttribute, this.variable, attribute));
+ //test if the attribute is a list
+ } else if ((Boolean) engine.eval("is.list(" + String.format(
+ getAttribute, this.variable, attribute) + ")")) {
+
+ //if attribute is a list, import it from R.
returnedAttribute = new RList(engine);
- ((RList) returnedAttribute).getFrom("attr(" + this.variable +
- ",\"" + attribute + "\")");
+ ((RList) returnedAttribute).getFrom(String.format(getAttribute,
+ this.variable, attribute));
} else {
- returnedAttribute = engine.eval(
- "attr(" + this.variable + ",\"" + attribute + "\")");
+ //else attribute is imported as any other R expression.
+ returnedAttribute = engine.eval(String.format(getAttribute,
+ this.variable, attribute));
}
+ //put the attribute in the attribute list
if (returnedAttribute != null) {
if (attributes.containsKey(attribute)) {
+ //if the attribute already exists, remove it first (or you
+ //will have too times the same attribubte with different
+ //values)
attributes.remove(attribute);
attributes.put(attribute, returnedAttribute);
} else {
@@ -154,82 +179,76 @@
throw new RException("Attribute does not exist");
}
} else if (!attributes.containsKey(attribute)) {
+ //if no autocommit and attribute does not exist
throw new RException("Attribute does not exist");
}
return attributes.get(attribute);
}
+ /** {@inheritDoc} */
@Override
public void setAttribute(String attribute, Object value) throws RException {
+ //variable to check if the attribute is valid before setting it.
+ boolean isOK = false;
if (value instanceof String) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, value);
- } else {
- attributes.put(attribute, value);
- }
- engine.voidEval("attr(" + this.variable + ",\"" + attribute +
- "\")<-\"" + value + "\"");
+ isOK = true;
+ engine.voidEval(String.format(assignAttribute, this.variable,
+ attribute, "\"" + value + "\""));
+ //if String, between quotes
} else if (value instanceof Double) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, value);
- } else {
- attributes.put(attribute, value);
- }
- engine.voidEval("attr(" + this.variable + ",\"" + attribute +
- "\")<-" + value);
+ isOK = true;
+ engine.voidEval(String.format(assignAttribute, this.variable,
+ attribute, value));
} else if (value instanceof Integer) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, value);
- } else {
- attributes.put(attribute, value);
- }
- engine.voidEval("attr(" + this.variable + ",\"" + attribute +
- "\")<-as.integer(" + value + ")");
+ isOK = true;
+ engine.voidEval(String.format(assignAttribute, this.variable,
+ attribute, "as.integer(" + value + ")"));
+ //If integer in the R function : as.integer()
} else if (value instanceof Boolean) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, value);
- } else {
- attributes.put(attribute, value);
- }
+ isOK = true;
if ((Boolean) value) {
- engine.voidEval("attr(" + this.variable + ",\"" + attribute +
- "\")<-TRUE");
+ engine.voidEval(String.format(assignAttribute, this.variable,
+ attribute, "TRUE"));
+ //If boolean true, replaced by TRUE
} else {
- engine.voidEval("attr(" + this.variable + ",\"" + attribute +
- "\")<-FALSE");
+ engine.voidEval(String.format(assignAttribute, this.variable,
+ attribute, "FALSE"));
+ //If boolean false, replaced by FALSE
}
} else if (value instanceof REXP) {
+ isOK = true;
+ engine.voidEval(String.format(assignAttribute, this.variable,
+ attribute, ((REXP) value).toRString()));
+ //if REXP, use its method toRString()
+ } else {
+ log.warn("This attribute is not valid : " + attribute + " ; " +
+ value + ". It will not be processed");
+ }
+ //if attribute is valid, out it in the attribute list.
+ if (isOK) {
if (attributes.containsKey(attribute)) {
attributes.remove(attribute);
attributes.put(attribute, value);
} else {
attributes.put(attribute, value);
}
- engine.voidEval("attr(" + this.variable + ",\"" + attribute +
- "\")<-" + ((REXP) value).toRString());
- } else {
- log.warn("This attribute is not valid : " + attribute + " ; " +
- value + ". It will not be processed");
}
}
+ /** {@inheritDoc} */
@Override
public String getVariable() {
return this.variable;
}
- /**
- * Method to set the list variable name
+ /**
+ * Method to set the R expression variable name
*
* @param variable
* the new variable name
* @throws RException
- * if something wrong happen while assigning the list to the new
- * variable in R.
+ * if something wrong happen while assigning the R expression to
+ * the new variable in R.
*/
public void setVariable(String variable) throws RException {
this.variable = variable;
1
0
[LutinJ2R-commits] r117 - trunk/src/main/java/org/nuiton/j2r/types
by jcouteau@users.labs.libre-entreprise.org 31 Jul '09
by jcouteau@users.labs.libre-entreprise.org 31 Jul '09
31 Jul '09
Author: jcouteau
Date: 2009-07-31 11:18:50 +0200 (Fri, 31 Jul 2009)
New Revision: 117
Modified:
trunk/src/main/java/org/nuiton/j2r/types/RList.java
Log:
Refactor and add some javadoc
Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-30 14:05:04 UTC (rev 116)
+++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-31 09:18:50 UTC (rev 117)
@@ -20,26 +20,37 @@
import java.util.HashMap;
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.RException;
public class RList extends REXPAbstract implements REXP {
-
- private Log log = LogFactory.getLog(RList.class);
+ //List of the names for each element of the list in R.
private List<String> names;
+ //Content of the list (eg elements of the list in R)
private List<Object> data;
+ //R instruction to assign names
private String setNamesString = "names(%s)<-c(%s)";
+ //R instruction to assign one name
private String setNameString = "names(%s)[%s]<-\"%s\"";
+ //R instruction to get the names
private String getNamesString = "names(%s)";
+ //R instruction to get one name
private String getNameString = "names(%s)[%s]";
+ //Default error message if indexOutOfBoundException.
private String indexExceptionText =
"Cannot perform operation, index is superior to size.\nIndex : %s\nSize : %s";
+ //Default error message if inconsistency between local and distant data.
private String dataInconsistencyText =
"There is an inconsistency between the local and distant data.\nLocal data size : %s\nDistant data size : %s";
+ //Default error if no variable name given.
private String noVariable = "No variable name given";
+ /**
+ * Create a default RList linked to a R engine (the List is not initialized
+ * in R.)
+ *
+ * @param engine the R engine in which to assign the RList.
+ */
public RList(REngine engine) {
super();
this.names = new ArrayList<String>();
@@ -49,6 +60,16 @@
this.attributes = new HashMap<String, Object>();
}
+ /**
+ * Create the RList with parameters and initialize it in R.
+ *
+ * @param names the names of each object of the list.
+ * @param data the list of the objects that compound the RList.
+ * @param engine the R engine in which to assign the RList.
+ * @param variable the variable name in R.
+ * @throws org.nuiton.j2r.RException if an error occur while initializing
+ * the list in R.
+ */
public RList(List<String> names, List<Object> data, REngine engine,
String variable) throws RException {
super();
@@ -258,13 +279,12 @@
}
if (data.get(i) instanceof String) {
returnString += "\"" + data.get(i) + "\",";
- } else if (data.get(i) instanceof Boolean) {
- if ((Boolean) data.get(i)) {
- returnString += "TRUE,";
- } else {
- returnString += "FALSE,";
- }
-
+ } else if ((data.get(i) instanceof Boolean) && ((Boolean) data.
+ get(i))) {
+ returnString += "TRUE,";
+ } else if ((data.get(i) instanceof Boolean) && (!(Boolean) data.
+ get(i))) {
+ returnString += "FALSE,";
} else if (data.get(i) instanceof Integer) {
returnString += "as.integer(" + data.get(i) + "),";
} else if (data.get(i) instanceof REXP) {
@@ -274,10 +294,8 @@
}
}
returnString = returnString.substring(0, returnString.length() - 1);
- returnString += ")";
- } else {
- returnString += ")";
}
+ returnString += ")";
return returnString;
}
@@ -545,7 +563,13 @@
}
}
- public void testVariable() throws RException {
+ /**
+ * Test that the variable name has been set so that the list can be sent to
+ * R.
+ *
+ * @throws org.nuiton.j2r.RException if the variable name have not been set.
+ */
+ private void testVariable() throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
throw new RException(
noVariable);
1
0
[LutinJ2R-commits] r116 - trunk/src/main/java/org/nuiton/j2r/types
by jcouteau@users.labs.libre-entreprise.org 30 Jul '09
by jcouteau@users.labs.libre-entreprise.org 30 Jul '09
30 Jul '09
Author: jcouteau
Date: 2009-07-30 16:05:04 +0200 (Thu, 30 Jul 2009)
New Revision: 116
Modified:
trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java
trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
trunk/src/main/java/org/nuiton/j2r/types/RList.java
Log:
Remove duplicate code, improve code (for sonar :) )
Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2009-07-26 17:11:04 UTC (rev 115)
+++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2009-07-30 14:05:04 UTC (rev 116)
@@ -19,8 +19,10 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
+import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
@@ -41,6 +43,19 @@
private List<String> rowNames;
//Vector containing the vectors of the data.frame
private List<List<? extends Object>> data;
+ private String setNamesString = "names(%s)<-c(%s)";
+ private String setNameString = "names(%s)[%s]<-\"%s\"";
+ private String getNamesString = "names(%s)";
+ private String getNameString = "names(%s)[%s]";
+ private String getRowNamesString = "row.names(%s)";
+ private String getRowNameString = "row.names(%s)[%s]";
+ private String setRowNamesString = "row.names(%s)<-c(%s)";
+ private String setRowNameString = "row.names(%s)[%s]<-\"%s\"";
+ private String indexExceptionText =
+ "Cannot perform operation, index is superior to size.\nIndex : %s\nSize : %s";
+ private String dataInconsistencyText =
+ "There is an inconsistency between the local and distant data.\nLocal data size : %s\nDistant data size : %s";
+ private String noVariable = "No variable name given";
public RDataFrame(REngine engine) throws RException {
super();
@@ -121,12 +136,12 @@
* when the row.names size get from R is bigger than the local
* data size.
*/
- public List<String> getNames() throws RException, IndexOutOfBoundsException {
+ public List<String> getNames() throws RException {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String[] namesArray = (String[]) engine.eval("names(" +
- this.variable + ")");
+ String[] namesArray = (String[]) engine.eval(String.format(
+ getNamesString, this.variable));
//Check if size is correct, if yes, modify local data.
if (namesArray.length <= this.data.size()) {
@@ -137,11 +152,10 @@
return names;
} else {
//if size is not correct, throw a RException.
- throw new IndexOutOfBoundsException(
- "Data inconsistency, local data is smaller (size : " +
- this.data.size() +
- ") than the names ArrayList in R (size : " +
- namesArray.length + ")");
+ throw new IndexOutOfBoundsException(String.format(
+ dataInconsistencyText, this.data.size(),
+ namesArray.length));
+
}
} else {
return this.names;
@@ -164,14 +178,14 @@
* @throws IndexOutOfBoundsException
* if the x index is out of bounds.
*/
- public String getName(int x) throws RException, IndexOutOfBoundsException {
+ public String getName(int x) throws RException {
//check if the index is valid
if (x < names.size()) {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String name = (String) engine.eval("names(" + this.variable +
- ")[" + (x + 1) + "]");
+ String name = (String) engine.eval(String.format(getNameString,
+ this.variable, x + 1));
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -180,7 +194,8 @@
}
} else {
//if String is not returned, throw an IndexOutOfBOundsException.
- throw new IndexOutOfBoundsException("Your index is not valid");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.names.size()));
}
return this.names.get(x);
@@ -197,29 +212,31 @@
* when the names ArrayList is longer than the data.frame
* length.
*/
- public void setNames(List<String> names) throws RException,
- IndexOutOfBoundsException {
+ public void setNames(List<String> names) throws RException {
//Check if the size is correct, if yes, do the modifications and send them to R.
if (names.size() <= data.size()) {
this.names = names;
//Create the r instruction (names(var)<-c("name 1",...,"name x")).
- String rexp = "names(" + this.variable + ")<-c(";
+
+ String namesString = "";
for (int i = 0; i < this.names.size(); i++) {
- rexp += "\"" + names.get(i) + "\",";
+ //add a "," to separate names beginning after the first name is displayed.
+ if (i != 0) {
+ namesString += ",";
+ }
+ //add the column name between quotes.
+ namesString += "\"" + names.get(i) + "\"";
}
- rexp = rexp.substring(0, rexp.length() - 1) + ")";
//Send the r instruction to the engine.
- engine.voidEval(rexp);
+ engine.voidEval(String.format(setNamesString, this.variable,
+ namesString));
} else {
//if size is not correct, throw a IndexOutOfBoundsException.
- throw new IndexOutOfBoundsException(
- "Cannot assign the names attribute, the names List is too long.\nThe size of names (" +
- names.size() +
- ") must be shorter or equal to the data.frame length(" +
- data.size() + ").");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ names.size(), data.size()));
}
}
@@ -235,23 +252,21 @@
* @throws IndexOutOfBoundsException
* when the index is out of the data.frame bounds.
*/
- public void setName(int x, String name) throws RException,
- IndexOutOfBoundsException {
+ public void setName(int x, String name) throws RException {
//check if the index is valid
if (x < names.size()) {
names.set(x, name);
//create the r instruction (names(var)[x]<-"name")
- String rexp = "names(" + this.variable + ")[" + (x + 1) + "]<-\"" +
- name + "\"";
+ String rexp = String.format(setNameString, this.variable, x + 1,
+ name);
//Send the R instruction to the engine.
engine.voidEval(rexp);
} else {
- //if index is out of bounds, throw a IndexOutOfBOundsException
- throw new IndexOutOfBoundsException(
- "Cannot assign the name, the index is out of bounds.\nIndex :" +
- x + "; data.frame size : " + data.size() + ".");
+ //if index is out of bounds, throw a IndexOutOfBoundsException
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, data.size()));
}
}
@@ -266,12 +281,11 @@
* when the row.names size get from R is bigger than the local
* data size.
*/
- public List<String> getRowNames() throws RException,
- IndexOutOfBoundsException {
+ public List<String> getRowNames() throws RException {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String[] rowNamesArray = (String[]) engine.eval("row.names(" +
- this.variable + ")");
+ String[] rowNamesArray = (String[]) engine.eval(String.format(
+ getRowNamesString, this.variable));
//Check if size is correct, if yes, modify local data.
if (rowNamesArray.length <= this.data.get(0).size()) {
@@ -282,11 +296,9 @@
return rowNames;
} else {
//if size is not correct, throw a RException.
- throw new IndexOutOfBoundsException(
- "Data inconsistency, local data is smaller (size : " + this.data.get(0).
- size() + ") than the names ArrayList in R (size : " +
- rowNamesArray.length +
- ").\nYou may want to force an update");
+ throw new IndexOutOfBoundsException(String.format(
+ dataInconsistencyText, rowNamesArray.length, this.data.
+ get(0)));
}
} else {
return this.rowNames;
@@ -307,11 +319,11 @@
*/
public String getRowName(int y) throws RException {
//check if the index is valid
- if (y < names.size()) {
+ if (y < rowNames.size()) {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String name = (String) engine.eval(
- "row.names(" + this.variable + ")[" + (y + 1) + "]");
+ String name = (String) engine.eval(String.format(
+ getRowNameString, this.variable, y + 1));
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -320,7 +332,8 @@
}
} else {
//if String is not returned, throw an IndexOutOfBOundsException.
- throw new IndexOutOfBoundsException("Your index is not valid");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ y, rowNames.size()));
}
return this.rowNames.get(y);
@@ -339,29 +352,29 @@
* when the row names ArrayList is longer than the data.frame
* length
*/
- public void setRowNames(List<String> rowNames) throws RException,
- IndexOutOfBoundsException {
+ public void setRowNames(List<String> rowNames) throws RException {
//Check if the size is correct, if yes, do the modifications and send them to R.
if (rowNames.size() == data.get(0).size()) {
this.rowNames = rowNames;
//Create the r instruction (row.names(var)<-c("name 1",...,"name x")).
- String rexp = "row.names(" + this.variable + ")<-c(";
+ String rowNamesString = "";
for (int i = 0; i < this.rowNames.size(); i++) {
- rexp += "\"" + rowNames.get(i) + "\",";
+ if (i != 0) {
+ rowNamesString += ",";
+ }
+ rowNamesString += "\"" + rowNames.get(i) + "\"";
}
- rexp = rexp.substring(0, rexp.length() - 1) + ")";
+ String rexp = String.format(setRowNamesString, this.variable,
+ rowNamesString);
//Send the r instruction to the engine.
engine.voidEval(rexp);
} else {
//if size is not correct, throw a RException.
- throw new IndexOutOfBoundsException(
- "Cannot assign the names attribute, the ArrayList is too long.\nThe size of names (" +
- rowNames.size() +
- ") must be shorter or equal to the data.frame length(" + data.get(0).
- size() + ").");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ rowNames.size(), data.get(0).size()));
}
}
@@ -377,23 +390,21 @@
* @throws IndexOutOfBoundsException
* when the index is out of the data.frame bounds.
*/
- public void setRowName(int x, String rowName) throws RException,
- IndexOutOfBoundsException {
+ public void setRowName(int x, String rowName) throws RException {
//check if the index is valid
if (x < rowNames.size()) {
rowNames.set(x, rowName);
//create the r instruction (row.names(var)[x]<-"rowName")
- String rexp = "row.names(" + this.variable + ")[" + (x + 1) +
- "]<-\"" + rowName + "\"";
+ String rexp = String.format(setRowNameString, this.variable, x + 1,
+ rowName);
//Send the R instruction to the engine.
engine.voidEval(rexp);
} else {
//if index is out of bounds, throw a RException
- throw new IndexOutOfBoundsException(
- "Cannot assign the name, the index is out of bounds.\nIndex :" +
- x + "; data.frame size : " + data.size() + ".");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, data.size()));
}
}
@@ -436,8 +447,7 @@
@Override
public String toRString() throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot instantiate data.frame in R, no variable name given");
+ throw new RException(noVariable);
}
String returnString = this.variable + "<-data.frame(";
if (!(this.data.isEmpty())) {
@@ -509,20 +519,18 @@
* @throws ArrayStoreException
* if the target destination cannot accept double value
*/
- public void set(int x, int y, double data) throws RException,
- IndexOutOfBoundsException, ArrayStoreException {
+ public void set(int x, int y, double data) throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
+ throw new RException(noVariable);
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : " + x +
- ">=" + this.data.size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.data.size()));
}
if (y >= this.data.get(0).size()) {
- throw new IndexOutOfBoundsException("The y value is too high : " + y +
- ">=" + this.data.get(0).size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ y, this.data.get(0).size()));
}
if (this.data.get(x).get(y) instanceof Double) {
((ArrayList<Double>) this.data.get(x)).set(y, data);
@@ -553,16 +561,15 @@
*/
public void set(int x, int y, Boolean data) throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
+ throw new RException(noVariable);
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : " + x +
- ">=" + this.data.size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.data.size()));
}
if (y >= this.data.get(0).size()) {
- throw new IndexOutOfBoundsException("The y value is too high : " + y +
- ">=" + this.data.get(0).size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ y, this.data.get(0).size()));
}
if (this.data.get(x).get(y) instanceof Boolean) {
((ArrayList<Boolean>) this.data.get(x)).set(y, data);
@@ -598,16 +605,15 @@
*/
public void set(int x, int y, String data) throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
+ throw new RException(noVariable);
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : " + x +
- ">=" + this.data.size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.data.size()));
}
if (y >= this.data.get(0).size()) {
- throw new IndexOutOfBoundsException("The y value is too high : " + y +
- ">=" + this.data.get(0).size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ y, this.data.get(0).size()));
}
if (this.data.get(x).get(y) instanceof String) {
((ArrayList<String>) this.data.get(x)).set(y, data);
@@ -637,16 +643,15 @@
*/
public void set(int x, int y, int data) throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
+ throw new RException(noVariable);
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : " + x +
- ">=" + this.data.size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.data.size()));
}
if (y >= this.data.get(0).size()) {
- throw new IndexOutOfBoundsException("The y value is too high : " + y +
- ">=" + this.data.get(0).size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ y, this.data.size()));
}
if (this.data.get(x).get(y) instanceof Integer) {
((ArrayList<Integer>) this.data.get(x)).set(y, data);
@@ -675,12 +680,12 @@
public Object get(int x, int y) throws RException,
IndexOutOfBoundsException {
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : " + x +
- ">=" + this.data.size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.data.size()));
}
if (y >= this.data.get(0).size()) {
- throw new IndexOutOfBoundsException("The y value is too high : " + y +
- ">=" + this.data.get(0).size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ y, this.data.get(0).size()));
}
if (engine.isAutoCommit()) {
Object returnObject = engine.eval(this.variable + "[" + (y + 1) +
@@ -703,26 +708,6 @@
}
/**
- * Method to set the data.frame variable name
- *
- * @param variable
- * the new variable name
- * @throws RException
- * if something wrong happen while assigning the data.frame to
- * the new variable in R.
- */
- public void setVariable(String variable) throws RException {
- this.variable = variable;
- try {
- engine.voidEval(toRString());
- } catch (RException eee) {
- throw new RException(
- "An error occured while trying to assign the data.frame in R",
- eee);
- }
- }
-
- /**
* Method to get the ArrayLists of the R data.frame (there is no
* synchronizing with R, use the update() method to synchronize data with R
* before using this method if you think data may have changed.
@@ -784,15 +769,15 @@
}
//update row names
- String[] rowNamesArray = (String[]) engine.eval("row.names(" +
- this.variable + ")");
+ String[] rowNamesArray = (String[]) engine.eval(String.format(
+ getRowNamesString, this.variable));
for (int i = 0; i < rowNamesArray.length; i++) {
rowNames.add(rowNamesArray[i]);
}
//update names
- String[] namesArray = (String[]) engine.eval("names(" +
- this.variable + ")");
+ String[] namesArray = (String[]) engine.eval(String.format(
+ getNamesString, this.variable));
for (int i = 0; i < namesArray.length; i++) {
names.add(namesArray[i]);
}
@@ -853,7 +838,7 @@
}
file.close();
} catch (Exception e) {
- e.printStackTrace();
+ log.error(e);
}
}
@@ -871,62 +856,10 @@
* Does the csv file contain names of the columns.
*/
public void importCsv(File inputFile, boolean rowNames, boolean names) {
- String tmp;
- Integer dataSize = 0;
- try {
+ List<Object> tmp = new ArrayList<Object>();
+ tmp.add("string");
+ importCsv(inputFile, rowNames, names, (List) tmp);
- BufferedReader first = new BufferedReader(new FileReader(inputFile));
- tmp = first.readLine();
- String[] splitted = tmp.split("\\;");
- if (rowNames) {
- dataSize = splitted.length - 1;
- } else {
- dataSize = splitted.length;
- }
-
- BufferedReader br = new BufferedReader(new FileReader(inputFile));
- if (this.data != null) {
- this.data.clear();
- }
- if ((rowNames) && (this.rowNames != null)) {
- this.rowNames.clear();
- }
- if (names) {
- if (this.names != null) {
- this.names.clear();
- }
- tmp = br.readLine();
- splitted = tmp.split("\\;");
- for (int i = 1; i < splitted.length; i++) {
- this.names.add(splitted[i]);
- }
- }
-
- List<List<? extends Object>> tempData =
- new ArrayList<List<? extends Object>>();
-
- for (int i = 0; i < dataSize; i++) {
- List<Object> column = new ArrayList<Object>();
- tempData.add(column);
- }
-
- while ((tmp = br.readLine()) != null) {
- splitted = tmp.split("\\;");
- int index = 0;
- if (rowNames) {
- this.rowNames.add(splitted[0]);
- index = 1;
- }
- for (int i = 0 + index; i < splitted.length; i++) {
- ((ArrayList<Object>) tempData.get(i - index)).add(
- (Object) splitted[i]);
- }
- }
- br.close();
- this.data = tempData;
- } catch (Exception e) {
- e.printStackTrace();
- }
}
/**
@@ -946,70 +879,17 @@
*/
public void importCsv(File inputFile, boolean rowNames, boolean names,
Object importType) {
- String tmp;
- Integer dataSize = 0;
- try {
-
- BufferedReader first = new BufferedReader(new FileReader(inputFile));
- tmp = first.readLine();
- String[] splitted = tmp.split("\\;");
- if (rowNames) {
- dataSize = splitted.length - 1;
- } else {
- dataSize = splitted.length;
- }
-
- BufferedReader br = new BufferedReader(new FileReader(inputFile));
- if (this.data != null) {
- this.data.clear();
- }
- if ((rowNames) && (this.rowNames != null)) {
- this.rowNames.clear();
-
- }
- if (names) {
- if (this.names != null) {
- this.names.clear();
- }
- tmp = br.readLine();
- splitted = tmp.split("\\;");
- for (int i = 1; i < splitted.length; i++) {
- this.names.add(splitted[i]);
- }
- }
-
- List<List<? extends Object>> tempData =
- new ArrayList<List<? extends Object>>();
-
- for (int i = 0; i < dataSize; i++) {
- List<Object> column = new ArrayList<Object>();
- tempData.add(column);
- }
-
- while ((tmp = br.readLine()) != null) {
- splitted = tmp.split("\\;");
- int index = 0;
- if (rowNames) {
- this.rowNames.add(splitted[0]);
- index = 1;
- }
- for (int i = 0 + index; i < splitted.length; i++) {
- if (importType instanceof String) {
- ((ArrayList<Object>) tempData.get(i - index)).add(
- (Object) splitted[i]);
- } else if (importType instanceof Double) {
- ((ArrayList<Object>) tempData.get(i - index)).add(Double.
- valueOf(splitted[i]));
- } else if (importType instanceof Integer) {
- ((ArrayList<Object>) tempData.get(i - index)).add(Integer.
- valueOf(splitted[i]));
- }
- }
- }
- br.close();
- this.data = tempData;
- } catch (Exception e) {
- e.printStackTrace();
+ List<Object> tmp = new ArrayList<Object>();
+ if (importType instanceof String) {
+ log.info("string");
+ tmp.add((String) importType);
+ importCsv(inputFile, rowNames, names, (List) tmp);
+ } else if (importType instanceof Double) {
+ tmp.add((Double) importType);
+ importCsv(inputFile, rowNames, names, tmp);
+ } else if (importType instanceof Integer) {
+ tmp.add((Integer) importType);
+ importCsv(inputFile, rowNames, names, tmp);
}
}
@@ -1035,10 +915,7 @@
String tmp;
Integer dataSize = 0;
try {
-
- BufferedReader first = new BufferedReader(new FileReader(inputFile));
- tmp = first.readLine();
- String[] splitted = tmp.split("\\;");
+ String[] splitted = getLines(inputFile);
if (rowNames) {
dataSize = splitted.length - 1;
} else {
@@ -1080,7 +957,21 @@
index = 1;
}
for (int i = 0 + index; i < splitted.length; i++) {
- if (importTypes.get(i - index) instanceof String) {
+ //test the size of the inputType list. If 1 import all the
+ //columns as the type of the first
+ if (importTypes.size() == 1) {
+ if (importTypes.get(0) instanceof String) {
+ ((ArrayList<Object>) tempData.get(i - index)).add(
+ (Serializable) splitted[i]);
+ } else if (importTypes.get(0) instanceof Double) {
+ ((ArrayList<Object>) tempData.get(i - index)).add(Double.
+ valueOf(splitted[i]));
+ } else if (importTypes.get(0) instanceof Integer) {
+ ((ArrayList<Object>) tempData.get(i - index)).add(Integer.
+ valueOf(splitted[i]));
+ }
+
+ } else if (importTypes.get(i - index) instanceof String) {
((ArrayList<Object>) tempData.get(i - index)).add(
(Serializable) splitted[i]);
} else if (importTypes.get(i - index) instanceof Double) {
@@ -1095,7 +986,16 @@
br.close();
this.data = tempData;
} catch (Exception e) {
- e.printStackTrace();
+ log.error(e);
}
}
+
+ private String[] getLines(File inputFile) throws IOException,
+ FileNotFoundException {
+ String tmp;
+ BufferedReader first = new BufferedReader(new FileReader(inputFile));
+ tmp = first.readLine();
+ String[] splitted = tmp.split("\\;");
+ return splitted;
+ }
}
Modified: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-07-26 17:11:04 UTC (rev 115)
+++ trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-07-30 14:05:04 UTC (rev 116)
@@ -221,4 +221,24 @@
public String getVariable() {
return this.variable;
}
+
+ /**
+ * Method to set the list variable name
+ *
+ * @param variable
+ * the new variable name
+ * @throws RException
+ * if something wrong happen while assigning the list to the new
+ * variable in R.
+ */
+ public void setVariable(String variable) throws RException {
+ this.variable = variable;
+ try {
+ engine.voidEval(toRString());
+ } catch (RException eee) {
+ throw new RException(
+ "An error occured while trying to assign the list in R",
+ eee);
+ }
+ }
}
Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-26 17:11:04 UTC (rev 115)
+++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-30 14:05:04 UTC (rev 116)
@@ -30,6 +30,15 @@
private Log log = LogFactory.getLog(RList.class);
private List<String> names;
private List<Object> data;
+ private String setNamesString = "names(%s)<-c(%s)";
+ private String setNameString = "names(%s)[%s]<-\"%s\"";
+ private String getNamesString = "names(%s)";
+ private String getNameString = "names(%s)[%s]";
+ private String indexExceptionText =
+ "Cannot perform operation, index is superior to size.\nIndex : %s\nSize : %s";
+ private String dataInconsistencyText =
+ "There is an inconsistency between the local and distant data.\nLocal data size : %s\nDistant data size : %s";
+ private String noVariable = "No variable name given";
public RList(REngine engine) {
super();
@@ -62,16 +71,13 @@
* the R list
* @throws RException
* if an error occurs while getting back the names from R.
- * @throws IndexOutOfBoundsException
- * when the row.names size get from R is bigger than the local
- * data size.
*/
- public List<String> getNames() throws RException, IndexOutOfBoundsException {
+ public List<String> getNames() throws RException {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String[] namesArray = (String[]) engine.eval("names(" +
- this.variable + ")");
+ String[] namesArray = (String[]) engine.eval(String.format(
+ getNamesString, this.variable));
//Check if size is correct, if yes, modify local data.
if (namesArray.length <= this.data.size()) {
@@ -82,10 +88,9 @@
return names;
} else {
//if size is not correct, throw a RException.
- throw new IndexOutOfBoundsException(
- "Data inconsistency, local data is smaller (size : " +
- this.data.size() + ") than the names in R (size : " +
- namesArray.length + ")");
+ throw new IndexOutOfBoundsException(String.format(
+ dataInconsistencyText, this.data.size(),
+ namesArray.length));
}
} else {
return this.names;
@@ -104,18 +109,15 @@
*
* @throws RException
* if an error occurs while getting back the name from R.
- *
- * @throws IndexOutOfBoundsException
- * if the x index is out of bounds.
*/
- public String getName(int x) throws RException, IndexOutOfBoundsException {
+ public String getName(int x) throws RException {
//check if the index is valid
if (x < names.size()) {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String name = (String) engine.eval("names(" + this.variable +
- ")[" + (x + 1) + "]");
+ String name = (String) engine.eval(String.format(getNameString,
+ this.variable, x + 1));
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -124,8 +126,8 @@
}
} else {
//if String is not returned, throw an IndexOutOfBOundsException.
- throw new IndexOutOfBoundsException("Your index " + x +
- " is not valid");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, names.size()));
}
return this.names.get(x);
@@ -140,29 +142,29 @@
* @throws RException
* when the names list is longer than the list length.
*/
- public void setNames(List<String> names) throws RException,
- IndexOutOfBoundsException {
+ public void setNames(List<String> names) throws RException {
//Check if the size is correct, if yes, do the modifications and send them to R.
if (names.size() <= data.size()) {
this.names = names;
//Create the r instruction (names(var)<-c("name 1",...,"name x")).
- String rexp = "names(" + this.variable + ")<-c(";
+ String namesString = "";
for (int i = 0; i < this.names.size(); i++) {
- rexp += "\"" + names.get(i) + "\",";
+ if (i != 0) {
+ namesString += ",";
+ }
+ namesString += "\"" + names.get(i) + "\"";
}
- rexp = rexp.substring(0, rexp.length() - 1) + ")";
+ String rexp = String.format(setNamesString, this.variable,
+ namesString);
//Send the r instruction to the engine.
engine.voidEval(rexp);
} else {
//if size is not correct, throw a IndexOutOfBoundsException.
- throw new IndexOutOfBoundsException(
- "Cannot assign the names attribute, the names List is too long.\nThe size of names (" +
- names.size() +
- ") must be shorter or equal to the list length(" +
- data.size() + ").");
+ throw new IndexOutOfBoundsException(String.format(
+ dataInconsistencyText, names.size(), data.size()));
}
}
@@ -175,11 +177,8 @@
* Name of the object.
* @throws RException
* if an error occur while in R
- * @throws IndexOutOfBoundsException
- * when the index is out of the list bounds.
*/
- public void setName(int x, String name) throws RException,
- IndexOutOfBoundsException {
+ public void setName(int x, String name) throws RException {
//check if the index is valid
if (x < data.size()) {
for (int i = 0; i <= x; i++) {
@@ -199,16 +198,15 @@
}
//create the r instruction (names(var)[x]<-"name")
- String rexp = "names(" + this.variable + ")[" + (x + 1) + "]<-\"" +
- name + "\"";
+ String rexp = String.format(setNameString, this.variable, x + 1,
+ name);
//Send the R instruction to the engine.
engine.voidEval(rexp);
} else {
//if index is out of bounds, throw a IndexOutOfBOundsException
- throw new IndexOutOfBoundsException(
- "Cannot assign the name, the index is out of bounds.\nIndex :" +
- x + "; data.frame size : " + data.size() + ".");
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, data.size()));
}
}
@@ -242,26 +240,6 @@
}
/**
- * Method to set the list variable name
- *
- * @param variable
- * the new variable name
- * @throws RException
- * if something wrong happen while assigning the list to the new
- * variable in R.
- */
- public void setVariable(String variable) throws RException {
- this.variable = variable;
- try {
- engine.voidEval(toRString());
- } catch (RException eee) {
- throw new RException(
- "An error occured while trying to assign the list in R",
- eee);
- }
- }
-
- /**
* Method to export the list in a String for evaluation in R.
*
* @return a R string representation of the list to create it in R.
@@ -270,10 +248,7 @@
*/
@Override
public String toRString() throws RException {
- if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot instantiate data.frame in R, no variable name given");
- }
+ testVariable();
String returnString = this.variable + "<-list(";
if ((this.data != null) && (!(this.data.isEmpty()))) {
@@ -317,10 +292,7 @@
* if no variable name has been given to the list
*/
public void set(int x, double data) throws RException {
- if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
- }
+ testVariable();
for (int i = 0; i <= x; i++) {
try {
this.data.get(i);
@@ -350,10 +322,7 @@
* if no variable name has been given to the list
*/
public void set(int x, Boolean data) throws RException {
- if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
- }
+ testVariable();
for (int i = 0; i <= x; i++) {
try {
this.data.get(i);
@@ -385,15 +354,9 @@
* value to set
* @throws RException
* if no variable name has been given to the list
- * @throws IndexOutOfBoundsException
- * if the x coordinate is not correct.
*/
- public void set(int x, String data) throws RException,
- IndexOutOfBoundsException {
- if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
- }
+ public void set(int x, String data) throws RException {
+ testVariable();
for (int i = 0; i <= x; i++) {
try {
this.data.get(i);
@@ -421,14 +384,9 @@
* value to set
* @throws RException
* if no variable name has been given to the list
- * @throws IndexOutOfBoundsException
- * if the x coordinate is not correct.
*/
public void set(int x, int data) throws RException {
- if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
- }
+ testVariable();
for (int i = 0; i <= x; i++) {
try {
this.data.get(i);
@@ -459,10 +417,7 @@
* if no variable name has been given to the list
*/
public void set(int x, REXP data) throws RException {
- if ((this.variable.equals("")) || (this.variable == null)) {
- throw new RException(
- "Cannot propage modification in R, no variable name given");
- }
+ testVariable();
for (int i = 0; i <= x; i++) {
try {
this.data.get(i);
@@ -491,13 +446,11 @@
* @return the Object located at the [x] coordinate
* @throws RException
* if no variable name has been given to the list
- * @throws IndexOutOfBoundsException
- * if the xcoordinate is not correct.
*/
- public Object get(int x) throws RException, IndexOutOfBoundsException {
+ public Object get(int x) throws RException {
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : " + x +
- ">=" + this.data.size());
+ throw new IndexOutOfBoundsException(String.format(indexExceptionText,
+ x, this.data.size()));
}
if (engine.isAutoCommit()) {
Object returnObject = engine.eval(this.variable + "[[" + (x + 1) +
@@ -566,8 +519,8 @@
}
//update names
- String[] namesArray = (String[]) engine.eval("names(" + this.variable +
- ")");
+ String[] namesArray = (String[]) engine.eval(String.format(
+ getNamesString, this.variable));
for (int i = 0; i < namesArray.length; i++) {
names.add(namesArray[i]);
}
@@ -591,4 +544,11 @@
attributes.put(key, attribute);
}
}
+
+ public void testVariable() throws RException {
+ if ((this.variable.equals("")) || (this.variable == null)) {
+ throw new RException(
+ noVariable);
+ }
+ }
}
1
0