diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/ExpressionContext.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/ExpressionContext.java new file mode 100644 index 000000000..25d0df613 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/ExpressionContext.java @@ -0,0 +1,27 @@ +package org.openrefine.wikidata.schema; + +import com.google.refine.model.Cell; +import com.google.refine.model.ColumnModel; +import com.google.refine.model.Row; + + +public class ExpressionContext { + private String baseIRI; + private Row row; + private ColumnModel columnModel; + + public ExpressionContext(String baseIRI, Row row, ColumnModel columnModel) { + this.baseIRI = baseIRI; + this.row = row; + this.columnModel = columnModel; + } + + public String getBaseIRI() { + return baseIRI; + } + + public Cell getCellByName(String name) { + int idx = columnModel.getColumnByName(name).getCellIndex(); + return row.getCell(idx); + } +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbChangeExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbChangeExpr.java new file mode 100644 index 000000000..a3eb4b587 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbChangeExpr.java @@ -0,0 +1,10 @@ +package org.openrefine.wikidata.schema; + +import com.google.refine.Jsonizable; + + +public interface WbChangeExpr extends Jsonizable { + /* Represents a change on an item: adding a statement, + * adding a label, adding a sitelink… + */ +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbClaimExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbClaimExpr.java new file mode 100644 index 000000000..f4e41dea0 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbClaimExpr.java @@ -0,0 +1,68 @@ +package org.openrefine.wikidata.schema; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.wikidata.wdtk.datamodel.helpers.Datamodel; +import org.wikidata.wdtk.datamodel.interfaces.Claim; +import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; +import org.wikidata.wdtk.datamodel.interfaces.Snak; +import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; +import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; + + +public class WbClaimExpr implements WbChangeExpr { + + private WbItemExpr subjectExpr; + private WbSnakExpr mainSnakExpr; + private List qualifierExprs; + // TODO: references + + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("subject"); + subjectExpr.write(writer, options); + writer.key("mainsnak"); + mainSnakExpr.write(writer, options); + writer.key("qualifiers"); + writer.array(); + for (WbSnakExpr expr : qualifierExprs) { + expr.write(writer, options); + } + writer.endArray(); + writer.endObject(); + } + + public static List groupSnaks(List snaks) { + Map > map = new HashMap>(); + for (Snak snak : snaks) { + PropertyIdValue pid = snak.getPropertyId(); + if (!map.containsKey(pid)) { + map.put(pid, new ArrayList()); + } + map.get(pid).add(snak); + } + List snakGroups = new ArrayList(); + for (List snaksGroup : map.values()) { + snakGroups.add(Datamodel.makeSnakGroup(snaksGroup)); + } + return snakGroups; + } + + public Claim evaluate(ExpressionContext ctxt) { + ItemIdValue subject = subjectExpr.evaluate(ctxt); + Snak mainSnak = mainSnakExpr.evaluate(ctxt); + List qualifiers = new ArrayList(qualifierExprs.size()); + List groupedQualifiers = groupSnaks(qualifiers); + return Datamodel.makeClaim(subject, mainSnak, groupedQualifiers); + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java new file mode 100644 index 000000000..5efe60ab2 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemConstant.java @@ -0,0 +1,39 @@ +package org.openrefine.wikidata.schema; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.wikidata.wdtk.datamodel.implementation.ItemIdValueImpl; +import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; + + +public class WbItemConstant implements WbItemExpr { + /* Represents an item that does not vary, + * it is independent of the row. */ + + private String qid; + private String label; + + public WbItemConstant(String qid, String label) { + this.qid = qid; + this.label = label; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("qid"); + writer.value(qid); + writer.key("label"); + writer.value(label); + writer.endObject(); + } + + @Override + public ItemIdValue evaluate(ExpressionContext ctxt) { + return ItemIdValueImpl.create(qid, ctxt.getBaseIRI()); + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemExpr.java new file mode 100644 index 000000000..5c9e02341 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemExpr.java @@ -0,0 +1,9 @@ +package org.openrefine.wikidata.schema; + +import org.openrefine.wikidata.schema.ExpressionContext; +import org.openrefine.wikidata.schema.WbValueExpr; +import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; + +public interface WbItemExpr extends WbValueExpr { + public ItemIdValue evaluate(ExpressionContext ctxt); +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java new file mode 100644 index 000000000..e6831108a --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbItemVariable.java @@ -0,0 +1,42 @@ +package org.openrefine.wikidata.schema; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.wikidata.wdtk.datamodel.implementation.ItemIdValueImpl; +import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; + +import com.google.refine.model.Cell; +import com.google.refine.model.ReconCandidate; + +public class WbItemVariable implements WbItemExpr { + /* An item that depends on a reconciled value in a column */ + + private String columnName; + + public WbItemVariable(String columnName) { + this.columnName = columnName; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("columnName"); + writer.value(columnName); + writer.endObject(); + + } + + @Override + public ItemIdValue evaluate(ExpressionContext ctxt) { + Cell cell = ctxt.getCellByName(columnName); + if (cell != null && cell.recon != null && cell.recon.match != null) { + ReconCandidate match = cell.recon.match; + return ItemIdValueImpl.create(match.id, ctxt.getBaseIRI()); + } + return null; + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java new file mode 100644 index 000000000..2a3724428 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropConstant.java @@ -0,0 +1,33 @@ +package org.openrefine.wikidata.schema; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.wikidata.wdtk.datamodel.implementation.PropertyIdValueImpl; +import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; + + +public class WbPropConstant implements WbPropExpr { + /* A constant property, that does not change depending on the row */ + + private String pid; + + public WbPropConstant(String pid) { + this.pid = pid; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("pid"); + writer.value(pid); + writer.endObject(); + } + + @Override + public PropertyIdValue evaluate(ExpressionContext ctxt) { + return PropertyIdValueImpl.create(pid, ctxt.getBaseIRI()); + } +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropExpr.java new file mode 100644 index 000000000..fdf7a7300 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbPropExpr.java @@ -0,0 +1,11 @@ +package org.openrefine.wikidata.schema; + +import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; + + +public interface WbPropExpr extends WbValueExpr { + /* An expression that represents a property */ + + public PropertyIdValue evaluate(ExpressionContext ctxt); + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbSnakExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbSnakExpr.java new file mode 100644 index 000000000..d282589ac --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbSnakExpr.java @@ -0,0 +1,42 @@ +package org.openrefine.wikidata.schema; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +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; + + +public class WbSnakExpr implements Jsonizable { + + private WbPropExpr propExpr; + private WbValueExpr valueExpr; + + public WbSnakExpr(WbPropExpr propExpr, WbValueExpr valueExpr) { + this.propExpr = propExpr; + this.valueExpr = 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 Snak evaluate(ExpressionContext ctxt) { + PropertyIdValue propertyId = propExpr.evaluate(ctxt); + Value value = valueExpr.evaluate(ctxt); + return Datamodel.makeValueSnak(propertyId, value); + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java new file mode 100644 index 000000000..7844564c5 --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringConstant.java @@ -0,0 +1,34 @@ +package org.openrefine.wikidata.schema; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONWriter; +import org.wikidata.wdtk.datamodel.helpers.Datamodel; +import org.wikidata.wdtk.datamodel.implementation.StringValueImpl; +import org.wikidata.wdtk.datamodel.interfaces.Value; + + +public class WbStringConstant implements WbValueExpr { + + private String value; + + public WbStringConstant(String value) { + this.value = value; + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("value"); + writer.value(value); + writer.endObject(); + } + + @Override + public Value evaluate(ExpressionContext ctxt) { + return Datamodel.makeStringValue(value); + } + +} diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java new file mode 100644 index 000000000..1e3970fec --- /dev/null +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbValueExpr.java @@ -0,0 +1,16 @@ +package org.openrefine.wikidata.schema; + +import org.wikidata.wdtk.datamodel.interfaces.Value; + +import com.google.refine.Jsonizable; + + +public interface WbValueExpr extends Jsonizable { + /* An expression that represents a Wikibase value, + * i.e. anything that can be on the right-hand side + * of a statement. + */ + + public Value evaluate(ExpressionContext ctxt); + +}