Nuiton-j2r-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2010 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2009 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2008 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2007 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2006 -----
- December
- November
- October
- September
- August
- 316 discussions
[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
18 May '09
Author: tchemit
Date: 2009-05-18 15:54:10 +0000 (Mon, 18 May 2009)
New Revision: 91
Modified:
nuiton-j2r/trunk/pom.xml
Log:
utilisation url de site sur nuiton :)
Modified: nuiton-j2r/trunk/pom.xml
===================================================================
--- nuiton-j2r/trunk/pom.xml 2009-05-18 10:47:34 UTC (rev 90)
+++ nuiton-j2r/trunk/pom.xml 2009-05-18 15:54:10 UTC (rev 91)
@@ -69,7 +69,8 @@
Network ou en JNI.
</description>
<inceptionYear>2006</inceptionYear>
- <url>http://www.nuiton.org/embedded/${project.artifactId}</url>
+ <url>http://maven-site.nuiton.org/${project.artifactId}</url>
+ <!--url>http://www.nuiton.org/embedded/${project.artifactId}</url-->
<!-- ************************************************************* -->
<!-- *** Build Settings ****************************************** -->
1
0
18 May '09
Author: tchemit
Date: 2009-05-18 10:47:34 +0000 (Mon, 18 May 2009)
New Revision: 90
Modified:
nuiton-j2r/trunk/pom.xml
Log:
use mavempom snapshot
Modified: nuiton-j2r/trunk/pom.xml
===================================================================
--- nuiton-j2r/trunk/pom.xml 2009-05-17 20:25:14 UTC (rev 89)
+++ nuiton-j2r/trunk/pom.xml 2009-05-18 10:47:34 UTC (rev 90)
@@ -10,7 +10,7 @@
<parent>
<groupId>org.nuiton</groupId>
<artifactId>mavenpom</artifactId>
- <version>1.0.0-alpha-2</version>
+ <version>1.0.0-alpha-3-SNAPSHOT</version>
</parent>
<artifactId>nuiton-j2r</artifactId>
1
0
17 May '09
Author: tchemit
Date: 2009-05-17 20:25:14 +0000 (Sun, 17 May 2009)
New Revision: 89
Modified:
nuiton-j2r/trunk/pom.xml
Log:
[maven-release-plugin] prepare for next development iteration
Modified: nuiton-j2r/trunk/pom.xml
===================================================================
--- nuiton-j2r/trunk/pom.xml 2009-05-17 20:25:11 UTC (rev 88)
+++ nuiton-j2r/trunk/pom.xml 2009-05-17 20:25:14 UTC (rev 89)
@@ -14,7 +14,7 @@
</parent>
<artifactId>nuiton-j2r</artifactId>
- <version>1.0.0-alpha-1</version>
+ <version>1.0.0-alpha-2-SNAPSHOT</version>
<dependencies>
@@ -129,9 +129,9 @@
<!-- Source control management. -->
<scm>
- <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinj2r/nuiton-j2r/tags/nuiton-j2r-1.0.0-alpha-1</connection>
- <developerConnection>scm:svn:svn+ssh://tchemit@labs.libre-entreprise.org/svnroot/lutinj2r/nuiton-j2r/tags/nuiton-j2r-1.0.0-alpha-1</developerConnection>
- <url>http://www.nuiton.org/repositories/browse/nuiton-j2r/tags/nuiton-j2r-1.0.0-…</url>
+ <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinj2r/nuiton-j2r/trunk</connection>
+ <developerConnection>scm:svn:svn+ssh://tchemit@labs.libre-entreprise.org/svnroot/lutinj2r/nuiton-j2r/trunk</developerConnection>
+ <url>http://www.nuiton.org/repositories/browse/nuiton-j2r/trunk</url>
<!-- a utiliser quand on sera sur nuiton -->
<!-- ne fonctionnera pas pour les multi-modules a cause de l'expansion de l'artifactId
1
0
17 May '09
Author: tchemit
Date: 2009-05-17 20:25:11 +0000 (Sun, 17 May 2009)
New Revision: 88
Added:
nuiton-j2r/tags/nuiton-j2r-1.0.0-alpha-1/
Log:
[maven-scm] copy for tag nuiton-j2r-1.0.0-alpha-1
Copied: nuiton-j2r/tags/nuiton-j2r-1.0.0-alpha-1 (from rev 87, nuiton-j2r/trunk)
1
0
17 May '09
Author: tchemit
Date: 2009-05-17 20:25:06 +0000 (Sun, 17 May 2009)
New Revision: 87
Modified:
nuiton-j2r/trunk/pom.xml
Log:
[maven-release-plugin] prepare release nuiton-j2r-1.0.0-alpha-1
Modified: nuiton-j2r/trunk/pom.xml
===================================================================
--- nuiton-j2r/trunk/pom.xml 2009-05-17 20:24:41 UTC (rev 86)
+++ nuiton-j2r/trunk/pom.xml 2009-05-17 20:25:06 UTC (rev 87)
@@ -14,7 +14,7 @@
</parent>
<artifactId>nuiton-j2r</artifactId>
- <version>1.0.0-alpha-1-SNAPSHOT</version>
+ <version>1.0.0-alpha-1</version>
<dependencies>
@@ -129,9 +129,9 @@
<!-- Source control management. -->
<scm>
- <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinj2r/${project.artifactId}/trunk</connection>
- <developerConnection>scm:svn:svn+ssh://tchemit@labs.libre-entreprise.org/svnroot/lutinj2r/${project.artifactId}/trunk</developerConnection>
- <url>http://www.nuiton.org/repositories/browse/${project.artifactId}/trunk</url>
+ <connection>scm:svn:svn://anonymous@labs.libre-entreprise.org/svnroot/lutinj2r/nuiton-j2r/tags/nuiton-j2r-1.0.0-alpha-1</connection>
+ <developerConnection>scm:svn:svn+ssh://tchemit@labs.libre-entreprise.org/svnroot/lutinj2r/nuiton-j2r/tags/nuiton-j2r-1.0.0-alpha-1</developerConnection>
+ <url>http://www.nuiton.org/repositories/browse/nuiton-j2r/tags/nuiton-j2r-1.0.0-…</url>
<!-- a utiliser quand on sera sur nuiton -->
<!-- ne fonctionnera pas pour les multi-modules a cause de l'expansion de l'artifactId
1
0