Search in sources :

Example 6 with Evaluable

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

the class ForNonBlank method call.

@Override
public Object call(Properties bindings, Evaluable[] args) {
    Object o = args[0].evaluate(bindings);
    Evaluable var = args[1];
    String name = ((VariableExpr) var).getName();
    if (ExpressionUtils.isNonBlankData(o)) {
        Object oldValue = bindings.get(name);
        bindings.put(name, o);
        try {
            return args[2].evaluate(bindings);
        } finally {
            /*
                 *  Restore the old value bound to the variable, if any.
                 */
            if (oldValue != null) {
                bindings.put(name, oldValue);
            } else {
                bindings.remove(name);
            }
        }
    } else {
        return args[3].evaluate(bindings);
    }
}
Also used : Evaluable(com.google.refine.expr.Evaluable) VariableExpr(com.google.refine.grel.ast.VariableExpr)

Example 7 with Evaluable

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

the class GetColumnsInfoCommand method getBinIndex.

private NumericBinIndex getBinIndex(Project project, Column column) {
    String expression = "value";
    String key = "numeric-bin:" + expression;
    Evaluable eval = null;
    try {
        eval = MetaParser.parse(expression);
    } catch (ParsingException e) {
    // this should never happen
    }
    NumericBinIndex index = (NumericBinIndex) column.getPrecompute(key);
    if (index == null) {
        index = new NumericBinRowIndex(project, new ExpressionBasedRowEvaluable(column.getName(), column.getCellIndex(), eval));
        column.setPrecompute(key, index);
    }
    return index;
}
Also used : Evaluable(com.google.refine.expr.Evaluable) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable) NumericBinRowIndex(com.google.refine.browsing.util.NumericBinRowIndex) NumericBinIndex(com.google.refine.browsing.util.NumericBinIndex) ParsingException(com.google.refine.expr.ParsingException) ExpressionBasedRowEvaluable(com.google.refine.browsing.util.ExpressionBasedRowEvaluable)

Example 8 with Evaluable

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

the class MassEditOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    Map<String, Serializable> fromTo = new HashMap<String, Serializable>();
    Serializable fromBlankTo = null;
    Serializable fromErrorTo = null;
    for (Edit edit : _edits) {
        for (String s : edit.from) {
            fromTo.put(s, edit.to);
        }
        // the last edit wins
        if (edit.fromBlank) {
            fromBlankTo = edit.to;
        }
        if (edit.fromError) {
            fromErrorTo = edit.to;
        }
    }
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellChange> cellChanges;

        Evaluable eval;

        Map<String, Serializable> fromTo;

        Serializable fromBlankTo;

        Serializable fromErrorTo;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellChange> cellChanges, Evaluable eval, Map<String, Serializable> fromTo, Serializable fromBlankTo, Serializable fromErrorTo) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellChanges = cellChanges;
            this.eval = eval;
            this.fromTo = fromTo;
            this.fromBlankTo = fromBlankTo;
            this.fromErrorTo = fromErrorTo;
            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, _columnName, cell);
            Object v = eval.evaluate(bindings);
            if (ExpressionUtils.isError(v)) {
                if (fromErrorTo != null) {
                    newCell = new Cell(fromErrorTo, (cell != null) ? cell.recon : null);
                }
            } else if (ExpressionUtils.isNonBlankData(v)) {
                String from = v.toString();
                Serializable to = fromTo.get(from);
                if (to != null) {
                    newCell = new Cell(to, (cell != null) ? cell.recon : null);
                }
            } else {
                if (fromBlankTo != null) {
                    newCell = new Cell(fromBlankTo, (cell != null) ? cell.recon : null);
                }
            }
            if (newCell != null) {
                CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                cellChanges.add(cellChange);
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo, fromBlankTo, fromErrorTo);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell)

Example 9 with Evaluable

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

the class TextTransformOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellChange> cellChanges;

        Evaluable eval;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellChange> cellChanges, Evaluable eval) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellChanges = cellChanges;
            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;
            Object oldValue = cell != null ? cell.value : null;
            ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
            Object o = eval.evaluate(bindings);
            if (o == null) {
                if (oldValue != null) {
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, null);
                    cellChanges.add(cellChange);
                }
            } else {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable newValue = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(newValue)) {
                        if (_onError == OnError.KeepOriginal) {
                            return false;
                        } else if (_onError == OnError.SetToBlank) {
                            newValue = null;
                        }
                    }
                    if (!ExpressionUtils.sameValue(oldValue, newValue)) {
                        newCell = new Cell(newValue, (cell != null) ? cell.recon : null);
                        if (_repeat) {
                            for (int i = 0; i < _repeatCount; i++) {
                                ExpressionUtils.bind(bindings, row, rowIndex, _columnName, newCell);
                                newValue = ExpressionUtils.wrapStorable(eval.evaluate(bindings));
                                if (ExpressionUtils.isError(newValue)) {
                                    break;
                                } else if (ExpressionUtils.sameValue(newCell.value, newValue)) {
                                    break;
                                }
                                newCell = new Cell(newValue, newCell.recon);
                            }
                        }
                    }
                }
                if (newCell != null) {
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                    cellChanges.add(cellChange);
                }
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellChanges, eval);
}
Also used : Serializable(java.io.Serializable) CellChange(com.google.refine.model.changes.CellChange) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 10 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)

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