Migrate schema expressions to Jackson

This commit is contained in:
Antonin Delpeuch 2018-01-03 14:09:22 +01:00
parent 989263d212
commit 955bb409bc
29 changed files with 363 additions and 706 deletions

View File

@ -81,9 +81,9 @@ public class PerformWikibaseEditsOperation extends EngineDependentOperation {
JSONObject engineConfig = obj.getJSONObject("engineConfig");
String strategy = obj.getString("duplicate_strategy");
String action = obj.getString("duplicate_action");
String summary = obj.getString("summary");
if (summary == null) {
summary = "#openrefine";
String summary = null;
if (obj.has("summary")) {
summary = obj.getString("summary");
}
return new PerformWikibaseEditsOperation(
engineConfig,

View File

@ -3,13 +3,22 @@ package org.openrefine.wikidata.operations;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrefine.wikidata.schema.WbItemConstant;
import org.openrefine.wikidata.schema.WbItemDocumentExpr;
import org.openrefine.wikidata.schema.WbNameDescExpr;
import org.openrefine.wikidata.schema.WbStatementGroupExpr;
import org.openrefine.wikidata.schema.WikibaseSchema;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.refine.history.Change;
import com.google.refine.history.HistoryEntry;
import com.google.refine.model.AbstractOperation;
@ -24,10 +33,20 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
public SaveWikibaseSchemaOperation(WikibaseSchema schema) {
this._schema = schema;
}
static public AbstractOperation reconstruct(Project project, JSONObject obj)
throws Exception {
System.out.println("Attempting to reconstruct save op");
try {
System.out.println(WikibaseSchema.reconstruct(obj.getJSONObject("schema")).toString());
} catch(Exception e) {
System.out.println("Failed to reconstruct");
e.printStackTrace();
}
System.out.println("SUCCESS");
return new SaveWikibaseSchemaOperation(WikibaseSchema.reconstruct(obj
.getJSONObject("schema")));
}

View File

@ -1,30 +0,0 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.Jsonizable;
public abstract class BiJsonizable implements Jsonizable {
// public static String jsonType;
public static String jsonType = null;
public static final String jsonTypeKey = "type";
public abstract void writeFields(JSONWriter writer, Properties options)
throws JSONException;
@Override
public void write(JSONWriter writer, Properties options) throws JSONException {
writer.object();
writer.key(jsonTypeKey);
writer.value(getJsonType());
writeFields(writer, options);
writer.endObject();
}
public abstract String getJsonType();
}

View File

@ -4,22 +4,20 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableMap;
public class WbDateConstant extends WbDateExpr {
public static final String jsonType = "wbdateconstant";
public static Map<SimpleDateFormat,Integer> acceptedFormats = ImmutableMap.<SimpleDateFormat,Integer>builder()
.put(new SimpleDateFormat("yyyy"), 9)
.put(new SimpleDateFormat("yyyy-MM"), 10)
@ -29,25 +27,22 @@ public class WbDateConstant extends WbDateExpr {
.put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), 14)
.build();
private TimeValue _parsed;
private String _origDatestamp;
private TimeValue parsed;
private String origDatestamp;
public WbDateConstant(String origDatestamp) {
_origDatestamp = origDatestamp;
try {
_parsed = parse(origDatestamp);
} catch(ParseException e) {
_parsed = null;
}
@JsonCreator
public WbDateConstant(
@JsonProperty("value") String origDatestamp) {
this.setOrigDatestamp(origDatestamp);
}
@Override
public TimeValue evaluate(ExpressionContext ctxt)
throws SkipStatementException {
if (_parsed == null) {
if (parsed == null) {
throw new SkipStatementException();
}
return _parsed;
return parsed;
}
public static TimeValue parse(String datestamp) throws ParseException {
@ -81,21 +76,20 @@ public class WbDateConstant extends WbDateExpr {
TimeValue.CM_GREGORIAN_PRO);
}
}
@JsonProperty("value")
public String getOrigDatestamp() {
return origDatestamp;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("value");
writer.value(_origDatestamp);
private void setOrigDatestamp(String origDatestamp) {
this.origDatestamp = origDatestamp;
try {
this.parsed = parse(origDatestamp);
} catch(ParseException e) {
this.parsed = null;
}
}
public static WbDateConstant fromJSON(JSONObject obj) throws JSONException {
return new WbDateConstant(obj.getString("value"));
}
@Override
public String getJsonType() {
return jsonType;
}
}

View File

@ -1,25 +1,10 @@
package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
public abstract class WbDateExpr extends WbValueExpr {
@Override
public abstract TimeValue evaluate(ExpressionContext ctxt)
throws SkipStatementException;
public static WbDateExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(jsonTypeKey);
if (WbDateConstant.jsonType.equals(type)) {
return WbDateConstant.fromJSON(obj);
} else if (WbDateVariable.jsonType.equals(type)) {
return WbDateVariable.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbDateExpr");
}
}
}

View File

@ -1,31 +1,29 @@
package org.openrefine.wikidata.schema;
import java.text.ParseException;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell;
public class WbDateVariable extends WbDateExpr {
public static final String jsonType = "wbdatevariable";
private String _columnName;
private String columnName;
public WbDateVariable(String columnName) {
_columnName = columnName;
@JsonCreator
public WbDateVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override
public TimeValue evaluate(ExpressionContext ctxt)
throws SkipStatementException {
Cell cell = ctxt.getCellByName(_columnName);
Cell cell = ctxt.getCellByName(columnName);
if (cell != null) {
try {
// TODO accept parsed dates (without converting them to strings)
@ -35,20 +33,8 @@ public class WbDateVariable extends WbDateExpr {
}
throw new SkipStatementException();
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("columnName");
writer.value(_columnName);
}
public static WbDateVariable fromJSON(JSONObject obj) throws JSONException {
return new WbDateVariable(obj.getString("columnName"));
}
@Override
public String getJsonType() {
return jsonType;
public String getColumnName() {
return columnName;
}
}

View File

@ -1,47 +1,37 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.implementation.ItemIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbItemConstant extends WbItemExpr {
/* Represents an item that does not vary,
* it is independent of the row. */
public static final String jsonType = "wbitemconstant";
private String qid;
private String label;
public WbItemConstant(String qid, String label) {
@JsonCreator
public WbItemConstant(
@JsonProperty("qid") String qid,
@JsonProperty("label") String label) {
this.qid = qid;
this.label = label;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("qid");
writer.value(qid);
writer.key("label");
writer.value(label);
}
public static WbItemConstant fromJSON(JSONObject obj) throws JSONException {
return new WbItemConstant(obj.getString("qid"), obj.getString("label"));
}
@Override
public ItemIdValue evaluate(ExpressionContext ctxt) {
return ItemIdValueImpl.create(qid, ctxt.getBaseIRI());
}
public String getJsonType() {
return jsonType;
public String getQid() {
return qid;
}
public String getLabel() {
return label;
}
}

View File

@ -1,90 +1,54 @@
package org.openrefine.wikidata.schema;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.ItemDocumentBuilder;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbItemDocumentExpr extends BiJsonizable {
public static final String jsonType = "wbitemdocument";
public class WbItemDocumentExpr extends JacksonJsonizable {
private WbItemExpr subjectExpr;
private List<WbNameDescExpr> nameDescExprs;
private List<WbStatementGroupExpr> statementGroupExprs;
private WbItemExpr subject;
private List<WbNameDescExpr> nameDescs;
private List<WbStatementGroupExpr> statementGroups;
public WbItemDocumentExpr(WbItemExpr subjectExpr,
List<WbNameDescExpr> nameDescExprs,
List<WbStatementGroupExpr> statementGroupExprs) {
this.subjectExpr = subjectExpr;
this.nameDescExprs = nameDescExprs;
this.statementGroupExprs = statementGroupExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("subject");
subjectExpr.write(writer, options);
writer.key("nameDescs");
writer.array();
for(WbNameDescExpr expr : nameDescExprs) {
expr.write(writer, options);
}
writer.endArray();
writer.key("statementGroups");
writer.array();
for(WbStatementGroupExpr expr : statementGroupExprs) {
expr.write(writer, options);
}
writer.endArray();
}
public static WbItemDocumentExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject subjectObj = obj.getJSONObject("subject");
JSONArray statementsArr = obj.getJSONArray("statementGroups");
List<WbStatementGroupExpr> statementExprs = new ArrayList<WbStatementGroupExpr>();
for (int i = 0; i != statementsArr.length(); i++) {
statementExprs.add(WbStatementGroupExpr.fromJSON(statementsArr.getJSONObject(i)));
}
List<WbNameDescExpr> nameDescExprs = new ArrayList<WbNameDescExpr>();
if (obj.has("nameDescs")) { // for compatibility with earlier versions
JSONArray nameDescArr = obj.getJSONArray("nameDescs");
for (int i = 0; i != nameDescArr.length(); i++) {
nameDescExprs.add(WbNameDescExpr.fromJSON(nameDescArr.getJSONObject(i)));
}
}
return new WbItemDocumentExpr(
WbItemExpr.fromJSON(subjectObj),
nameDescExprs,
statementExprs);
@JsonCreator
public WbItemDocumentExpr(
@JsonProperty("subject") WbItemExpr subjectExpr,
@JsonProperty("nameDescs") List<WbNameDescExpr> nameDescExprs,
@JsonProperty("statementGroups") List<WbStatementGroupExpr> statementGroupExprs) {
this.subject = subjectExpr;
this.nameDescs = nameDescExprs;
this.statementGroups = statementGroupExprs;
}
public ItemUpdate evaluate(ExpressionContext ctxt) throws SkipStatementException {
ItemIdValue subjectId = subjectExpr.evaluate(ctxt);
ItemIdValue subjectId = getSubject().evaluate(ctxt);
ItemUpdate update = new ItemUpdate(subjectId);
for(WbStatementGroupExpr expr : statementGroupExprs) {
for(WbStatementGroupExpr expr : getStatementGroups()) {
for(Statement s : expr.evaluate(ctxt, subjectId).getStatements()) {
update.addStatement(s);
}
}
for(WbNameDescExpr expr : nameDescExprs) {
for(WbNameDescExpr expr : getNameDescs()) {
expr.contributeTo(update, ctxt);
}
return update;
}
public String getJsonType() {
return jsonType;
public WbItemExpr getSubject() {
return subject;
}
public List<WbNameDescExpr> getNameDescs() {
return nameDescs;
}
public List<WbStatementGroupExpr> getStatementGroups() {
return statementGroups;
}
}

View File

@ -1,22 +1,10 @@
package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.openrefine.wikidata.schema.ExpressionContext;
import org.openrefine.wikidata.schema.WbValueExpr;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
public abstract class WbItemExpr extends WbValueExpr {
public abstract ItemIdValue evaluate(ExpressionContext ctxt) throws SkipStatementException;
public static WbItemExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(jsonTypeKey);
if (WbItemConstant.jsonType.equals(type)) {
return WbItemConstant.fromJSON(obj);
} else if (WbItemVariable.jsonType.equals(type)) {
return WbItemVariable.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbItemExpr");
}
}
}

View File

@ -1,14 +1,12 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.implementation.ItemIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell;
import com.google.refine.model.Recon;
import com.google.refine.model.ReconCandidate;
@ -16,28 +14,17 @@ import com.google.refine.model.ReconCandidate;
public class WbItemVariable extends WbItemExpr {
/* An item that depends on a reconciled value in a column */
public static final String jsonType = "wbitemvariable";
private String columnName;
public WbItemVariable(String columnName) {
@JsonCreator
public WbItemVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("columnName");
writer.value(columnName);
}
public static WbItemVariable fromJSON(JSONObject obj) throws JSONException {
return new WbItemVariable(obj.getString("columnName"));
}
@Override
public ItemIdValue evaluate(ExpressionContext ctxt) throws SkipStatementException {
Cell cell = ctxt.getCellByName(columnName);
Cell cell = ctxt.getCellByName(getColumnName());
if (cell != null && cell.recon != null) {
Recon recon = cell.recon;
if (recon.judgment == Recon.Judgment.Matched && cell.recon.match != null) {
@ -45,13 +32,13 @@ public class WbItemVariable extends WbItemExpr {
return Datamodel.makeItemIdValue(match.id, ctxt.getBaseIRI());
} else if (recon.judgment == Recon.Judgment.New) {
return new NewEntityIdValue(ctxt.getRowId(),
ctxt.getCellIndexByName(columnName));
ctxt.getCellIndexByName(getColumnName()));
}
}
throw new SkipStatementException();
}
public String getJsonType() {
return jsonType;
public String getColumnName() {
return columnName;
}
}

View File

@ -1,12 +1,7 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbLanguageConstant extends WbLanguageExpr {
@ -15,30 +10,24 @@ public class WbLanguageConstant extends WbLanguageExpr {
protected String _langId;
protected String _langLabel;
@JsonCreator
public WbLanguageConstant(
@JsonProperty("lang") String langId,
@JsonProperty("label") String langLabel) {
_langId = langId;
_langLabel = langLabel;
}
public String evaluate(ExpressionContext ctxt) throws SkipStatementException {
return _langId;
}
public WbLanguageConstant(String langId, String langLabel) {
_langId = langId;
_langLabel = langLabel;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("id");
writer.value(_langId);
writer.key("label");
writer.value(_langLabel);
}
@Override
public String getJsonType() {
return jsonType;
public String getLang() {
return _langId;
}
public static WbLanguageExpr fromJSON(JSONObject obj) throws JSONException {
return new WbLanguageConstant(obj.getString("id"), obj.getString("label"));
public String getLabel() {
return _langLabel;
}
}

View File

@ -1,32 +1,14 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.Value;
public abstract class WbLanguageExpr extends BiJsonizable {
public abstract class WbLanguageExpr extends JacksonJsonizable {
/**
* Evaluates the language expression to a Wikimedia language code
*
* @param ctxt the evulation context
* @param ctxt the evaluation context
* @return a Wikimedia language code
* @throws SkipStatementException when the code is invalid
*/
public abstract String evaluate(ExpressionContext ctxt) throws SkipStatementException;
public static WbLanguageExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(jsonTypeKey);
if (WbLanguageConstant.jsonType.equals(type)) {
return WbLanguageConstant.fromJSON(obj);
} else if (WbLanguageVariable.jsonType.equals(type)) {
return WbLanguageVariable.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbLanguageExpr");
}
}
}

View File

@ -1,27 +1,25 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell;
public class WbLanguageVariable extends WbLanguageExpr {
public static final String jsonType = "wblanguagevariable";
private String _columnName;
private String columnName;
public WbLanguageVariable(String columnName) {
_columnName = columnName;
@JsonCreator
public WbLanguageVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override
public String evaluate(ExpressionContext ctxt)
throws SkipStatementException {
Cell cell = ctxt.getCellByName(_columnName);
Cell cell = ctxt.getCellByName(getColumnName());
if (cell != null) {
// TODO some validation here?
return cell.value.toString();
@ -29,20 +27,8 @@ public class WbLanguageVariable extends WbLanguageExpr {
throw new SkipStatementException();
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("columnName");
writer.value(_columnName);
}
public static WbLanguageVariable fromJSON(JSONObject obj) throws JSONException {
return new WbLanguageVariable(obj.getString("columnName"));
}
@Override
public String getJsonType() {
return jsonType;
public String getColumnName() {
return columnName;
}
}

View File

@ -1,24 +1,28 @@
package org.openrefine.wikidata.schema;
import java.text.ParseException;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbLocationConstant extends WbLocationExpr {
public static final String jsonType = "wblocationconstant";
private String _origValue;
private GlobeCoordinatesValue _parsed;
private String value;
private GlobeCoordinatesValue parsed;
public WbLocationConstant(String origValue) {
_origValue = origValue;
_parsed = null;
@JsonCreator
public WbLocationConstant(
@JsonProperty("value") String origValue) {
this.value = origValue;
try {
this.parsed = parse(origValue);
} catch (ParseException e) {
this.parsed = null;
}
}
public static GlobeCoordinatesValue parse(String expr) throws ParseException {
@ -45,24 +49,12 @@ public class WbLocationConstant extends WbLocationExpr {
@Override
public GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipStatementException {
if (_parsed == null)
if (parsed == null)
throw new SkipStatementException();
return _parsed;
return parsed;
}
public static WbLocationConstant fromJSON(JSONObject obj) throws JSONException {
return new WbLocationConstant(obj.getString("value"));
public String getValue() {
return value;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("value");
writer.value(_origValue);
}
@Override
public String getJsonType() {
return jsonType;
}
}

View File

@ -1,23 +1,9 @@
package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
public abstract class WbLocationExpr extends WbValueExpr {
@Override
public abstract GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipStatementException;
public static WbLocationExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(jsonTypeKey);
if (WbLocationConstant.jsonType.equals(type)) {
return WbLocationConstant.fromJSON(obj);
} else if (WbLocationVariable.jsonType.equals(type)) {
return WbLocationVariable.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbLocationExpr");
}
}
}

View File

@ -1,31 +1,29 @@
package org.openrefine.wikidata.schema;
import java.text.ParseException;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell;
public class WbLocationVariable extends WbLocationExpr {
public static final String jsonType = "wblocationvariable";
private String columnName;
public WbLocationVariable(String columnName) {
@JsonCreator
public WbLocationVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override
public GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipStatementException {
Cell cell = ctxt.getCellByName(columnName);
Cell cell = ctxt.getCellByName(getColumnName());
if (cell != null) {
String expr = cell.value.toString();
try {
@ -36,19 +34,7 @@ public class WbLocationVariable extends WbLocationExpr {
throw new SkipStatementException();
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("columnName");
writer.value(columnName);
}
public static WbLocationVariable fromJSON(JSONObject obj) throws JSONException {
return new WbLocationVariable(obj.getString("columnName"));
}
@Override
public String getJsonType() {
return jsonType;
public String getColumnName() {
return columnName;
}
}

View File

@ -1,52 +1,38 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbMonolingualExpr extends WbValueExpr {
public static final String jsonType = "wbmonolingualexpr";
private WbLanguageExpr languageExpr;
private WbStringExpr valueExpr;
protected WbLanguageExpr _languageExpr;
protected WbStringExpr _valueExpr;
public WbMonolingualExpr(WbLanguageExpr languageExpr, WbStringExpr valueExpr) {
_languageExpr = languageExpr;
_valueExpr = valueExpr;
@JsonCreator
public WbMonolingualExpr(
@JsonProperty("language") WbLanguageExpr languageExpr,
@JsonProperty("value") WbStringExpr valueExpr) {
this.languageExpr = languageExpr;
this.valueExpr = valueExpr;
}
@Override
public MonolingualTextValue evaluate(ExpressionContext ctxt)
throws SkipStatementException {
return Datamodel.makeMonolingualTextValue(
_valueExpr.evaluate(ctxt).getString(),
_languageExpr.evaluate(ctxt));
getValueExpr().evaluate(ctxt).getString(),
getLanguageExpr().evaluate(ctxt));
}
public static WbMonolingualExpr fromJSON(JSONObject obj) throws JSONException {
return new WbMonolingualExpr(
WbLanguageExpr.fromJSON(obj.getJSONObject("language")),
WbStringExpr.fromJSON(obj.getJSONObject("value")));
public WbLanguageExpr getLanguageExpr() {
return languageExpr;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("language");
_languageExpr.write(writer, options);
writer.key("value");
_valueExpr.write(writer, options);
public WbStringExpr getValueExpr() {
return valueExpr;
}
@Override
public String getJsonType() {
return jsonType;
}
}

View File

@ -1,16 +1,13 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbNameDescExpr extends BiJsonizable {
public final String jsonType = "wbnamedescexpr";
public class WbNameDescExpr extends JacksonJsonizable {
enum NameDescrType {
LABEL,
@ -18,27 +15,21 @@ public class WbNameDescExpr extends BiJsonizable {
ALIAS,
}
private NameDescrType _type;
private WbMonolingualExpr _value;
public WbNameDescExpr(NameDescrType type, WbMonolingualExpr value) {
_type = type;
_value = value;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("name_type");
writer.value(_type.name());
writer.key("value");
_value.write(writer, options);
private NameDescrType type;
private WbMonolingualExpr value;
@JsonCreator
public WbNameDescExpr(
@JsonProperty("type") NameDescrType type,
@JsonProperty("value") WbMonolingualExpr value) {
this.type = type;
this.value = value;
}
public void contributeTo(ItemUpdate item, ExpressionContext ctxt) {
try {
MonolingualTextValue val = _value.evaluate(ctxt);
switch (_type) {
MonolingualTextValue val = getValue().evaluate(ctxt);
switch (getType()) {
case LABEL:
item.addLabel(val);
break;
@ -54,14 +45,11 @@ public class WbNameDescExpr extends BiJsonizable {
}
}
@Override
public String getJsonType() {
return jsonType;
public NameDescrType getType() {
return type;
}
public static WbNameDescExpr fromJSON(JSONObject obj) throws JSONException {
return new WbNameDescExpr(NameDescrType.valueOf((String) obj.get("name_type")),
WbMonolingualExpr.fromJSON(obj.getJSONObject("value")));
public WbMonolingualExpr getValue() {
return value;
}
}

View File

@ -1,53 +1,45 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.implementation.PropertyIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbPropConstant extends WbPropExpr {
/* A constant property, that does not change depending on the row */
public static final String jsonType = "wbpropconstant";
private String pid;
private String label;
private String datatype;
public WbPropConstant(String pid, String label, String datatype) {
@JsonCreator
public WbPropConstant(
@JsonProperty("pid") String pid,
@JsonProperty("label") String label,
@JsonProperty("datatype") String datatype) {
this.pid = pid;
this.label = label;
this.datatype = datatype;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("pid");
writer.value(pid);
writer.key("label");
writer.value(label);
writer.key("datatype");
writer.value(datatype);
}
public static WbPropConstant fromJSON(JSONObject obj) throws JSONException {
return new WbPropConstant(
obj.getString("pid"),
obj.getString("label"),
obj.getString("datatype"));
}
@Override
public PropertyIdValue evaluate(ExpressionContext ctxt) {
return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI());
}
public String getJsonType() {
return jsonType;
public String getPid() {
return pid;
}
public String getLabel() {
return label;
}
public String getDatatype() {
return datatype;
}
}

View File

@ -4,7 +4,13 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
@JsonSubTypes({
@Type(value = WbPropConstant.class, name = "wbpropconstant")
})
public abstract class WbPropExpr extends WbValueExpr {
/* An expression that represents a property */

View File

@ -2,50 +2,29 @@ package org.openrefine.wikidata.schema;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbReferenceExpr extends BiJsonizable {
public static final String jsonType = "wbreferences";
public class WbReferenceExpr extends JacksonJsonizable {
private List<WbSnakExpr> snakExprs;
List<WbSnakExpr> snakExprs;
public WbReferenceExpr(List<WbSnakExpr> snakExprs) {
@JsonCreator
public WbReferenceExpr(
@JsonProperty("snaks") List<WbSnakExpr> snakExprs) {
this.snakExprs = snakExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("snaks");
writer.array();
for (WbSnakExpr expr : snakExprs) {
expr.write(writer, options);
}
writer.endArray();
}
public static WbReferenceExpr fromJSON(JSONObject obj) throws JSONException {
JSONArray arr = obj.getJSONArray("snaks");
List<WbSnakExpr> lst = new ArrayList<WbSnakExpr>(arr.length());
for (int i = 0; i != arr.length(); i++) {
lst.add(WbSnakExpr.fromJSON(arr.getJSONObject(i)));
}
return new WbReferenceExpr(lst);
}
public Reference evaluate(ExpressionContext ctxt) throws SkipStatementException {
List<SnakGroup> snakGroups = new ArrayList<SnakGroup>();
for (WbSnakExpr expr : snakExprs) {
for (WbSnakExpr expr : getSnaks()) {
List<Snak> snakList = new ArrayList<Snak>(1);
snakList.add(expr.evaluate(ctxt));
snakGroups.add(Datamodel.makeSnakGroup(snakList));
@ -53,9 +32,8 @@ public class WbReferenceExpr extends BiJsonizable {
return Datamodel.makeReference(snakGroups);
}
@Override
public String getJsonType() {
return jsonType;
public List<WbSnakExpr> getSnaks() {
return snakExprs;
}
}

View File

@ -1,51 +1,39 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import com.google.refine.Jsonizable;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbSnakExpr implements Jsonizable {
public class WbSnakExpr extends JacksonJsonizable {
private WbPropExpr propExpr;
private WbValueExpr valueExpr;
private WbPropExpr prop;
private WbValueExpr value;
public WbSnakExpr(WbPropExpr propExpr, WbValueExpr valueExpr) {
this.propExpr = propExpr;
this.valueExpr = valueExpr;
@JsonCreator
public WbSnakExpr(
@JsonProperty("prop") WbPropExpr propExpr,
@JsonProperty("value") WbValueExpr valueExpr) {
this.prop = propExpr;
this.value = valueExpr;
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("prop");
propExpr.write(writer, options);
writer.key("value");
valueExpr.write(writer, options);
writer.endObject();
}
public static WbSnakExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject propObj = obj.getJSONObject("prop");
WbPropExpr propExpr = WbPropConstant.fromJSON(propObj);
JSONObject valueObj = obj.getJSONObject("value");
WbValueExpr valueExpr = WbValueExpr.fromJSON(valueObj);
return new WbSnakExpr(propExpr, valueExpr);
}
public Snak evaluate(ExpressionContext ctxt) throws SkipStatementException {
PropertyIdValue propertyId = propExpr.evaluate(ctxt);
Value value = valueExpr.evaluate(ctxt);
return Datamodel.makeValueSnak(propertyId, value);
PropertyIdValue propertyId = getProp().evaluate(ctxt);
Value evaluatedValue = value.evaluate(ctxt);
return Datamodel.makeValueSnak(propertyId, evaluatedValue);
}
public WbPropExpr getProp() {
return prop;
}
public WbValueExpr getValue() {
return value;
}
}

View File

@ -10,6 +10,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
@ -21,66 +22,25 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStatementExpr extends BiJsonizable {
public static final String jsonType = "wbstatementexpr";
public class WbStatementExpr extends JacksonJsonizable {
private WbValueExpr mainSnakValueExpr;
private List<WbSnakExpr> qualifierExprs;
private List<WbReferenceExpr> referenceExprs;
// TODO: references
public WbStatementExpr(WbValueExpr mainSnakValueExpr,
List<WbSnakExpr> qualifierExprs,
List<WbReferenceExpr> referenceExprs) {
@JsonCreator
public WbStatementExpr(
@JsonProperty("value") WbValueExpr mainSnakValueExpr,
@JsonProperty("qualifiers") List<WbSnakExpr> qualifierExprs,
@JsonProperty("references") List<WbReferenceExpr> referenceExprs) {
this.mainSnakValueExpr = mainSnakValueExpr;
this.qualifierExprs = qualifierExprs;
this.referenceExprs = referenceExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("value");
mainSnakValueExpr.write(writer, options);
writer.key("qualifiers");
writer.array();
for (WbSnakExpr expr : qualifierExprs) {
expr.write(writer, options);
}
writer.endArray();
writer.key("references");
writer.array();
for (WbReferenceExpr expr : referenceExprs) {
expr.write(writer, options);
}
writer.endArray();
}
public static WbStatementExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject mainSnakObj = obj.getJSONObject("value");
List<WbSnakExpr> qualifierExprs = new ArrayList<WbSnakExpr>();
if (obj.has("qualifiers")) {
JSONArray qualifiersArr = obj.getJSONArray("qualifiers");
for (int i = 0; i != qualifiersArr.length(); i++) {
qualifierExprs.add(WbSnakExpr.fromJSON(qualifiersArr.getJSONObject(i)));
}
}
List<WbReferenceExpr> referenceExprs = new ArrayList<WbReferenceExpr>();
if (obj.has("references")) {
JSONArray referencesArr = obj.getJSONArray("references");
for (int i = 0; i != referencesArr.length(); i++) {
referenceExprs.add(WbReferenceExpr.fromJSON(referencesArr.getJSONObject(i)));
}
}
return new WbStatementExpr(
WbValueExpr.fromJSON(mainSnakObj),
qualifierExprs,
referenceExprs);
}
public static List<SnakGroup> groupSnaks(List<Snak> snaks) {
List<SnakGroup> snakGroups = new ArrayList<SnakGroup>();
@ -93,12 +53,12 @@ public class WbStatementExpr extends BiJsonizable {
}
public Statement evaluate(ExpressionContext ctxt, ItemIdValue subject, PropertyIdValue propertyId) throws SkipStatementException {
Value mainSnakValue = mainSnakValueExpr.evaluate(ctxt);
Value mainSnakValue = getMainsnak().evaluate(ctxt);
Snak mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue);
// evaluate qualifiers
List<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size());
for (WbSnakExpr qExpr : qualifierExprs) {
List<Snak> qualifiers = new ArrayList<Snak>(getQualifiers().size());
for (WbSnakExpr qExpr : getQualifiers()) {
qualifiers.add(qExpr.evaluate(ctxt));
}
List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
@ -106,7 +66,7 @@ public class WbStatementExpr extends BiJsonizable {
// evaluate references
List<Reference> references = new ArrayList<Reference>();
for (WbReferenceExpr rExpr : referenceExprs) {
for (WbReferenceExpr rExpr : getReferences()) {
references.add(rExpr.evaluate(ctxt));
}
@ -114,7 +74,16 @@ public class WbStatementExpr extends BiJsonizable {
return Datamodel.makeStatement(claim, references, rank, "");
}
public String getJsonType() {
return jsonType;
@JsonProperty("value")
public WbValueExpr getMainsnak() {
return mainSnakValueExpr;
}
public List<WbSnakExpr> getQualifiers() {
return qualifierExprs;
}
public List<WbReferenceExpr> getReferences() {
return referenceExprs;
}
}

View File

@ -2,67 +2,45 @@ package org.openrefine.wikidata.schema;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStatementGroupExpr extends BiJsonizable {
public static final String jsonType = "wbstatementgroupexpr";
public class WbStatementGroupExpr extends JacksonJsonizable {
private WbPropExpr propertyExpr;
private List<WbStatementExpr> claimExprs;
private List<WbStatementExpr> statementExprs;
public WbStatementGroupExpr(WbPropExpr propertyExpr, List<WbStatementExpr> claimExprs) {
@JsonCreator
public WbStatementGroupExpr(
@JsonProperty("property") WbPropExpr propertyExpr,
@JsonProperty("statements") List<WbStatementExpr> claimExprs) {
this.propertyExpr = propertyExpr;
this.claimExprs = claimExprs;
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("property");
propertyExpr.write(writer, options);
writer.key("statements");
writer.array();
for(WbStatementExpr expr : claimExprs) {
expr.write(writer, options);
}
writer.endArray();
}
public static WbStatementGroupExpr fromJSON(JSONObject obj) throws JSONException {
JSONObject propertyObj = obj.getJSONObject("property");
JSONArray claimsArr = obj.getJSONArray("statements");
List<WbStatementExpr> claimExprs = new ArrayList<WbStatementExpr>();
for (int i = 0; i != claimsArr.length(); i++) {
claimExprs.add(WbStatementExpr.fromJSON(claimsArr.getJSONObject(i)));
}
return new WbStatementGroupExpr(
WbPropExpr.fromJSON(propertyObj),
claimExprs);
this.statementExprs = claimExprs;
}
public StatementGroup evaluate(ExpressionContext ctxt, ItemIdValue subject) throws SkipStatementException {
PropertyIdValue propertyId = propertyExpr.evaluate(ctxt);
List<Statement> statements = new ArrayList<Statement>(claimExprs.size());
for(WbStatementExpr expr : claimExprs) {
List<Statement> statements = new ArrayList<Statement>(statementExprs.size());
for(WbStatementExpr expr : statementExprs) {
statements.add(expr.evaluate(ctxt, subject, propertyId));
}
// List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
return Datamodel.makeStatementGroup(statements);
}
public String getJsonType() {
return jsonType;
public WbPropExpr getProperty() {
return propertyExpr;
}
public List<WbStatementExpr> getStatements() {
return statementExprs;
}
}

View File

@ -1,40 +1,27 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStringConstant extends WbStringExpr {
public static final String jsonType = "wbstringconstant";
private String value;
public WbStringConstant(String value) {
@JsonCreator
public WbStringConstant(@JsonProperty("value") String value) {
this.value = value;
}
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("value");
writer.value(value);
}
public static WbStringConstant fromJSON(JSONObject obj) throws JSONException {
return new WbStringConstant(obj.getString("value"));
}
@Override
public StringValue evaluate(ExpressionContext ctxt) {
return Datamodel.makeStringValue(value);
}
public String getJsonType() {
return jsonType;
public String getValue() {
return value;
}
}

View File

@ -1,21 +1,7 @@
package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
public abstract class WbStringExpr extends WbValueExpr {
public abstract StringValue evaluate(ExpressionContext ctxt) throws SkipStatementException;
public static WbStringExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(jsonTypeKey);
if (WbStringConstant.jsonType.equals(type)) {
return WbStringConstant.fromJSON(obj);
} else if (WbStringVariable.jsonType.equals(type)) {
return WbStringVariable.fromJSON(obj);
} else {
throw new JSONException("unknown type for WbStringExpr");
}
}
}

View File

@ -1,13 +1,11 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell;
public class WbStringVariable extends WbStringExpr {
@ -15,7 +13,9 @@ public class WbStringVariable extends WbStringExpr {
private String columnName;
public WbStringVariable(String columnName) {
@JsonCreator
public WbStringVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@ -29,20 +29,7 @@ public class WbStringVariable extends WbStringExpr {
throw new SkipStatementException();
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("columnName");
writer.value(columnName);
public String getColumnName() {
return columnName;
}
public static WbStringExpr fromJSON(JSONObject obj) throws JSONException {
return new WbStringVariable(obj.getString("columnName"));
}
@Override
public String getJsonType() {
return jsonType;
}
}

View File

@ -3,9 +3,29 @@ package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
public abstract class WbValueExpr extends BiJsonizable {
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.PROPERTY,
property="type")
@JsonSubTypes({
@Type(value = WbStringConstant.class, name = "wbstringconstant"),
@Type(value = WbStringVariable.class, name = "wbstringvariable"),
@Type(value = WbLocationConstant.class, name = "wblocationconstant"),
@Type(value = WbLocationVariable.class, name = "wblocationvariable"),
@Type(value = WbItemConstant.class, name = "wbitemconstant"),
@Type(value = WbItemVariable.class, name = "wbitemvariable"),
@Type(value = WbLanguageConstant.class, name = "wblanguageconstant"),
@Type(value = WbLanguageVariable.class, name = "wblanguagevariable"),
@Type(value = WbDateConstant.class, name = "wbdateconstant"),
@Type(value = WbDateVariable.class, name = "wbdatevariable") ,
})
public abstract class WbValueExpr extends JacksonJsonizable {
/* An expression that represents a Wikibase value,
* i.e. anything that can be on the right-hand side
* of a statement.
@ -18,31 +38,6 @@ public abstract class WbValueExpr extends BiJsonizable {
public abstract Value evaluate(ExpressionContext ctxt) throws SkipStatementException;
public static WbValueExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(WbValueExpr.jsonTypeKey);
WbValueExpr valueExpr = null;
if (WbPropConstant.jsonType.equals(type)) {
valueExpr = WbPropConstant.fromJSON(obj);
} else if (WbItemConstant.jsonType.equals(type)) {
valueExpr = WbItemConstant.fromJSON(obj);
} else if (WbItemVariable.jsonType.equals(type)) {
valueExpr = WbItemVariable.fromJSON(obj);
} else if (WbStringVariable.jsonType.equals(type)) {
valueExpr = WbStringVariable.fromJSON(obj);
} else if (WbStringConstant.jsonType.equals(type)) {
valueExpr = WbStringConstant.fromJSON(obj);
} else if (WbDateVariable.jsonType.equals(type)) {
valueExpr = WbDateVariable.fromJSON(obj);
} else if (WbDateConstant.jsonType.equals(type)) {
valueExpr = WbDateConstant.fromJSON(obj);
} else if (WbLocationVariable.jsonType.equals(type)) {
valueExpr = WbLocationVariable.fromJSON(obj);
} else if (WbLocationConstant.jsonType.equals(type)) {
valueExpr = WbLocationConstant.fromJSON(obj);
} else if (WbMonolingualExpr.jsonType.equals(type)) {
valueExpr = WbMonolingualExpr.fromJSON(obj);
} else {
throw new JSONException("unknown type '"+type+"' for WbValueExpr");
}
return valueExpr;
return fromJSONClass(obj, WbValueExpr.class);
}
}

View File

@ -1,5 +1,6 @@
package org.openrefine.wikidata.schema;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
@ -12,19 +13,26 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.refine.browsing.Engine;
import com.google.refine.browsing.FilteredRows;
import com.google.refine.browsing.RowVisitor;
import com.google.refine.model.OverlayModel;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
import org.openrefine.wikidata.schema.WbItemDocumentExpr;
import org.openrefine.wikidata.schema.ExpressionContext;
import org.openrefine.wikidata.utils.JacksonJsonizable;
public class WikibaseSchema implements OverlayModel {
final static Logger logger = LoggerFactory.getLogger("RdfSchema");
final protected List<WbItemDocumentExpr> itemDocumentExprs = new ArrayList<WbItemDocumentExpr>();
protected List<WbItemDocumentExpr> itemDocumentExprs = new ArrayList<WbItemDocumentExpr>();
protected String baseUri = "http://www.wikidata.org/entity/";
@ -58,6 +66,10 @@ public class WikibaseSchema implements OverlayModel {
return itemDocumentExprs;
}
public void setItemDocumentExpressions(List<WbItemDocumentExpr> exprs) {
this.itemDocumentExprs = exprs;
}
/**
* Evaluates all item documents in a particular expression context.
* @param ctxt
@ -108,10 +120,11 @@ public class WikibaseSchema implements OverlayModel {
}
static public WikibaseSchema reconstruct(JSONObject o) throws JSONException {
JSONArray changeArr = o.getJSONArray("itemDocuments");
WikibaseSchema schema = new WikibaseSchema();
for (int i = 0; i != changeArr.length(); i++) {
WbItemDocumentExpr changeExpr = WbItemDocumentExpr.fromJSON(changeArr.getJSONObject(i));
WbItemDocumentExpr changeExpr = JacksonJsonizable.fromJSONClass(changeArr.getJSONObject(i), WbItemDocumentExpr.class);
schema.itemDocumentExprs.add(changeExpr);
}
return schema;