use of org.rosuda.REngine.REXPLogical in project apex-malhar by apache.
the class RScript method processTuple.
/**
* Execute R code with variable value map. Here,the RScript will be called for each of the tuples.The data will be
* emitted on an outputport depending on its type. It is assumed that the downstream operator knows the type of data
* being emitted by this operator and will be receiving input tuples from the right output port of this operator.
*/
public void processTuple(Map<String, Object> tuple) {
try {
for (Map.Entry<String, Object> entry : tuple.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
switch(argTypeMap.get(key)) {
case REXP_INT:
int[] iArr = new int[1];
iArr[0] = (Integer) value;
connectable.getRengine().assign(key, new REXPInteger(iArr));
break;
case REXP_DOUBLE:
double[] dArr = new double[1];
dArr[0] = (Double) value;
connectable.getRengine().assign(key, new REXPDouble(dArr));
break;
case REXP_STR:
String[] sArr = new String[1];
sArr[0] = (String) value;
connectable.getRengine().assign(key, new REXPString(sArr));
break;
case REXP_BOOL:
Boolean[] bArr = new Boolean[1];
bArr[0] = (Boolean) value;
connectable.getRengine().assign(key, new REXPLogical(bArr[0]));
break;
case REXP_ARRAY_INT:
connectable.getRengine().assign(key, new REXPInteger((int[]) value));
break;
case REXP_ARRAY_DOUBLE:
connectable.getRengine().assign(key, new REXPDouble((double[]) value));
break;
case REXP_ARRAY_STR:
connectable.getRengine().assign(key, new REXPString((String[]) value));
break;
case REXP_ARRAY_BOOL:
connectable.getRengine().assign(key, new REXPLogical((boolean[]) value));
break;
default:
throw new IllegalArgumentException("Unsupported data type ... ");
}
}
REXP result = connectable.getRengine().parseAndEval(getReturnVariable() + "<-" + getFunctionName() + "()");
REXP retVal = connectable.getRengine().parseAndEval(getReturnVariable());
// Clear R workspace, except functions.
connectable.getRengine().parseAndEval("rm(list = setdiff(ls(), lsf.str()))");
// Get the returned value and emit it on the appropriate output port depending
// on its datatype.
int len = 0;
if (retVal.isInteger()) {
len = retVal.length();
if (len > 1) {
Integer[] iAList = new Integer[len];
for (int i = 0; i < len; i++) {
iAList[i] = (retVal.asIntegers()[i]);
}
intArrayOutput.emit(iAList);
} else {
intOutput.emit(retVal.asInteger());
}
} else if (retVal.isNumeric()) {
len = retVal.length();
if (len > 1) {
Double[] dAList = new Double[len];
for (int i = 0; i < len; i++) {
dAList[i] = (retVal.asDoubles()[i]);
}
doubleArrayOutput.emit(dAList);
} else {
doubleOutput.emit(retVal.asDouble());
}
} else if (retVal.isString()) {
len = retVal.length();
if (len > 1) {
strArrayOutput.emit(retVal.asStrings());
} else {
strOutput.emit(retVal.asString());
}
} else if (retVal.isLogical()) {
len = retVal.length();
boolean[] bData = new boolean[len];
if (len > 1) {
Boolean[] bAList = new Boolean[len];
for (int i = 0; i < len; i++) {
bAList[i] = ((REXPLogical) retVal).isTRUE()[i];
}
boolArrayOutput.emit(bAList);
} else {
bData = (((REXPLogical) retVal).isTRUE());
boolOutput.emit(bData[0]);
}
} else {
throw new IllegalArgumentException("Unsupported data type returned ... ");
}
} catch (Exception e) {
log.error("Exception: ", e);
DTThrowable.rethrow(e);
}
}
Aggregations