Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple expression languages from core infrastructure #6502

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions main/src/com/google/refine/LookupCacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.google.refine.expr.ExpressionUtils;
import com.google.refine.expr.HasFieldsListImpl;
import com.google.refine.expr.WrappedRow;
import com.google.refine.expr.functions.Cross;
import com.google.refine.model.Column;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
Expand All @@ -50,6 +49,8 @@
*/
public class LookupCacheManager {

public static final String INDEX_COLUMN_NAME = "_OpenRefine_Index_Column_Name_";
tfmorris marked this conversation as resolved.
Show resolved Hide resolved

protected final Map<String, ProjectLookup> _lookups = new HashMap<>();

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ protected void computeLookup(ProjectLookup lookup) throws LookupException {
}

// if this is a lookup on the index column
if (lookup.targetColumnName.equals(Cross.INDEX_COLUMN_NAME)) {
if (INDEX_COLUMN_NAME.equals(lookup.targetColumnName)) {
for (int r = 0; r < targetProject.rows.size(); r++) {
lookup.valueToRowIndices.put(String.valueOf(r), Collections.singletonList(r));
}
Expand Down
11 changes: 9 additions & 2 deletions main/src/com/google/refine/browsing/facets/TextSearchFacet.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

package com.google.refine.browsing.facets;

import java.util.Properties;
import java.util.regex.Pattern;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -44,7 +45,6 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import com.google.refine.browsing.filters.AnyRowRecordFilter;
import com.google.refine.browsing.filters.ExpressionStringComparisonRowFilter;
import com.google.refine.expr.Evaluable;
import com.google.refine.grel.ast.VariableExpr;
import com.google.refine.model.Column;
import com.google.refine.model.Project;
import com.google.refine.util.PatternSyntaxExceptionParser;
Expand Down Expand Up @@ -156,7 +156,14 @@ public RowFilter getRowFilter(Project project) {
return null;
}

Evaluable eval = new VariableExpr("value");
Evaluable eval = new Evaluable() {

@Override
public Object evaluate(Properties bindings) {
return bindings.get("value");
}

};

if ("regex".equals(_config._mode)) {
return new ExpressionStringComparisonRowFilter(eval, _config._invert, _config._columnName, _cellIndex) {
Expand Down
88 changes: 88 additions & 0 deletions main/src/com/google/refine/expr/ClojureParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*

Copyright 2010,2011. Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

package com.google.refine.expr;

import java.io.StringReader;
import java.util.Properties;

import clojure.lang.IFn;
import clojure.lang.RT;

/**
* A parser for expressions written in Clojure.
*/
public class ClojureParser implements LanguageSpecificParser {

@Override
public Evaluable parse(String s) throws ParsingException {
try {
// RT.load("clojure/core"); // Make sure RT is initialized
Object foo = RT.CURRENT_NS; // Make sure RT is initialized
IFn fn = (IFn) clojure.lang.Compiler.load(new StringReader(
"(fn [value cell cells row rowIndex] " + s + ")"));

// TODO: We should to switch from using Compiler.load
// because it's technically an internal interface
// Object code = CLOJURE_READ_STRING.invoke(
// "(fn [value cell cells row rowIndex] " + s + ")"
// );

return new Evaluable() {

private IFn _fn;

public Evaluable init(IFn fn) {
_fn = fn;
return this;
}

@Override
public Object evaluate(Properties bindings) {
try {
return _fn.invoke(
bindings.get("value"),
bindings.get("cell"),
bindings.get("cells"),
bindings.get("row"),
bindings.get("rowIndex"));
} catch (Exception e) {
return new EvalError(e.getMessage());
}
}
}.init(fn);
} catch (Exception e) {
throw new ParsingException(e.getMessage());
}
}
}
73 changes: 5 additions & 68 deletions main/src/com/google/refine/expr/MetaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,13 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

package com.google.refine.expr;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import clojure.lang.IFn;
import clojure.lang.RT;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import com.google.refine.grel.Parser;

abstract public class MetaParser {

static public class LanguageInfo {
Expand All @@ -66,65 +60,6 @@ static public class LanguageInfo {

static final protected Map<String, LanguageInfo> s_languages = new HashMap<String, LanguageInfo>();

// TODO: We should switch from using the internal compiler class
// final static private Var CLOJURE_READ_STRING = RT.var("clojure.core", "read-string");
// final static private Var CLOJURE_EVAL = RT.var("clojure.core", "eval");

static {
registerLanguageParser("grel", "General Refine Expression Language (GREL)", new LanguageSpecificParser() {

@Override
public Evaluable parse(String s) throws ParsingException {
return parseGREL(s);
}
}, "value");

registerLanguageParser("clojure", "Clojure", new LanguageSpecificParser() {

@Override
public Evaluable parse(String s) throws ParsingException {
try {
// RT.load("clojure/core"); // Make sure RT is initialized
Object foo = RT.CURRENT_NS; // Make sure RT is initialized
IFn fn = (IFn) clojure.lang.Compiler.load(new StringReader(
"(fn [value cell cells row rowIndex] " + s + ")"));

// TODO: We should to switch from using Compiler.load
// because it's technically an internal interface
// Object code = CLOJURE_READ_STRING.invoke(
// "(fn [value cell cells row rowIndex] " + s + ")"
// );

return new Evaluable() {

private IFn _fn;

public Evaluable init(IFn fn) {
_fn = fn;
return this;
}

@Override
public Object evaluate(Properties bindings) {
try {
return _fn.invoke(
bindings.get("value"),
bindings.get("cell"),
bindings.get("cells"),
bindings.get("row"),
bindings.get("rowIndex"));
} catch (Exception e) {
return new EvalError(e.getMessage());
}
}
}.init(fn);
} catch (Exception e) {
throw new ParsingException(e.getMessage());
}
}
}, "value");
}

/**
* languagePrefix will be stored in the meta model as an identifier. so be careful when change it as it will break
* the backward compatibility for the old project
Expand Down Expand Up @@ -174,8 +109,10 @@ static public Evaluable parse(String s) throws ParsingException {
}

static protected Evaluable parseGREL(String s) throws ParsingException {
Parser parser = new Parser(s);

return parser.getExpression();
LanguageInfo info = s_languages.get("grel");
if (info == null) {
throw new ParsingException("Default language GREL is not available");
}
return info.parser.parse(s);
}
}
9 changes: 7 additions & 2 deletions main/src/com/google/refine/expr/functions/Cross.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

import java.util.Properties;

import com.google.refine.LookupCacheManager;
import com.google.refine.LookupCacheManager.ProjectLookup;
import com.google.refine.ProjectManager;
import com.google.refine.expr.EvalError;
Expand All @@ -49,7 +50,11 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

public class Cross implements Function {

public static final String INDEX_COLUMN_NAME = "_OpenRefine_Index_Column_Name_";
/**
* @deprecated use {@link LookupCacheManager#INDEX_COLUMN_NAME}.
*/
@Deprecated
wetneb marked this conversation as resolved.
Show resolved Hide resolved
public static final String INDEX_COLUMN_NAME = LookupCacheManager.INDEX_COLUMN_NAME;

@Override
public Object call(Properties bindings, Object[] args) {
Expand All @@ -65,7 +70,7 @@ public Object call(Properties bindings, Object[] args) {
targetProjectName = args[1];
}
// if 3rd argument is omitted or set to "", use the index column
Object targetColumnName = args.length < 3 || "".equals(args[2]) ? INDEX_COLUMN_NAME : args[2];
Object targetColumnName = args.length < 3 || "".equals(args[2]) ? LookupCacheManager.INDEX_COLUMN_NAME : args[2];

long targetProjectID;
ProjectLookup lookup;
Expand Down
10 changes: 10 additions & 0 deletions main/src/com/google/refine/grel/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import java.util.regex.Pattern;

import com.google.refine.expr.Evaluable;
import com.google.refine.expr.LanguageSpecificParser;
import com.google.refine.expr.ParsingException;
import com.google.refine.expr.functions.arrays.ArgsToArray;
import com.google.refine.grel.Scanner.NumberToken;
Expand All @@ -53,6 +54,15 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

public class Parser {

static public LanguageSpecificParser grelParser = new LanguageSpecificParser() {

@Override
public Evaluable parse(String source) throws ParsingException {
Parser parser = new Parser(source);
return parser.getExpression();
}
};

protected Scanner _scanner;
protected Token _token;
protected Evaluable _root;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -56,7 +58,6 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import com.google.refine.expr.functions.ToDate;
import com.google.refine.model.ReconCandidate;
import com.google.refine.model.ReconType;
import com.google.refine.util.HttpClient;
Expand Down Expand Up @@ -250,11 +251,13 @@ protected ReconciledDataExtensionJob.DataExtension collectResult(
int v = val.get("int").asInt();
storeCell(rows, rowindex, colindex, v);
} else if (val.has("date")) {
ToDate td = new ToDate();
String[] args = new String[1];
args[0] = val.get("date").asText();
Object v = td.call(null, args);
storeCell(rows, rowindex, colindex, v);
Object date;
try {
date = OffsetDateTime.parse(val.get("date").asText());
} catch (DateTimeParseException e) {
date = val.get("date").asText();
}
storeCell(rows, rowindex, colindex, date);
} else if (val.has("bool")) {
boolean v = val.get("bool").asBoolean();
storeCell(rows, rowindex, colindex, v);
Expand Down
2 changes: 2 additions & 0 deletions main/tests/server/src/com/google/refine/RefineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
import com.google.refine.expr.ParsingException;
import com.google.refine.grel.ControlFunctionRegistry;
import com.google.refine.grel.Function;
import com.google.refine.grel.Parser;
import com.google.refine.importing.ImportingJob;
import com.google.refine.importing.ImportingManager;
import com.google.refine.io.FileProjectManager;
Expand Down Expand Up @@ -118,6 +119,7 @@ public void init() {
}
// This just keeps track of any failed test, for cleanupWorkspace
testFailed = false;
MetaParser.registerLanguageParser("grel", "GREL", Parser.grelParser, "value");
}

@BeforeMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ public class ColumnAdditionByFetchingURLsOperationTests extends RefineTest {
" \"status\" : \"pending\"\n" +
" }";

@Override
@BeforeTest
public void init() {
public void initOperation() {
tfmorris marked this conversation as resolved.
Show resolved Hide resolved
logger = LoggerFactory.getLogger(this.getClass());
OperationRegistry.registerOperation(getCoreModule(), "column-addition-by-fetching-urls",
ColumnAdditionByFetchingURLsOperation.class);
Expand Down