Jackson deserialization for EngineConfig

This commit is contained in:
Antonin Delpeuch 2018-10-22 10:07:50 +01:00
parent ae61368796
commit 393eec1586
10 changed files with 86 additions and 188 deletions

View File

@ -1,21 +1,17 @@
package com.google.refine.browsing; package com.google.refine.browsing;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.browsing.Engine.Mode; import com.google.refine.browsing.Engine.Mode;
import com.google.refine.browsing.facets.FacetConfig; import com.google.refine.browsing.facets.FacetConfig;
import com.google.refine.browsing.facets.ListFacet.ListFacetConfig; import com.google.refine.util.ParsingUtilities;
import com.google.refine.browsing.facets.RangeFacet.RangeFacetConfig;
import com.google.refine.browsing.facets.ScatterplotFacet.ScatterplotFacetConfig;
import com.google.refine.browsing.facets.TextSearchFacet.TextSearchFacetConfig;
import com.google.refine.browsing.facets.TimeRangeFacet.TimeRangeFacetConfig;
public class EngineConfig { public class EngineConfig {
@ -23,9 +19,14 @@ public class EngineConfig {
protected final List<FacetConfig> _facets; protected final List<FacetConfig> _facets;
protected final Mode _mode; protected final Mode _mode;
public EngineConfig(List<FacetConfig> facets, Mode mode) { @JsonCreator
_facets = facets; public EngineConfig(
_mode = mode; @JsonProperty("facets")
List<FacetConfig> facets,
@JsonProperty("mode")
Mode mode) {
_facets = facets == null ? Collections.emptyList() : facets;
_mode = mode == null ? Mode.RowBased : mode;
} }
@JsonProperty("mode") @JsonProperty("mode")
@ -39,49 +40,14 @@ public class EngineConfig {
} }
public static EngineConfig reconstruct(JSONObject o) { public static EngineConfig reconstruct(JSONObject o) {
if (o == null) { if(o == null) {
return new EngineConfig(Collections.emptyList(), Mode.RowBased); return new EngineConfig(Collections.emptyList(), Mode.RowBased);
} }
try {
List<FacetConfig> facets = new LinkedList<>(); return ParsingUtilities.mapper.readValue(o.toString(), EngineConfig.class);
if (o.has("facets") && !o.isNull("facets")) { } catch (IOException e) {
JSONArray a = o.getJSONArray("facets"); e.printStackTrace();
int length = a.length(); return null;
for (int i = 0; i < length; i++) {
JSONObject fo = a.getJSONObject(i);
String type = fo.has("type") ? fo.getString("type") : "list";
FacetConfig facet = null;
if ("list".equals(type)) {
facet = new ListFacetConfig();
} else if ("range".equals(type)) {
facet = new RangeFacetConfig();
} else if ("timerange".equals(type)) {
facet = new TimeRangeFacetConfig();
} else if ("scatterplot".equals(type)) {
facet = new ScatterplotFacetConfig();
} else if ("text".equals(type)) {
facet = new TextSearchFacetConfig();
}
if (facet != null) {
facet.initializeFromJSON(fo);
facets.add(facet);
}
}
} }
Mode mode = Mode.RowBased;
// for backward compatibility
if (o.has(Engine.INCLUDE_DEPENDENT) && !o.isNull(Engine.INCLUDE_DEPENDENT)) {
mode = o.getBoolean(Engine.INCLUDE_DEPENDENT) ? Mode.RecordBased : Mode.RowBased;
}
if (o.has(Engine.MODE) && !o.isNull(Engine.MODE)) {
mode = Engine.MODE_ROW_BASED.equals(o.getString(Engine.MODE)) ? Mode.RowBased : Mode.RecordBased;
}
return new EngineConfig(facets, mode);
} }
} }

View File

