Added a few more expression functions.

Bind row index when filtering rows, so we can create facets based on row indices.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@125 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-22 23:51:44 +00:00
parent 0f505c72c5
commit 94fbd97bc4
18 changed files with 126 additions and 10 deletions

View File

@ -27,7 +27,7 @@ public class ExpressionNominalRowGrouper implements RowVisitor {
Cell cell = row.getCell(_cellIndex); Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null) {

View File

@ -27,7 +27,7 @@ public class ExpressionNumericRowBinner implements RowVisitor {
Cell cell = row.getCell(_cellIndex); Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null) {

View File

@ -27,7 +27,7 @@ public class NumericBinIndex {
Row row = project.rows.get(i); Row row = project.rows.get(i);
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, i, cell);
Object value = eval.evaluate(bindings); Object value = eval.evaluate(bindings);
if (value != null) { if (value != null) {

View File

@ -22,7 +22,7 @@ public class ExpressionEqualRowFilter implements RowFilter {
public boolean filterRow(Project project, int rowIndex, Row row) { public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = row.getCell(_cellIndex); Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null) {

View File

@ -21,7 +21,7 @@ abstract public class ExpressionNumberComparisonRowFilter implements RowFilter {
Cell cell = row.getCell(_cellIndex); Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null) {

View File

@ -20,7 +20,7 @@ abstract public class ExpressionStringComparisonRowFilter implements RowFilter {
public boolean filterRow(Project project, int rowIndex, Row row) { public boolean filterRow(Project project, int rowIndex, Row row) {
Cell cell = row.getCell(_cellIndex); Cell cell = row.getCell(_cellIndex);
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object value = _evaluable.evaluate(bindings); Object value = _evaluable.evaluate(bindings);
if (value != null) { if (value != null) {

View File

@ -59,7 +59,7 @@ public class PreviewExpressionCommand extends Command {
Row row = project.rows.get(rowIndex); Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
try { try {
result = eval.evaluate(bindings); result = eval.evaluate(bindings);

View File

@ -18,8 +18,9 @@ public class ExpressionUtils {
return bindings; return bindings;
} }
static public void bind(Properties bindings, Row row, Cell cell) { static public void bind(Properties bindings, Row row, int rowIndex, Cell cell) {
bindings.put("row", row); bindings.put("row", row);
bindings.put("rowIndex", rowIndex);
bindings.put("cells", row.getField("cells", bindings)); bindings.put("cells", row.getField("cells", bindings));
if (cell == null) { if (cell == null) {

View File

@ -13,7 +13,9 @@ import com.metaweb.gridworks.expr.controls.ForNonBlank;
import com.metaweb.gridworks.expr.controls.If; import com.metaweb.gridworks.expr.controls.If;
import com.metaweb.gridworks.expr.controls.With; import com.metaweb.gridworks.expr.controls.With;
import com.metaweb.gridworks.expr.functions.And; import com.metaweb.gridworks.expr.functions.And;
import com.metaweb.gridworks.expr.functions.Ceil;
import com.metaweb.gridworks.expr.functions.EndsWith; import com.metaweb.gridworks.expr.functions.EndsWith;
import com.metaweb.gridworks.expr.functions.Floor;
import com.metaweb.gridworks.expr.functions.Get; import com.metaweb.gridworks.expr.functions.Get;
import com.metaweb.gridworks.expr.functions.IndexOf; import com.metaweb.gridworks.expr.functions.IndexOf;
import com.metaweb.gridworks.expr.functions.IsBlank; import com.metaweb.gridworks.expr.functions.IsBlank;
@ -23,13 +25,17 @@ import com.metaweb.gridworks.expr.functions.IsNull;
import com.metaweb.gridworks.expr.functions.Join; import com.metaweb.gridworks.expr.functions.Join;
import com.metaweb.gridworks.expr.functions.LastIndexOf; import com.metaweb.gridworks.expr.functions.LastIndexOf;
import com.metaweb.gridworks.expr.functions.Length; import com.metaweb.gridworks.expr.functions.Length;
import com.metaweb.gridworks.expr.functions.Mod;
import com.metaweb.gridworks.expr.functions.Not; import com.metaweb.gridworks.expr.functions.Not;
import com.metaweb.gridworks.expr.functions.Or; import com.metaweb.gridworks.expr.functions.Or;
import com.metaweb.gridworks.expr.functions.Replace; import com.metaweb.gridworks.expr.functions.Replace;
import com.metaweb.gridworks.expr.functions.Round;
import com.metaweb.gridworks.expr.functions.Slice; import com.metaweb.gridworks.expr.functions.Slice;
import com.metaweb.gridworks.expr.functions.Split; import com.metaweb.gridworks.expr.functions.Split;
import com.metaweb.gridworks.expr.functions.StartsWith; import com.metaweb.gridworks.expr.functions.StartsWith;
import com.metaweb.gridworks.expr.functions.ToLowercase; import com.metaweb.gridworks.expr.functions.ToLowercase;
import com.metaweb.gridworks.expr.functions.ToNumber;
import com.metaweb.gridworks.expr.functions.ToString;
import com.metaweb.gridworks.expr.functions.ToTitlecase; import com.metaweb.gridworks.expr.functions.ToTitlecase;
import com.metaweb.gridworks.expr.functions.ToUppercase; import com.metaweb.gridworks.expr.functions.ToUppercase;
@ -42,6 +48,9 @@ public class Parser {
static public Map<String, Control> controlTable = new HashMap<String, Control>(); static public Map<String, Control> controlTable = new HashMap<String, Control>();
static { static {
functionTable.put("toString", new ToString());
functionTable.put("toNumber", new ToNumber());
functionTable.put("toUppercase", new ToUppercase()); functionTable.put("toUppercase", new ToUppercase());
functionTable.put("toLowercase", new ToLowercase()); functionTable.put("toLowercase", new ToLowercase());
functionTable.put("toTitlecase", new ToTitlecase()); functionTable.put("toTitlecase", new ToTitlecase());
@ -59,6 +68,11 @@ public class Parser {
functionTable.put("endsWith", new EndsWith()); functionTable.put("endsWith", new EndsWith());
functionTable.put("join", new Join()); functionTable.put("join", new Join());
functionTable.put("round", new Round());
functionTable.put("floor", new Floor());
functionTable.put("ceil", new Ceil());
functionTable.put("mod", new Mod());
functionTable.put("and", new And()); functionTable.put("and", new And());
functionTable.put("or", new Or()); functionTable.put("or", new Or());
functionTable.put("not", new Not()); functionTable.put("not", new Not());

View File

@ -0,0 +1,16 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class Ceil implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] instanceof Number) {
return (long) Math.ceil(((Number) args[0]).doubleValue());
}
return null;
}
}

View File

@ -0,0 +1,16 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class Floor implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] instanceof Number) {
return (long) Math.floor(((Number) args[0]).doubleValue());
}
return null;
}
}

View File

@ -0,0 +1,19 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class Mod implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 2 && args[0] instanceof Number && args[1] instanceof Number) {
int a = ((Number) args[0]).intValue();
int b = ((Number) args[0]).intValue();
return a % b;
}
return null;
}
}

View File

@ -0,0 +1,16 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class Round implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1 && args[0] instanceof Number) {
return ((Number) args[0]).longValue();
}
return null;
}
}

View File

@ -0,0 +1,16 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class ToNumber implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
return args[0] instanceof Number ? args[0] : Double.parseDouble(args[0].toString());
}
return null;
}
}

View File

@ -0,0 +1,16 @@
package com.metaweb.gridworks.expr.functions;
import java.util.Properties;
import com.metaweb.gridworks.expr.Function;
public class ToString implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length == 1) {
return args[0] instanceof String ? args[0] : args[0].toString();
}
return null;
}
}

View File

@ -42,6 +42,8 @@ public class Row implements Serializable, HasFields, Jsonizable {
return starred; return starred;
} else if ("cells".equals(name)) { } else if ("cells".equals(name)) {
return new Cells(); return new Cells();
} else if ("index".equals(name)) {
return bindings.get("rowIndex");
} }
return null; return null;
} }

View File

@ -133,7 +133,7 @@ public class ColumnAdditionOperation extends EngineDependentOperation {
public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { public boolean visit(Project project, int rowIndex, Row row, boolean contextual) {
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object v = eval.evaluate(bindings); Object v = eval.evaluate(bindings);
if (v != null) { if (v != null) {

View File

@ -84,7 +84,7 @@ public class TextTransformOperation extends EngineDependentMassCellOperation {
public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { public boolean visit(Project project, int rowIndex, Row row, boolean contextual) {
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
ExpressionUtils.bind(bindings, row, cell); ExpressionUtils.bind(bindings, row, rowIndex, cell);
Object v = eval.evaluate(bindings); Object v = eval.evaluate(bindings);
if ((cell != null && cell.value != null) || v != null) { if ((cell != null && cell.value != null) || v != null) {