Search in sources :

Example 1 with REXPString

use of org.rosuda.REngine.REXPString 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);
    }
}
Also used : REXPString(org.rosuda.REngine.REXPString) REXPInteger(org.rosuda.REngine.REXPInteger) REXP(org.rosuda.REngine.REXP) IOException(java.io.IOException) REXPLogical(org.rosuda.REngine.REXPLogical) REXPString(org.rosuda.REngine.REXPString) REXPInteger(org.rosuda.REngine.REXPInteger) REXPDouble(org.rosuda.REngine.REXPDouble) Map(java.util.Map)

Aggregations

IOException (java.io.IOException)1 Map (java.util.Map)1 REXP (org.rosuda.REngine.REXP)1 REXPDouble (org.rosuda.REngine.REXPDouble)1 REXPInteger (org.rosuda.REngine.REXPInteger)1 REXPLogical (org.rosuda.REngine.REXPLogical)1 REXPString (org.rosuda.REngine.REXPString)1