Search in sources :

Example 6 with VariableExpr

use of com.google.refine.grel.ast.VariableExpr in project OpenRefine by OpenRefine.

the class ForEachIndex method call.

@Override
public Object call(Properties bindings, Evaluable[] args) {
    Object o = args[0].evaluate(bindings);
    if (ExpressionUtils.isError(o)) {
        return o;
    } else if (!ExpressionUtils.isArrayOrCollection(o) && !(o instanceof JSONArray)) {
        return new EvalError("First argument to forEach is not an array");
    }
    String indexName = ((VariableExpr) args[1]).getName();
    String elementName = ((VariableExpr) args[2]).getName();
    Object oldIndexValue = bindings.get(indexName);
    Object oldElementValue = bindings.get(elementName);
    try {
        List<Object> results = null;
        if (o.getClass().isArray()) {
            Object[] values = (Object[]) o;
            results = new ArrayList<Object>(values.length);
            for (int i = 0; i < values.length; i++) {
                Object v = values[i];
                bindings.put(indexName, i);
                bindings.put(elementName, v);
                Object r = args[3].evaluate(bindings);
                results.add(r);
            }
        } else if (o instanceof JSONArray) {
            JSONArray a = (JSONArray) o;
            int l = a.length();
            results = new ArrayList<Object>(l);
            for (int i = 0; i < l; i++) {
                try {
                    Object v = a.get(i);
                    bindings.put(indexName, i);
                    bindings.put(elementName, v);
                    Object r = args[3].evaluate(bindings);
                    results.add(r);
                } catch (JSONException e) {
                    results.add(new EvalError(e.getMessage()));
                }
            }
        } else {
            List<Object> list = ExpressionUtils.toObjectList(o);
            results = new ArrayList<Object>(list.size());
            for (int i = 0; i < list.size(); i++) {
                Object v = list.get(i);
                bindings.put(indexName, i);
                bindings.put(elementName, v);
                Object r = args[3].evaluate(bindings);
                results.add(r);
            }
        }
        return results.toArray();
    } finally {
        /*
             *  Restore the old values bound to the variables, if any.
             */
        if (oldIndexValue != null) {
            bindings.put(indexName, oldIndexValue);
        } else {
            bindings.remove(indexName);
        }
        if (oldElementValue != null) {
            bindings.put(elementName, oldElementValue);
        } else {
            bindings.remove(elementName);
        }
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) EvalError(com.google.refine.expr.EvalError) VariableExpr(com.google.refine.grel.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with VariableExpr

use of com.google.refine.grel.ast.VariableExpr in project OpenRefine by OpenRefine.

the class ForRange method call.

@Override
public Object call(Properties bindings, Evaluable[] args) {
    Object fromO = args[0].evaluate(bindings);
    Object toO = args[1].evaluate(bindings);
    Object stepO = args[2].evaluate(bindings);
    if (ExpressionUtils.isError(fromO)) {
        return fromO;
    } else if (ExpressionUtils.isError(toO)) {
        return toO;
    } else if (ExpressionUtils.isError(stepO)) {
        return stepO;
    } else if (!(fromO instanceof Number) || !(toO instanceof Number) || !(stepO instanceof Number)) {
        return new EvalError("First, second, and third arguments of forRange must all be numbers");
    }
    String indexName = ((VariableExpr) args[3]).getName();
    Object oldIndexValue = bindings.get(indexName);
    try {
        List<Object> results = new ArrayList<Object>();
        if (isIntegral((Number) fromO) && isIntegral((Number) stepO)) {
            long from = ((Number) fromO).longValue();
            long step = ((Number) stepO).longValue();
            double to = ((Number) toO).doubleValue();
            while (from < to) {
                bindings.put(indexName, from);
                Object r = args[4].evaluate(bindings);
                results.add(r);
                from += step;
            }
        } else {
            double from = ((Number) fromO).longValue();
            double step = ((Number) stepO).longValue();
            double to = ((Number) toO).doubleValue();
            while (from < to) {
                bindings.put(indexName, from);
                Object r = args[4].evaluate(bindings);
                results.add(r);
                from += step;
            }
        }
        return results.toArray();
    } finally {
        /*
             *  Restore the old values bound to the variables, if any.
             */
        if (oldIndexValue != null) {
            bindings.put(indexName, oldIndexValue);
        } else {
            bindings.remove(indexName);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) EvalError(com.google.refine.expr.EvalError) VariableExpr(com.google.refine.grel.ast.VariableExpr)

Aggregations

VariableExpr (com.google.refine.grel.ast.VariableExpr)7 ArrayList (java.util.ArrayList)5 EvalError (com.google.refine.expr.EvalError)4 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 Evaluable (com.google.refine.expr.Evaluable)2 FieldAccessorExpr (com.google.refine.grel.ast.FieldAccessorExpr)2 Collection (java.util.Collection)2 List (java.util.List)2 ParsingException (com.google.refine.expr.ParsingException)1 ArgsToArray (com.google.refine.expr.functions.arrays.ArgsToArray)1 NumberToken (com.google.refine.grel.Scanner.NumberToken)1 RegexToken (com.google.refine.grel.Scanner.RegexToken)1 ControlCallExpr (com.google.refine.grel.ast.ControlCallExpr)1 FunctionCallExpr (com.google.refine.grel.ast.FunctionCallExpr)1 LiteralExpr (com.google.refine.grel.ast.LiteralExpr)1 LinkedList (java.util.LinkedList)1 Pattern (java.util.regex.Pattern)1