Unified the way expression bindings are created, so everywhere we'll have the same variable bindings.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@38 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
4ee1684fa3
commit
9a63bdd821
@ -7,6 +7,7 @@ import java.util.Properties;
|
|||||||
import com.metaweb.gridworks.browsing.DecoratedValue;
|
import com.metaweb.gridworks.browsing.DecoratedValue;
|
||||||
import com.metaweb.gridworks.browsing.RowVisitor;
|
import com.metaweb.gridworks.browsing.RowVisitor;
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
@ -27,11 +28,8 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
|
|||||||
if (_cellIndex < row.cells.size()) {
|
if (_cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(_cellIndex);
|
Cell cell = row.cells.get(_cellIndex);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("project", project);
|
|
||||||
bindings.put("cell", cell);
|
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Object value = _evaluable.evaluate(bindings);
|
Object value = _evaluable.evaluate(bindings);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -4,6 +4,7 @@ import java.util.Properties;
|
|||||||
|
|
||||||
import com.metaweb.gridworks.browsing.RowVisitor;
|
import com.metaweb.gridworks.browsing.RowVisitor;
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
@ -27,11 +28,8 @@ public class ExpressionNumericRowBinner implements RowVisitor {
|
|||||||
if (_cellIndex < row.cells.size()) {
|
if (_cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(_cellIndex);
|
Cell cell = row.cells.get(_cellIndex);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("project", project);
|
|
||||||
bindings.put("cell", cell);
|
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Object value = _evaluable.evaluate(bindings);
|
Object value = _evaluable.evaluate(bindings);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -5,6 +5,7 @@ import java.util.List;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
@ -16,7 +17,7 @@ public class NumericBinIndex {
|
|||||||
private int[] _bins;
|
private int[] _bins;
|
||||||
|
|
||||||
public NumericBinIndex(Project project, int cellIndex, Evaluable eval) {
|
public NumericBinIndex(Project project, int cellIndex, Evaluable eval) {
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
|
||||||
_min = Double.POSITIVE_INFINITY;
|
_min = Double.POSITIVE_INFINITY;
|
||||||
_max = Double.NEGATIVE_INFINITY;
|
_max = Double.NEGATIVE_INFINITY;
|
||||||
@ -28,9 +29,7 @@ public class NumericBinIndex {
|
|||||||
if (cellIndex < row.cells.size()) {
|
if (cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(cellIndex);
|
Cell cell = row.cells.get(cellIndex);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
bindings.put("project", project);
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("cell", cell);
|
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Object value = eval.evaluate(bindings);
|
Object value = eval.evaluate(bindings);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -3,6 +3,7 @@ package com.metaweb.gridworks.browsing.filters;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
@ -23,10 +24,8 @@ public class ExpressionEqualRowFilter implements RowFilter {
|
|||||||
if (_cellIndex < row.cells.size()) {
|
if (_cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(_cellIndex);
|
Cell cell = row.cells.get(_cellIndex);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("cell", cell);
|
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Object value = _evaluable.evaluate(bindings);
|
Object value = _evaluable.evaluate(bindings);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -3,6 +3,7 @@ package com.metaweb.gridworks.browsing.filters;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
@ -21,10 +22,8 @@ abstract public class ExpressionNumberComparisonRowFilter implements RowFilter {
|
|||||||
if (_cellIndex < row.cells.size()) {
|
if (_cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(_cellIndex);
|
Cell cell = row.cells.get(_cellIndex);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("cell", cell);
|
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Object value = _evaluable.evaluate(bindings);
|
Object value = _evaluable.evaluate(bindings);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -3,6 +3,7 @@ package com.metaweb.gridworks.browsing.filters;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
import com.metaweb.gridworks.model.Row;
|
import com.metaweb.gridworks.model.Row;
|
||||||
@ -21,10 +22,8 @@ abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
|
|||||||
if (_cellIndex < row.cells.size()) {
|
if (_cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(_cellIndex);
|
Cell cell = row.cells.get(_cellIndex);
|
||||||
if (cell != null) {
|
if (cell != null) {
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("cell", cell);
|
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Object value = _evaluable.evaluate(bindings);
|
Object value = _evaluable.evaluate(bindings);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
@ -15,6 +15,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
|
|||||||
import com.metaweb.gridworks.browsing.RowVisitor;
|
import com.metaweb.gridworks.browsing.RowVisitor;
|
||||||
import com.metaweb.gridworks.commands.Command;
|
import com.metaweb.gridworks.commands.Command;
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.expr.Parser;
|
import com.metaweb.gridworks.expr.Parser;
|
||||||
import com.metaweb.gridworks.history.HistoryEntry;
|
import com.metaweb.gridworks.history.HistoryEntry;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
@ -46,8 +47,7 @@ public class DoTextTransformCommand extends Command {
|
|||||||
String expression = request.getParameter("expression");
|
String expression = request.getParameter("expression");
|
||||||
|
|
||||||
Evaluable eval = new Parser(expression).getExpression();
|
Evaluable eval = new Parser(expression).getExpression();
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
bindings.put("project", project);
|
|
||||||
|
|
||||||
List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
|
List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
|
||||||
|
|
||||||
@ -71,8 +71,7 @@ public class DoTextTransformCommand extends Command {
|
|||||||
if (cellIndex < row.cells.size()) {
|
if (cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(cellIndex);
|
Cell cell = row.cells.get(cellIndex);
|
||||||
if (cell.value != null) {
|
if (cell.value != null) {
|
||||||
bindings.put("cell", cell);
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
Cell newCell = new Cell(eval.evaluate(bindings), cell.recon);
|
Cell newCell = new Cell(eval.evaluate(bindings), cell.recon);
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ import org.json.JSONWriter;
|
|||||||
|
|
||||||
import com.metaweb.gridworks.commands.Command;
|
import com.metaweb.gridworks.commands.Command;
|
||||||
import com.metaweb.gridworks.expr.Evaluable;
|
import com.metaweb.gridworks.expr.Evaluable;
|
||||||
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
||||||
import com.metaweb.gridworks.expr.Parser;
|
import com.metaweb.gridworks.expr.Parser;
|
||||||
import com.metaweb.gridworks.model.Cell;
|
import com.metaweb.gridworks.model.Cell;
|
||||||
import com.metaweb.gridworks.model.Project;
|
import com.metaweb.gridworks.model.Project;
|
||||||
@ -47,8 +48,7 @@ public class PreviewExpressionCommand extends Command {
|
|||||||
writer.key("code"); writer.value("ok");
|
writer.key("code"); writer.value("ok");
|
||||||
writer.key("results"); writer.array();
|
writer.key("results"); writer.array();
|
||||||
|
|
||||||
Properties bindings = new Properties();
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
bindings.put("project", project);
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
Object result = null;
|
Object result = null;
|
||||||
|
|
||||||
@ -58,8 +58,7 @@ public class PreviewExpressionCommand extends Command {
|
|||||||
if (cellIndex < row.cells.size()) {
|
if (cellIndex < row.cells.size()) {
|
||||||
Cell cell = row.cells.get(cellIndex);
|
Cell cell = row.cells.get(cellIndex);
|
||||||
if (cell.value != null) {
|
if (cell.value != null) {
|
||||||
bindings.put("cell", cell);
|
ExpressionUtils.bind(bindings, row, cell);
|
||||||
bindings.put("value", cell.value);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = eval.evaluate(bindings);
|
result = eval.evaluate(bindings);
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.metaweb.gridworks.expr;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.metaweb.gridworks.model.Cell;
|
||||||
|
import com.metaweb.gridworks.model.Project;
|
||||||
|
import com.metaweb.gridworks.model.Row;
|
||||||
|
|
||||||
|
public class ExpressionUtils {
|
||||||
|
static public Properties createBindings(Project project) {
|
||||||
|
Properties bindings = new Properties();
|
||||||
|
bindings.put("project", project);
|
||||||
|
return bindings;
|
||||||
|
}
|
||||||
|
|
||||||
|
static public void bind(Properties bindings, Row row, Cell cell) {
|
||||||
|
bindings.put("row", row);
|
||||||
|
bindings.put("cells", row.getField("cells", bindings));
|
||||||
|
|
||||||
|
bindings.put("cell", cell);
|
||||||
|
bindings.put("value", cell.value);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user