Support for properties with varying target datatype

This commit is contained in:
Antonin Delpeuch 2017-09-05 15:21:42 +01:00
parent fcde3d2c7d
commit 781c3b0467
7 changed files with 204 additions and 76 deletions

View File

@ -117,7 +117,6 @@ SchemaAlignmentDialog._reset = function(schema, initial) {
SchemaAlignmentDialog._save = function(onDone) { SchemaAlignmentDialog._save = function(onDone) {
var self = this; var self = this;
var schema = this.getJSON(); var schema = this.getJSON();
console.log(schema);
Refine.postProcess( Refine.postProcess(
"wikidata", "wikidata",
@ -213,8 +212,6 @@ SchemaAlignmentDialog._createDialog = function() {
}; };
SchemaAlignmentDialog._addItem = function(json) { SchemaAlignmentDialog._addItem = function(json) {
console.log('addItem')
console.log(json);
var subject = null; var subject = null;
var statementGroups = null; var statementGroups = null;
if (json) { if (json) {
@ -224,7 +221,7 @@ SchemaAlignmentDialog._addItem = function(json) {
var item = $('<div></div>').addClass('wbs-item'); var item = $('<div></div>').addClass('wbs-item');
var inputContainer = $('<div></div>').addClass('wbs-item-input').appendTo(item); var inputContainer = $('<div></div>').addClass('wbs-item-input').appendTo(item);
SchemaAlignmentDialog._initField(inputContainer, "item", subject); SchemaAlignmentDialog._initField(inputContainer, "wikibase-item", subject);
var right = $('<div></div>').addClass('wbs-right').appendTo(item); var right = $('<div></div>').addClass('wbs-right').appendTo(item);
$('<div></div>').addClass('wbs-statement-group-container').appendTo(right); $('<div></div>').addClass('wbs-statement-group-container').appendTo(right);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right); var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
@ -253,8 +250,6 @@ SchemaAlignmentDialog._itemToJSON = function (item) {
}; };
SchemaAlignmentDialog._addStatementGroup = function(item, json) { SchemaAlignmentDialog._addStatementGroup = function(item, json) {
console.log('addStatementGroup')
console.log(json);
var property = null; var property = null;
var statements = null; var statements = null;
if (json) { if (json) {
@ -265,22 +260,24 @@ SchemaAlignmentDialog._addStatementGroup = function(item, json) {
var container = item.find('.wbs-statement-group-container').first(); var container = item.find('.wbs-statement-group-container').first();
var statementGroup = $('<div></div>').addClass('wbs-statement-group'); var statementGroup = $('<div></div>').addClass('wbs-statement-group');
var inputContainer = $('<div></div>').addClass('wbs-prop-input').appendTo(statementGroup); var inputContainer = $('<div></div>').addClass('wbs-prop-input').appendTo(statementGroup);
SchemaAlignmentDialog._initField(inputContainer, "property", property);
var right = $('<div></div>').addClass('wbs-right').appendTo(statementGroup); var right = $('<div></div>').addClass('wbs-right').appendTo(statementGroup);
$('<div></div>').addClass('wbs-statement-container').appendTo(right); var statementContainer = $('<div></div>').addClass('wbs-statement-container').appendTo(right);
SchemaAlignmentDialog._initPropertyField(inputContainer, statementContainer, property);
var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right); var toolbar = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
$('<a></a>').addClass('wbs-add-statement').text('add value').click(function() { var addValueButton = $('<a></a>').addClass('wbs-add-statement').text('add value').click(function() {
SchemaAlignmentDialog._addStatement(statementGroup, null); var datatype = inputContainer.data("jsonValue").datatype;
}).appendTo(toolbar); SchemaAlignmentDialog._addStatement(statementContainer, datatype, null);
}).appendTo(toolbar).hide();
container.append(statementGroup); container.append(statementGroup);
if (statements) { if (statements) {
for (var i = 0; i != statements.length; i++) { for (var i = 0; i != statements.length; i++) {
SchemaAlignmentDialog._addStatement(statementGroup, statements[i]); SchemaAlignmentDialog._addStatement(statementContainer, property.datatype, statements[i]);
addValueButton.show();
} }
} else {
SchemaAlignmentDialog._addStatement(statementGroup, null);
} }
} }
SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) { SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
@ -294,24 +291,23 @@ SchemaAlignmentDialog._statementGroupToJSON = function (statementGroup) {
}; };
SchemaAlignmentDialog._addStatement = function(statementGroup, json) { SchemaAlignmentDialog._addStatement = function(container, datatype, json) {
console.log('addStatement');
console.log(datatype);
var qualifiers = null; var qualifiers = null;
var value = null; var value = null;
console.log('add statement');
console.log(json);
if (json) { if (json) {
qualifiers = json.qualifiers; qualifiers = json.qualifiers;
value = json.value; value = json.value;
} }
var container = statementGroup.find('.wbs-statement-container').first();
var statement = $('<div></div>').addClass('wbs-statement'); var statement = $('<div></div>').addClass('wbs-statement');
var toolbar1 = $('<div></div>').addClass('wbs-toolbar').appendTo(statement); var toolbar1 = $('<div></div>').addClass('wbs-toolbar').appendTo(statement);
$('<img src="images/close.png" />').attr('alt', 'remove statement').click(function() { $('<img src="images/close.png" />').attr('alt', 'remove statement').click(function() {
SchemaAlignmentDialog._removeStatement(statement); SchemaAlignmentDialog._removeStatement(statement);
}).appendTo(toolbar1); }).appendTo(toolbar1);
var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement); var inputContainer = $('<div></div>').addClass('wbs-target-input').appendTo(statement);
SchemaAlignmentDialog._initField(inputContainer, "target", value); SchemaAlignmentDialog._initField(inputContainer, datatype, value);
var right = $('<div></div>').addClass('wbs-right').appendTo(statement); var right = $('<div></div>').addClass('wbs-right').appendTo(statement);
$('<div></div>').addClass('wbs-qualifier-container').appendTo(right); $('<div></div>').addClass('wbs-qualifier-container').appendTo(right);
var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right); var toolbar2 = $('<div></div>').addClass('wbs-toolbar').appendTo(right);
@ -328,37 +324,94 @@ SchemaAlignmentDialog._statementToJSON = function (statement) {
}; };
}; };
SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue) { SchemaAlignmentDialog._getPropertyType = function(pid, callback) {
$.ajax({
url:'https://www.wikidata.org/w/api.php',
data: {
action: "wbgetentities",
format: "json",
ids: pid,
props: "datatype",
},
dataType: "jsonp",
success: function(data) {
callback(data.entities[pid].datatype);
}});
}
SchemaAlignmentDialog._initPropertyField = function(inputContainer, targetContainer, initialValue) {
var input = $('<input></input>').appendTo(inputContainer); var input = $('<input></input>').appendTo(inputContainer);
if (this._reconService !== null) { if (this._reconService !== null) {
endpoint = this._reconService.suggest.property;
var suggestConfig = $.extend({}, endpoint);
suggestConfig.key = null;
suggestConfig.query_param_name = "prefix";
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
// Fetch the type of this property and add the appropriate target value type
var statementGroup = inputContainer.parents(".wbs-statement-group").first();
SchemaAlignmentDialog._getPropertyType(data.id, function(datatype) {
inputContainer.data("jsonValue", {
type : "wbpropconstant",
pid : data.id,
label: data.name,
datatype: datatype,
});
SchemaAlignmentDialog._addStatement(targetContainer, datatype, null);
var addValueButtons = targetContainer.parent().find('.wbs-add-statement');
addValueButtons.show();
});
SchemaAlignmentDialog._hasChanged();
}).bind("fb-textchange", function(evt, data) {
inputContainer.data("jsonValue", null);
targetContainer.find('.wbs-statement').remove();
var addValueButtons = targetContainer.parent().find('.wbs-add-statement');
addValueButtons.hide();
});
}
// Init with the provided initial value.
if (initialValue) {
if (initialValue.type === "wbpropconstant") {
input.val(initialValue.label);
}
inputContainer.data("jsonValue", initialValue);
}
}
SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue) {
var input = $('<input></input>').appendTo(inputContainer);
if (this._reconService !== null && mode === "wikibase-item") {
var endpoint = null; var endpoint = null;
if (mode === "item" || mode === "target") { endpoint = this._reconService.suggest.entity;
endpoint = this._reconService.suggest.entity;
} else if (mode === "property") {
endpoint = this._reconService.suggest.property;
}
var suggestConfig = $.extend({}, endpoint); var suggestConfig = $.extend({}, endpoint);
suggestConfig.key = null; suggestConfig.key = null;
suggestConfig.query_param_name = "prefix"; suggestConfig.query_param_name = "prefix";
input.suggestP(suggestConfig).bind("fb-select", function(evt, data) { input.suggestP(suggestConfig).bind("fb-select", function(evt, data) {
if (mode === "item" || mode === "target") { inputContainer.data("jsonValue", {
inputContainer.data("jsonValue", { type : "wbitemconstant",
type : "wbitemconstant", qid : data.id,
qid : data.id, label: data.name,
label: data.name, });
});
} else if (mode === "property") {
inputContainer.data("jsonValue", {
type : "wbpropconstant",
pid : data.id,
label: data.name,
});
}
SchemaAlignmentDialog._hasChanged(); SchemaAlignmentDialog._hasChanged();
}); });
} else if (mode === "external-id") {
var propagateValue = function(val) {
inputContainer.data("jsonValue", {
type: "wbstringconstant",
value: val,
});
};
propagateValue("");
input.change(function() {
propagateValue($(this).val());
SchemaAlignmentDialog._hasChanged();
});
} }
var acceptDraggableColumn = function(column) { var acceptDraggableColumn = function(column) {
@ -374,42 +427,44 @@ SchemaAlignmentDialog._initField = function(inputContainer, mode, initialValue)
}); });
}; };
// If it isn't a property, make it droppable // Make it droppable
if (mode !== "property") { var acceptClass = ".wbs-draggable-column";
var acceptClass = ".wbs-draggable-column"; var wbVariableType = "wbstringvariable";
if (mode === "item") { if (mode === "wikibase-item") {
acceptClass = ".wbs-reconciled-column"; acceptClass = ".wbs-reconciled-column";
} wbVariableType = "wbitemvariable";
inputContainer.droppable({
accept: acceptClass,
}).on("drop", function (evt, ui) {
var column = ui.draggable.clone();
acceptDraggableColumn(column);
inputContainer.data("jsonValue", {
type : "wbitemvariable",
columnName: ui.draggable.text(),
});
SchemaAlignmentDialog._hasChanged();
return true;
}).on("dropactivate", function(evt, ui) {
input.addClass("wbs-accepting-input");
}).on("dropdeactivate", function(evt, ui) {
input.removeClass("wbs-accepting-input");
});
} }
inputContainer.droppable({
accept: acceptClass,
}).on("drop", function (evt, ui) {
var column = ui.draggable.clone();
acceptDraggableColumn(column);
inputContainer.data("jsonValue", {
type : wbVariableType,
columnName: ui.draggable.text(),
});
SchemaAlignmentDialog._hasChanged();
return true;
}).on("dropactivate", function(evt, ui) {
input.addClass("wbs-accepting-input");
}).on("dropdeactivate", function(evt, ui) {
input.removeClass("wbs-accepting-input");
});
// Init with the provided initial value. // Init with the provided initial value.
console.log('initField');
if (initialValue) { if (initialValue) {
console.log(initialValue); if (initialValue.type === "wbitemconstant") {
console.log(initialValue.type);
if (initialValue.type === "wbitemconstant" || initialValue.type === "wbpropconstant") {
input.val(initialValue.label); input.val(initialValue.label);
} else if (initialValue.type == "wbitemvariable") { } else if (initialValue.type == "wbitemvariable") {
var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, true); var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, true);
acceptDraggableColumn(cell); acceptDraggableColumn(cell);
} else if (initialValue.type == "wbstringconstant") {
input.val(initialValue.value);
} else if (initialValue.type == "wbstringvariable") {
var cell = SchemaAlignmentDialog._createDraggableColumn(initialValue.columnName, false);
acceptDraggableColumn(cell);
} }
inputContainer.data("jsonValue", initialValue); inputContainer.data("jsonValue", initialValue);
} }

