Refactor schema expressions

This commit is contained in:
Antonin Delpeuch 2018-01-24 17:21:13 +00:00
parent a476b2bf0a
commit b99b72b071
24 changed files with 195 additions and 249 deletions

View File

@ -17,7 +17,7 @@ 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 implements WbValueExpr<TimeValue> {
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)

View File

@ -1,11 +0,0 @@
package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
public abstract class WbDateExpr extends WbValueExpr {
@Override
public abstract TimeValue evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException;
}

View File

@ -5,38 +5,19 @@ import java.text.ParseException;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
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 WbVariableExpr<TimeValue> {
private String columnName;
@JsonCreator
public WbDateVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override @Override
public TimeValue evaluate(ExpressionContext ctxt) public TimeValue fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
Cell cell = ctxt.getCellByName(columnName); try {
if (cell != null) { // TODO accept parsed dates (without converting them to strings)
try { return WbDateConstant.parse(cell.value.toString());
// TODO accept parsed dates (without converting them to strings) } catch (ParseException e) {
return WbDateConstant.parse(cell.value.toString()); throw new SkipSchemaExpressionException();
} catch (ParseException e) {
}
} }
throw new SkipSchemaExpressionException();
}
@JsonProperty("columnName")
public String getColumnName() {
return columnName;
} }
} }

View File

