Search in sources :

Example 26 with Cell

use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.

the class DualExpressionsNumberComparisonRowFilter method filterRow.

@Override
public boolean filterRow(Project project, int rowIndex, Row row) {
    Cell x_cell = _x_cellIndex < 0 ? null : row.getCell(_x_cellIndex);
    Properties x_bindings = ExpressionUtils.createBindings(project);
    ExpressionUtils.bind(x_bindings, row, rowIndex, _x_columnName, x_cell);
    Object x_value = _x_evaluable.evaluate(x_bindings);
    Cell y_cell = _y_cellIndex < 0 ? null : row.getCell(_y_cellIndex);
    Properties y_bindings = ExpressionUtils.createBindings(project);
    ExpressionUtils.bind(y_bindings, row, rowIndex, _y_columnName, y_cell);
    Object y_value = _y_evaluable.evaluate(y_bindings);
    if (x_value != null && y_value != null) {
        if (x_value.getClass().isArray() || y_value.getClass().isArray()) {
            return false;
        } else if (x_value instanceof Collection<?> || y_value instanceof Collection<?>) {
            return false;
        }
    // else, fall through
    }
    return checkValue(x_value, y_value);
}
Also used : Collection(java.util.Collection) Properties(java.util.Properties) Cell(com.google.refine.model.Cell)

Example 27 with Cell

use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.

the class ExpressionEqualRowFilter method internalInvertedFilterRow.

