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)) {