Author: jcouteau Date: 2010-04-19 15:20:43 +0200 (Mon, 19 Apr 2010) New Revision: 204 Log: Improve attributes management Modified: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java trunk/src/test/java/org/nuiton/j2r/ListTest.java Modified: trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java =================================================================== --- trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2010-04-19 10:50:13 UTC (rev 203) +++ trunk/src/main/java/org/nuiton/j2r/types/REXPAbstract.java 2010-04-19 13:20:43 UTC (rev 204) @@ -25,10 +25,8 @@ package org.nuiton.j2r.types; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.j2r.REngine; @@ -65,54 +63,9 @@ @Override public void setAttributes(Map<String, Object> attributes) throws RException { - //TODO JC should be useful to test the validity of attributes before assigning it. - this.attributes = attributes; - - if ((this.attributes != null) && (!this.attributes.isEmpty())) { - Set<String> keyset = attributes.keySet(); - for (String key : keyset) { - //foreach type of attribute, adapt the R instruction. - if (this.attributes.get(key) instanceof String) { - engine.eval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, key, - "\"" + this.attributes.get(key) + "\"")); - //String, value between quotes - } else if (this.attributes.get(key) instanceof Double) { - engine.eval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, - key, this.attributes.get(key))); - } else if (this.attributes.get(key) instanceof Integer) { - engine.eval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, - key, String.format(RInstructions.AS_INTEGER, - this.attributes.get(key)))); - //Integer, value formated by the R function : as.integer() - } else if (this.attributes.get(key) instanceof Boolean) { - if ((Boolean) this.attributes.get(key)) { - engine.eval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, - key, RInstructions.TRUE)); - //Boolean true replaced by TRUE - } else { - engine.eval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, - key, RInstructions.FALSE)); - //Boolean false replaced by REPLACE - } - } else if (this.attributes.get(key) instanceof REXP) { - engine.eval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, - key, - ((REXP) this.attributes.get(key)).toRString())); - //REXP replaced by the result of their toRString() method. - } else { - if (log.isWarnEnabled()) { - log.warn("This attribute is not valid : " + key + - " ; " + this.attributes.get(key) + - ". It will not be sent to R"); - //if the attribute is not recognised. - } - } + if (attributes!=null){ + for(Map.Entry<String, Object> entry : attributes.entrySet()){ + setAttribute(entry.getKey(),entry.getValue()); } } } @@ -166,7 +119,7 @@ if (returnedAttribute != null) { if (attributes.containsKey(attribute)) { //if the attribute already exists, remove it first (or you - //will have too times the same attribubte with different + //will have too times the same attribute with different //values) attributes.remove(attribute); attributes.put(attribute, returnedAttribute); @@ -186,45 +139,63 @@ /** {@inheritDoc} */ @Override public void setAttribute(String attribute, Object value) throws RException { - //variable to check if the attribute is valid before setting it. - boolean isOK = false; if (value instanceof String) { - isOK = true; - engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, attribute, "\"" + value + "\"")); - //if String, between quotes + setAttribute(attribute, (String)value); } else if (value instanceof Double) { - isOK = true; - engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, attribute, value)); + setAttribute(attribute, (Double)value); } else if (value instanceof Integer) { - isOK = true; - engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, attribute, String.format( - RInstructions.AS_INTEGER, value))); - //If integer in the R function : as.integer() + setAttribute(attribute, (Integer)value); } else if (value instanceof Boolean) { - isOK = true; - //R boolean is upper case - engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, attribute, value.toString().toUpperCase())); + setAttribute(attribute,(Boolean)value); } else if (value instanceof REXP) { - isOK = true; - engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, - this.variable, attribute, ((REXP) value).toRString())); - //if REXP, use its method toRString() + setAttribute(attribute, (REXP)value); } else { log.warn("This attribute is not valid : " + attribute + " ; " + value + ". It will not be processed"); } - //if attribute is valid, out it in the attribute list. - if (isOK) { - if (attributes.containsKey(attribute)) { - attributes.remove(attribute); - attributes.put(attribute, value); - } else { - attributes.put(attribute, value); - } + } + + public void setAttribute(String attribute, Double value) throws RException { + engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, + this.variable, attribute, value)); + putAttributeInData(attribute,value); + } + + public void setAttribute(String attribute, Integer value) throws RException { + //If integer in the R function : as.integer() + engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, + this.variable, attribute, String.format( + RInstructions.AS_INTEGER, value))); + putAttributeInData(attribute,value); + } + + public void setAttribute(String attribute, Boolean value) throws RException { + //R boolean is upper case + engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, + this.variable, attribute, value.toString().toUpperCase())); + putAttributeInData(attribute,value); + } + + public void setAttribute(String attribute, String value) throws RException { + //if String, between quotes + engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, + this.variable, attribute, "\"" + value + "\"")); + putAttributeInData(attribute,value); + } + + public void setAttribute(String attribute, REXP value) throws RException { + //if REXP, use its method toRString() + engine.voidEval(String.format(RInstructions.SET_ATTRIBUTE, + this.variable, attribute, value.toRString())); + putAttributeInData(attribute,value); + } + + private void putAttributeInData(String attribute, Object value){ + if (attributes.containsKey(attribute)) { + attributes.remove(attribute); + attributes.put(attribute, value); + } else { + attributes.put(attribute, value); } } Modified: trunk/src/test/java/org/nuiton/j2r/ListTest.java =================================================================== --- trunk/src/test/java/org/nuiton/j2r/ListTest.java 2010-04-19 10:50:13 UTC (rev 203) +++ trunk/src/test/java/org/nuiton/j2r/ListTest.java 2010-04-19 13:20:43 UTC (rev 204) @@ -134,8 +134,7 @@ 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)"); + 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");