Jackson deserialization for ReconJudgeSimilarCellsOperation

This commit is contained in:
Antonin Delpeuch 2018-10-22 10:27:39 +01:00
parent 408703f13c
commit 3271e53ecf
2 changed files with 34 additions and 44 deletions

View File

@ -33,14 +33,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.operations.recon;
import java.util.HashMap;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
@ -61,6 +61,7 @@ import com.google.refine.model.changes.CellChange;
import com.google.refine.model.changes.ReconChange;
import com.google.refine.model.recon.ReconConfig;
import com.google.refine.operations.EngineDependentMassCellOperation;
import com.google.refine.util.ParsingUtilities;
public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOperation {
final protected String _similarValue;
@ -68,55 +69,30 @@ public class ReconJudgeSimilarCellsOperation extends EngineDependentMassCellOper
final protected ReconCandidate _match;
final protected boolean _shareNewTopics;
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws JSONException {
JSONObject engineConfig = obj.getJSONObject("engineConfig");
ReconCandidate match = null;
if (obj.has("match")) {
JSONObject matchObj = obj.getJSONObject("match");
JSONArray types = matchObj.getJSONArray("types");
String[] typeIDs = new String[types.length()];
for (int i = 0; i < typeIDs.length; i++) {
typeIDs[i] = types.getString(i);
}
match = new ReconCandidate(
matchObj.getString("id"),
matchObj.getString("name"),
typeIDs,
matchObj.getDouble("score")
);
}
Judgment judgment = Judgment.None;
if (obj.has("judgment")) {
judgment = Recon.stringToJudgment(obj.getString("judgment"));
}
return new ReconJudgeSimilarCellsOperation(
EngineConfig.reconstruct(engineConfig),
obj.getString("columnName"),
obj.getString("similarValue"),
judgment,
match,
obj.has("shareNewTopics") ? obj.getBoolean("shareNewTopics") : false
);
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws IOException {
return ParsingUtilities.mapper.readValue(obj.toString(), ReconJudgeSimilarCellsOperation.class);
}
@JsonCreator
public ReconJudgeSimilarCellsOperation(
EngineConfig engineConfig,
String columnName,
@JsonProperty("engineConfig")
EngineConfig engineConfig,
@JsonProperty("columnName")
String columnName,
@JsonProperty("similarValue")
String similarValue,
@JsonProperty("judgment")
Judgment judgment,
@JsonProperty("match")
ReconCandidate match,
boolean shareNewTopics
@JsonProperty("shareNewTopics")
Boolean shareNewTopics
) {
super(engineConfig, columnName, false);
this._similarValue = similarValue;
this._judgment = judgment;
this._match = match;
this._shareNewTopics = shareNewTopics;
this._shareNewTopics = shareNewTopics == null ? false : shareNewTopics;
}
@JsonProperty("columnName")

View File

@ -4,10 +4,10 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.util.Collections;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeTest;
@ -39,7 +39,7 @@ public class ReconJudgeSimilarCellsTests extends RefineTest {
}
@Test
public void serializeReconJudgeSimilarCellsOperation() throws JSONException {
public void serializeReconJudgeSimilarCellsOperation() throws IOException {
String json = "{\"op\":\"core/recon-judge-similar-cells\","
+ "\"description\":\"Mark to create one single new item for all cells containing \\\"foo\\\" in column A\","
+ "\"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]},"
@ -50,6 +50,20 @@ public class ReconJudgeSimilarCellsTests extends RefineTest {
TestUtils.isSerializedTo(ReconJudgeSimilarCellsOperation.reconstruct(mock(Project.class), new JSONObject(json)), json);
}
@Test
public void serializeReconJudgeSimilarCellsOperationMatch() throws IOException {
String json = "{\"op\":\"core/recon-judge-similar-cells\","
+ "\"description\":\"Match item Douglas Adams (Q42) for cells containing \\\"foo\\\" in column A\","
+ "\"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]},"
+ "\"columnName\":\"A\","
+ "\"similarValue\":\"foo\","
+ "\"judgment\":\"matched\","
+ "\"match\":{\"id\":\"Q42\",\"name\":\"Douglas Adams\",\"types\":[\"Q5\"],\"score\":85},"
+ "\"shareNewTopics\":false"
+ "}";
TestUtils.isSerializedTo(ReconJudgeSimilarCellsOperation.reconstruct(mock(Project.class), new JSONObject(json)), json);
}
@Test
public void testMarkNewTopics() throws Exception {
Project project = createCSVProject(