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"); JSONObject engineConfig = obj.getJSONObject("engineConfig");
String strategy = obj.getString("duplicate_strategy"); String strategy = obj.getString("duplicate_strategy");
String action = obj.getString("duplicate_action"); String action = obj.getString("duplicate_action");
String summary = obj.getString("summary"); String summary = null;
if (summary == null) { if (obj.has("summary")) {
summary = "#openrefine"; summary = obj.getString("summary");
} }
return new PerformWikibaseEditsOperation( return new PerformWikibaseEditsOperation(
engineConfig, engineConfig,

View File

@ -3,13 +3,22 @@ package org.openrefine.wikidata.operations;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; 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 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.Change;
import com.google.refine.history.HistoryEntry; import com.google.refine.history.HistoryEntry;
import com.google.refine.model.AbstractOperation; import com.google.refine.model.AbstractOperation;
@ -24,10 +33,20 @@ public class SaveWikibaseSchemaOperation extends AbstractOperation {
public SaveWikibaseSchemaOperation(WikibaseSchema schema) { public SaveWikibaseSchemaOperation(WikibaseSchema schema) {
this._schema = schema; this._schema = schema;
} }
static public AbstractOperation reconstruct(Project project, JSONObject obj) static public AbstractOperation reconstruct(Project project, JSONObject obj)
throws Exception { 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 return new SaveWikibaseSchemaOperation(WikibaseSchema.reconstruct(obj
.getJSONObject("schema"))); .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,21 +4,19 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Properties;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; 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.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue; 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; import com.google.common.collect.ImmutableMap;
public class WbDateConstant extends WbDateExpr { public class WbDateConstant extends WbDateExpr {
public static final String jsonType = "wbdateconstant";
public static Map<SimpleDateFormat,Integer> acceptedFormats = ImmutableMap.<SimpleDateFormat,Integer>builder() public static Map<SimpleDateFormat,Integer> acceptedFormats = ImmutableMap.<SimpleDateFormat,Integer>builder()
.put(new SimpleDateFormat("yyyy"), 9) .put(new SimpleDateFormat("yyyy"), 9)
@ -29,25 +27,22 @@ public class WbDateConstant extends WbDateExpr {
.put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), 14) .put(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"), 14)
.build(); .build();
private TimeValue _parsed; private TimeValue parsed;
private String _origDatestamp; private String origDatestamp;
public WbDateConstant(String origDatestamp) { @JsonCreator
_origDatestamp = origDatestamp; public WbDateConstant(
try { @JsonProperty("value") String origDatestamp) {
_parsed = parse(origDatestamp); this.setOrigDatestamp(origDatestamp);
} catch(ParseException e) {
_parsed = null;
}
} }
@Override @Override
public TimeValue evaluate(ExpressionContext ctxt) public TimeValue evaluate(ExpressionContext ctxt)
throws SkipStatementException { throws SkipStatementException {
if (_parsed == null) { if (parsed == null) {
throw new SkipStatementException(); throw new SkipStatementException();
} }
return _parsed; return parsed;
} }
public static TimeValue parse(String datestamp) throws ParseException { public static TimeValue parse(String datestamp) throws ParseException {
@ -82,20 +77,19 @@ public class WbDateConstant extends WbDateExpr {
} }
} }
@Override @JsonProperty("value")
public void writeFields(JSONWriter writer, Properties options) public String getOrigDatestamp() {
throws JSONException { return origDatestamp;
writer.key("value");
writer.value(_origDatestamp);
} }
public static WbDateConstant fromJSON(JSONObject obj) throws JSONException { private void setOrigDatestamp(String origDatestamp) {
return new WbDateConstant(obj.getString("value")); this.origDatestamp = origDatestamp;
try {
this.parsed = parse(origDatestamp);
} catch(ParseException e) {
this.parsed = null;
}
} }
@Override
public String getJsonType() {
return jsonType;
}
} }

View File

@ -1,25 +1,10 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue; import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
public abstract class WbDateExpr extends WbValueExpr { public abstract class WbDateExpr extends WbValueExpr {
@Override @Override
public abstract TimeValue evaluate(ExpressionContext ctxt) public abstract TimeValue evaluate(ExpressionContext ctxt)
throws SkipStatementException; 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; package org.openrefine.wikidata.schema;
import java.text.ParseException; 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 org.wikidata.wdtk.datamodel.interfaces.TimeValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
public class WbDateVariable extends WbDateExpr { public class WbDateVariable extends WbDateExpr {
public static final String jsonType = "wbdatevariable";
private String _columnName; private String columnName;
public WbDateVariable(String columnName) { @JsonCreator
_columnName = columnName; public WbDateVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
} }
@Override @Override
public TimeValue evaluate(ExpressionContext ctxt) public TimeValue evaluate(ExpressionContext ctxt)
throws SkipStatementException { throws SkipStatementException {
Cell cell = ctxt.getCellByName(_columnName); Cell cell = ctxt.getCellByName(columnName);
if (cell != null) { if (cell != null) {
try { try {
// TODO accept parsed dates (without converting them to strings) // TODO accept parsed dates (without converting them to strings)
@ -36,19 +34,7 @@ public class WbDateVariable extends WbDateExpr {
throw new SkipStatementException(); throw new SkipStatementException();
} }
@Override public String getColumnName() {
public void writeFields(JSONWriter writer, Properties options) return columnName;
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;
} }
} }

View File

@ -1,47 +1,37 @@
package org.openrefine.wikidata.schema; 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.implementation.ItemIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbItemConstant extends WbItemExpr { public class WbItemConstant extends WbItemExpr {
/* Represents an item that does not vary, /* Represents an item that does not vary,
* it is independent of the row. */ * it is independent of the row. */
public static final String jsonType = "wbitemconstant";
private String qid; private String qid;
private String label; private String label;
public WbItemConstant(String qid, String label) { @JsonCreator
public WbItemConstant(
@JsonProperty("qid") String qid,
@JsonProperty("label") String label) {
this.qid = qid; this.qid = qid;
this.label = label; 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 @Override
public ItemIdValue evaluate(ExpressionContext ctxt) { public ItemIdValue evaluate(ExpressionContext ctxt) {
return ItemIdValueImpl.create(qid, ctxt.getBaseIRI()); return ItemIdValueImpl.create(qid, ctxt.getBaseIRI());
} }
public String getJsonType() { public String getQid() {
return jsonType; return qid;
}
public String getLabel() {
return label;
} }
} }

View File

@ -1,90 +1,54 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import org.json.JSONArray; import org.openrefine.wikidata.utils.JacksonJsonizable;
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.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; 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 class WbItemDocumentExpr extends JacksonJsonizable {
public static final String jsonType = "wbitemdocument"; private WbItemExpr subject;
private List<WbNameDescExpr> nameDescs;
private List<WbStatementGroupExpr> statementGroups;
private WbItemExpr subjectExpr; @JsonCreator
private List<WbNameDescExpr> nameDescExprs; public WbItemDocumentExpr(
private List<WbStatementGroupExpr> statementGroupExprs; @JsonProperty("subject") WbItemExpr subjectExpr,
@JsonProperty("nameDescs") List<WbNameDescExpr> nameDescExprs,
public WbItemDocumentExpr(WbItemExpr subjectExpr, @JsonProperty("statementGroups") List<WbStatementGroupExpr> statementGroupExprs) {
List<WbNameDescExpr> nameDescExprs, this.subject = subjectExpr;
List<WbStatementGroupExpr> statementGroupExprs) { this.nameDescs = nameDescExprs;
this.subjectExpr = subjectExpr; this.statementGroups = statementGroupExprs;
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);
} }
public ItemUpdate evaluate(ExpressionContext ctxt) throws SkipStatementException { public ItemUpdate evaluate(ExpressionContext ctxt) throws SkipStatementException {
ItemIdValue subjectId = subjectExpr.evaluate(ctxt); ItemIdValue subjectId = getSubject().evaluate(ctxt);
ItemUpdate update = new ItemUpdate(subjectId); ItemUpdate update = new ItemUpdate(subjectId);
for(WbStatementGroupExpr expr : statementGroupExprs) { for(WbStatementGroupExpr expr : getStatementGroups()) {
for(Statement s : expr.evaluate(ctxt, subjectId).getStatements()) { for(Statement s : expr.evaluate(ctxt, subjectId).getStatements()) {
update.addStatement(s); update.addStatement(s);
} }
} }
for(WbNameDescExpr expr : nameDescExprs) { for(WbNameDescExpr expr : getNameDescs()) {
expr.contributeTo(update, ctxt); expr.contributeTo(update, ctxt);
} }
return update; return update;
} }
public String getJsonType() { public WbItemExpr getSubject() {
return jsonType; 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; package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.openrefine.wikidata.schema.ExpressionContext; import org.openrefine.wikidata.schema.ExpressionContext;
import org.openrefine.wikidata.schema.WbValueExpr; import org.openrefine.wikidata.schema.WbValueExpr;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
public abstract class WbItemExpr extends WbValueExpr { public abstract class WbItemExpr extends WbValueExpr {
public abstract ItemIdValue evaluate(ExpressionContext ctxt) throws SkipStatementException; 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; 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.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.implementation.ItemIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; 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.Cell;
import com.google.refine.model.Recon; import com.google.refine.model.Recon;
import com.google.refine.model.ReconCandidate; import com.google.refine.model.ReconCandidate;
@ -16,28 +14,17 @@ import com.google.refine.model.ReconCandidate;
public class WbItemVariable extends WbItemExpr { public class WbItemVariable extends WbItemExpr {
/* An item that depends on a reconciled value in a column */ /* An item that depends on a reconciled value in a column */
public static final String jsonType = "wbitemvariable";
private String columnName; private String columnName;
public WbItemVariable(String columnName) { @JsonCreator
public WbItemVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = 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 @Override
public ItemIdValue evaluate(ExpressionContext ctxt) throws SkipStatementException { public ItemIdValue evaluate(ExpressionContext ctxt) throws SkipStatementException {
Cell cell = ctxt.getCellByName(columnName); Cell cell = ctxt.getCellByName(getColumnName());
if (cell != null && cell.recon != null) { if (cell != null && cell.recon != null) {
Recon recon = cell.recon; Recon recon = cell.recon;
if (recon.judgment == Recon.Judgment.Matched && cell.recon.match != null) { 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()); return Datamodel.makeItemIdValue(match.id, ctxt.getBaseIRI());
} else if (recon.judgment == Recon.Judgment.New) { } else if (recon.judgment == Recon.Judgment.New) {
return new NewEntityIdValue(ctxt.getRowId(), return new NewEntityIdValue(ctxt.getRowId(),
ctxt.getCellIndexByName(columnName)); ctxt.getCellIndexByName(getColumnName()));
} }
} }
throw new SkipStatementException(); throw new SkipStatementException();
} }
public String getJsonType() { public String getColumnName() {
return jsonType; return columnName;
} }
} }

View File

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

View File

@ -1,32 +1,14 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.util.Properties; import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.json.JSONException; public abstract class WbLanguageExpr extends JacksonJsonizable {
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 {
/** /**
* Evaluates the language expression to a Wikimedia language code * Evaluates the language expression to a Wikimedia language code
* *
* @param ctxt the evulation context * @param ctxt the evaluation context
* @return a Wikimedia language code * @return a Wikimedia language code
* @throws SkipStatementException when the code is invalid * @throws SkipStatementException when the code is invalid
*/ */
public abstract String evaluate(ExpressionContext ctxt) throws SkipStatementException; 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; package org.openrefine.wikidata.schema;
import java.util.Properties; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
public class WbLanguageVariable extends WbLanguageExpr { public class WbLanguageVariable extends WbLanguageExpr {
public static final String jsonType = "wblanguagevariable";
private String _columnName; private String columnName;
public WbLanguageVariable(String columnName) { @JsonCreator
_columnName = columnName; public WbLanguageVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
} }
@Override @Override
public String evaluate(ExpressionContext ctxt) public String evaluate(ExpressionContext ctxt)
throws SkipStatementException { throws SkipStatementException {
Cell cell = ctxt.getCellByName(_columnName); Cell cell = ctxt.getCellByName(getColumnName());
if (cell != null) { if (cell != null) {
// TODO some validation here? // TODO some validation here?
return cell.value.toString(); return cell.value.toString();
@ -29,20 +27,8 @@ public class WbLanguageVariable extends WbLanguageExpr {
throw new SkipStatementException(); throw new SkipStatementException();
} }
@Override public String getColumnName() {
public void writeFields(JSONWriter writer, Properties options) return columnName;
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;
} }
} }

View File

@ -1,24 +1,28 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import java.text.ParseException; 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.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue; 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 class WbLocationConstant extends WbLocationExpr {
public static final String jsonType = "wblocationconstant";
private String _origValue; private String value;
private GlobeCoordinatesValue _parsed; private GlobeCoordinatesValue parsed;
public WbLocationConstant(String origValue) { @JsonCreator
_origValue = origValue; public WbLocationConstant(
_parsed = null; @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 { public static GlobeCoordinatesValue parse(String expr) throws ParseException {
@ -45,24 +49,12 @@ public class WbLocationConstant extends WbLocationExpr {
@Override @Override
public GlobeCoordinatesValue evaluate(ExpressionContext ctxt) public GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipStatementException { throws SkipStatementException {
if (_parsed == null) if (parsed == null)
throw new SkipStatementException(); throw new SkipStatementException();
return _parsed; return parsed;
} }
public static WbLocationConstant fromJSON(JSONObject obj) throws JSONException { public String getValue() {
return new WbLocationConstant(obj.getString("value")); 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; package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue; import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
public abstract class WbLocationExpr extends WbValueExpr { public abstract class WbLocationExpr extends WbValueExpr {
@Override @Override
public abstract GlobeCoordinatesValue evaluate(ExpressionContext ctxt) public abstract GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipStatementException; 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; package org.openrefine.wikidata.schema;
import java.text.ParseException; 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.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; import com.google.refine.model.Cell;
public class WbLocationVariable extends WbLocationExpr { public class WbLocationVariable extends WbLocationExpr {
public static final String jsonType = "wblocationvariable";
private String columnName; private String columnName;
public WbLocationVariable(String columnName) { @JsonCreator
public WbLocationVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName; this.columnName = columnName;
} }
@Override @Override
public GlobeCoordinatesValue evaluate(ExpressionContext ctxt) public GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipStatementException { throws SkipStatementException {
Cell cell = ctxt.getCellByName(columnName); Cell cell = ctxt.getCellByName(getColumnName());
if (cell != null) { if (cell != null) {
String expr = cell.value.toString(); String expr = cell.value.toString();
try { try {
@ -36,19 +34,7 @@ public class WbLocationVariable extends WbLocationExpr {
throw new SkipStatementException(); throw new SkipStatementException();
} }
@Override public String getColumnName() {
public void writeFields(JSONWriter writer, Properties options) return columnName;
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;
} }
} }

View File

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

View File

@ -1,16 +1,13 @@
package org.openrefine.wikidata.schema; 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.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 { enum NameDescrType {
LABEL, LABEL,
@ -18,27 +15,21 @@ public class WbNameDescExpr extends BiJsonizable {
ALIAS, ALIAS,
} }
private NameDescrType _type; private NameDescrType type;
private WbMonolingualExpr _value; private WbMonolingualExpr value;
public WbNameDescExpr(NameDescrType type, WbMonolingualExpr value) { @JsonCreator
_type = type; public WbNameDescExpr(
_value = value; @JsonProperty("type") NameDescrType type,
} @JsonProperty("value") WbMonolingualExpr value) {
this.type = type;
@Override this.value = value;
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("name_type");
writer.value(_type.name());
writer.key("value");
_value.write(writer, options);
} }
public void contributeTo(ItemUpdate item, ExpressionContext ctxt) { public void contributeTo(ItemUpdate item, ExpressionContext ctxt) {
try { try {
MonolingualTextValue val = _value.evaluate(ctxt); MonolingualTextValue val = getValue().evaluate(ctxt);
switch (_type) { switch (getType()) {
case LABEL: case LABEL:
item.addLabel(val); item.addLabel(val);
break; break;
@ -54,14 +45,11 @@ public class WbNameDescExpr extends BiJsonizable {
} }
} }
@Override public NameDescrType getType() {
public String getJsonType() { return type;
return jsonType;
} }
public static WbNameDescExpr fromJSON(JSONObject obj) throws JSONException { public WbMonolingualExpr getValue() {
return new WbNameDescExpr(NameDescrType.valueOf((String) obj.get("name_type")), return value;
WbMonolingualExpr.fromJSON(obj.getJSONObject("value")));
} }
} }

View File

@ -1,53 +1,45 @@
package org.openrefine.wikidata.schema; 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.implementation.PropertyIdValueImpl;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class WbPropConstant extends WbPropExpr { public class WbPropConstant extends WbPropExpr {
/* A constant property, that does not change depending on the row */ /* A constant property, that does not change depending on the row */
public static final String jsonType = "wbpropconstant";
private String pid; private String pid;
private String label; private String label;
private String datatype; 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.pid = pid;
this.label = label; this.label = label;
this.datatype = datatype; 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 @Override
public PropertyIdValue evaluate(ExpressionContext ctxt) { public PropertyIdValue evaluate(ExpressionContext ctxt) {
return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI()); return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI());
} }
public String getJsonType() { public String getPid() {
return jsonType; 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.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; 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 { public abstract class WbPropExpr extends WbValueExpr {
/* An expression that represents a property */ /* An expression that represents a property */

View File

@ -2,50 +2,29 @@ package org.openrefine.wikidata.schema;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import org.json.JSONArray; import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Reference; import org.wikidata.wdtk.datamodel.interfaces.Reference;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; 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";
List<WbSnakExpr> snakExprs; public class WbReferenceExpr extends JacksonJsonizable {
private List<WbSnakExpr> snakExprs;
public WbReferenceExpr(List<WbSnakExpr> snakExprs) { @JsonCreator
public WbReferenceExpr(
@JsonProperty("snaks") List<WbSnakExpr> snakExprs) {
this.snakExprs = 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 { public Reference evaluate(ExpressionContext ctxt) throws SkipStatementException {
List<SnakGroup> snakGroups = new ArrayList<SnakGroup>(); List<SnakGroup> snakGroups = new ArrayList<SnakGroup>();
for (WbSnakExpr expr : snakExprs) { for (WbSnakExpr expr : getSnaks()) {
List<Snak> snakList = new ArrayList<Snak>(1); List<Snak> snakList = new ArrayList<Snak>(1);
snakList.add(expr.evaluate(ctxt)); snakList.add(expr.evaluate(ctxt));
snakGroups.add(Datamodel.makeSnakGroup(snakList)); snakGroups.add(Datamodel.makeSnakGroup(snakList));
@ -53,9 +32,8 @@ public class WbReferenceExpr extends BiJsonizable {
return Datamodel.makeReference(snakGroups); return Datamodel.makeReference(snakGroups);
} }
@Override public List<WbSnakExpr> getSnaks() {
public String getJsonType() { return snakExprs;
return jsonType;
} }
} }

View File

@ -1,51 +1,39 @@
package org.openrefine.wikidata.schema; 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.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Value; 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 WbPropExpr prop;
private WbValueExpr valueExpr; private WbValueExpr value;
public WbSnakExpr(WbPropExpr propExpr, WbValueExpr valueExpr) { @JsonCreator
this.propExpr = propExpr; public WbSnakExpr(
this.valueExpr = valueExpr; @JsonProperty("prop") WbPropExpr propExpr,
} @JsonProperty("value") WbValueExpr valueExpr) {
this.prop = propExpr;
@Override this.value = valueExpr;
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 { public Snak evaluate(ExpressionContext ctxt) throws SkipStatementException {
PropertyIdValue propertyId = propExpr.evaluate(ctxt); PropertyIdValue propertyId = getProp().evaluate(ctxt);
Value value = valueExpr.evaluate(ctxt); Value evaluatedValue = value.evaluate(ctxt);
return Datamodel.makeValueSnak(propertyId, value); 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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.Claim; import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
@ -21,67 +22,26 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementRank; import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
import org.wikidata.wdtk.datamodel.interfaces.Value; 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 WbValueExpr mainSnakValueExpr;
private List<WbSnakExpr> qualifierExprs; private List<WbSnakExpr> qualifierExprs;
private List<WbReferenceExpr> referenceExprs; private List<WbReferenceExpr> referenceExprs;
// TODO: references
public WbStatementExpr(WbValueExpr mainSnakValueExpr, @JsonCreator
List<WbSnakExpr> qualifierExprs, public WbStatementExpr(
List<WbReferenceExpr> referenceExprs) { @JsonProperty("value") WbValueExpr mainSnakValueExpr,
@JsonProperty("qualifiers") List<WbSnakExpr> qualifierExprs,
@JsonProperty("references") List<WbReferenceExpr> referenceExprs) {
this.mainSnakValueExpr = mainSnakValueExpr; this.mainSnakValueExpr = mainSnakValueExpr;
this.qualifierExprs = qualifierExprs; this.qualifierExprs = qualifierExprs;
this.referenceExprs = referenceExprs; 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) { public static List<SnakGroup> groupSnaks(List<Snak> snaks) {
List<SnakGroup> snakGroups = new ArrayList<SnakGroup>(); List<SnakGroup> snakGroups = new ArrayList<SnakGroup>();
for (Snak snak : snaks) { for (Snak snak : snaks) {
@ -93,12 +53,12 @@ public class WbStatementExpr extends BiJsonizable {
} }
public Statement evaluate(ExpressionContext ctxt, ItemIdValue subject, PropertyIdValue propertyId) throws SkipStatementException { 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); Snak mainSnak = Datamodel.makeValueSnak(propertyId, mainSnakValue);
// evaluate qualifiers // evaluate qualifiers
List<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size()); List<Snak> qualifiers = new ArrayList<Snak>(getQualifiers().size());
for (WbSnakExpr qExpr : qualifierExprs) { for (WbSnakExpr qExpr : getQualifiers()) {
qualifiers.add(qExpr.evaluate(ctxt)); qualifiers.add(qExpr.evaluate(ctxt));
} }
List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers); List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
@ -106,7 +66,7 @@ public class WbStatementExpr extends BiJsonizable {
// evaluate references // evaluate references
List<Reference> references = new ArrayList<Reference>(); List<Reference> references = new ArrayList<Reference>();
for (WbReferenceExpr rExpr : referenceExprs) { for (WbReferenceExpr rExpr : getReferences()) {
references.add(rExpr.evaluate(ctxt)); references.add(rExpr.evaluate(ctxt));
} }
@ -114,7 +74,16 @@ public class WbStatementExpr extends BiJsonizable {
return Datamodel.makeStatement(claim, references, rank, ""); return Datamodel.makeStatement(claim, references, rank, "");
} }
public String getJsonType() { @JsonProperty("value")
return jsonType; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import org.json.JSONArray; import org.openrefine.wikidata.utils.JacksonJsonizable;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.StatementGroup; 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 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.propertyExpr = propertyExpr;
this.claimExprs = claimExprs; this.statementExprs = 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);
} }
public StatementGroup evaluate(ExpressionContext ctxt, ItemIdValue subject) throws SkipStatementException { public StatementGroup evaluate(ExpressionContext ctxt, ItemIdValue subject) throws SkipStatementException {
PropertyIdValue propertyId = propertyExpr.evaluate(ctxt); PropertyIdValue propertyId = propertyExpr.evaluate(ctxt);
List<Statement> statements = new ArrayList<Statement>(claimExprs.size()); List<Statement> statements = new ArrayList<Statement>(statementExprs.size());
for(WbStatementExpr expr : claimExprs) { for(WbStatementExpr expr : statementExprs) {
statements.add(expr.evaluate(ctxt, subject, propertyId)); statements.add(expr.evaluate(ctxt, subject, propertyId));
} }
// List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
return Datamodel.makeStatementGroup(statements); return Datamodel.makeStatementGroup(statements);
} }
public String getJsonType() { public WbPropExpr getProperty() {
return jsonType; return propertyExpr;
}
public List<WbStatementExpr> getStatements() {
return statementExprs;
} }
} }

View File

@ -1,40 +1,27 @@
package org.openrefine.wikidata.schema; 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.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue; 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 class WbStringConstant extends WbStringExpr {
public static final String jsonType = "wbstringconstant";
private String value; private String value;
public WbStringConstant(String value) { @JsonCreator
public WbStringConstant(@JsonProperty("value") String value) {
this.value = 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 @Override
public StringValue evaluate(ExpressionContext ctxt) { public StringValue evaluate(ExpressionContext ctxt) {
return Datamodel.makeStringValue(value); return Datamodel.makeStringValue(value);
} }
public String getJsonType() { public String getValue() {
return jsonType; return value;
} }
} }

View File

@ -1,21 +1,7 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.json.JSONException;
import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.StringValue; import org.wikidata.wdtk.datamodel.interfaces.StringValue;
public abstract class WbStringExpr extends WbValueExpr { public abstract class WbStringExpr extends WbValueExpr {
public abstract StringValue evaluate(ExpressionContext ctxt) throws SkipStatementException; 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; 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.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue; 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; import com.google.refine.model.Cell;
public class WbStringVariable extends WbStringExpr { public class WbStringVariable extends WbStringExpr {
@ -15,7 +13,9 @@ public class WbStringVariable extends WbStringExpr {
private String columnName; private String columnName;
public WbStringVariable(String columnName) { @JsonCreator
public WbStringVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName; this.columnName = columnName;
} }
@ -29,20 +29,7 @@ public class WbStringVariable extends WbStringExpr {
throw new SkipStatementException(); throw new SkipStatementException();
} }
@Override public String getColumnName() {
public void writeFields(JSONWriter writer, Properties options) return columnName;
throws JSONException {
writer.key("columnName");
writer.value(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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.wikidata.wdtk.datamodel.interfaces.Value; 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, /* An expression that represents a Wikibase value,
* i.e. anything that can be on the right-hand side * i.e. anything that can be on the right-hand side
* of a statement. * of a statement.
@ -18,31 +38,6 @@ public abstract class WbValueExpr extends BiJsonizable {
public abstract Value evaluate(ExpressionContext ctxt) throws SkipStatementException; public abstract Value evaluate(ExpressionContext ctxt) throws SkipStatementException;
public static WbValueExpr fromJSON(JSONObject obj) throws JSONException { public static WbValueExpr fromJSON(JSONObject obj) throws JSONException {
String type = obj.getString(WbValueExpr.jsonTypeKey); return fromJSONClass(obj, WbValueExpr.class);
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;
} }
} }

View File

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