Author: jcouteau Date: 2010-04-21 17:08:21 +0200 (Wed, 21 Apr 2010) New Revision: 210 Log: Simplify code Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java trunk/src/test/java/org/nuiton/j2r/DataframeTest.java Modified: trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2010-04-21 09:59:36 UTC (rev 209) +++ trunk/src/main/java/org/nuiton/j2r/jni/RJniEngine.java 2010-04-21 15:08:21 UTC (rev 210) @@ -119,7 +119,7 @@ */ @Override public Object eval(String expr) throws RException { - REXP result = null; + REXP result; if (log.isDebugEnabled()) { log.debug(String.format(RInstructions.RTRY, expr)); } @@ -162,18 +162,18 @@ break; case REXP.XT_INT: //if integer, return the rexp as integer - result = (Integer) rexp.asInt(); + result = rexp.asInt(); break; case REXP.XT_ARRAY_INT: int[] array = rexp.asIntArray(); Integer[] bigArray = new Integer[array.length]; for (int i = 0; i < array.length; i++) { - bigArray[i] = (Integer) array[i]; + bigArray[i] = array[i]; } result = bigArray; //Check if only one integer, return an integer. if (array.length == 1) { - result = (Integer) array[0]; + result = array[0]; } break; case REXP.XT_ARRAY_DOUBLE: @@ -181,7 +181,7 @@ double[] doublearray = rexp.asDoubleArray(); Double[] bigdoublearray = new Double[doublearray.length]; for (int i = 0; i < doublearray.length; i++) { - bigdoublearray[i] = (Double) doublearray[i]; + bigdoublearray[i] = doublearray[i]; } result = bigdoublearray; //Check if only one double, return a double. @@ -198,7 +198,7 @@ //Get a double array result = rexp.asDoubleArray(); //return only the first element. - result = (Double) ((double[]) result)[0]; + result = ((double[]) result)[0]; break; case REXP.XT_NULL: //if null return null @@ -243,18 +243,16 @@ if (klass.equals(RInstructions.CLASS_DATAFRAME)) { //if rexp is a data.frame - RDataFrame temp = new RDataFrame((REngine) this); + RDataFrame temp; //create the data list. - List<List<? extends Object>> data = - new ArrayList<List<? extends Object>>(); + List<List<?>> data = new ArrayList<List<?>>(); //get rexp as a list (data.frame is a list of vectors) - org.rosuda.JRI.RList dataList = rexp.asList(); - for (int i = 0; i < dataList.keys().length; i++) { + for (int i = 0; i < list.keys().length; i++) { //for each vector, create a list and fill it with the //content of the vector. - List<Object> templist = new ArrayList<Object>(); - REXP tempREXP = dataList.at(i); + List<Object> templist; + REXP tempREXP = list.at(i); Object[] convertedREXP = (Object[]) convertResult( tempREXP); templist = Arrays.asList(convertedREXP); @@ -265,13 +263,14 @@ //Create a new dataframe with the names, row.names and data //gotten from rexp. It has no variable name so throws a //RException. - temp = new RDataFrame((REngine) this, rexp.getAttribute( + + temp = new RDataFrame(this, rexp.getAttribute( RInstructions.ATTRIBUTE_NAMES).asStringArray(), rexp.getAttribute(RInstructions.ATTRIBUTE_ROWNAMES).asStringArray(), data, ""); result = temp; } else if (list != null) { - RList temp = new RList((REngine) this); + RList temp = new RList(this); List<Object> data = new ArrayList<Object>(); org.rosuda.JRI.RList dataList = rexp.asList(); for (int i = 0; i < dataList.keys().length; i++) { @@ -289,7 +288,7 @@ try { temp = new RList( rexp.getAttribute(RInstructions.ATTRIBUTE_NAMES).asStringArray(), - data, (REngine) this, ""); + data, this, ""); } catch (RException re) { //don't propagate the error as it is normal. Log it for debug. if (log.isDebugEnabled()) { @@ -371,10 +370,9 @@ */ @Override public void commit() throws RException { - for (int i = 0; i < rInstructions.size(); i++) { + for (String expr:rInstructions) { //Send one by one, all the Rinstructions stored, beginning by //the first one stored - String expr = rInstructions.get(i); engine.eval(expr); } //Clean the list of R instructions Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-04-21 09:59:36 UTC (rev 209) +++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-04-21 15:08:21 UTC (rev 210) @@ -31,7 +31,6 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -537,23 +536,13 @@ public Object get(int x, int y) throws RException { checkX(x); checkY(y); - if ((engine.isAutoCommit())&&(this.variable!=null)&&(!this.variable.isEmpty())) { + if (engine.isAutoCommit()) { + checkVariable(); Object returnObject = engine.eval(String.format( RInstructions.GET_DATAFRAME_ITEM, this.variable, y + 1, x + 1)); - if (returnObject instanceof String) { - ((ArrayList<String>) this.data.get(x)).set(y, - (String) returnObject); - } else if (returnObject instanceof Double) { - ((ArrayList<Double>) this.data.get(x)).set(y, - (Double) returnObject); - } else if (returnObject instanceof Integer) { - ((ArrayList<Integer>) this.data.get(x)).set(y, - (Integer) returnObject); - } else if (returnObject instanceof Boolean) { - ((ArrayList<Boolean>) this.data.get(x)).set(y, - (Boolean) returnObject); - } + + ((ArrayList<Object>) this.data.get(x)).set(y,returnObject); } return this.data.get(x).get(y); } @@ -579,6 +568,8 @@ return data; } + initData(rowNumber, columnNumber); + try { for(int i=0;i<columnNumber;i++){ for(int j=0;j<rowNumber;j++){ @@ -594,6 +585,20 @@ return data; } + private void initData(int rowNumber, int columnNumber){ + + + this.data = new ArrayList<List<?>>(); + + for (int i=0;i<columnNumber;i++){ + List<Object> column = new ArrayList<Object>(); + for(int j=0;j<rowNumber;j++){ + column.add(new Object()); + } + this.data.add(column); + } + } + /** * Method to assign the data of the R data.frame (there is no synchronizing * with R, use the commit() method to send data to R. @@ -619,68 +624,23 @@ public void getFrom(String variable) throws RException { this.variable = variable; if (engine.isAutoCommit()) { - if (rowNames != null) { - rowNames.clear(); - } else { - rowNames = new ArrayList<String>(); - } - if (names != null) { - names.clear(); - } else { - names = new ArrayList<String>(); - } - if (data != null) { - data.clear(); - } else { - data = new ArrayList<List<?>>(); - } - if (attributes != null) { - attributes.clear(); - } else { - attributes = new HashMap<String, Object>(); - } - //update row names - String[] rowNamesArray = (String[]) engine.eval(String.format( - RInstructions.GET_ROW_NAMES, this.variable)); - rowNames.addAll(Arrays.asList(rowNamesArray)); + rowNames.clear(); + names.clear(); + data.clear(); + attributes.clear(); - //update names - String[] namesArray = (String[]) engine.eval(String.format( - RInstructions.GET_NAMES, this.variable)); - names.addAll(Arrays.asList(namesArray)); - //update data - Integer dataframelength = (Integer) engine.eval(String.format( - RInstructions.LENGTH, this.variable)); + getData(); - for (int i = 0; i < dataframelength; i++) { - Integer arrayListLength = (Integer) engine.eval(String.format( - RInstructions.LENGTH_COLUMN, this.variable, i + 1)); - ArrayList<Serializable> thisColumn = - new ArrayList<Serializable>(); - for (int j = 0; j < arrayListLength; j++) { - thisColumn.add((Serializable) engine.eval(String.format( - RInstructions.GET_DATAFRAME_ITEM, this.variable, - j + 1, i + 1))); - } - data.add(thisColumn); + //update row names + getRowNames(); - } + //update names + getNames(); //update attributes - Integer attributeslength = (Integer) engine.eval(String.format( - RInstructions.LENGTH_ATTRIBUTES, this.variable)); - - for (int i = 0; - i < attributeslength; i++) { - String key = (String) engine.eval(String.format( - RInstructions.GET_ATTRIBUTE_NAME, this.variable, i + 1)); - - String attribute = (String) engine.eval("toString(" + String.format( - RInstructions.GET_ATTRIBUTE, this.variable, key) + ")"); - attributes.put(key, attribute); - } + getAttributes(); } } @@ -814,21 +774,11 @@ dataSize = splitted.length; } - //if data has already been initialized, clear it. - if (this.data != null) { - this.data.clear(); - } + //clear data, rowNames and names + this.data.clear(); + this.rowNames.clear(); + this.names.clear(); - //Clear rowNames - if ((this.rowNames != null)) { - this.rowNames.clear(); - } - - //if names has already been initialized, clear it. - if (this.names != null) { - this.names.clear(); - } - if (names) { //if names are present in the file, parse the first line to get //the names. Modified: trunk/src/test/java/org/nuiton/j2r/DataframeTest.java =================================================================== --- trunk/src/test/java/org/nuiton/j2r/DataframeTest.java 2010-04-21 09:59:36 UTC (rev 209) +++ trunk/src/test/java/org/nuiton/j2r/DataframeTest.java 2010-04-21 15:08:21 UTC (rev 210) @@ -786,6 +786,23 @@ dataframe2.importCsv(new File("/tmp/test.csv"), true, true, 3.0); dataframe2.setVariable("a"); + List<List<?>> data2 = dataframe2.getData(); + + //Test data + Assert.assertEquals(3.0, data2.get(0).get(0)); + Assert.assertEquals(4.5, data2.get(0).get(1)); + Assert.assertEquals(0.01, data2.get(0).get(2)); + Assert.assertEquals(1.0, data2.get(1).get(0)); + Assert.assertEquals(5555555555555555555555.0, data2.get(1).get(1)); + Assert.assertEquals(3.0, data2.get(1).get(2)); + //Test names + Assert.assertEquals("column1", dataframe2.getNames().get(0)); + Assert.assertEquals("column2", dataframe2.getNames().get(1)); + //Test row names + Assert.assertEquals("row 1", dataframe2.getRowNames().get(0)); + Assert.assertEquals("row 2", dataframe2.getRowNames().get(1)); + Assert.assertEquals("row 3", dataframe2.getRowNames().get(2)); + //Test import with same type for each column (String). names = new ArrayList<String>();