r241 - in trunk/src: main/java/org/nuiton/j2r main/java/org/nuiton/j2r/types site/rst test/java/org/nuiton/j2r
Author: jcouteau Date: 2010-10-22 10:51:00 +0200 (Fri, 22 Oct 2010) New Revision: 241 Url: http://nuiton.org/repositories/revision/nuiton-j2r/241 Log: Improve code : use string builders, close streams in finally blocks, replace private by protected Modified: trunk/src/main/java/org/nuiton/j2r/REngine.java trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java trunk/src/main/java/org/nuiton/j2r/types/RList.java trunk/src/site/rst/installTest.rst trunk/src/test/java/org/nuiton/j2r/AbstractEngineTest.java trunk/src/test/java/org/nuiton/j2r/NetTest.java Modified: trunk/src/main/java/org/nuiton/j2r/REngine.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/REngine.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/main/java/org/nuiton/j2r/REngine.java 2010-10-22 08:51:00 UTC (rev 241) @@ -289,7 +289,7 @@ */ void saveRData(String filename) throws RException; - public void plot(String filename, String x, String y, String type, + void plot(String filename, String x, String y, String type, String main, String sub, String xlab, String ylab, String asp) throws RException; } //REngine Modified: trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/main/java/org/nuiton/j2r/REngineAbstract.java 2010-10-22 08:51:00 UTC (rev 241) @@ -39,7 +39,7 @@ * If true, commit each R instruction on the fly, if false, commit only when * the commit() method is called. */ - private Boolean autocommit = true; + protected Boolean autocommit = true; /** * Load a specific R session : @@ -343,45 +343,58 @@ eval("jpeg(\"" + filename + ".jpg\")"); - String Rinstruction = "plot("; + StringBuilder rinstruction = new StringBuilder("plot("); if ((x != null) && (!x.isEmpty())) { - Rinstruction += x; + rinstruction.append(x); } else { throw new IllegalArgumentException("x argument must be filled"); } if ((y != null) && (!y.isEmpty())) { - Rinstruction += ",y=" + y; + rinstruction.append(",y="); + rinstruction.append(y); } if (type != null) { - Rinstruction += ",type=\"" + type + "\""; + rinstruction.append(",type=\""); + rinstruction.append(type); + rinstruction.append("\""); } if (main != null) { - Rinstruction += ",main=\"" + main + "\""; + rinstruction.append(",main=\""); + rinstruction.append(main); + rinstruction.append("\""); } if (sub != null) { - Rinstruction += ",sub=\"" + sub + "\""; + rinstruction.append(",sub=\""); + rinstruction.append(sub); + rinstruction.append("\""); } if (xlab != null) { - Rinstruction += ",xlab=\"" + xlab + "\""; + rinstruction.append(",xlab=\""); + rinstruction.append(xlab); + rinstruction.append("\""); } if (ylab != null) { - Rinstruction += ",ylab=\"" + ylab + "\""; + rinstruction.append(",ylab=\""); + rinstruction.append(ylab); + rinstruction.append("\""); } if (asp != null) { - Rinstruction += ",asp=" + asp + "\""; + rinstruction.append(",asp="); + rinstruction.append(asp); + rinstruction.append("\""); } - Rinstruction += ")"; + rinstruction.append(")"); - eval(Rinstruction); + eval(rinstruction.toString()); eval("dev.off()"); } Modified: trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/main/java/org/nuiton/j2r/types/RDataFrame.java 2010-10-22 08:51:00 UTC (rev 241) @@ -273,15 +273,17 @@ this.rowNames = rowNames; //Create the r instruction (row.names(var)<-c("name 1",...,"name x")). - String rowNamesString = ""; + StringBuilder rowNamesString = new StringBuilder(); for (int i = 0; i < this.rowNames.size(); i++) { if (i != 0) { - rowNamesString += ","; + rowNamesString.append(","); } - rowNamesString += "\"" + rowNames.get(i) + "\""; + rowNamesString.append("\""); + rowNamesString.append(rowNames.get(i)); + rowNamesString.append("\""); } String rexp = String.format(RInstructions.SET_ROW_NAMES, - this.variable, rowNamesString); + this.variable, rowNamesString.toString()); //Send the r instruction to the engine. engine.voidEval(rexp); @@ -333,17 +335,21 @@ public String toRString() throws RException { checkVariable(); - String returnString = this.variable + "<-data.frame("; + StringBuilder returnString = new StringBuilder(); + returnString.append(this.variable); + returnString.append("<-data.frame("); + for (List<?> column:this.data){ if (!column.isEmpty()){ if (!(this.names.isEmpty())) { int index = this.data.indexOf(column); - returnString += this.names.get(index) + "=c("; + returnString.append(this.names.get(index)); + returnString.append("=c("); } else { - returnString += "c("; + returnString.append("c("); } Object firstElement = column.get(0); @@ -351,56 +357,62 @@ if (firstElement instanceof String) { for (Object obj:column) { - returnString += "\"" + obj + "\","; + returnString.append("\""); + returnString.append(obj); + returnString.append("\","); } } else if (firstElement instanceof Boolean) { for (Object obj:column) { if ((Boolean) obj) { - returnString += RInstructions.TRUE + ","; + returnString.append(RInstructions.TRUE + ","); } else { - returnString += RInstructions.FALSE + ","; + returnString.append(RInstructions.FALSE + ","); } } } else if (firstElement instanceof Integer) { for (Object obj:column) { - returnString += String.format(RInstructions.AS_INTEGER, - obj) + ","; + returnString.append( + String.format(RInstructions.AS_INTEGER,obj)); + returnString.append(","); } } else { for (Object obj:column) { - returnString += obj + ","; + returnString.append(obj); + returnString.append(","); } } - returnString = returnString.substring(0, - returnString.length() - 1); - returnString = returnString + "),"; + returnString = new StringBuilder(returnString.substring(0, + returnString.length() - 1)); + returnString.append("),"); } } if (!(this.rowNames.isEmpty())) { - returnString += "row.names=c("; + returnString.append("row.names=c("); for (String rowName : rowNames) { - returnString += "\"" + rowName + "\","; + returnString.append("\""); + returnString.append(rowName); + returnString.append("\","); } - returnString = - returnString.substring(0, returnString.length() - 1) + - "),stringsAsFactors=FALSE)"; + returnString = new StringBuilder(returnString.substring(0, returnString.length() - 1)); + returnString.append("),stringsAsFactors=FALSE)"); + } else if (this.data.isEmpty()) { - returnString += ")"; + returnString.append(")"); } else { - returnString += "stringsAsFactors=FALSE)"; + returnString.append("stringsAsFactors=FALSE)"); } if(log.isDebugEnabled()){ log.debug(returnString); } - return returnString; + return returnString.toString(); } /** @@ -656,28 +668,35 @@ */ public void exportCsv(File outputFile, boolean rowNames, boolean names) throws IOException { - BufferedWriter file = new BufferedWriter(new FileWriter(outputFile)); - if (names) { - if (rowNames) { - file.write(";"); + BufferedWriter file = null; + try { + file = new BufferedWriter(new FileWriter(outputFile)); + + if (names) { + if (rowNames) { + file.write(";"); + } + for (String name : this.names) { + file.write(name + ";"); + } + file.newLine(); } - for (String name : this.names) { - file.write(name + ";"); - } - file.newLine(); - } - for (int i = 0; i < this.data.get(0).size(); i++) { - if (rowNames) { - file.write(this.rowNames.get(i) + ";"); + for (int i = 0; i < this.data.get(0).size(); i++) { + if (rowNames) { + file.write(this.rowNames.get(i) + ";"); + } + for (List<?> aData : this.data) { + file.write(aData.get(i) + ";"); + } + file.newLine(); } - for (List<?> aData : this.data) { - file.write(aData.get(i) + ";"); + } finally { + if (file!=null){ + file.close(); } - file.newLine(); } - file.close(); } @@ -760,93 +779,100 @@ //temporary String to read lines. String tmp; + //temporary data list. + List<List<?>> tempData = new ArrayList<List<?>>(); + Integer dataSize; - //get the first line of the file - BufferedReader br = new BufferedReader(new FileReader(inputFile)); - tmp = br.readLine(); - String[] splitted = tmp.split(";"); + BufferedReader br = null; + try { + //get the first line of the file + br = new BufferedReader(new FileReader(inputFile)); + tmp = br.readLine(); + String[] splitted = tmp.split(";"); - //get the data size (number of columns) - if (rowNames) { - dataSize = splitted.length - 1; - } else { - dataSize = splitted.length; - } + //get the data size (number of columns) + if (rowNames) { + dataSize = splitted.length - 1; + } else { + dataSize = splitted.length; + } - //clear data, rowNames and names - this.data.clear(); - this.rowNames.clear(); - this.names.clear(); + //clear data, rowNames and names + this.data.clear(); + this.rowNames.clear(); + this.names.clear(); - if (names) { - //if names are present in the file, parse the first line to get - //the names. - this.names.addAll(Arrays.asList(splitted).subList(1, splitted.length)); - } + if (names) { + //if names are present in the file, parse the first line to get + //the names. + this.names.addAll(Arrays.asList(splitted).subList(1, splitted.length)); + } - //temporary data list. - List<List<?>> tempData = new ArrayList<List<?>>(); + //Initialize all the data columns with empty lists + for (int i = 0; i < dataSize; i++) { + List<Object> column = new ArrayList<Object>(); + tempData.add(column); + } - //Initialize all the data columns with empty lists - for (int i = 0; i < dataSize; i++) { - List<Object> column = new ArrayList<Object>(); - tempData.add(column); - } + while ((tmp = br.readLine()) != null) { - while ((tmp = br.readLine()) != null) { + //parse each line + splitted = tmp.split(";"); - //parse each line - splitted = tmp.split(";"); + //to determine the data index on the line. + int index = 0; - //to determine the data index on the line. - int index = 0; + //if there are row names, extract the first item as the row name + if (rowNames) { + this.rowNames.add(splitted[0]); + index = 1; + } - //if there are row names, extract the first item as the row name - if (rowNames) { - this.rowNames.add(splitted[0]); - index = 1; - } + for (int i = index; i < splitted.length; i++) { + //cast the data imported to the specified type. + //test the size of the inputType list. If 1 import all the + //columns as the type of the first element. - for (int i = index; i < splitted.length; i++) { - //cast the data imported to the specified type. - //test the size of the inputType list. If 1 import all the - //columns as the type of the first element. + //Get the column for index + ArrayList<Object> objects = (ArrayList<Object>)tempData.get(i - index); - //Get the column for index - ArrayList<Object> objects = (ArrayList<Object>)tempData.get(i - index); - - //add to the column the converted value. If importTypes contains - //only one element, cast into this type, else cast into the - //column type. - if (((importTypes.size() == 1) && + //add to the column the converted value. If importTypes contains + //only one element, cast into this type, else cast into the + //column type. + if (((importTypes.size() == 1) && (importTypes.get(0) instanceof String) ) || - ((importTypes.size() > (i -index )) && - (importTypes.get(i - index) instanceof String)) ) { + ((importTypes.size() > (i -index )) && + (importTypes.get(i - index) instanceof String)) ) { - //We import strings so no cast in case of string - objects.add(splitted[i]); + //We import strings so no cast in case of string + objects.add(splitted[i]); - } else if (((importTypes.size() == 1) && - (importTypes.get(0) instanceof Double) ) || - ((importTypes.size() > (i -index )) && - (importTypes.get(i - index) instanceof Double))) { + } else if (((importTypes.size() == 1) && + (importTypes.get(0) instanceof Double) ) || + ((importTypes.size() > (i -index )) && + (importTypes.get(i - index) instanceof Double))) { - //in case of Double - objects.add(Double.valueOf(splitted[i])); + //in case of Double + objects.add(Double.valueOf(splitted[i])); - } else if (((importTypes.size() == 1) && - (importTypes.get(0) instanceof Integer) ) || - ((importTypes.size() > (i -index )) && - (importTypes.get(i - index) instanceof Integer))) { + } else if (((importTypes.size() == 1) && + (importTypes.get(0) instanceof Integer) ) || + ((importTypes.size() > (i -index )) && + (importTypes.get(i - index) instanceof Integer))) { - //in case of Integer - objects.add(Integer.valueOf(splitted[i])); + //in case of Integer + objects.add(Integer.valueOf(splitted[i])); + } } } + } finally { + if (br != null){ + br.close(); + } } - br.close(); + this.data = tempData; if (log.isDebugEnabled()){ @@ -857,7 +883,13 @@ } - private void checkY(int y) { + /** + * Check if the index is into the data.frame length throws a + * IndexOutOfBoundsException if the index is too big. + * + * @param y index + */ + protected void checkY(int y) { if (!(this.data.get(0).isEmpty()) && (y > this.data.get(0).size())) { throw new IndexOutOfBoundsException(String.format(indexExceptionText, y, this.data.size())); @@ -866,7 +898,7 @@ /** * Check if the index is into the data.frame length throws a - * IndexOutOfBoundsException if not the index is too big. + * IndexOutOfBoundsException if the index is too big. * * @param x index */ @@ -878,7 +910,7 @@ } } - private void checkType(Object o) throws RException { + protected void checkType(Object o) throws RException { if (!(o instanceof String) && !(o instanceof Double) && !(o instanceof Integer) && !(o instanceof Boolean)) { throw new RException("Not supported type"); @@ -1001,12 +1033,13 @@ } } - String returnString = ""; + StringBuffer returnString = new StringBuffer(); for (String str:linesToDisplay){ - returnString+= str +"\n"; + returnString.append(str); + returnString.append("\n"); } - return returnString; + return returnString.toString(); } } Modified: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2010-10-22 08:51:00 UTC (rev 241) @@ -352,12 +352,14 @@ this.names = names; //Create the r instruction (names(var)<-c("name 1",...,"name x")). - String namesString = ""; + StringBuilder namesString = new StringBuilder(""); for (int i = 0; i < this.names.size(); i++) { if (i != 0) { - namesString += ","; + namesString.append(","); } - namesString += "\"" + names.get(i) + "\""; + namesString.append("\""); + namesString.append(names.get(i)); + namesString.append("\""); } String rexp = String.format(RInstructions.SET_NAMES, this.variable, namesString); Modified: trunk/src/main/java/org/nuiton/j2r/types/RList.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/RList.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/main/java/org/nuiton/j2r/types/RList.java 2010-10-22 08:51:00 UTC (rev 241) @@ -44,8 +44,8 @@ public class RList extends REXPAbstract implements REXP { //Content of the list (eg elements of the list in R) - private List<Object> data; - private Log log = LogFactory.getLog(RDataFrame.class); + protected List<Object> data; + protected Log log = LogFactory.getLog(RDataFrame.class); /** * Create a default RList linked to a R engine (the List is not initialized * in R.) @@ -138,20 +138,22 @@ @Override public String toRString() throws RException { checkVariable(); - String returnString = this.variable + "<-list("; + StringBuilder returnString = new StringBuilder(); + returnString.append(this.variable); + returnString.append("<-list("); if ((this.data != null) && (!(this.data.isEmpty()))) { for (int i = 0; i < data.size(); i++) { - returnString += toRString(i); + returnString.append(toRString(i)); } - returnString = returnString.substring(0, returnString.length() - 1); + returnString = new StringBuilder(returnString.substring(0, returnString.length() - 1)); } - returnString += ")"; + returnString.append(")"); if (log.isDebugEnabled()){ log.debug(returnString); } - return returnString; + return returnString.toString(); } /** @@ -160,7 +162,7 @@ * @return the corresponding R instruction * @throws RException if an error occur creating R instruction for REXPs */ - private String toRString(int i) throws RException { + protected String toRString(int i) throws RException { String returnString=""; Object obj = data.get(i); @@ -295,7 +297,7 @@ } - private void setInData(int x, Object data){ + protected void setInData(int x, Object data){ for (int i = 0; i <= x; i++) { try { this.data.get(i); @@ -431,20 +433,25 @@ @Override public String toString(){ - String returnString=""; + StringBuilder returnString = new StringBuilder(""); for(int i=0;i<this.data.size();i++){ //display the index if( (names!=null) && (!names.isEmpty()) && (names.get(i)!=null)){ - returnString += "[[" + names.get(i) + "]]\n"; + returnString.append("[["); + returnString.append(names.get(i)); + returnString.append("]]\n"); } else { - returnString += "[[" + i + "]]\n"; + returnString.append("[["); + returnString.append(i); + returnString.append("]]\n"); } //display the item at index i - returnString += this.data.get(i) + "\n\n"; + returnString.append(this.data.get(i)); + returnString.append("\n\n"); } - return returnString; + return returnString.toString(); } } Modified: trunk/src/site/rst/installTest.rst =================================================================== --- trunk/src/site/rst/installTest.rst 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/site/rst/installTest.rst 2010-10-22 08:51:00 UTC (rev 241) @@ -1,3 +1,27 @@ +.. - +.. * #%L +.. * Nuiton Java-2-R library +.. * +.. * $Id$ +.. * $HeadURL$ +.. * %% +.. * Copyright (C) 2006 - 2010 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>. +.. * #L% +.. - Installer l'environnement de tests ================================== @@ -77,5 +101,12 @@ (C:\Program Files\R\R-(version) ou ...) (en fait sur le dossier contenant la librairie R.dll) +Lancer les tests +================ + +Il faut lancer un serveur Rserve, en lançant la commande :: + + R CMD Rserve + Tout doit maintenant être prêt et vous pouvez faire tourner les tests de nuiton-j2r. \ No newline at end of file Modified: trunk/src/test/java/org/nuiton/j2r/AbstractEngineTest.java =================================================================== --- trunk/src/test/java/org/nuiton/j2r/AbstractEngineTest.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/test/java/org/nuiton/j2r/AbstractEngineTest.java 2010-10-22 08:51:00 UTC (rev 241) @@ -1,3 +1,27 @@ +/* + * #%L + * Nuiton Java-2-R library + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2006 - 2010 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>. + * #L% + */ package org.nuiton.j2r; import java.io.File; Modified: trunk/src/test/java/org/nuiton/j2r/NetTest.java =================================================================== --- trunk/src/test/java/org/nuiton/j2r/NetTest.java 2010-10-19 15:12:39 UTC (rev 240) +++ trunk/src/test/java/org/nuiton/j2r/NetTest.java 2010-10-22 08:51:00 UTC (rev 241) @@ -8,16 +8,16 @@ * Copyright (C) 2006 - 2010 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 + * 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 + * + * 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>. * #L%
participants (1)
-
jcouteau@users.nuiton.org