@ -1,17 +1,16 @@
package org.openrefine.wikidata.schema; package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.openrefine.wikidata.schema.entityvalues.SuggestedItemIdValue; import org.openrefine.wikidata.schema.entityvalues.SuggestedItemIdValue;
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.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/**
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 class WbItemConstant implements WbValueExpr<ItemIdValue> {
private String qid; private String qid;
private String label; private String label;

View File

@ -10,16 +10,22 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/**
* The representation of an item document, which can contain
* variables both for its own id and in its contents.
*
* @author antonin
*
*/
public class WbItemDocumentExpr extends JacksonJsonizable { public class WbItemDocumentExpr extends JacksonJsonizable {
private WbItemExpr subject; private WbValueExpr<? extends ItemIdValue> subject;
private List<WbNameDescExpr> nameDescs; private List<WbNameDescExpr> nameDescs;
private List<WbStatementGroupExpr> statementGroups; private List<WbStatementGroupExpr> statementGroups;
@JsonCreator @JsonCreator
public WbItemDocumentExpr( public WbItemDocumentExpr(
@JsonProperty("subject") WbItemExpr subjectExpr, @JsonProperty("subject") WbValueExpr<? extends ItemIdValue> subjectExpr,
@JsonProperty("nameDescs") List<WbNameDescExpr> nameDescExprs, @JsonProperty("nameDescs") List<WbNameDescExpr> nameDescExprs,
@JsonProperty("statementGroups") List<WbStatementGroupExpr> statementGroupExprs) { @JsonProperty("statementGroups") List<WbStatementGroupExpr> statementGroupExprs) {
this.subject = subjectExpr; this.subject = subjectExpr;
@ -46,7 +52,7 @@ public class WbItemDocumentExpr extends JacksonJsonizable {
} }
@JsonProperty("subject") @JsonProperty("subject")
public WbItemExpr getSubject() { public WbValueExpr<? extends ItemIdValue> getSubject() {
return subject; return subject;
} }

View File

@ -1,11 +0,0 @@
package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.ExpressionContext;
import org.openrefine.wikidata.schema.WbValueExpr;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
public abstract class WbItemExpr extends WbValueExpr {
public abstract ItemIdValue evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException;
}

View File

@ -5,36 +5,24 @@ import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
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.Judgment; import com.google.refine.model.Recon.Judgment;
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.
*
private String columnName; * @author antonin
*
@JsonCreator */
public WbItemVariable( public class WbItemVariable extends WbVariableExpr<ItemIdValue> {
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override @Override
public ItemIdValue evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException { public ItemIdValue fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException {
Cell cell = ctxt.getCellByName(getColumnName()); if (cell.recon != null
if (cell != null && cell.recon != null
&& (Judgment.Matched.equals(cell.recon.judgment) || && (Judgment.Matched.equals(cell.recon.judgment) ||
Judgment.New.equals(cell.recon.judgment))) { Judgment.New.equals(cell.recon.judgment))) {
return new ReconItemIdValue(cell.recon, cell.value.toString()); return new ReconItemIdValue(cell.recon, cell.value.toString());
} }
throw new SkipSchemaExpressionException(); throw new SkipSchemaExpressionException();
} }
@JsonProperty("columnName")
public String getColumnName() {
return columnName;
}
} }

View File

@ -5,7 +5,15 @@ import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class WbLanguageConstant extends WbLanguageExpr { /**
* A constant that represents a Wikimedia language code.
*
* TODO: migrate to a class more specific than String, with validation.
*
* @author antonin
*
*/
public class WbLanguageConstant implements WbValueExpr<String> {
protected String _langId; protected String _langId;
protected String _langLabel; protected String _langLabel;

View File

@ -1,26 +0,0 @@
package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
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;
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.PROPERTY,
property="type")
@JsonSubTypes({
@Type(value = WbLanguageConstant.class, name = "wblanguageconstant"),
@Type(value = WbLanguageVariable.class, name = "wblanguagevariable")
})
public abstract class WbLanguageExpr extends JacksonJsonizable {
/**
* Evaluates the language expression to a Wikimedia language code
*
* @param ctxt the evaluation context
* @return a Wikimedia language code
* @throws SkipSchemaExpressionException when the code is invalid
*/
public abstract String evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException;
}

View File

@ -7,31 +7,18 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
/**
public class WbLanguageVariable extends WbLanguageExpr { * A language variable generates a language code from a cell.
*/
private String columnName; public class WbLanguageVariable extends WbVariableExpr<String> {
@JsonCreator
public WbLanguageVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override @Override
public String evaluate(ExpressionContext ctxt) public String fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
Cell cell = ctxt.getCellByName(getColumnName()); if (cell.value != null && !cell.value.toString().isEmpty()) {
if (cell != null && cell.value != null && !cell.value.toString().isEmpty()) {
// TODO some validation here? // TODO some validation here?
return cell.value.toString(); return cell.value.toString();
} }
throw new SkipSchemaExpressionException(); throw new SkipSchemaExpressionException();
} }
@JsonProperty("columnName")
public String getColumnName() {
return columnName;
}
} }

View File

@ -10,7 +10,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class WbLocationConstant extends WbLocationExpr { public class WbLocationConstant implements WbValueExpr<GlobeCoordinatesValue> {
private String value; private String value;
private GlobeCoordinatesValue parsed; private GlobeCoordinatesValue parsed;

View File

@ -1,10 +0,0 @@
package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
public abstract class WbLocationExpr extends WbValueExpr {
@Override
public abstract GlobeCoordinatesValue evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException;
}

View File

@ -5,38 +5,19 @@ import java.text.ParseException;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
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;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
public class WbLocationVariable extends WbLocationExpr { public class WbLocationVariable extends WbVariableExpr<GlobeCoordinatesValue> {
private String columnName;
@JsonCreator
public WbLocationVariable(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override @Override
public GlobeCoordinatesValue evaluate(ExpressionContext ctxt) public GlobeCoordinatesValue fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
Cell cell = ctxt.getCellByName(getColumnName()); String expr = cell.value.toString();
if (cell != null) { try {
String expr = cell.value.toString(); return WbLocationConstant.parse(expr);
try { } catch (ParseException e) {
return WbLocationConstant.parse(expr); throw new SkipSchemaExpressionException();
} catch (ParseException e) {
}
} }
throw new SkipSchemaExpressionException();
}
@JsonProperty("columnName")
public String getColumnName() {
return columnName;
} }
} }

View File

@ -4,20 +4,21 @@ import org.openrefine.wikidata.qa.QAWarning;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException; import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
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 org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class WbMonolingualExpr extends WbValueExpr { public class WbMonolingualExpr implements WbValueExpr<MonolingualTextValue> {
private WbLanguageExpr languageExpr; private WbValueExpr<? extends String> languageExpr;
private WbStringExpr valueExpr; private WbValueExpr<? extends StringValue> valueExpr;
@JsonCreator @JsonCreator
public WbMonolingualExpr( public WbMonolingualExpr(
@JsonProperty("language") WbLanguageExpr languageExpr, @JsonProperty("language") WbValueExpr<? extends String> languageExpr,
@JsonProperty("value") WbStringExpr valueExpr) { @JsonProperty("value") WbValueExpr<? extends StringValue> valueExpr) {
this.languageExpr = languageExpr; this.languageExpr = languageExpr;
this.valueExpr = valueExpr; this.valueExpr = valueExpr;
} }
@ -45,12 +46,12 @@ public class WbMonolingualExpr extends WbValueExpr {
} }
@JsonProperty("language") @JsonProperty("language")
public WbLanguageExpr getLanguageExpr() { public WbValueExpr<? extends String> getLanguageExpr() {
return languageExpr; return languageExpr;
} }
@JsonProperty("value") @JsonProperty("value")
public WbStringExpr getValueExpr() { public WbValueExpr<? extends StringValue> getValueExpr() {
return valueExpr; return valueExpr;
} }
} }

View File

@ -6,9 +6,13 @@ import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
/**
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 */ *
* @author antonin
*
*/
public class WbPropConstant implements WbValueExpr<PropertyIdValue> {
private String pid; private String pid;
private String label; private String label;

View File

@ -1,23 +0,0 @@
package org.openrefine.wikidata.schema;
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 */
public abstract PropertyIdValue evaluate(ExpressionContext ctxt);
public static WbPropExpr fromJSON(JSONObject obj) throws JSONException {
return WbPropConstant.fromJSON(obj);
}
}

View File

@ -13,13 +13,13 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class WbSnakExpr extends JacksonJsonizable { public class WbSnakExpr extends JacksonJsonizable {
private WbPropExpr prop; private WbValueExpr<? extends PropertyIdValue> prop;
private WbValueExpr value; private WbValueExpr<? extends Value> value;
@JsonCreator @JsonCreator
public WbSnakExpr( public WbSnakExpr(
@JsonProperty("prop") WbPropExpr propExpr, @JsonProperty("prop") WbValueExpr<? extends PropertyIdValue> propExpr,
@JsonProperty("value") WbValueExpr valueExpr) { @JsonProperty("value") WbValueExpr<? extends Value> valueExpr) {
this.prop = propExpr; this.prop = propExpr;
this.value = valueExpr; this.value = valueExpr;
} }
@ -31,12 +31,12 @@ public class WbSnakExpr extends JacksonJsonizable {
} }
@JsonProperty("prop") @JsonProperty("prop")
public WbPropExpr getProp() { public WbValueExpr<? extends PropertyIdValue> getProp() {
return prop; return prop;
} }
@JsonProperty("value") @JsonProperty("value")
public WbValueExpr getValue() { public WbValueExpr<? extends Value> getValue() {
return value; return value;
} }
} }

