Search in sources :

Example 11 with Evaluable

use of com.google.refine.expr.Evaluable in project OpenRefine by OpenRefine.

the class ColumnAdditionOperation method createRowVisitor.

protected RowVisitor createRowVisitor(Project project, List<CellAtRow> cellsAtRows) throws Exception {
    Column column = project.columnModel.getColumnByName(_baseColumnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellAtRow> cellsAtRows;

        Evaluable eval;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellAtRow> cellsAtRows, Evaluable eval) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellsAtRows = cellsAtRows;
            this.eval = eval;
            return this;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(cellIndex);
            Cell newCell = null;
            ExpressionUtils.bind(bindings, row, rowIndex, _baseColumnName, cell);
            Object o = eval.evaluate(bindings);
            if (o != null) {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable v = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(v)) {
                        if (_onError == OnError.SetToBlank) {
                            return false;
                        } else if (_onError == OnError.KeepOriginal) {
                            v = cell != null ? cell.value : null;
                        }
                    }
                    if (v != null) {
                        newCell = new Cell(v, null);
                    }
                }
            }
            if (newCell != null) {
                cellsAtRows.add(new CellAtRow(rowIndex, newCell));
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellsAtRows, eval);
}
Also used : Serializable(java.io.Serializable) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) CellAtRow(com.google.refine.model.changes.CellAtRow) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) CellAtRow(com.google.refine.model.changes.CellAtRow) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 12 with Evaluable

use of com.google.refine.expr.Evaluable in project OpenRefine by OpenRefine.

the class GrelTests method testEvalError.

@Test
public void testEvalError() {
    String[] tests = { //                "1=1", // TODO: Throws NullPointerException
    "a.value" };
    for (String test : tests) {
        try {
            Evaluable eval = MetaParser.parse("grel:" + test);
            Object result = eval.evaluate(bindings);
            Assert.assertTrue(result instanceof EvalError);
        } catch (ParsingException e) {
            Assert.fail("Unexpected parse failure: " + test);
        }
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) ParsingException(com.google.refine.expr.ParsingException) EvalError(com.google.refine.expr.EvalError) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) RefineTest(com.google.refine.tests.RefineTest)

Example 13 with Evaluable

use of com.google.refine.expr.Evaluable in project OpenRefine by OpenRefine.

the class GetScatterplotCommand method draw.