public boolean internalInvertedFilterRow(Project project, int rowIndex, Row row) {
    Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
    Properties bindings = ExpressionUtils.createBindings(project);
    ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
    Object value = _evaluable.evaluate(bindings);
    if (value != null) {
        if (value.getClass().isArray()) {
            Object[] a = (Object[]) value;
            for (Object v : a) {
                if (testValue(v)) {
                    return false;
                }
            }
            return true;
        } else if (value instanceof Collection<?>) {
            for (Object v : ExpressionUtils.toObjectCollection(value)) {
                if (testValue(v)) {
                    return false;
                }
            }
            return true;
        } else if (value instanceof JSONArray) {
            JSONArray a = (JSONArray) value;
            int l = a.length();
            for (int i = 0; i < l; i++) {
                try {
                    if (testValue(a.get(i))) {
                        return false;
                    }
                } catch (JSONException e) {
                // ignore
                }
            }
            return true;
        }
    // else, fall through
    }
    return !testValue(value);
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Properties(java.util.Properties) Cell(com.google.refine.model.Cell)

Example 28 with Cell

use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.

the class ExpressionStringComparisonRowFilter method filterRow.

@Override
public boolean filterRow(Project project, int rowIndex, Row row) {
    Cell cell = _cellIndex < 0 ? null : row.getCell(_cellIndex);
    Properties bindings = ExpressionUtils.createBindings(project);
    ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
    Object value = _evaluable.evaluate(bindings);
    if (value != null) {
        if (value.getClass().isArray()) {
            Object[] a = (Object[]) value;
            for (Object v : a) {
                if (checkValue(v instanceof String ? ((String) v) : v.toString())) {
                    return true;
                }
            }
        } else if (value instanceof Collection<?>) {
            for (Object v : ExpressionUtils.toObjectCollection(value)) {
                if (checkValue(v.toString())) {
                    return true;
                }
            }
            return false;
        } else if (value instanceof JSONArray) {
            JSONArray a = (JSONArray) value;
            int l = a.length();
            for (int i = 0; i < l; i++) {
                try {
                    if (checkValue(a.get(i).toString())) {
                        return true;
                    }
                } catch (JSONException e) {
                // ignore
                }
            }
            return false;
        } else {
            if (checkValue(value instanceof String ? ((String) value) : value.toString())) {
                return true;
            }
        }
    }
    return false;
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Properties(java.util.Properties) Cell(com.google.refine.model.Cell)

Example 29 with Cell

use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.

the class ExpressionBasedRowEvaluable method eval.

@Override
public Object eval(Project project, int rowIndex, Row row, Properties bindings) {
    Cell cell = row.getCell(_cellIndex);
    ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
    return _eval.evaluate(bindings);
}
Also used : Cell(com.google.refine.model.Cell)

Example 30 with Cell

use of com.google.refine.model.Cell in project OpenRefine by OpenRefine.

the class PreviewExpressionCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Project project = getProject(request);
        int cellIndex = Integer.parseInt(request.getParameter("cellIndex"));
        String columnName = cellIndex < 0 ? "" : project.columnModel.getColumnByCellIndex(cellIndex).getName();
        String expression = request.getParameter("expression");
        String rowIndicesString = request.getParameter("rowIndices");
        if (rowIndicesString == null) {
            respond(response, "{ \"code\" : \"error\", \"message\" : \"No row indices specified\" }");
            return;
        }
        boolean repeat = "true".equals(request.getParameter("repeat"));
        int repeatCount = 10;
        if (repeat) {
            String repeatCountString = request.getParameter("repeatCount");
            try {
                repeatCount = Math.max(Math.min(Integer.parseInt(repeatCountString), 10), 0);
            } catch (Exception e) {
            }
        }
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString);
        int length = rowIndices.length();
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        try {
            Evaluable eval = MetaParser.parse(expression);
            writer.key("code");
            writer.value("ok");
            writer.key("results");
            writer.array();
            Properties bindings = ExpressionUtils.createBindings(project);
            for (int i = 0; i < length; i++) {
                Object result = null;
                int rowIndex = rowIndices.getInt(i);
                if (rowIndex >= 0 && rowIndex < project.rows.size()) {
                    Row row = project.rows.get(rowIndex);
                    Cell cell = row.getCell(cellIndex);
                    try {
                        ExpressionUtils.bind(bindings, row, rowIndex, columnName, cell);
                        result = eval.evaluate(bindings);
                        if (repeat) {
                            for (int r = 0; r < repeatCount && ExpressionUtils.isStorable(result); r++) {
                                Cell newCell = new Cell((Serializable) result, (cell != null) ? cell.recon : null);
                                ExpressionUtils.bind(bindings, row, rowIndex, columnName, newCell);
                                Object newResult = eval.evaluate(bindings);
                                if (ExpressionUtils.isError(newResult)) {
                                    break;
                                } else if (ExpressionUtils.sameValue(result, newResult)) {
                                    break;
                                } else {
                                    result = newResult;
                                }
                            }
                        }
                    } catch (Exception e) {
                    // ignore
                    }
                }
                if (result == null) {
                    writer.value(null);
                } else if (ExpressionUtils.isError(result)) {
                    writer.object();
                    writer.key("message");
                    writer.value(((EvalError) result).message);
                    writer.endObject();
                } else {
                    StringBuffer sb = new StringBuffer();
                    writeValue(sb, result, false);
                    writer.value(sb.toString());
                }
            }
            writer.endArray();
        } catch (ParsingException e) {
            writer.key("code");
            writer.value("error");
            writer.key("type");
            writer.value("parser");
            writer.key("message");
            writer.value(e.getMessage());
        } catch (Exception e) {
            writer.key("code");
            writer.value("error");
            writer.key("type");
            writer.value("other");
            writer.key("message");
            writer.value(e.getMessage());
        }
        writer.endObject();
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) JSONArray(org.json.JSONArray) EvalError(com.google.refine.expr.EvalError) Properties(java.util.Properties) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) ParsingException(com.google.refine.expr.ParsingException) Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) JSONObject(org.json.JSONObject) WrappedRow(com.google.refine.expr.WrappedRow) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Aggregations

Cell (com.google.refine.model.Cell)58 Row (com.google.refine.model.Row)36 Column (com.google.refine.model.Column)19 Test (org.testng.annotations.Test)16 RefineTest (com.google.refine.tests.RefineTest)15 BeforeTest (org.testng.annotations.BeforeTest)15 JSONObject (org.json.JSONObject)13 ArrayList (java.util.ArrayList)12 Project (com.google.refine.model.Project)11 IOException (java.io.IOException)11 Properties (java.util.Properties)11 JSONException (org.json.JSONException)9 RowVisitor (com.google.refine.browsing.RowVisitor)7 HistoryEntry (com.google.refine.history.HistoryEntry)7 Serializable (java.io.Serializable)7 Recon (com.google.refine.model.Recon)6 CellChange (com.google.refine.model.changes.CellChange)6 HashMap (java.util.HashMap)6 Evaluable (com.google.refine.expr.Evaluable)5 JSONArray (org.json.JSONArray)4