Forgot to register blank down operation in operation registry previously.
Added uniques GEL function for eliminating duplicates in an array. git-svn-id: http://google-refine.googlecode.com/svn/trunk@1158 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
fa816007a7
commit
a90a9c724e
@ -0,0 +1,63 @@
|
|||||||
|
package com.google.gridworks.expr.functions.arrays;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONWriter;
|
||||||
|
|
||||||
|
import com.google.gridworks.expr.EvalError;
|
||||||
|
import com.google.gridworks.expr.ExpressionUtils;
|
||||||
|
import com.google.gridworks.gel.ControlFunctionRegistry;
|
||||||
|
import com.google.gridworks.gel.Function;
|
||||||
|
import com.google.gridworks.util.JSONUtilities;
|
||||||
|
|
||||||
|
public class Uniques implements Function {
|
||||||
|
|
||||||
|
public Object call(Properties bindings, Object[] args) {
|
||||||
|
if (args.length == 1) {
|
||||||
|
Object v = args[0];
|
||||||
|
|
||||||
|
if (v != null) {
|
||||||
|
if (v instanceof JSONArray) {
|
||||||
|
try {
|
||||||
|
v = JSONUtilities.toArray((JSONArray) v);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
return new EvalError(ControlFunctionRegistry.getFunctionName(this) +
|
||||||
|
" fails to process a JSON array: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v.getClass().isArray() || v instanceof List<?>) {
|
||||||
|
Set<Object> set = null;
|
||||||
|
|
||||||
|
if (v.getClass().isArray()) {
|
||||||
|
Object[] a = (Object[]) v;
|
||||||
|
|
||||||
|
set = new HashSet<Object>(a.length);
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
set.add(a[i]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
set = new HashSet<Object>(ExpressionUtils.toObjectList(v));
|
||||||
|
}
|
||||||
|
return set.toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects an array");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void write(JSONWriter writer, Properties options)
|
||||||
|
throws JSONException {
|
||||||
|
|
||||||
|
writer.object();
|
||||||
|
writer.key("description"); writer.value("Returns array a with duplicates removed");
|
||||||
|
writer.key("params"); writer.value("array a");
|
||||||
|
writer.key("returns"); writer.value("array");
|
||||||
|
writer.endObject();
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ import com.google.gridworks.expr.functions.Type;
|
|||||||
import com.google.gridworks.expr.functions.arrays.Join;
|
import com.google.gridworks.expr.functions.arrays.Join;
|
||||||
import com.google.gridworks.expr.functions.arrays.Reverse;
|
import com.google.gridworks.expr.functions.arrays.Reverse;
|
||||||
import com.google.gridworks.expr.functions.arrays.Sort;
|
import com.google.gridworks.expr.functions.arrays.Sort;
|
||||||
|
import com.google.gridworks.expr.functions.arrays.Uniques;
|
||||||
import com.google.gridworks.expr.functions.booleans.And;
|
import com.google.gridworks.expr.functions.booleans.And;
|
||||||
import com.google.gridworks.expr.functions.booleans.Not;
|
import com.google.gridworks.expr.functions.booleans.Not;
|
||||||
import com.google.gridworks.expr.functions.booleans.Or;
|
import com.google.gridworks.expr.functions.booleans.Or;
|
||||||
@ -171,6 +172,7 @@ public class ControlFunctionRegistry {
|
|||||||
registerFunction("join", new Join());
|
registerFunction("join", new Join());
|
||||||
registerFunction("reverse", new Reverse());
|
registerFunction("reverse", new Reverse());
|
||||||
registerFunction("sort", new Sort());
|
registerFunction("sort", new Sort());
|
||||||
|
registerFunction("uniques", new Uniques());
|
||||||
|
|
||||||
registerFunction("now", new Now());
|
registerFunction("now", new Now());
|
||||||
registerFunction("inc", new Inc());
|
registerFunction("inc", new Inc());
|
||||||
|
@ -8,6 +8,7 @@ import org.json.JSONObject;
|
|||||||
|
|
||||||
import com.google.gridworks.model.AbstractOperation;
|
import com.google.gridworks.model.AbstractOperation;
|
||||||
import com.google.gridworks.model.Project;
|
import com.google.gridworks.model.Project;
|
||||||
|
import com.google.gridworks.operations.cell.BlankDownOperation;
|
||||||
import com.google.gridworks.operations.cell.FillDownOperation;
|
import com.google.gridworks.operations.cell.FillDownOperation;
|
||||||
import com.google.gridworks.operations.cell.MassEditOperation;
|
import com.google.gridworks.operations.cell.MassEditOperation;
|
||||||
import com.google.gridworks.operations.cell.MultiValuedCellJoinOperation;
|
import com.google.gridworks.operations.cell.MultiValuedCellJoinOperation;
|
||||||
@ -55,6 +56,7 @@ public abstract class OperationRegistry {
|
|||||||
register("multivalued-cell-join", MultiValuedCellJoinOperation.class);
|
register("multivalued-cell-join", MultiValuedCellJoinOperation.class);
|
||||||
register("multivalued-cell-split", MultiValuedCellSplitOperation.class);
|
register("multivalued-cell-split", MultiValuedCellSplitOperation.class);
|
||||||
register("fill-down", FillDownOperation.class);
|
register("fill-down", FillDownOperation.class);
|
||||||
|
register("blank-down", BlankDownOperation.class);
|
||||||
register("transpose-columns-into-rows", TransposeColumnsIntoRowsOperation.class);
|
register("transpose-columns-into-rows", TransposeColumnsIntoRowsOperation.class);
|
||||||
register("transpose-rows-into-columns", TransposeRowsIntoColumnsOperation.class);
|
register("transpose-rows-into-columns", TransposeRowsIntoColumnsOperation.class);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user