View File

@ -30,13 +30,13 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStatementExpr extends JacksonJsonizable { public class WbStatementExpr extends JacksonJsonizable {
private WbValueExpr mainSnakValueExpr; private WbValueExpr<? extends Value> mainSnakValueExpr;
private List<WbSnakExpr> qualifierExprs; private List<WbSnakExpr> qualifierExprs;
private List<WbReferenceExpr> referenceExprs; private List<WbReferenceExpr> referenceExprs;
@JsonCreator @JsonCreator
public WbStatementExpr( public WbStatementExpr(
@JsonProperty("value") WbValueExpr mainSnakValueExpr, @JsonProperty("value") WbValueExpr<? extends Value> mainSnakValueExpr,
@JsonProperty("qualifiers") List<WbSnakExpr> qualifierExprs, @JsonProperty("qualifiers") List<WbSnakExpr> qualifierExprs,
@JsonProperty("references") List<WbReferenceExpr> referenceExprs) { @JsonProperty("references") List<WbReferenceExpr> referenceExprs) {
this.mainSnakValueExpr = mainSnakValueExpr; this.mainSnakValueExpr = mainSnakValueExpr;
@ -99,7 +99,7 @@ public class WbStatementExpr extends JacksonJsonizable {
} }
@JsonProperty("value") @JsonProperty("value")
public WbValueExpr getMainsnak() { public WbValueExpr<? extends Value> getMainsnak() {
return mainSnakValueExpr; return mainSnakValueExpr;
} }

View File

@ -17,12 +17,12 @@ import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStatementGroupExpr extends JacksonJsonizable { public class WbStatementGroupExpr extends JacksonJsonizable {
private WbPropExpr propertyExpr; private WbValueExpr<? extends PropertyIdValue> propertyExpr;
private List<WbStatementExpr> statementExprs; private List<WbStatementExpr> statementExprs;
@JsonCreator @JsonCreator
public WbStatementGroupExpr( public WbStatementGroupExpr(
@JsonProperty("property") WbPropExpr propertyExpr, @JsonProperty("property") WbValueExpr<? extends PropertyIdValue> propertyExpr,
@JsonProperty("statements") List<WbStatementExpr> claimExprs) { @JsonProperty("statements") List<WbStatementExpr> claimExprs) {
this.propertyExpr = propertyExpr; this.propertyExpr = propertyExpr;
this.statementExprs = claimExprs; this.statementExprs = claimExprs;
@ -46,7 +46,7 @@ public class WbStatementGroupExpr extends JacksonJsonizable {
} }
@JsonProperty("property") @JsonProperty("property")
public WbPropExpr getProperty() { public WbValueExpr<? extends PropertyIdValue> getProperty() {
return propertyExpr; return propertyExpr;
} }

View File

@ -7,7 +7,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
public class WbStringConstant extends WbStringExpr { public class WbStringConstant implements WbValueExpr<StringValue> {
private String value; private String value;

View File

@ -1,8 +0,0 @@
package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
public abstract class WbStringExpr extends WbValueExpr {
public abstract StringValue evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException;
}

View File

@ -9,29 +9,20 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
public class WbStringVariable extends WbStringExpr { /**
public static final String jsonType = "wbstringvariable"; * A variable that returns a simple string value.
*
private String columnName; * @author antonin
*
@JsonCreator */
public WbStringVariable( public class WbStringVariable extends WbVariableExpr<StringValue> {
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
@Override @Override
public StringValue evaluate(ExpressionContext ctxt) public StringValue fromCell(Cell cell, ExpressionContext ctxt)
throws SkipSchemaExpressionException { throws SkipSchemaExpressionException {
Cell cell = ctxt.getCellByName(columnName); if (!cell.value.toString().isEmpty()) {
if (cell != null && !cell.value.toString().isEmpty()) {
return Datamodel.makeStringValue(cell.value.toString()); return Datamodel.makeStringValue(cell.value.toString());
} }
throw new SkipSchemaExpressionException(); throw new SkipSchemaExpressionException();
} }
@JsonProperty("columnName")
public String getColumnName() {
return columnName;
}
} }

View File

@ -10,7 +10,10 @@ import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type; import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
/**
* The base interface for all expressions, which evaluate to a
* particular type T in an ExpressionContext.
*/
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, @JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.PROPERTY, include=JsonTypeInfo.As.PROPERTY,
property="type") property="type")
@ -26,20 +29,15 @@ import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
@Type(value = WbDateConstant.class, name = "wbdateconstant"), @Type(value = WbDateConstant.class, name = "wbdateconstant"),
@Type(value = WbDateVariable.class, name = "wbdatevariable"), @Type(value = WbDateVariable.class, name = "wbdatevariable"),
@Type(value = WbMonolingualExpr.class, name = "wbmonolingualexpr"), @Type(value = WbMonolingualExpr.class, name = "wbmonolingualexpr"),
@Type(value = WbPropConstant.class, name = "wbpropconstant"),
@Type(value = WbLanguageConstant.class, name = "wblanguageconstant"),
@Type(value = WbLanguageVariable.class, name = "wblanguagevariable"),
}) })
public abstract class WbValueExpr extends JacksonJsonizable { public interface WbValueExpr<T> {
/* An expression that represents a Wikibase value,
* i.e. anything that can be on the right-hand side
* of a statement.
*/
/* /**
* Evaluates the value expression in a given context, * Evaluates the value expression in a given context,
* returns a wikibase value suitable to be the target of a claim. * returns a Wikibase value suitable to be the target of a claim.
*/ */
public abstract Value evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException; public T evaluate(ExpressionContext ctxt) throws SkipSchemaExpressionException;
public static WbValueExpr fromJSON(JSONObject obj) throws JSONException {
return fromJSONClass(obj, WbValueExpr.class);
}
} }

View File

@ -0,0 +1,91 @@
package org.openrefine.wikidata.schema;
import org.openrefine.wikidata.schema.exceptions.SkipSchemaExpressionException;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Cell;
/**
* A base class for expressions which draw their values
* from a particular column.
*
* @author antonin
*
* @param <T>
* the type of Wikibase value returned by the expression.
*/
public abstract class WbVariableExpr<T> implements WbValueExpr<T> {
private String columnName;
/**
* Constructs a variable expression from a column name.
*
* @param columnName
* the name of the column the expression should draw its value from.
*/
@JsonCreator
public WbVariableExpr(
@JsonProperty("columnName") String columnName) {
this.columnName = columnName;
}
/**
* Constructs a variable without setting the column name yet.
*/
@JsonCreator
public WbVariableExpr() {
columnName = null;
}
/**
* Returns the column name used by the variable.
* @return
* the OpenRefine column name
*/
@JsonProperty("columnName")
public String getColumnName() {
return columnName;
}
/**
* Changes the column name used by the variable.
* This is useful for deserialization, as well as updates when
* column names change.
*/
@JsonProperty("columnName")
public void setColumnName(String columnName) {
this.columnName = columnName;
}
/**
* Evaluates the expression in a given context, returning
*/
@Override
public T evaluate(ExpressionContext ctxt)
throws SkipSchemaExpressionException {
Cell cell = ctxt.getCellByName(columnName);
if (cell != null) {
return fromCell(cell, ctxt);
}
throw new SkipSchemaExpressionException();
}
/**
* Method that should be implemented by subclasses,
* converting an OpenRefine cell to a Wikibase value.
* Access to other values and emiting warnings is possible via
* the supplied EvaluationContext object.
*
* @param cell
* the cell to convert
* @param ctxt
* the evaluation context
* @return
* the corresponding Wikibase value
*/
public abstract T fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException;
}