public void draw(OutputStream output, Project project, Engine engine, JSONObject o) throws IOException, JSONException {
    double min_x = 0;
    double min_y = 0;
    double max_x = 0;
    double max_y = 0;
    int columnIndex_x = 0;
    int columnIndex_y = 0;
    Evaluable eval_x = null;
    Evaluable eval_y = null;
    int size = (o.has(ScatterplotFacet.SIZE)) ? o.getInt(ScatterplotFacet.SIZE) : 100;
    double dot = (o.has(ScatterplotFacet.DOT)) ? o.getDouble(ScatterplotFacet.DOT) : 100;
    int dim_x = (o.has(ScatterplotFacet.DIM_X)) ? ScatterplotFacet.getAxisDim(o.getString(ScatterplotFacet.DIM_X)) : ScatterplotFacet.LIN;
    int dim_y = (o.has(ScatterplotFacet.DIM_Y)) ? ScatterplotFacet.getAxisDim(o.getString(ScatterplotFacet.DIM_Y)) : ScatterplotFacet.LIN;
    int rotation = (o.has(ScatterplotFacet.ROTATION)) ? ScatterplotFacet.getRotation(o.getString(ScatterplotFacet.ROTATION)) : ScatterplotFacet.NO_ROTATION;
    String color_str = (o.has(ScatterplotFacet.COLOR)) ? o.getString(ScatterplotFacet.COLOR) : "000000";
    Color color = new Color(Integer.parseInt(color_str, 16));
    String base_color_str = (o.has(ScatterplotFacet.BASE_COLOR)) ? o.getString(ScatterplotFacet.BASE_COLOR) : null;
    Color base_color = base_color_str != null ? new Color(Integer.parseInt(base_color_str, 16)) : null;
    String columnName_x = o.getString(ScatterplotFacet.X_COLUMN_NAME);
    String expression_x = (o.has(ScatterplotFacet.X_EXPRESSION)) ? o.getString(ScatterplotFacet.X_EXPRESSION) : "value";
    if (columnName_x.length() > 0) {
        Column x_column = project.columnModel.getColumnByName(columnName_x);
        if (x_column != null) {
            columnIndex_x = x_column.getCellIndex();
        }
    } else {
        columnIndex_x = -1;
    }
    try {
        eval_x = MetaParser.parse(expression_x);
    } catch (ParsingException e) {
        logger.warn("error parsing expression", e);
    }
    String columnName_y = o.getString(ScatterplotFacet.Y_COLUMN_NAME);
    String expression_y = (o.has(ScatterplotFacet.Y_EXPRESSION)) ? o.getString(ScatterplotFacet.Y_EXPRESSION) : "value";
    if (columnName_y.length() > 0) {
        Column y_column = project.columnModel.getColumnByName(columnName_y);
        if (y_column != null) {
            columnIndex_y = y_column.getCellIndex();
        }
    } else {
        columnIndex_y = -1;
    }
    try {
        eval_y = MetaParser.parse(expression_y);
    } catch (ParsingException e) {
        logger.warn("error parsing expression", e);
    }
    NumericBinIndex index_x = null;
    NumericBinIndex index_y = null;
    String col_x_name = o.getString(ScatterplotFacet.X_COLUMN_NAME);
    Column column_x = project.columnModel.getColumnByName(col_x_name);
    if (column_x != null) {
        columnIndex_x = column_x.getCellIndex();
        index_x = ScatterplotFacet.getBinIndex(project, column_x, eval_x, expression_x);
        min_x = index_x.getMin();
        max_x = index_x.getMax();
    }
    String col_y_name = o.getString(ScatterplotFacet.Y_COLUMN_NAME);
    Column column_y = project.columnModel.getColumnByName(col_y_name);
    if (column_y != null) {
        columnIndex_y = column_y.getCellIndex();
        index_y = ScatterplotFacet.getBinIndex(project, column_y, eval_y, expression_y);
        min_y = index_y.getMin();
        max_y = index_y.getMax();
    }
    if (index_x != null && index_y != null && index_x.isNumeric() && index_y.isNumeric()) {
        ScatterplotDrawingRowVisitor drawer = new ScatterplotDrawingRowVisitor(columnIndex_x, columnIndex_y, min_x, max_x, min_y, max_y, size, dim_x, dim_y, rotation, dot, color);
        if (base_color != null) {
            drawer.setColor(base_color);
            FilteredRows filteredRows = engine.getAllRows();
            filteredRows.accept(project, drawer);
            drawer.setColor(color);
        }
        {
            FilteredRows filteredRows = engine.getAllFilteredRows();
            filteredRows.accept(project, drawer);
        }
        ImageIO.write(drawer.getImage(), "png", output);
    } else {
        ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR), "png", output);
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) Column(com.google.refine.model.Column) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) Color(java.awt.Color) ParsingException(com.google.refine.expr.ParsingException) ScatterplotDrawingRowVisitor(com.google.refine.browsing.facets.ScatterplotDrawingRowVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) BufferedImage(java.awt.image.BufferedImage)

Example 14 with Evaluable

use of com.google.refine.expr.Evaluable 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)

Example 15 with Evaluable

use of com.google.refine.expr.Evaluable in project OpenRefine by OpenRefine.

the class Parser method parseTerm.

/**
     *  <term> := <factor>
     *          | <term> [ "*" "/" "%" ] <factor>
     */
protected Evaluable parseTerm() throws ParsingException {
    Evaluable factor = parseFactor();
    while (_token != null && _token.type == TokenType.Operator && "*/%".indexOf(_token.text) >= 0) {
        String op = _token.text;
        next(true);
        Evaluable factor2 = parseFactor();
        factor = new OperatorCallExpr(new Evaluable[] { factor, factor2 }, op);
    }
    return factor;
}
Also used : OperatorCallExpr(com.google.refine.grel.ast.OperatorCallExpr) Evaluable(com.google.refine.expr.Evaluable)

Aggregations

Evaluable (com.google.refine.expr.Evaluable)18 ParsingException (com.google.refine.expr.ParsingException)6 Project (com.google.refine.model.Project)6 Cell (com.google.refine.model.Cell)5 Column (com.google.refine.model.Column)5 Row (com.google.refine.model.Row)5 Properties (java.util.Properties)5 JSONObject (org.json.JSONObject)4 RowVisitor (com.google.refine.browsing.RowVisitor)3 EvalError (com.google.refine.expr.EvalError)3 WrappedCell (com.google.refine.expr.WrappedCell)3 OperatorCallExpr (com.google.refine.grel.ast.OperatorCallExpr)3 Serializable (java.io.Serializable)3 Engine (com.google.refine.browsing.Engine)2 NumericBinIndex (com.google.refine.browsing.util.NumericBinIndex)2 VariableExpr (com.google.refine.grel.ast.VariableExpr)2 CellChange (com.google.refine.model.changes.CellChange)2 LinkedList (java.util.LinkedList)2 Test (org.testng.annotations.Test)2 FilteredRows (com.google.refine.browsing.FilteredRows)1