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
July 2009
- 1 participants
- 8 discussions
[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
[LutinJ2R-commits] r115 - trunk/src/main/java/org/nuiton/j2r/types
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
26 Jul '09
Author: jcouteau
Date: 2009-07-26 19:11:04 +0200 (Sun, 26 Jul 2009)
New Revision: 115
Added:
trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
Modified:
trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java
trunk/src/main/java/org/nuiton/j2r/types/RList.java
Log:
Adding an REXPAbstract to avoid duplicate code
Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2009-07-26 14:21:18 UTC (rev 114)
+++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2009-07-26 17:11:04 UTC (rev 115)
@@ -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.types;
import java.io.BufferedReader;
@@ -26,15 +25,13 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.j2r.REngine;
import org.nuiton.j2r.RException;
-public class RDataFrame implements REXP {
+public class RDataFrame extends REXPAbstract implements REXP {
private Log log = LogFactory.getLog(RDataFrame.class);
@@ -44,11 +41,6 @@
private List<String> rowNames;
//Vector containing the vectors of the data.frame
private List<List<? extends Object>> data;
- //Name of the data.frame in R, default = a
- private String variable;
- //Attributes of the data.frame under a map -> name : R expression
- private Map<String, Object> attributes;
- private REngine engine;
public RDataFrame(REngine engine) throws RException {
super();
@@ -133,8 +125,8 @@
if (engine.isAutoCommit()) {
//Get back the names from R.
- String[] namesArray = (String[]) engine.eval("names("
- + this.variable + ")");
+ String[] namesArray = (String[]) engine.eval("names(" +
+ this.variable + ")");
//Check if size is correct, if yes, modify local data.
if (namesArray.length <= this.data.size()) {
@@ -146,10 +138,10 @@
} 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 + ")");
+ "Data inconsistency, local data is smaller (size : " +
+ this.data.size() +
+ ") than the names ArrayList in R (size : " +
+ namesArray.length + ")");
}
} else {
return this.names;
@@ -178,8 +170,8 @@
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("names(" + this.variable +
+ ")[" + (x + 1) + "]");
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -224,10 +216,10 @@
} 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() + ").");
+ "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() + ").");
}
}
@@ -250,16 +242,16 @@
names.set(x, name);
//create the r instruction (names(var)[x]<-"name")
- String rexp = "names(" + this.variable + ")[" + (x + 1) + "]<-\""
- + name + "\"";
+ String rexp = "names(" + 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() + ".");
+ "Cannot assign the name, the index is out of bounds.\nIndex :" +
+ x + "; data.frame size : " + data.size() + ".");
}
}
@@ -278,8 +270,8 @@
IndexOutOfBoundsException {
if (engine.isAutoCommit()) {
//Get back the names from R.
- String[] rowNamesArray = (String[]) engine.eval("row.names("
- + this.variable + ")");
+ String[] rowNamesArray = (String[]) engine.eval("row.names(" +
+ this.variable + ")");
//Check if size is correct, if yes, modify local data.
if (rowNamesArray.length <= this.data.get(0).size()) {
@@ -291,11 +283,10 @@
} 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");
+ "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");
}
} else {
return this.rowNames;
@@ -319,8 +310,8 @@
if (y < names.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(
+ "row.names(" + this.variable + ")[" + (y + 1) + "]");
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -367,10 +358,10 @@
} 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() + ").");
+ "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() + ").");
}
}
@@ -393,16 +384,16 @@
rowNames.set(x, rowName);
//create the r instruction (row.names(var)[x]<-"rowName")
- String rexp = "row.names(" + this.variable + ")[" + (x + 1)
- + "]<-\"" + rowName + "\"";
+ String rexp = "row.names(" + 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() + ".");
+ "Cannot assign the name, the index is out of bounds.\nIndex :" +
+ x + "; data.frame size : " + data.size() + ".");
}
}
@@ -442,6 +433,7 @@
* @throws RException
* If no variable name is given
*/
+ @Override
public String toRString() throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
throw new RException(
@@ -470,8 +462,8 @@
}
} 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 += "as.integer(" + data.get(i).get(j) +
+ "),";
}
} else {
for (int j = 0; j < data.get(i).size(); j++) {
@@ -489,8 +481,9 @@
for (int i = 0; i < rowNames.size(); i++) {
returnString += "\"" + rowNames.get(i) + "\",";
}
- returnString = returnString.substring(0, returnString.length() - 1)
- + "),stringsAsFactors=FALSE)";
+ returnString =
+ returnString.substring(0, returnString.length() - 1) +
+ "),stringsAsFactors=FALSE)";
} else if (this.data.isEmpty()) {
returnString += ")";
@@ -524,17 +517,17 @@
"Cannot propage modification in R, no variable name given");
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : "
- + x + ">=" + this.data.size());
+ throw new IndexOutOfBoundsException("The x value is too high : " + 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("The y value is too high : " + y +
+ ">=" + this.data.get(0).size());
}
if (this.data.get(x).get(y) instanceof Double) {
((ArrayList<Double>) this.data.get(x)).set(y, data);
- engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1)
- + "]<-" + data);
+ engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1) +
+ "]<-" + data);
} else {
throw new ArrayStoreException(
"The data.frame does not accept doubles on those coordinates");
@@ -564,21 +557,21 @@
"Cannot propage modification in R, no variable name given");
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : "
- + x + ">=" + this.data.size());
+ throw new IndexOutOfBoundsException("The x value is too high : " + 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("The y value is too high : " + y +
+ ">=" + this.data.get(0).size());
}
if (this.data.get(x).get(y) instanceof Boolean) {
((ArrayList<Boolean>) this.data.get(x)).set(y, data);
if (data) {
- engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1)
- + "]<-TRUE");
+ engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1) +
+ "]<-TRUE");
} else {
- engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1)
- + "]<-FALSE");
+ engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1) +
+ "]<-FALSE");
}
engine.voidEval("");
} else {
@@ -609,17 +602,17 @@
"Cannot propage modification in R, no variable name given");
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : "
- + x + ">=" + this.data.size());
+ throw new IndexOutOfBoundsException("The x value is too high : " + 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("The y value is too high : " + y +
+ ">=" + this.data.get(0).size());
}
if (this.data.get(x).get(y) instanceof String) {
((ArrayList<String>) this.data.get(x)).set(y, data);
- engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1)
- + "]<-\"" + data + "\"");
+ engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1) +
+ "]<-\"" + data + "\"");
} else {
throw new ArrayStoreException(
"The data.frame does not accept booleans on those coordinates");
@@ -648,17 +641,17 @@
"Cannot propage modification in R, no variable name given");
}
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : "
- + x + ">=" + this.data.size());
+ throw new IndexOutOfBoundsException("The x value is too high : " + 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("The y value is too high : " + y +
+ ">=" + this.data.get(0).size());
}
if (this.data.get(x).get(y) instanceof Integer) {
((ArrayList<Integer>) this.data.get(x)).set(y, data);
- engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1)
- + "]<-as.integer(" + data + ")");
+ engine.voidEval(this.variable + "[" + (y + 1) + "," + (x + 1) +
+ "]<-as.integer(" + data + ")");
engine.voidEval("");
} else {
throw new ArrayStoreException(
@@ -682,16 +675,16 @@
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("The x value is too high : " + 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("The y value is too high : " + y +
+ ">=" + this.data.get(0).size());
}
if (engine.isAutoCommit()) {
- Object returnObject = engine.eval(this.variable + "[" + (y + 1)
- + "," + (x + 1) + "]");
+ Object returnObject = engine.eval(this.variable + "[" + (y + 1) +
+ "," + (x + 1) + "]");
if (returnObject instanceof String) {
((ArrayList<String>) this.data.get(x)).set(y,
(String) returnObject);
@@ -730,15 +723,6 @@
}
/**
- * Method to get the data.frame variable name.
- *
- * @return the data.frame variable name.
- */
- public String getVariable() {
- return this.variable;
- }
-
- /**
* 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.
@@ -774,6 +758,7 @@
* a REngine where the R session is located.
* @throws RException
*/
+ @Override
public void getFrom(String variable) throws RException {
this.variable = variable;
if (engine.isAutoCommit()) {
@@ -799,216 +784,50 @@
}
//update row names
- String[] rowNamesArray = (String[]) engine.eval("row.names("
- + this.variable + ")");
+ String[] rowNamesArray = (String[]) engine.eval("row.names(" +
+ 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("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("length(" +
+ this.variable + ")");
for (int i = 0; i < dataframelength; i++) {
- Integer arrayListLength = (Integer) engine.eval("length("
- + this.variable + "[," + (i + 1) + "])");
- ArrayList<Serializable> thisColumn = new ArrayList<Serializable>();
+ Integer arrayListLength = (Integer) engine.eval("length(" +
+ 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(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(
+ "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("names(attributes(" +
+ this.variable + "))[" + (i + 1) + "]");
- String attribute = (String) engine.eval("toString(attributes("
- + this.variable + ")$" + key + ")");
+ String attribute = (String) engine.eval("toString(attributes(" +
+ this.variable + ")$" + key + ")");
attributes.put(key, attribute);
}
}
}
- /**
- * Method to set all the attributes of the data.frame (there is no
- * synchronizing with R, use the commit() method to send data to R.
- *
- * @param attributes
- * a Map containing the attributes (key) and values (value)
- */
- public void setAttributes(Map<String, Object> attributes) throws RException {
- this.attributes = attributes;
-
- Set<String> keyset = attributes.keySet();
- String[] keys = keyset.toArray(new String[0]);
- for (int i = 0; i < keys.length; i++) {
- if (this.attributes.get(keys[i]) instanceof String) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i]
- + "\")<-\"" + this.attributes.get(keys[i]) + "\"");
- } else if (this.attributes.get(keys[i]) instanceof Double) {
- engine.eval("attr(" + 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])
- + ")");
- } else if (this.attributes.get(keys[i]) instanceof Boolean) {
- if ((Boolean) this.attributes.get(keys[i])) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i]
- + "\")<-TRUE");
- } else {
- engine.eval("attr(" + this.variable + ",\"" + keys[i]
- + "\")<-FALSE");
- }
- } else if (this.attributes.get(keys[i]) instanceof REXP) {
- engine.eval("attr(" + this.variable + ",\"" + keys[i] + "\")<-"
- + ((REXP) this.attributes.get(keys[i])).toRString());
- } else {
- log.warn("This attribute is not valid : " + keys[i] + " ; "
- + this.attributes.get(keys[i])
- + ". It will not be sent to R");
- }
- }
- }
-
- /**
- * Method to get all the attributes of the 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.
- *
- * @return a Map containing the attributes (key) and values (value)
- */
- public Map<String, Object> getAttributes() throws RException {
-
- if (engine.isAutoCommit()) {
- Integer attributeslength = (Integer) engine
- .eval("length(attributes(" + this.variable + "))");
-
- for (int i = 0; i < attributeslength; i++) {
- String key = (String) engine.eval("names(attributes("
- + this.variable + "))[" + (i + 1) + "]");
-
- Object attribute = engine.eval("attributes(" + this.variable
- + ")$" + key);
- if (attributes.containsKey(key)) {
- attributes.remove(key);
- attributes.put(key, attribute);
- } else {
- attributes.put(key, attribute);
- }
- }
- }
- return this.attributes;
- }
-
- /**
- * Method to get the value of an attribute (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.
- *
- * @param attribute
- * name of the attribute
- * @return
- */
- public Object getAttribute(String attribute) throws RException {
- if (engine.isAutoCommit()) {
- Object returnedAttribute = engine.eval("attr(" + this.variable
- + ",\"" + attribute + "\")");
- if (returnedAttribute != null) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, returnedAttribute);
- } else {
- attributes.put(attribute, returnedAttribute);
- }
- } else {
- throw new RException("Attribute does not exist");
- }
- }
- return attributes.get(attribute);
- }
-
- /**
- * Method to set the value of an attribute (there is no synchronizing with
- * R, use the commit() method to send data to R.
- *
- * @param attribute
- * name of the attribute
- * @param value
- * the value to be set (this is a R expression, String may be
- * rounded with escaped quote like : \"this is a R string\" ).
- */
- public void setAttribute(String attribute, Object value) throws RException {
- 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 + "\"");
- } 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);
- } 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 + ")");
- } else if (value instanceof Boolean) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, value);
- } else {
- attributes.put(attribute, value);
- }
- if ((Boolean) value) {
- engine.voidEval("attr(" + this.variable + ",\"" + attribute
- + "\")<-TRUE");
- } else {
- engine.voidEval("attr(" + this.variable + ",\"" + attribute
- + "\")<-FALSE");
- }
- } else if (value instanceof REXP) {
- 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");
- }
-
- }
-
public void exportCsv(File outputFile, boolean rowNames, boolean names) {
try {
BufferedWriter file = new BufferedWriter(new FileWriter(outputFile));
@@ -1083,7 +902,8 @@
}
}
- List<List<? extends Object>> tempData = new ArrayList<List<? extends Object>>();
+ List<List<? extends Object>> tempData =
+ new ArrayList<List<? extends Object>>();
for (int i = 0; i < dataSize; i++) {
List<Object> column = new ArrayList<Object>();
@@ -1098,8 +918,8 @@
index = 1;
}
for (int i = 0 + index; i < splitted.length; i++) {
- ((ArrayList<Object>) tempData.get(i - index))
- .add((Object) splitted[i]);
+ ((ArrayList<Object>) tempData.get(i - index)).add(
+ (Object) splitted[i]);
}
}
br.close();
@@ -1158,7 +978,8 @@
}
}
- List<List<? extends Object>> tempData = new ArrayList<List<? extends Object>>();
+ List<List<? extends Object>> tempData =
+ new ArrayList<List<? extends Object>>();
for (int i = 0; i < dataSize; i++) {
List<Object> column = new ArrayList<Object>();
@@ -1174,14 +995,14 @@
}
for (int i = 0 + index; i < splitted.length; i++) {
if (importType instanceof String) {
- ((ArrayList<Object>) tempData.get(i - index))
- .add((Object) splitted[i]);
+ ((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]));
+ ((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]));
+ ((ArrayList<Object>) tempData.get(i - index)).add(Integer.
+ valueOf(splitted[i]));
}
}
}
@@ -1243,7 +1064,8 @@
}
}
- List<List<? extends Object>> tempData = new ArrayList<List<? extends Object>>();
+ List<List<? extends Object>> tempData =
+ new ArrayList<List<? extends Object>>();
for (int i = 0; i < dataSize; i++) {
List<Object> column = new ArrayList<Object>();
@@ -1259,14 +1081,14 @@
}
for (int i = 0 + index; i < splitted.length; i++) {
if (importTypes.get(i - index) instanceof String) {
- ((ArrayList<Object>) tempData.get(i - index))
- .add((Serializable) splitted[i]);
+ ((ArrayList<Object>) tempData.get(i - index)).add(
+ (Serializable) splitted[i]);
} else if (importTypes.get(i - index) instanceof Double) {
- ((ArrayList<Object>) tempData.get(i - index))
- .add(Double.valueOf(splitted[i]));
+ ((ArrayList<Object>) tempData.get(i - index)).add(Double.
+ valueOf(splitted[i]));
} else if (importTypes.get(i - index) instanceof Integer) {
- ((ArrayList<Object>) tempData.get(i - index))
- .add(Integer.valueOf(splitted[i]));
+ ((ArrayList<Object>) tempData.get(i - index)).add(Integer.
+ valueOf(splitted[i]));
}
}
}
@@ -1276,5 +1098,4 @@
e.printStackTrace();
}
}
-
}
Added: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java (rev 0)
+++ trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2009-07-26 17:11:04 UTC (rev 115)
@@ -0,0 +1,224 @@
+/* *##% Nuiton Java-2-R library
+ * Copyright (C) 2006 - 2009 CodeLutin
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Lesser Public License for more details.
+ *
+ * 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.types;
+
+import java.util.Map;
+import java.util.Set;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.nuiton.j2r.REngine;
+import org.nuiton.j2r.RException;
+
+/**
+ * Abstract class for REXP interface, in order to avoid duplicate code for attributes related methods.
+ *
+ * @author couteau
+ */
+public class REXPAbstract implements REXP {
+
+ protected Log log = LogFactory.getLog(REXPAbstract.class);
+
+ //Name of the data.frame in R
+ protected String variable;
+
+ //Attributes of the data.frame under a map -> name : R expression
+ protected Map<String, Object> attributes;
+
+ //Engine used for R instructions.
+ protected REngine engine;
+
+ /**
+ * Always need to be implemented by extending classes
+ * @return null
+ * @throws org.nuiton.j2r.RException
+ */
+ @Override
+ public String toRString() throws RException {
+ return null;
+ }
+
+ /**
+ * Always need to be implemented by extending classes. Do nothing.
+ * @param variable
+ * @throws org.nuiton.j2r.RException
+ */
+ @Override
+ public void getFrom(String variable) throws RException {
+ }
+
+ @Override
+ public void setAttributes(Map<String, Object> attributes) throws RException {
+
+ 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++) {
+ if (this.attributes.get(keys[i]) instanceof String) {
+ engine.eval("attr(" + this.variable + ",\"" + keys[i] +
+ "\")<-\"" + this.attributes.get(keys[i]) + "\"");
+ } else if (this.attributes.get(keys[i]) instanceof Double) {
+ engine.eval("attr(" + 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]) +
+ ")");
+ } else if (this.attributes.get(keys[i]) instanceof Boolean) {
+ if ((Boolean) this.attributes.get(keys[i])) {
+ engine.eval("attr(" + this.variable + ",\"" + keys[i] +
+ "\")<-TRUE");
+ } else {
+ engine.eval("attr(" + this.variable + ",\"" + keys[i] +
+ "\")<-FALSE");
+ }
+ } else if (this.attributes.get(keys[i]) instanceof REXP) {
+ engine.eval("attr(" + this.variable + ",\"" + keys[i] +
+ "\")<-" + ((REXP) this.attributes.get(keys[i])).
+ toRString());
+ } else {
+ log.warn("This attribute is not valid : " + keys[i] + " ; " +
+ this.attributes.get(keys[i]) +
+ ". It will not be sent to R");
+ }
+ }
+ }
+ }
+
+ @Override
+ public Map<String, Object> getAttributes() throws RException {
+ if (engine.isAutoCommit()) {
+ Integer attributeslength = (Integer) engine.eval(
+ "length(attributes(" + this.variable + "))");
+
+ for (int i = 0; i < attributeslength; i++) {
+ 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);
+ }
+ }
+ }
+ return this.attributes;
+ }
+
+ @Override
+ public Object getAttribute(String attribute) throws RException {
+ if (engine.isAutoCommit()) {
+ Object returnedAttribute;
+ if ((Boolean) engine.eval("is.data.frame(attr(" + this.variable +
+ ",\"" + attribute + "\"))")) {
+ returnedAttribute = new RDataFrame(engine);
+ ((RDataFrame) returnedAttribute).getFrom("attr(" + this.variable +
+ ",\"" + attribute + "\")");
+ } else if ((Boolean) engine.eval("is.list(attr(" + this.variable +
+ ",\"" + attribute + "\"))")) {
+ returnedAttribute = new RList(engine);
+ ((RList) returnedAttribute).getFrom("attr(" + this.variable +
+ ",\"" + attribute + "\")");
+ } else {
+ returnedAttribute = engine.eval(
+ "attr(" + this.variable + ",\"" + attribute + "\")");
+ }
+
+ if (returnedAttribute != null) {
+ if (attributes.containsKey(attribute)) {
+ attributes.remove(attribute);
+ attributes.put(attribute, returnedAttribute);
+ } else {
+ attributes.put(attribute, returnedAttribute);
+ }
+ } else {
+ throw new RException("Attribute does not exist");
+ }
+ } else if (!attributes.containsKey(attribute)) {
+ throw new RException("Attribute does not exist");
+ }
+ return attributes.get(attribute);
+ }
+
+ @Override
+ public void setAttribute(String attribute, Object value) throws RException {
+ 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 + "\"");
+ } 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);
+ } 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 + ")");
+ } else if (value instanceof Boolean) {
+ if (attributes.containsKey(attribute)) {
+ attributes.remove(attribute);
+ attributes.put(attribute, value);
+ } else {
+ attributes.put(attribute, value);
+ }
+ if ((Boolean) value) {
+ engine.voidEval("attr(" + this.variable + ",\"" + attribute +
+ "\")<-TRUE");
+ } else {
+ engine.voidEval("attr(" + this.variable + ",\"" + attribute +
+ "\")<-FALSE");
+ }
+ } else if (value instanceof REXP) {
+ 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");
+ }
+ }
+
+ @Override
+ public String getVariable() {
+ return this.variable;
+ }
+}
Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-26 14:21:18 UTC (rev 114)
+++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-26 17:11:04 UTC (rev 115)
@@ -14,33 +14,23 @@
* 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.types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import java.util.Set;
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 implements REXP {
+public class RList extends REXPAbstract implements REXP {
private Log log = LogFactory.getLog(RList.class);
-
private List<String> names;
private List<Object> data;
- //Name of the data.frame in R
- private String variable;
- //Attributes of the data.frame under a map -> name : R expression
- private Map<String, Object> attributes;
- private REngine engine;
-
public RList(REngine engine) {
super();
this.names = new ArrayList<String>();
@@ -80,8 +70,8 @@
if (engine.isAutoCommit()) {
//Get back the names from R.
- String[] namesArray = (String[]) engine.eval("names("
- + this.variable + ")");
+ String[] namesArray = (String[]) engine.eval("names(" +
+ this.variable + ")");
//Check if size is correct, if yes, modify local data.
if (namesArray.length <= this.data.size()) {
@@ -93,10 +83,9 @@
} 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 + ")");
+ "Data inconsistency, local data is smaller (size : " +
+ this.data.size() + ") than the names in R (size : " +
+ namesArray.length + ")");
}
} else {
return this.names;
@@ -125,8 +114,8 @@
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("names(" + this.variable +
+ ")[" + (x + 1) + "]");
//Check if the String is returned.
if ((name != null) && (!name.equals(""))) {
@@ -135,8 +124,8 @@
}
} else {
//if String is not returned, throw an IndexOutOfBOundsException.
- throw new IndexOutOfBoundsException("Your index " + x
- + " is not valid");
+ throw new IndexOutOfBoundsException("Your index " + x +
+ " is not valid");
}
return this.names.get(x);
@@ -170,10 +159,10 @@
} 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() + ").");
+ "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() + ").");
}
}
@@ -210,16 +199,16 @@
}
//create the r instruction (names(var)[x]<-"name")
- String rexp = "names(" + this.variable + ")[" + (x + 1) + "]<-\""
- + name + "\"";
+ String rexp = "names(" + 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() + ".");
+ "Cannot assign the name, the index is out of bounds.\nIndex :" +
+ x + "; data.frame size : " + data.size() + ".");
}
}
@@ -273,16 +262,6 @@
}
/**
- * Method to get the list variable name.
- *
- * @return the list variable name.
- */
- @Override
- public String getVariable() {
- return this.variable;
- }
-
- /**
* 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.
@@ -430,9 +409,7 @@
}
}
- engine
- .voidEval(this.variable + "[[" + (x + 1) + "]]<-\"" + data
- + "\"");
+ engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-\"" + data + "\"");
}
/**
@@ -467,8 +444,8 @@
}
}
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-as.integer("
- + data + ")");
+ engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-as.integer(" +
+ data + ")");
}
/**
@@ -502,8 +479,8 @@
}
engine.voidEval(data.toRString());
- engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-"
- + data.getVariable());
+ engine.voidEval(this.variable + "[[" + (x + 1) + "]]<-" + data.
+ getVariable());
}
/**
@@ -519,12 +496,12 @@
*/
public Object get(int x) throws RException, IndexOutOfBoundsException {
if (x >= this.data.size()) {
- throw new IndexOutOfBoundsException("The x value is too high : "
- + x + ">=" + this.data.size());
+ throw new IndexOutOfBoundsException("The x value is too high : " + x +
+ ">=" + this.data.size());
}
if (engine.isAutoCommit()) {
- Object returnObject = engine.eval(this.variable + "[[" + (x + 1)
- + "]]");
+ Object returnObject = engine.eval(this.variable + "[[" + (x + 1) +
+ "]]");
if (returnObject instanceof String) {
this.data.set(x, (String) returnObject);
} else if (returnObject instanceof Double) {
@@ -589,8 +566,8 @@
}
//update names
- String[] namesArray = (String[]) engine.eval("names(" + this.variable
- + ")");
+ String[] namesArray = (String[]) engine.eval("names(" + this.variable +
+ ")");
for (int i = 0; i < namesArray.length; i++) {
names.add(namesArray[i]);
}
@@ -602,199 +579,16 @@
}
//update attributes
- Integer attributeslength = (Integer) engine.eval("length(attributes("
- + this.variable + "))");
+ Integer attributeslength = (Integer) engine.eval("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("names(attributes(" +
+ this.variable + "))[" + (i + 1) + "]");
- Object attribute = engine.eval("attributes(" + this.variable + ")$"
- + key);
+ Object attribute = engine.eval(
+ "attributes(" + this.variable + ")$" + key);
attributes.put(key, attribute);
}
}
-
- /**
- * Method to set all the attributes of the list
- *
- * @param attributes
- * a Map containing the attributes (key) and values (value).
- */
- @Override
- public void setAttributes(Map<String, Object> attributes) throws RException {
- this.attributes = attributes;
- if ((this.attributes != null) && (!this.attributes.isEmpty())) {
- String expr = "attributes(" + this.variable + ")<-list(";
-
- Set<String> keyset = attributes.keySet();
- String[] keys = keyset.toArray(new String[0]);
- for (int i = 0; i < keys.length; i++) {
- if (this.attributes.get(keys[i]) instanceof String) {
- expr += keys[i] + "=\"" + this.attributes.get(keys[i])
- + "\",";
- } else if (this.attributes.get(keys[i]) instanceof Double) {
- expr += keys[i] + "=" + this.attributes.get(keys[i]) + ",";
- } else if (this.attributes.get(keys[i]) instanceof Integer) {
- expr += keys[i] + "=as.integer("
- + this.attributes.get(keys[i]) + "),";
- } else if (this.attributes.get(keys[i]) instanceof Boolean) {
- if ((Boolean) this.attributes.get(keys[i])) {
- expr += keys[i] + "=TRUE,";
- } else {
- expr += keys[i] + "=FALSE,";
- }
- } else if (this.attributes.get(keys[i]) instanceof REXP) {
- expr += keys[i] + "="
- + ((REXP) this.attributes.get(keys[i])).toRString()
- + "),";
- } else {
- log.warn("This attribute is not valid : " + keys[i] + " ; "
- + this.attributes.get(keys[i])
- + ". It will not be sent to R");
- }
- }
-
- expr = expr.substring(0, expr.length() - 1) + ")";
- engine.voidEval(expr);
- }
- }
-
- /**
- * Method to get all the attributes of the list
- *
- * @return a Map containing the attributes (key) and values (value)
- */
- @Override
- public Map<String, Object> getAttributes() throws RException {
-
- if (engine.isAutoCommit()) {
- Integer attributeslength = (Integer) engine
- .eval("length(attributes(" + this.variable + "))");
-
- for (int i = 0; i < attributeslength; i++) {
- String key = (String) engine.eval("names(attributes("
- + this.variable + "))[" + (i + 1) + "]");
-
- Object attribute = engine.eval("attributes(" + this.variable
- + ")$" + key);
- if (attributes.containsKey(key)) {
- attributes.remove(key);
- attributes.put(key, attribute);
- } else {
- attributes.put(key, attribute);
- }
- }
- }
- return this.attributes;
- }
-
- /**
- * Method to get the value of an attribute
- *
- * @param attribute
- * name of the attribute
- * @return a String representing the attribute.
- */
- @Override
- public Object getAttribute(String attribute) throws RException {
- if (engine.isAutoCommit()) {
- Object returnedAttribute;
- if ((Boolean) engine.eval("is.data.frame(attr(" + this.variable
- + ",\"" + attribute + "\"))")) {
- returnedAttribute = new RDataFrame(engine);
- ((RDataFrame) returnedAttribute).getFrom("attr("
- + this.variable + ",\"" + attribute + "\")");
- } else if ((Boolean) engine.eval("is.list(attr(" + this.variable
- + ",\"" + attribute + "\"))")) {
- returnedAttribute = new RList(engine);
- ((RList) returnedAttribute).getFrom("attr(" + this.variable
- + ",\"" + attribute + "\")");
- } else {
- returnedAttribute = engine.eval("attr(" + this.variable + ",\""
- + attribute + "\")");
- }
-
- if (returnedAttribute != null) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, returnedAttribute);
- } else {
- attributes.put(attribute, returnedAttribute);
- }
- } else {
- throw new RException("Attribute does not exist");
- }
- } else if (!attributes.containsKey(attribute)) {
- throw new RException("Attribute does not exist");
- }
- return attributes.get(attribute);
- }
-
- /**
- * Method to set the value of an attribute
- *
- * @param attribute
- * name of the attribute
- * @param value
- * the value to be set.
- */
- @Override
- public void setAttribute(String attribute, Object value) throws RException {
- 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 + "\"");
- } 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);
- } 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 + ")");
- } else if (value instanceof Boolean) {
- if (attributes.containsKey(attribute)) {
- attributes.remove(attribute);
- attributes.put(attribute, value);
- } else {
- attributes.put(attribute, value);
- }
- if ((Boolean) value) {
- engine.voidEval("attr(" + this.variable + ",\"" + attribute
- + "\")<-TRUE");
- } else {
- engine.voidEval("attr(" + this.variable + ",\"" + attribute
- + "\")<-FALSE");
- }
- } else if (value instanceof REXP) {
- 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");
- }
-
- }
}
1
0
[LutinJ2R-commits] r114 - trunk/src/test/java/org/nuiton/j2r
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
26 Jul '09
Author: jcouteau
Date: 2009-07-26 16:21:18 +0200 (Sun, 26 Jul 2009)
New Revision: 114
Modified:
trunk/src/test/java/org/nuiton/j2r/ListTest.java
Log:
Improve RList tests
Modified: trunk/src/test/java/org/nuiton/j2r/ListTest.java
===================================================================
--- trunk/src/test/java/org/nuiton/j2r/ListTest.java 2009-07-26 14:21:03 UTC (rev 113)
+++ trunk/src/test/java/org/nuiton/j2r/ListTest.java 2009-07-26 14:21:18 UTC (rev 114)
@@ -125,6 +125,15 @@
assertEquals("b", list1.getNames().get(1));
assertEquals("c", list1.getNames().get(2));
assertEquals("d", list1.getNames().get(3));
+
+ engine
+ .voidEval("a<-list(a=as.integer(1),b=\"toto\",c=3.6,d=TRUE,e=FALSE)");
+ try {
+ list1.getNames();
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
+
}
@Test
@@ -147,6 +156,12 @@
assertEquals("b", list1.getName(1));
assertEquals("c", list1.getName(2));
assertEquals("d", list1.getName(3));
+
+ try {
+ list1.getName(4);
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
}
@Test
@@ -172,6 +187,19 @@
assertEquals("b", list1.getName(1));
assertEquals("c", list1.getName(2));
assertEquals("d", list1.getName(3));
+
+ names = new ArrayList<String>();
+ names.add("a");
+ names.add("b");
+ names.add("c");
+ names.add("d");
+ names.add("tooLongNow");
+
+ try {
+ list1.setNames(names);
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
}
@Test
@@ -182,24 +210,27 @@
data.add(3.6);
data.add(true);
- List<String> names = new ArrayList<String>();
- names.add("a");
- names.add("b");
- names.add("c");
- names.add("d");
-
RList list1 = new RList(engine);
list1.setVariable("a");
list1.setData(data);
+ list1.setName(3, "d");
list1.setName(0, "a");
list1.setName(1, "b");
list1.setName(2, "c");
- list1.setName(3, "d");
assertEquals("a", list1.getName(0));
assertEquals("b", list1.getName(1));
assertEquals("c", list1.getName(2));
assertEquals("d", list1.getName(3));
+
+ list1.setName(3, "d");
+ assertEquals("d", list1.getName(3));
+
+ try {
+ list1.setName(7, "badName");
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
}
@Test
@@ -259,6 +290,9 @@
list1.setVariable("a");
list1.set(3, 3.6);
assertEquals(3.6, engine.eval("a[[4]]"));
+ //test if everything is ok is there is another item at index
+ list1.set(3, 4.5);
+ assertEquals(4.5, engine.eval("a[[4]]"));
}
@Test
@@ -267,6 +301,9 @@
list1.setVariable("a");
list1.set(3, true);
assertEquals(true, engine.eval("a[[4]]"));
+ //test if everything is ok is there is another item at index, test if false works
+ list1.set(3, false);
+ assertEquals(false, engine.eval("a[[4]]"));
}
@Test
@@ -275,6 +312,9 @@
list1.setVariable("a");
list1.set(3, "titi");
assertEquals("titi", engine.eval("a[[4]]"));
+ //test if everything is ok is there is another item at index
+ list1.set(3, "toto");
+ assertEquals("toto", engine.eval("a[[4]]"));
}
@Test
@@ -283,6 +323,9 @@
list1.setVariable("a");
list1.set(3, 4);
assertEquals(4, engine.eval("a[[4]]"));
+ //test if everything is ok is there is another item at index
+ list1.set(3, 6);
+ assertEquals(6, engine.eval("a[[4]]"));
}
@Test
@@ -294,16 +337,34 @@
list1.set(2, true);
list1.set(3, "titi");
+ RList list3 = new RList(engine);
+ list3.setVariable("r");
+ list3.set(0, 3.6);
+ list3.set(1, 3);
+ list3.set(2, true);
+ list3.set(3, "titi");
+
RList list2 = new RList(engine);
list2.setVariable("zerty");
- list2.set(0, list1);
- list2.set(1, "a");
+ list2.set(1, list1);
+ list2.set(2, "a");
- assertEquals(3.6, engine.eval("zerty[[1]][[1]]"));
- assertEquals(3, engine.eval("zerty[[1]][[2]]"));
- assertEquals(true, engine.eval("zerty[[1]][[3]]"));
- assertEquals("titi", engine.eval("zerty[[1]][[4]]"));
- assertEquals("a", engine.eval("zerty[[2]]"));
+ assertEquals(3.6, engine.eval("zerty[[2]][[1]]"));
+ assertEquals(3, engine.eval("zerty[[2]][[2]]"));
+ assertEquals(true, engine.eval("zerty[[2]][[3]]"));
+ assertEquals("titi", engine.eval("zerty[[2]][[4]]"));
+ assertEquals("a", engine.eval("zerty[[3]]"));
+
+ list2.set(2, list3);
+ assertEquals(3.6, engine.eval("zerty[[2]][[1]]"));
+ assertEquals(3, engine.eval("zerty[[2]][[2]]"));
+ assertEquals(true, engine.eval("zerty[[2]][[3]]"));
+ assertEquals("titi", engine.eval("zerty[[2]][[4]]"));
+ assertEquals(3.6, engine.eval("zerty[[3]][[1]]"));
+ assertEquals(3, engine.eval("zerty[[3]][[2]]"));
+ assertEquals(true, engine.eval("zerty[[3]][[3]]"));
+ assertEquals("titi", engine.eval("zerty[[3]][[4]]"));
+
}
@Test
@@ -322,10 +383,17 @@
assertEquals(3, engine.eval("a[[2]]"));
assertEquals(true, engine.eval("a[[3]]"));
assertEquals("titi", engine.eval("a[[4]]"));
+
+ try {
+ list1.get(45);
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
}
@Test
public void testGetFromGetData() throws Exception {
+ //test getting a list with double, integer, string and boolean
RList list1 = new RList(engine);
engine.voidEval("a<-list(a=as.integer(1),b=\"toto\",c=3.6,d=TRUE)");
list1.getFrom("a");
@@ -340,6 +408,20 @@
assertEquals(3.6, list1.getData().get(2));
assertEquals(true, list1.getData().get(3));
+ //test if names, data and attributes are null (same list)
+ RList list2 = new RList(null, null, engine, "ble");
+ list2.setAttributes(null);
+ list2.getFrom("a");
+
+ assertEquals("a", list2.getName(0));
+ assertEquals("b", list2.getName(1));
+ assertEquals("c", list2.getName(2));
+ assertEquals("d", list2.getName(3));
+
+ assertEquals(1, list2.getData().get(0));
+ assertEquals("toto", list2.getData().get(1));
+ assertEquals(3.6, list2.getData().get(2));
+ assertEquals(true, list2.getData().get(3));
}
@Test
@@ -350,6 +432,31 @@
list1.setAttribute("doubleAttribute", 3.6);
list1.setAttribute("integerAttribute", 3);
list1.setAttribute("booleanTrueAttribute", true);
+ list1.setAttribute("booleanFalseAttribute", false);
+ Boolean[] booleanArray = { true, false };
+ list1.setAttribute("badAttribute", booleanArray);
+
+ Assert.assertEquals("test_value", engine
+ .eval("attributes(a)$stringAttribute"));
+ Assert.assertEquals(3.6, engine.eval("attributes(a)$doubleAttribute"));
+ Assert.assertEquals(3, engine.eval("attributes(a)$integerAttribute"));
+ Assert.assertEquals(true, engine
+ .eval("attributes(a)$booleanTrueAttribute"));
+ Assert.assertEquals(false, engine
+ .eval("attributes(a)$booleanFalseAttribute"));
+ Assert
+ .assertEquals("test_value", list1
+ .getAttribute("stringAttribute"));
+ Assert.assertEquals(3.6, list1.getAttribute("doubleAttribute"));
+ Assert.assertEquals(3, list1.getAttribute("integerAttribute"));
+ Assert.assertEquals(true, list1.getAttribute("booleanTrueAttribute"));
+ Assert.assertEquals(false, list1.getAttribute("booleanFalseAttribute"));
+
+ //Try resetting attributes to see everything goes well when attribute already exists.
+ list1.setAttribute("stringAttribute", "test_value");
+ list1.setAttribute("doubleAttribute", 3.6);
+ list1.setAttribute("integerAttribute", 3);
+ list1.setAttribute("booleanTrueAttribute", true);
list1.setAttribute("booleanFalseAttribute", false);
Assert.assertEquals("test_value", engine
@@ -368,9 +475,10 @@
Assert.assertEquals(true, list1.getAttribute("booleanTrueAttribute"));
Assert.assertEquals(false, list1.getAttribute("booleanFalseAttribute"));
- list1.setAttribute("test_attribute", "third_value");
- Assert.assertEquals("third_value", (String) engine
- .eval("attributes(a)$test_attribute"));
+ //Try if exception normally thrown when attribute does not exist
+ list1.setAttributes(new HashMap<String, Object>());
+ engine.eval("attr(a,\"BooleanFalseAttribute\")<-FALSE");
+ Assert.assertEquals(false, list1.getAttribute("booleanFalseAttribute"));
try {
list1.getAttribute("toto");
@@ -392,8 +500,12 @@
attributes.put("integerAttribute", 3);
attributes.put("booleanTrueAttribute", true);
attributes.put("booleanFalseAttribute", false);
+ Boolean[] booleanArray = { true, false };
+ attributes.put("badAttribute", booleanArray);
list1.setAttributes(attributes);
+ engine.eval("attr(a,\"newBooleanFalseAttribute\")<-FALSE");
+
Assert.assertEquals("test_value", engine
.eval("attributes(a)$stringAttribute"));
Assert.assertEquals(3.6, engine.eval("attributes(a)$doubleAttribute"));
@@ -410,6 +522,8 @@
"booleanTrueAttribute"));
Assert.assertEquals(false, list1.getAttributes().get(
"booleanFalseAttribute"));
+ Assert.assertEquals(false, list1.getAttributes().get(
+ "newBooleanFalseAttribute"));
}
@Test
@@ -522,6 +636,12 @@
assertEquals("c", list1.getName(2));
assertEquals("d", list1.getName(3));
+ try {
+ list1.getName(4);
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
+
engine.setAutoCommit(true);
}
@@ -723,6 +843,12 @@
assertEquals(true, list1.get(2));
assertEquals("titi", list1.get(3));
engine.setAutoCommit(true);
+
+ try {
+ list1.get(45);
+ Assert.fail("Error was not thrown although it should have been");
+ } catch (IndexOutOfBoundsException eee) {
+ }
}
@Test
1
0
[LutinJ2R-commits] r113 - trunk/src/main/java/org/nuiton/j2r/types
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
26 Jul '09
Author: jcouteau
Date: 2009-07-26 16:21:03 +0200 (Sun, 26 Jul 2009)
New Revision: 113
Modified:
trunk/src/main/java/org/nuiton/j2r/types/RList.java
Log:
Improve Attribute management for the RList
Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-26 14:19:16 UTC (rev 112)
+++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2009-07-26 14:21:03 UTC (rev 113)
@@ -277,6 +277,7 @@
*
* @return the list variable name.
*/
+ @Override
public String getVariable() {
return this.variable;
}
@@ -288,6 +289,7 @@
* @throws RException
* If no variable name is given
*/
+ @Override
public String toRString() throws RException {
if ((this.variable.equals("")) || (this.variable == null)) {
throw new RException(
@@ -295,7 +297,7 @@
}
String returnString = this.variable + "<-list(";
- if (!(this.data.isEmpty())) {
+ if ((this.data != null) && (!(this.data.isEmpty()))) {
for (int i = 0; i < data.size(); i++) {
if (!(this.names.isEmpty())) {
returnString += this.names.get(i) + "=";
@@ -567,6 +569,7 @@
* a REngine where the R session is located.
* @throws RException
*/
+ @Override
public void getFrom(String variable) throws RException {
this.variable = variable;
if (names != null) {
@@ -618,40 +621,43 @@
* @param attributes
* a Map containing the attributes (key) and values (value).
*/
+ @Override
public void setAttributes(Map<String, Object> attributes) throws RException {
this.attributes = attributes;
- String expr = "attributes(" + this.variable + ")<-list(";
+ if ((this.attributes != null) && (!this.attributes.isEmpty())) {
+ String expr = "attributes(" + this.variable + ")<-list(";
- Set<String> keyset = attributes.keySet();
- String[] keys = keyset.toArray(new String[0]);
- for (int i = 0; i < keys.length; i++) {
- if (this.attributes.get(keys[i]) instanceof String) {
- expr += keys[i] + "=\"" + this.attributes.get(keys[i]) + "\",";
- } else if (this.attributes.get(keys[i]) instanceof Double) {
- expr += keys[i] + "=" + this.attributes.get(keys[i]) + ",";
- } else if (this.attributes.get(keys[i]) instanceof Integer) {
- expr += keys[i] + "=as.integer(" + this.attributes.get(keys[i])
- + "),";
- } else if (this.attributes.get(keys[i]) instanceof Boolean) {
- if ((Boolean) this.attributes.get(keys[i])) {
- expr += keys[i] + "=TRUE,";
+ Set<String> keyset = attributes.keySet();
+ String[] keys = keyset.toArray(new String[0]);
+ for (int i = 0; i < keys.length; i++) {
+ if (this.attributes.get(keys[i]) instanceof String) {
+ expr += keys[i] + "=\"" + this.attributes.get(keys[i])
+ + "\",";
+ } else if (this.attributes.get(keys[i]) instanceof Double) {
+ expr += keys[i] + "=" + this.attributes.get(keys[i]) + ",";
+ } else if (this.attributes.get(keys[i]) instanceof Integer) {
+ expr += keys[i] + "=as.integer("
+ + this.attributes.get(keys[i]) + "),";
+ } else if (this.attributes.get(keys[i]) instanceof Boolean) {
+ if ((Boolean) this.attributes.get(keys[i])) {
+ expr += keys[i] + "=TRUE,";
+ } else {
+ expr += keys[i] + "=FALSE,";
+ }
+ } else if (this.attributes.get(keys[i]) instanceof REXP) {
+ expr += keys[i] + "="
+ + ((REXP) this.attributes.get(keys[i])).toRString()
+ + "),";
} else {
- expr += keys[i] + "=FALSE,";
+ log.warn("This attribute is not valid : " + keys[i] + " ; "
+ + this.attributes.get(keys[i])
+ + ". It will not be sent to R");
}
- } else if (this.attributes.get(keys[i]) instanceof REXP) {
- expr += keys[i] + "="
- + ((REXP) this.attributes.get(keys[i])).toRString()
- + "),";
- } else {
- log.warn("This attribute is not valid : " + keys[i] + " ; "
- + this.attributes.get(keys[i])
- + ". It will not be sent to R");
}
+
+ expr = expr.substring(0, expr.length() - 1) + ")";
+ engine.voidEval(expr);
}
-
- expr = expr.substring(0, expr.length() - 1) + ")";
- engine.voidEval(expr);
-
}
/**
@@ -659,6 +665,7 @@
*
* @return a Map containing the attributes (key) and values (value)
*/
+ @Override
public Map<String, Object> getAttributes() throws RException {
if (engine.isAutoCommit()) {
@@ -689,10 +696,25 @@
* name of the attribute
* @return a String representing the attribute.
*/
+ @Override
public Object getAttribute(String attribute) throws RException {
if (engine.isAutoCommit()) {
- Object returnedAttribute = engine.eval("attr(" + this.variable
- + ",\"" + attribute + "\")");
+ Object returnedAttribute;
+ if ((Boolean) engine.eval("is.data.frame(attr(" + this.variable
+ + ",\"" + attribute + "\"))")) {
+ returnedAttribute = new RDataFrame(engine);
+ ((RDataFrame) returnedAttribute).getFrom("attr("
+ + this.variable + ",\"" + attribute + "\")");
+ } else if ((Boolean) engine.eval("is.list(attr(" + this.variable
+ + ",\"" + attribute + "\"))")) {
+ returnedAttribute = new RList(engine);
+ ((RList) returnedAttribute).getFrom("attr(" + this.variable
+ + ",\"" + attribute + "\")");
+ } else {
+ returnedAttribute = engine.eval("attr(" + this.variable + ",\""
+ + attribute + "\")");
+ }
+
if (returnedAttribute != null) {
if (attributes.containsKey(attribute)) {
attributes.remove(attribute);
@@ -703,7 +725,7 @@
} else {
throw new RException("Attribute does not exist");
}
- } else if (!attributes.containsKey(attribute)){
+ } else if (!attributes.containsKey(attribute)) {
throw new RException("Attribute does not exist");
}
return attributes.get(attribute);
@@ -717,6 +739,7 @@
* @param value
* the value to be set.
*/
+ @Override
public void setAttribute(String attribute, Object value) throws RException {
if (value instanceof String) {
if (attributes.containsKey(attribute)) {
1
0
[LutinJ2R-commits] r112 - in trunk/src/main/java/org/nuiton/j2r: . jni net
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
26 Jul '09
Author: jcouteau
Date: 2009-07-26 16:19:16 +0200 (Sun, 26 Jul 2009)
New Revision: 112
Added:
trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java
Modified:
trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java
trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java
Log:
Create an abstract REngine to avoid similar code in different files.
Added: trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java (rev 0)
+++ trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java 2009-07-26 14:19:16 UTC (rev 112)
@@ -0,0 +1,229 @@
+package org.nuiton.j2r;
+
+import java.io.File;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.nuiton.j2r.jni.RJniEngine;
+import org.rosuda.JRI.Rengine;
+import org.rosuda.REngine.REXP;
+
+public class REngineAbstract implements REngine {
+
+ private Log log = LogFactory.getLog(RJniEngine.class);
+
+ /**
+ * Rengine is made to be static
+ */
+ private static Rengine engine;
+
+ /**
+ * If true, commit each R instruction on the fly, if false, commit only when
+ * the commit() method is called.
+ */
+ private Boolean autocommit = true;
+
+ /**
+ * List used to store all the R instructions when not in autocommit mode
+ * (when autocommit == false).
+ */
+ private List<String> rInstructions = new LinkedList<String>();
+
+ @Override
+ public boolean init() {
+ return false;
+ }
+
+ public boolean init(String host, int port) {
+ return false;
+ }
+
+ @Override
+ public Object eval(String expr) throws RException {
+ return null;
+ }
+
+ private Object convertResult(REXP rexp) {
+ return null;
+ }
+
+ @Override
+ public void terminate() throws RException {
+ }
+
+ @Override
+ public void voidEval(String expr) throws RException {
+ }
+
+ /**
+ * Load .RData file located in directory
+ *
+ * @param directory
+ * directory where the .RData file is located
+ * @throws RException
+ */
+ @Override
+ public void loadRData(File directory) throws RException {
+ setwd(directory);
+ voidEval("load(\".RData\")");
+ }
+
+ /**
+ * Save the session in a .RData file in directory
+ *
+ * @param directory
+ * where the .RData file will be saved
+ * @throws RException
+ */
+ @Override
+ public void saveRData(File directory) throws RException {
+ setwd(directory);
+ voidEval("save.image()");
+ }
+
+ /**
+ * Set the R working directory
+ *
+ * @param directory
+ * to set
+ * @throws RException
+ */
+ @Override
+ public void setwd(File directory) throws RException {
+ voidEval("setwd(\""
+ + directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")");
+ }
+
+ /**
+ * Get the actual R session working directory
+ *
+ * @return a File that is the actual R session working directory
+ * @throws RException
+ */
+
+ @Override
+ public File getwd() throws RException {
+ String directory = (String) eval("getwd()");
+ return new File(directory);
+ }
+
+ /**
+ * Use the dput R instruction to store the content of a R object to a file.
+ * The file created will be in the working directory
+ *
+ * @param rObject
+ * name of the R object to save
+ * @param outputFileName
+ * name of the file to save
+ * @throws RException
+ */
+ @Override
+ public void dput(String rObject, String outputFileName) throws RException {
+ String rInstruction = "dput(%s,file=\"%s\")";
+ voidEval(String.format(rInstruction, rObject, outputFileName));
+ }
+
+ /**
+ * Use the dget rInstruction to store the content of a file (created with
+ * the dput instruction) into a R object. The file used have to be in the
+ * working directory
+ *
+ * @param rObject
+ * name of the R object created
+ * @param inputFileName
+ * name of the file to load
+ * @throws RException
+ */
+ @Override
+ public void dget(String rObject, String inputFileName) throws RException {
+ String rInstruction = "%s <- dget(\"%s\")";
+ voidEval(String.format(rInstruction, rObject, inputFileName));
+
+ }
+
+ /**
+ * Use the dput R instruction to store the content of a R object to a file.
+ *
+ * @param rObject
+ * R object to save
+ * @param outputFile
+ * the file to save
+ * @throws RException
+ */
+ @Override
+ public void dput(String rObject, File outputFile) throws RException {
+ File workingdir = getwd();
+ setwd(outputFile.getParentFile());
+ String rInstruction = "dput(%s,file=\"%s\")";
+ voidEval(String.format(rInstruction, rObject, outputFile.getName()));
+ setwd(workingdir);
+ }
+
+ /**
+ * Use the dget rInstruction to store the content of a file (created with
+ * the dput instruction) into a R object.
+ *
+ * @param rObject
+ * name of the R object created
+ * @param inputFile
+ * file to load
+ * @throws RException
+ */
+ @Override
+ public void dget(String rObject, File inputFile) throws RException {
+ File workingdir = getwd();
+ setwd(inputFile.getParentFile());
+ String rInstruction = "%s <- dget(\"%s\")";
+ voidEval(String.format(rInstruction, rObject, inputFile.getName()));
+ setwd(workingdir);
+ }
+
+ @Override
+ public void remove(String rObject) throws RException {
+ voidEval("remove(" + rObject + ")");
+ }
+
+ @Override
+ public void mv(String inputObject, String outputObject) throws RException {
+ cp(inputObject, outputObject);
+ remove(inputObject);
+
+ }
+
+ @Override
+ public void cp(String inputObject, String outputObject) throws RException {
+ voidEval(outputObject + "<-" + inputObject);
+
+ }
+
+ @Override
+ public String[] ls() throws RException {
+ String[] rObjects = (String[]) eval("ls()");
+ return rObjects;
+ }
+
+ @Override
+ public void clearSession() throws RException {
+ voidEval("rm(list=ls())");
+ }
+
+ @Override
+ public void setAutoCommit(Boolean autocommit) throws RException {
+ if ((!this.autocommit) && (autocommit)) {
+ commit();
+ }
+ this.autocommit = autocommit;
+ }
+
+ @Override
+ public Boolean isAutoCommit() {
+ return this.autocommit;
+ }
+
+ @Override
+ public void commit() throws RException {
+ }
+
+}
Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-07-26 14:09:47 UTC (rev 111)
+++ trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2009-07-26 14:19:16 UTC (rev 112)
@@ -17,13 +17,13 @@
package org.nuiton.j2r.jni;
-import java.io.File;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.j2r.REngine;
+import org.nuiton.j2r.REngineAbstract;
import org.nuiton.j2r.RException;
import org.rosuda.JRI.REXP;
import org.rosuda.JRI.Rengine;
@@ -38,7 +38,7 @@
*
* Mise a jour: $Date: $ par : $Author: $
*/
-public class RJniEngine implements REngine {
+public class RJniEngine extends REngineAbstract implements REngine {
private Log log = LogFactory.getLog(RJniEngine.class);
@@ -131,7 +131,7 @@
}
break;
case REXP.XT_ARRAY_DOUBLE:
- //if double array, return the rexp as double array. Check if only one double, return a double.
+ //if double array, return the rexp as double array. Check if only one double, return a double.
result = rexp.asDoubleArray();
double[] doublearray = (double[]) result;
if (doublearray.length == 1) {
@@ -224,167 +224,7 @@
}
}
- /**
- * Load .RData file located in directory
- *
- * @param directory
- * directory where the .RData file is located
- * @throws RException
- */
@Override
- public void loadRData(File directory) throws RException {
- setwd(directory);
- voidEval("load(\".RData\")");
- }
-
- /**
- * Save the session in a .RData file in directory
- *
- * @param directory
- * where the .RData file will be saved
- * @throws RException
- */
- @Override
- public void saveRData(File directory) throws RException {
- setwd(directory);
- voidEval("save.image()");
- }
-
- /**
- * Set the R working directory
- *
- * @param directory
- * to set
- * @throws RException
- */
- @Override
- public void setwd(File directory) throws RException {
- voidEval("setwd(\""
- + directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")");
- }
-
- /**
- * Get the actual R session working directory
- *
- * @return a File that is the actual R session working directory
- * @throws RException
- */
-
- @Override
- public File getwd() throws RException {
- String directory = (String) eval("getwd()");
- return new File(directory);
- }
-
- /**
- * Use the dput R instruction to store the content of a R object to a file.
- * The file created will be in the working directory
- *
- * @param rObject
- * name of the R object to save
- * @param outputFileName
- * name of the file to save
- * @throws RException
- */
- @Override
- public void dput(String rObject, String outputFileName) throws RException {
- String rInstruction = "dput(%s,file=\"%s\")";
- voidEval(String.format(rInstruction, rObject, outputFileName));
- }
-
- /**
- * Use the dget rInstruction to store the content of a file (created with
- * the dput instruction) into a R object. The file used have to be in the
- * working directory
- *
- * @param rObject
- * name of the R object created
- * @param inputFileName
- * name of the file to load
- * @throws RException
- */
- @Override
- public void dget(String rObject, String inputFileName) throws RException {
- String rInstruction = "%s <- dget(\"%s\")";
- voidEval(String.format(rInstruction, rObject, inputFileName));
-
- }
-
- /**
- * Use the dput R instruction to store the content of a R object to a file.
- *
- * @param rObject
- * R object to save
- * @param outputFile
- * the file to save
- * @throws RException
- */
- @Override
- public void dput(String rObject, File outputFile) throws RException {
- File workingdir = getwd();
- setwd(outputFile.getParentFile());
- String rInstruction = "dput(%s,file=\"%s\")";
- voidEval(String.format(rInstruction, rObject, outputFile.getName()));
- setwd(workingdir);
- }
-
- /**
- * Use the dget rInstruction to store the content of a file (created with
- * the dput instruction) into a R object.
- *
- * @param rObject
- * name of the R object created
- * @param inputFile
- * file to load
- * @throws RException
- */
- @Override
- public void dget(String rObject, File inputFile) throws RException {
- File workingdir = getwd();
- setwd(inputFile.getParentFile());
- String rInstruction = "%s <- dget(\"%s\")";
- voidEval(String.format(rInstruction, rObject, inputFile.getName()));
- setwd(workingdir);
- }
-
- @Override
- public void remove(String rObject) throws RException {
- voidEval("remove(" + rObject + ")");
- }
-
- @Override
- public void mv(String inputObject, String outputObject) throws RException {
- cp(inputObject, outputObject);
- remove(inputObject);
-
- }
-
- @Override
- public void cp(String inputObject, String outputObject) throws RException {
- voidEval(outputObject + "<-" + inputObject);
-
- }
-
- @Override
- public String[] ls() throws RException {
- String[] rObjects = (String[]) eval("ls()");
- return rObjects;
- }
-
- @Override
- public void clearSession() throws RException {
- voidEval("rm(list=ls())");
- }
-
- @Override
- public void setAutoCommit(Boolean autocommit) throws RException {
- if ((!this.autocommit)&&(autocommit)){
- commit();
- }
- this.autocommit = autocommit;
- }
-
- @Override
public void commit() throws RException {
for (int i = 0; i < rInstructions.size(); i++) {
try {
@@ -398,10 +238,4 @@
}
rInstructions = new LinkedList<String>();
}
-
- @Override
- public Boolean isAutoCommit() {
- return this.autocommit;
- }
-
} // RJniEngine
Modified: trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java 2009-07-26 14:09:47 UTC (rev 111)
+++ trunk/src/main/java/org/nuiton/j2r/net/RNetEngine.java 2009-07-26 14:19:16 UTC (rev 112)
@@ -29,13 +29,13 @@
package org.nuiton.j2r.net;
-import java.io.File;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.j2r.REngine;
+import org.nuiton.j2r.REngineAbstract;
import org.nuiton.j2r.RException;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
@@ -52,7 +52,7 @@
* l'adresse de la machine distante et 6312 le port sur lequel tourne le
* serveur.
*/
-public class RNetEngine implements REngine {
+public class RNetEngine extends REngineAbstract implements REngine {
public static final int DEFAULT_PORT = 6311;
public static final String DEFAULT_HOST = "127.0.0.1";
@@ -111,6 +111,7 @@
return init(host, port);
}
+ @Override
public boolean init(String host, int port) {
if (log.isInfoEnabled()) {
log.info("Trying to connect to the Rserve on '" + host + ":" + port
@@ -246,168 +247,7 @@
}
}
- /**
- * Load .RData file located in directory
- *
- * @param directory
- * directory where the .RData file is located
- * @throws RException
- */
@Override
- public void loadRData(File directory) throws RException {
- setwd(directory);
- voidEval("load(\".RData\")");
- }
-
- /**
- * Save the session in a .RData file in directory
- *
- * @param directory
- * where the .RData file will be saved
- * @throws RException
- */
- @Override
- public void saveRData(File directory) throws RException {
- setwd(directory);
- voidEval("save.image()");
- }
-
- /**
- * Set the R working directory
- *
- * @param directory
- * to set
- * @throws RException
- */
- @Override
- public void setwd(File directory) throws RException {
- voidEval("setwd(\""
- + directory.getAbsolutePath().replaceAll("\\\\", "/") + "\")");
- }
-
- /**
- * Get the actual R session working directory
- *
- * @return a File that is the actual R session working directory
- * @throws RException
- */
-
- @Override
- public File getwd() throws RException {
- String directory = (String) eval("getwd()");
- return new File(directory);
- }
-
- /**
- * Use the dput R instruction to store the content of a R object to a file.
- * The file created will be in the working directory
- *
- * @param rObject
- * name of the R object to save
- * @param outputFileName
- * name of the file to save
- * @throws RException
- */
- @Override
- public void dput(String rObject, String outputFileName) throws RException {
- String rInstruction = "dput(%s,file=\"%s\")";
- voidEval(String.format(rInstruction, rObject, outputFileName));
- }
-
- /**
- * Use the dget rInstruction to store the content of a file (created with
- * the dput instruction) into a R object. The file used have to be in the
- * working directory
- *
- * @param rObject
- * name of the R object created
- * @param inputFileName
- * name of the file to load
- * @throws RException
- */
- @Override
- public void dget(String rObject, String inputFileName) throws RException {
- String rInstruction = "%s <- dget(\"%s\")";
- voidEval(String.format(rInstruction, rObject, inputFileName));
-
- }
-
- /**
- * Use the dput R instruction to store the content of a R object to a file.
- *
- * @param rObject
- * R object to save
- * @param outputFile
- * the file to save
- * @throws RException
- */
- @Override
- public void dput(String rObject, File outputFile) throws RException {
- File workingdir = getwd();
- setwd(outputFile.getParentFile());
- String rInstruction = "dput(%s,file=\"%s\")";
- voidEval(String.format(rInstruction, rObject, outputFile.getName()));
- setwd(workingdir);
- }
-
- /**
- * Use the dget rInstruction to store the content of a file (created with
- * the dput instruction) into a R object.
- *
- * @param rObject
- * name of the R object created
- * @param inputFile
- * file to load
- * @throws RException
- */
- @Override
- public void dget(String rObject, File inputFile) throws RException {
- File workingdir = getwd();
- setwd(inputFile.getParentFile());
- String rInstruction = "%s <- dget(\"%s\")";
- voidEval(String.format(rInstruction, rObject, inputFile.getName()));
- setwd(workingdir);
- }
-
- @Override
- public void remove(String rObject) throws RException {
- voidEval("remove(" + rObject + ")");
- }
-
- @Override
- public void mv(String inputObject, String outputObject) throws RException {
- cp(inputObject, outputObject);
- remove(inputObject);
-
- }
-
- @Override
- public void cp(String inputObject, String outputObject) throws RException {
- voidEval(outputObject + "<-" + inputObject);
-
- }
-
- @Override
- public String[] ls() throws RException {
- String[] rObjects = (String[]) eval("ls()");
- return rObjects;
- }
-
- @Override
- public void clearSession() throws RException {
- voidEval("rm(list=ls())");
-
- }
-
- @Override
- public void setAutoCommit(Boolean autocommit) throws RException{
- if ((!this.autocommit)&&(autocommit)){
- commit();
- }
- this.autocommit = autocommit;
- }
-
- @Override
public void commit() throws RException {
for (int i = 0; i < rInstructions.size(); i++) {
String expr = rInstructions.get(i);
@@ -421,10 +261,4 @@
}
rInstructions = new LinkedList<String>();
}
-
- @Override
- public Boolean isAutoCommit(){
- return this.autocommit;
- }
-
} // RNetEngine
1
0
[LutinJ2R-commits] r111 - trunk/src/main/java/org/nuiton/j2r
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
by jcouteau@users.labs.libre-entreprise.org 26 Jul '09
26 Jul '09
Author: jcouteau
Date: 2009-07-26 16:09:47 +0200 (Sun, 26 Jul 2009)
New Revision: 111
Modified:
trunk/src/main/java/org/nuiton/j2r/REngine.java
trunk/src/main/java/org/nuiton/j2r/RProxy.java
Log:
Clean imports
Modified: trunk/src/main/java/org/nuiton/j2r/REngine.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/REngine.java 2009-06-21 12:17:16 UTC (rev 110)
+++ trunk/src/main/java/org/nuiton/j2r/REngine.java 2009-07-26 14:09:47 UTC (rev 111)
@@ -16,23 +16,20 @@
* <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/
/* *
-* RInterface.java
-*
-* Created: 21 aout 2006
-*
-* @author Arnaud Thimel <thimel(a)codelutin.com>
-* @version $Revision: $
-*
-* Mise a jour: $Date: $
-* par : $Author: $
-*/
-
+ * RInterface.java
+ *
+ * Created: 21 aout 2006
+ *
+ * @author Arnaud Thimel <thimel(a)codelutin.com>
+ * @version $Revision: $
+ *
+ * Mise a jour: $Date: $
+ * par : $Author: $
+ */
package org.nuiton.j2r;
import java.io.File;
-import org.nuiton.j2r.RException;
-
/**
* Cette interface est le point commun entre les differentes technologies
* utilisees pour l'acces a R
@@ -109,7 +106,6 @@
* @return a File that is the actual R session working directory
* @throws RException
*/
-
public File getwd() throws RException;
/**
@@ -230,5 +226,4 @@
* Commit all the R instructions that have been stored and not commited yet.
*/
public void commit() throws RException;
-
} //RJniEngine
Modified: trunk/src/main/java/org/nuiton/j2r/RProxy.java
===================================================================
--- trunk/src/main/java/org/nuiton/j2r/RProxy.java 2009-06-21 12:17:16 UTC (rev 110)
+++ trunk/src/main/java/org/nuiton/j2r/RProxy.java 2009-07-26 14:09:47 UTC (rev 111)
@@ -33,7 +33,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.nuiton.j2r.RException;
import org.nuiton.j2r.jni.RJniEngine;
import org.nuiton.j2r.net.RNetEngine;
1
0