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:
David Huynh 2010-02-04 06:30:56 +00:00
parent 4ee1684fa3
commit 9a63bdd821
9 changed files with 47 additions and 34 deletions

View File

@ -7,6 +7,7 @@ import java.util.Properties;
import com.metaweb.gridworks.browsing.DecoratedValue;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -27,11 +28,8 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
if (_cellIndex < row.cells.size()) {
Cell cell = row.cells.get(_cellIndex);
if (cell != null) {
Properties bindings = new Properties();
bindings.put("project", project);
bindings.put("cell", cell);
bindings.put("value", cell.value);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {

View File

@ -4,6 +4,7 @@ import java.util.Properties;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -27,11 +28,8 @@ public class ExpressionNumericRowBinner implements RowVisitor {
if (_cellIndex < row.cells.size()) {
Cell cell = row.cells.get(_cellIndex);
if (cell != null) {
Properties bindings = new Properties();
bindings.put("project", project);
bindings.put("cell", cell);
bindings.put("value", cell.value);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {

View File

@ -5,6 +5,7 @@ import java.util.List;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -16,7 +17,7 @@ public class NumericBinIndex {
private int[] _bins;
public NumericBinIndex(Project project, int cellIndex, Evaluable eval) {
Properties bindings = new Properties();
Properties bindings = ExpressionUtils.createBindings(project);
_min = Double.POSITIVE_INFINITY;
_max = Double.NEGATIVE_INFINITY;
@ -28,9 +29,7 @@ public class NumericBinIndex {
if (cellIndex < row.cells.size()) {
Cell cell = row.cells.get(cellIndex);
if (cell != null) {
bindings.put("project", project);
bindings.put("cell", cell);
bindings.put("value", cell.value);
ExpressionUtils.bind(bindings, row, cell);
Object value = eval.evaluate(bindings);
if (value != null) {

View File

@ -3,6 +3,7 @@ package com.metaweb.gridworks.browsing.filters;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -23,10 +24,8 @@ public class ExpressionEqualRowFilter implements RowFilter {
if (_cellIndex < row.cells.size()) {
Cell cell = row.cells.get(_cellIndex);
if (cell != null) {
Properties bindings = new Properties();
bindings.put("cell", cell);
bindings.put("value", cell.value);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {

View File

@ -3,6 +3,7 @@ package com.metaweb.gridworks.browsing.filters;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -21,10 +22,8 @@ abstract public class ExpressionNumberComparisonRowFilter implements RowFilter {
if (_cellIndex < row.cells.size()) {
Cell cell = row.cells.get(_cellIndex);
if (cell != null) {
Properties bindings = new Properties();
bindings.put("cell", cell);
bindings.put("value", cell.value);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {

View File

@ -3,6 +3,7 @@ package com.metaweb.gridworks.browsing.filters;
import java.util.Properties;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
import com.metaweb.gridworks.model.Row;
@ -21,10 +22,8 @@ abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
if (_cellIndex < row.cells.size()) {
Cell cell = row.cells.get(_cellIndex);
if (cell != null) {
Properties bindings = new Properties();
bindings.put("cell", cell);
bindings.put("value", cell.value);
Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell);
Object value = _evaluable.evaluate(bindings);
if (value != null) {

View File

@ -15,6 +15,7 @@ import com.metaweb.gridworks.browsing.FilteredRows;
import com.metaweb.gridworks.browsing.RowVisitor;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.Parser;
import com.metaweb.gridworks.history.HistoryEntry;
import com.metaweb.gridworks.model.Cell;
@ -46,8 +47,7 @@ public class DoTextTransformCommand extends Command {
String expression = request.getParameter("expression");
Evaluable eval = new Parser(expression).getExpression();
Properties bindings = new Properties();
bindings.put("project", project);
Properties bindings = ExpressionUtils.createBindings(project);
List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
@ -71,8 +71,7 @@ public class DoTextTransformCommand extends Command {
if (cellIndex < row.cells.size()) {
Cell cell = row.cells.get(cellIndex);
if (cell.value != null) {
bindings.put("cell", cell);
bindings.put("value", cell.value);
ExpressionUtils.bind(bindings, row, cell);
Cell newCell = new Cell(eval.evaluate(bindings), cell.recon);

View File

@ -13,6 +13,7 @@ import org.json.JSONWriter;
import com.metaweb.gridworks.commands.Command;
import com.metaweb.gridworks.expr.Evaluable;
import com.metaweb.gridworks.expr.ExpressionUtils;
import com.metaweb.gridworks.expr.Parser;
import com.metaweb.gridworks.model.Cell;
import com.metaweb.gridworks.model.Project;
@ -47,8 +48,7 @@ public class PreviewExpressionCommand extends Command {
writer.key("code"); writer.value("ok");
writer.key("results"); writer.array();
Properties bindings = new Properties();
bindings.put("project", project);
Properties bindings = ExpressionUtils.createBindings(project);
for (int i = 0; i < length; i++) {
Object result = null;
@ -58,8 +58,7 @@ public class PreviewExpressionCommand extends Command {
if (cellIndex < row.cells.size()) {
Cell cell = row.cells.get(cellIndex);
if (cell.value != null) {
bindings.put("cell", cell);
bindings.put("value", cell.value);
ExpressionUtils.bind(bindings, row, cell);
try {
result = eval.evaluate(bindings);

View File

@ -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);
}
}