View File

@ -16,10 +16,12 @@ public class WbPropConstant extends WbPropExpr {
private String pid; private String pid;
private String label; private String label;
private String datatype;
public WbPropConstant(String pid, String label) { public WbPropConstant(String pid, String label, String datatype) {
this.pid = pid; this.pid = pid;
this.label = label; this.label = label;
this.datatype = datatype;
} }
@Override @Override
@ -29,10 +31,15 @@ public class WbPropConstant extends WbPropExpr {
writer.value(pid); writer.value(pid);
writer.key("label"); writer.key("label");
writer.value(label); writer.value(label);
writer.key("datatype");
writer.value(datatype);
} }
public static WbPropConstant fromJSON(JSONObject obj) throws JSONException { public static WbPropConstant fromJSON(JSONObject obj) throws JSONException {
return new WbPropConstant(obj.getString("pid"), obj.getString("label")); return new WbPropConstant(
obj.getString("pid"),
obj.getString("label"),
obj.getString("datatype"));
} }
@Override @Override

View File

@ -9,14 +9,10 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
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.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.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
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 org.wikidata.wdtk.datamodel.interfaces.Value;
public class WbStatementGroupExpr extends BiJsonizable { public class WbStatementGroupExpr extends BiJsonizable {

View File

@ -6,11 +6,10 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter; import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.implementation.StringValueImpl; import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import org.wikidata.wdtk.datamodel.interfaces.Value;
public class WbStringConstant extends WbValueExpr { public class WbStringConstant extends WbStringExpr {
public static final String jsonType = "wbstringconstant"; public static final String jsonType = "wbstringconstant";
@ -31,7 +30,7 @@ public class WbStringConstant extends WbValueExpr {
} }
@Override @Override
public Value evaluate(ExpressionContext ctxt) { public StringValue evaluate(ExpressionContext ctxt) {
return Datamodel.makeStringValue(value); return Datamodel.makeStringValue(value);
} }

View File

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

View File

@ -0,0 +1,48 @@
package org.openrefine.wikidata.schema;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONWriter;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.StringValue;
import com.google.refine.model.Cell;
public class WbStringVariable extends WbStringExpr {
public static final String jsonType = "wbstringvariable";
private String columnName;
public WbStringVariable(String columnName) {
this.columnName = columnName;
}
@Override
public StringValue evaluate(ExpressionContext ctxt)
throws SkipStatementException {
Cell cell = ctxt.getCellByName(columnName);
if (cell != null) {
return Datamodel.makeStringValue(cell.value.toString());
}
throw new SkipStatementException();
}
@Override
public void writeFields(JSONWriter writer, Properties options)
throws JSONException {
writer.key("columnName");
writer.value(columnName);
}
public static WbStringVariable fromJSON2(JSONObject obj) throws JSONException {
return new WbStringVariable(obj.getString("columnName"));
}
@Override
public String getJsonType() {
return jsonType;
}
}

View File

@ -26,6 +26,8 @@ public abstract class WbValueExpr extends BiJsonizable {
valueExpr = WbItemConstant.fromJSON(obj); valueExpr = WbItemConstant.fromJSON(obj);
} else if (WbItemVariable.jsonType.equals(type)) { } else if (WbItemVariable.jsonType.equals(type)) {
valueExpr = WbItemVariable.fromJSON(obj); valueExpr = WbItemVariable.fromJSON(obj);
} else if (WbStringVariable.jsonType.equals(type)) {
valueExpr = WbStringVariable.fromJSON2(obj);
} else if (WbStringConstant.jsonType.equals(type)) { } else if (WbStringConstant.jsonType.equals(type)) {
valueExpr = WbStringConstant.fromJSON(obj); valueExpr = WbStringConstant.fromJSON(obj);
} else { } else {