From a90a9c724edafabbc66a2d41324c0922a211fe57 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 16 Aug 2010 03:00:43 +0000 Subject: [PATCH] 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 --- .../expr/functions/arrays/Uniques.java | 63 +++++++++++++++++++ .../gel/ControlFunctionRegistry.java | 2 + .../operations/OperationRegistry.java | 2 + 3 files changed, 67 insertions(+) create mode 100644 main/src/com/google/gridworks/expr/functions/arrays/Uniques.java diff --git a/main/src/com/google/gridworks/expr/functions/arrays/Uniques.java b/main/src/com/google/gridworks/expr/functions/arrays/Uniques.java new file mode 100644 index 000000000..cc14fb2b9 --- /dev/null +++ b/main/src/com/google/gridworks/expr/functions/arrays/Uniques.java @@ -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 set = null; + + if (v.getClass().isArray()) { + Object[] a = (Object[]) v; + + set = new HashSet(a.length); + for (int i = 0; i < a.length; i++) { + set.add(a[i]); + } + } else { + set = new HashSet(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(); + } +} diff --git a/main/src/com/google/gridworks/gel/ControlFunctionRegistry.java b/main/src/com/google/gridworks/gel/ControlFunctionRegistry.java index 8e83098bc..cf50eabd7 100644 --- a/main/src/com/google/gridworks/gel/ControlFunctionRegistry.java +++ b/main/src/com/google/gridworks/gel/ControlFunctionRegistry.java @@ -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.Reverse; 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.Not; import com.google.gridworks.expr.functions.booleans.Or; @@ -171,6 +172,7 @@ public class ControlFunctionRegistry { registerFunction("join", new Join()); registerFunction("reverse", new Reverse()); registerFunction("sort", new Sort()); + registerFunction("uniques", new Uniques()); registerFunction("now", new Now()); registerFunction("inc", new Inc()); diff --git a/main/src/com/google/gridworks/operations/OperationRegistry.java b/main/src/com/google/gridworks/operations/OperationRegistry.java index 001eb219d..e661bc0b5 100644 --- a/main/src/com/google/gridworks/operations/OperationRegistry.java +++ b/main/src/com/google/gridworks/operations/OperationRegistry.java @@ -8,6 +8,7 @@ import org.json.JSONObject; import com.google.gridworks.model.AbstractOperation; 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.MassEditOperation; import com.google.gridworks.operations.cell.MultiValuedCellJoinOperation; @@ -55,6 +56,7 @@ public abstract class OperationRegistry { register("multivalued-cell-join", MultiValuedCellJoinOperation.class); register("multivalued-cell-split", MultiValuedCellSplitOperation.class); register("fill-down", FillDownOperation.class); + register("blank-down", BlankDownOperation.class); register("transpose-columns-into-rows", TransposeColumnsIntoRowsOperation.class); register("transpose-rows-into-columns", TransposeRowsIntoColumnsOperation.class);