Start schema classes

This commit is contained in:
Antonin Delpeuch 2017-08-27 19:17:01 +01:00
parent 19a1e5e007
commit ad475849b9
11 changed files with 331 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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
*/
}

View File

@ -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<WbSnakExpr> 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<SnakGroup> groupSnaks(List<Snak> snaks) {
Map<PropertyIdValue, List<Snak> > map = new HashMap<PropertyIdValue, List<Snak>>();
for (Snak snak : snaks) {
PropertyIdValue pid = snak.getPropertyId();
if (!map.containsKey(pid)) {
map.put(pid, new ArrayList<Snak>());
}
map.get(pid).add(snak);
}
List<SnakGroup> snakGroups = new ArrayList<SnakGroup>();
for (List<Snak> 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<Snak> qualifiers = new ArrayList<Snak>(qualifierExprs.size());
List<SnakGroup> groupedQualifiers = groupSnaks(qualifiers);
return Datamodel.makeClaim(subject, mainSnak, groupedQualifiers);
}
}

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}