@ -1,8 +1,9 @@
package com.google.refine.browsing.facets; package com.google.refine.browsing.facets;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.refine.model.Project; import com.google.refine.model.Project;
@ -13,16 +14,20 @@ import com.google.refine.model.Project;
* of operations. It does not contain the actual values displayed by * of operations. It does not contain the actual values displayed by
* the facet. * the facet.
* *
* @author antonin * @author Antonin Delpeuch
* *
*/ */
public interface FacetConfig { @JsonTypeInfo(
/** use=JsonTypeInfo.Id.NAME,
* Reads the facet configuration from a JSON object (will be removed once we migrate to Jackson) include=JsonTypeInfo.As.PROPERTY,
* @param fo property="type")
*/ @JsonSubTypes({
public void initializeFromJSON(JSONObject fo); @Type(value = ListFacet.ListFacetConfig.class, name = "list"),
@Type(value = RangeFacet.RangeFacetConfig.class, name = "range"),
@Type(value = TimeRangeFacet.TimeRangeFacetConfig.class, name = "timerange"),
@Type(value = TextSearchFacet.TextSearchFacetConfig.class, name = "text"),
@Type(value = ScatterplotFacet.ScatterplotFacetConfig.class, name = "scatterplot") })
public interface FacetConfig {
/** /**
* Instantiates the given facet on a particular project. * Instantiates the given facet on a particular project.
* @param project * @param project

View File

@ -0,0 +1,36 @@
package com.google.refine.browsing.facets;
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.databind.DatabindContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.refine.model.recon.ReconConfig;
public class FacetConfigResolver extends TypeIdResolverBase {
protected TypeFactory factory = TypeFactory.defaultInstance();
@Override
public Id getMechanism() {
return Id.NAME;
}
@Override
public String idFromValue(Object instance) {
return ((ReconConfig)instance).getMode();
}
@Override
public String idFromValueAndType(Object instance, Class<?> type) {
return ReconConfig.s_opClassToName.get(type);
}
@Override
public JavaType typeFromId(DatabindContext context, String id) throws IOException {
return factory.constructSimpleType(ReconConfig.getClassFromMode(id), new JavaType[0]);
}
}

View File

@ -37,9 +37,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.json.JSONArray;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -61,7 +58,6 @@ import com.google.refine.expr.MetaParser;
import com.google.refine.expr.ParsingException; import com.google.refine.expr.ParsingException;
import com.google.refine.model.Column; import com.google.refine.model.Column;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.util.JSONUtilities;
public class ListFacet implements Facet { public class ListFacet implements Facet {
public static final String ERR_TOO_MANY_CHOICES = "Too many choices"; public static final String ERR_TOO_MANY_CHOICES = "Too many choices";
@ -125,36 +121,6 @@ public class ListFacet implements Facet {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@Override
public void initializeFromJSON(JSONObject o) {
name = o.getString("name");
expression = o.getString("expression");
columnName = o.getString("columnName");
invert = o.has("invert") && o.getBoolean("invert");
JSONArray a = o.getJSONArray("selection");
int length = a.length();
for (int i = 0; i < length; i++) {
JSONObject oc = a.getJSONObject(i);
JSONObject ocv = oc.getJSONObject("v");
DecoratedValue decoratedValue = new DecoratedValue(
ocv.get("v"), ocv.getString("l"));
selection.add(decoratedValue);
}
omitBlank = JSONUtilities.getBoolean(o, "omitBlank", false);
omitError = JSONUtilities.getBoolean(o, "omitError", false);
selectNumber = JSONUtilities.getBoolean(o, "selectNumber", false);
selectDateTime = JSONUtilities.getBoolean(o, "selectDateTime", false);
selectBoolean = JSONUtilities.getBoolean(o, "selectBoolean", false);
selectBlank = JSONUtilities.getBoolean(o, "selectBlank", false);
selectError = JSONUtilities.getBoolean(o, "selectError", false);
}
@Override @Override
public Facet apply(Project project) { public Facet apply(Project project) {
ListFacet facet = new ListFacet(); ListFacet facet = new ListFacet();

View File

@ -33,8 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.browsing.facets; package com.google.refine.browsing.facets;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
@ -58,7 +56,6 @@ import com.google.refine.expr.MetaParser;
import com.google.refine.expr.ParsingException; import com.google.refine.expr.ParsingException;
import com.google.refine.model.Column; import com.google.refine.model.Column;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.util.JSONUtilities;
public class RangeFacet implements Facet { public class RangeFacet implements Facet {
@ -125,26 +122,6 @@ public class RangeFacet implements Facet {
_selected = !_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError || from != null || to != null; _selected = !_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError || from != null || to != null;
} }
@Override
public void initializeFromJSON(JSONObject o) {
_name = o.getString("name");
_expression = o.getString("expression");
_columnName = o.getString("columnName");
if (o.has(FROM) || o.has(TO)) {
_from = o.has(FROM) ? o.getDouble(FROM) : 0;
_to = o.has(TO) ? o.getDouble(TO) : 0;
_selected = true;
}
_selectNumeric = JSONUtilities.getBoolean(o, "selectNumeric", true);
_selectNonNumeric = JSONUtilities.getBoolean(o, "selectNonNumeric", true);
_selectBlank = JSONUtilities.getBoolean(o, "selectBlank", true);
_selectError = JSONUtilities.getBoolean(o, "selectError", true);
if (!_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError) {
_selected = true;
}
}
@Override @Override
public RangeFacet apply(Project project) { public RangeFacet apply(Project project) {
RangeFacet facet = new RangeFacet(); RangeFacet facet = new RangeFacet();

View File

@ -44,7 +44,6 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -111,9 +110,11 @@ public class ScatterplotFacet implements Facet {
protected double dot; protected double dot;
@JsonIgnore @JsonIgnore
protected String color_str; protected String color_str = "000000";
@JsonIgnore @JsonIgnore
protected Color color; protected Color getColor() {
return new Color(Integer.parseInt(color_str,16));
}
@JsonProperty(FROM_X) @JsonProperty(FROM_X)
protected double from_x; // the numeric selection for the x axis, from 0 to 1 protected double from_x; // the numeric selection for the x axis, from 0 to 1
@ -147,43 +148,6 @@ public class ScatterplotFacet implements Facet {
return facet; return facet;
} }
@Override
public void initializeFromJSON(JSONObject o) {
name = o.getString(NAME);
l = size = (o.has(SIZE)) ? o.getInt(SIZE) : 100;
dot = (o.has(DOT)) ? o.getInt(DOT) : 0.5d;
dim_x = (o.has(DIM_X)) ? getAxisDim(o.getString(DIM_X)) : LIN;
if (o.has(FROM_X) && o.has(TO_X)) {
from_x = o.getDouble(FROM_X);
to_x = o.getDouble(TO_X);
} else {
from_x = 0;
to_x = 1;
}
dim_y = (o.has(DIM_Y)) ? getAxisDim(o.getString(DIM_Y)) : LIN;
if (o.has(FROM_Y) && o.has(TO_Y)) {
from_y = o.getDouble(FROM_Y);
to_y = o.getDouble(TO_Y);
} else {
from_y = 0;
to_y = 1;
}
rotation_str = (o.has(ROTATION) ? o.getString(ROTATION) : "");
rotation = getRotation(rotation_str);
color_str = (o.has(COLOR)) ? o.getString(COLOR) : "000000";
color = new Color(Integer.parseInt(color_str,16));
columnName_x = o.getString(X_COLUMN_NAME);
expression_x = o.getString(X_EXPRESSION);
columnName_y = o.getString(Y_COLUMN_NAME);
expression_y = o.getString(Y_EXPRESSION);
}
public static int getRotation(String rotation) { public static int getRotation(String rotation) {
rotation = rotation.toLowerCase(); rotation = rotation.toLowerCase();
if ("cw".equals(rotation) || "right".equals(rotation)) { if ("cw".equals(rotation) || "right".equals(rotation)) {
@ -469,7 +433,7 @@ public class ScatterplotFacet implements Facet {
if (index_x.isNumeric() && index_y.isNumeric()) { if (index_x.isNumeric() && index_y.isNumeric()) {
ScatterplotDrawingRowVisitor drawer = new ScatterplotDrawingRowVisitor( ScatterplotDrawingRowVisitor drawer = new ScatterplotDrawingRowVisitor(
columnIndex_x, columnIndex_y, min_x, max_x, min_y, max_y, columnIndex_x, columnIndex_y, min_x, max_x, min_y, max_y,
config.size, config.dim_x, config.dim_y, config.rotation, config.dot, config.color config.size, config.dim_x, config.dim_y, config.rotation, config.dot, config.getColor()
); );
filteredRows.accept(project, drawer); filteredRows.accept(project, drawer);
@ -500,7 +464,7 @@ public class ScatterplotFacet implements Facet {
if (index_x.isNumeric() && index_y.isNumeric()) { if (index_x.isNumeric() && index_y.isNumeric()) {
ScatterplotDrawingRowVisitor drawer = new ScatterplotDrawingRowVisitor( ScatterplotDrawingRowVisitor drawer = new ScatterplotDrawingRowVisitor(
columnIndex_x, columnIndex_y, min_x, max_x, min_y, max_y, columnIndex_x, columnIndex_y, min_x, max_x, min_y, max_y,
config.size, config.dim_x, config.dim_y, config.rotation, config.dot, config.color config.size, config.dim_x, config.dim_y, config.rotation, config.dot, config.getColor()
); );
filteredRecords.accept(project, drawer); filteredRecords.accept(project, drawer);

View File

@ -36,7 +36,6 @@ package com.google.refine.browsing.facets;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
@ -78,18 +77,6 @@ public class TextSearchFacet implements Facet {
return facet; return facet;
} }
@Override
public void initializeFromJSON(JSONObject o) {
_name = o.getString("name");
_columnName = o.getString("columnName");
_mode = o.getString("mode");
_caseSensitive = o.getBoolean("caseSensitive");
if (!o.isNull("query")) {
_query = o.getString("query");
}
_invert = o.has("invert") && o.getBoolean("invert");
}
@Override @Override
public String getJsonType() { public String getJsonType() {
return "text"; return "text";

View File

@ -33,9 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.browsing.facets; package com.google.refine.browsing.facets;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
@ -58,7 +55,6 @@ import com.google.refine.expr.MetaParser;
import com.google.refine.expr.ParsingException; import com.google.refine.expr.ParsingException;
import com.google.refine.model.Column; import com.google.refine.model.Column;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.util.JSONUtilities;
public class TimeRangeFacet implements Facet { public class TimeRangeFacet implements Facet {
/* /*

View File

@ -2,7 +2,6 @@ package com.google.refine.tests.browsing.facets;
import java.io.IOException; import java.io.IOException;
import org.json.JSONObject;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParseException;
@ -65,9 +64,8 @@ public class ListFacetTests extends RefineTest {
+ "]}"; + "]}";
@Test @Test
public void serializeListFacetConfig() { public void serializeListFacetConfig() throws JsonParseException, JsonMappingException, IOException {
ListFacetConfig facetConfig = new ListFacetConfig(); ListFacetConfig facetConfig = ParsingUtilities.mapper.readValue(jsonConfig, ListFacetConfig.class);
facetConfig.initializeFromJSON(new JSONObject(jsonConfig));
TestUtils.isSerializedTo(facetConfig, jsonConfig); TestUtils.isSerializedTo(facetConfig, jsonConfig);
} }

View File

@ -1,8 +1,12 @@
package com.google.refine.tests.browsing.facets; package com.google.refine.tests.browsing.facets;
import org.json.JSONObject; import java.io.IOException;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.refine.browsing.Engine; import com.google.refine.browsing.Engine;
import com.google.refine.browsing.facets.ScatterplotFacet; import com.google.refine.browsing.facets.ScatterplotFacet;
import com.google.refine.browsing.facets.ScatterplotFacet.ScatterplotFacetConfig; import com.google.refine.browsing.facets.ScatterplotFacet.ScatterplotFacetConfig;
@ -10,6 +14,7 @@ import com.google.refine.model.Cell;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils; import com.google.refine.tests.util.TestUtils;
import com.google.refine.util.ParsingUtilities;
public class ScatterplotFacetTests extends RefineTest { public class ScatterplotFacetTests extends RefineTest {
public static String configJson = "{\n" + public static String configJson = "{\n" +
@ -48,14 +53,13 @@ public class ScatterplotFacetTests extends RefineTest {
+ "}"; + "}";
@Test @Test
public void serializeScatterplotFacetConfig() { public void serializeScatterplotFacetConfig() throws JsonParseException, JsonMappingException, IOException {
ScatterplotFacetConfig config = new ScatterplotFacetConfig(); ScatterplotFacetConfig config = ParsingUtilities.mapper.readValue(configJson, ScatterplotFacetConfig.class);
config.initializeFromJSON(new JSONObject(configJson));
TestUtils.isSerializedTo(config, configJson); TestUtils.isSerializedTo(config, configJson);
} }
@Test @Test
public void serializeScatterplotFacet() { public void serializeScatterplotFacet() throws JsonParseException, JsonMappingException, IOException {
Project project = createCSVProject("my column,e\n" Project project = createCSVProject("my column,e\n"
+ "89.2,89.2\n" + + "89.2,89.2\n" +
"-45.9,-45.9\n" + "-45.9,-45.9\n" +
@ -69,8 +73,7 @@ public class ScatterplotFacetTests extends RefineTest {
project.rows.get(3).cells.set(0, new Cell(0.4, null)); project.rows.get(3).cells.set(0, new Cell(0.4, null));
project.rows.get(3).cells.set(1, new Cell(0.4, null)); project.rows.get(3).cells.set(1, new Cell(0.4, null));
ScatterplotFacetConfig config = new ScatterplotFacetConfig(); ScatterplotFacetConfig config = ParsingUtilities.mapper.readValue(configJson, ScatterplotFacetConfig.class);
config.initializeFromJSON(new JSONObject(configJson));
ScatterplotFacet facet = config.apply(project); ScatterplotFacet facet = config.apply(project);
facet.computeChoices(project, engine.getAllFilteredRows()); facet.computeChoices(project, engine.getAllFilteredRows());